Skip to content

Commit

Permalink
Merge pull request #437 from MatthewMueller/preserve_options
Browse files Browse the repository at this point in the history
Preserve options
  • Loading branch information
matthewmueller committed Apr 8, 2014
2 parents 35fc758 + a1f2896 commit e478de2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
27 changes: 16 additions & 11 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ var _ = require('lodash'),
domEach = utils.domEach,
encode = utils.encode,
slice = Array.prototype.slice;

// Create an array of nodes, recursing into arrays and parsing strings if
// necessary
var makeDomArray = function(elem) {
exports._makeDomArray = function makeDomArray(elem) {
if (elem == null) {
return [];
} else if (elem.cheerio) {
return elem.get();
} else if (_.isArray(elem)) {
return _.flatten(elem.map(makeDomArray));
return _.flatten(elem.map(makeDomArray, this));
} else if (_.isString(elem)) {
return evaluate(elem);
return evaluate(elem, this.options);
} else {
return [elem];
}
Expand All @@ -26,11 +27,11 @@ var makeDomArray = function(elem) {
var _insert = function(concatenator) {
return function() {
var elems = slice.call(arguments),
dom = makeDomArray(elems);
dom = this._makeDomArray(elems);

if (_.isFunction(elems[0])) {
return this.each(function(i, el) {
dom = makeDomArray(elems[0].call(el, i, this.html()));
dom = this._makeDomArray(elems[0].call(el, i, this.html()));
concatenator(dom, el.children, el);
});
} else {
Expand Down Expand Up @@ -96,7 +97,8 @@ var prepend = exports.prepend = _insert(function(dom, children, parent) {

var after = exports.after = function() {
var elems = slice.call(arguments),
dom = makeDomArray(elems);
dom = this._makeDomArray(elems),
self = this;

domEach(this, function(i, el) {
var parent = el.parent || el.root,
Expand All @@ -107,7 +109,7 @@ var after = exports.after = function() {
if (!~index) return;

if (_.isFunction(elems[0])) {
dom = makeDomArray(elems[0].call(el, i));
dom = self._makeDomArray(elems[0].call(el, i));
}

// Add element after `this` element
Expand All @@ -119,7 +121,8 @@ var after = exports.after = function() {

var before = exports.before = function() {
var elems = slice.call(arguments),
dom = makeDomArray(elems);
dom = this._makeDomArray(elems),
self = this;

domEach(this, function(i, el) {
var parent = el.parent || el.root,
Expand All @@ -130,7 +133,7 @@ var before = exports.before = function() {
if (!~index) return;

if (_.isFunction(elems[0])) {
dom = makeDomArray(elems[0].call(el, i));
dom = self._makeDomArray(elems[0].call(el, i));
}

// Add element before `el` element
Expand Down Expand Up @@ -172,10 +175,12 @@ var remove = exports.remove = function(selector) {
};

var replaceWith = exports.replaceWith = function(content) {
var self = this;

domEach(this, function(i, el) {
var parent = el.parent || el.root,
siblings = parent.children,
dom = makeDomArray(_.isFunction(content) ? content.call(el, i, el) : content),
dom = self._makeDomArray(_.isFunction(content) ? content.call(el, i, el) : content),
index;

// In the case that `dom` contains nodes that already exist in other
Expand Down Expand Up @@ -212,7 +217,7 @@ var html = exports.html = function(str) {
return $.html(this[0].children);
}

str = str.cheerio ? str.get() : evaluate(str);
str = str.cheerio ? str.get() : evaluate(str, this.options);

domEach(this, function(i, el) {
_.each(el.children, function(el) {
Expand Down
19 changes: 10 additions & 9 deletions lib/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

var path = require('path'),
parse = require('./parse'),
evaluate = parse.evaluate,
_ = require('lodash');

/*
Expand Down Expand Up @@ -34,14 +33,16 @@ var $ = require('./static');
* Instance of cheerio
*/

var Cheerio = module.exports = function(selector, context, root) {
if (!(this instanceof Cheerio)) return new Cheerio(selector, context, root);
var Cheerio = module.exports = function(selector, context, root, options) {
if (!(this instanceof Cheerio)) return new Cheerio(selector, context, root, options);

this.options = _.defaults(options || {}, this.options);

// $(), $(null), $(undefined), $(false)
if (!selector) return this;

if (root) {
if (typeof root === 'string') root = parse(root);
if (typeof root === 'string') root = parse(root, this.options);
this._root = Cheerio.call(this, root);
}

Expand All @@ -63,7 +64,7 @@ var Cheerio = module.exports = function(selector, context, root) {

// $(<html>)
if (typeof selector === 'string' && isHtml(selector)) {
return Cheerio.call(this, parse(selector).children);
return Cheerio.call(this, parse(selector, this.options).children);
}

// If we don't have a context, maybe we have a root, from loading
Expand All @@ -72,7 +73,7 @@ var Cheerio = module.exports = function(selector, context, root) {
} else if (typeof context === 'string') {
if (isHtml(context)) {
// $('li', '<ul>...</ul>')
context = parse(context);
context = parse(context, this.options);
context = Cheerio.call(this, context);
} else {
// $('li', 'ul')
Expand All @@ -81,7 +82,7 @@ var Cheerio = module.exports = function(selector, context, root) {
}
// $('li', node), $('li', [nodes])
} else if (!context.cheerio) {
context = new Cheerio(context);
context = Cheerio.call(this, context);
}

// If we still don't have a context, return
Expand Down Expand Up @@ -110,7 +111,7 @@ Cheerio.prototype.cheerio = '[cheerio object]';
Cheerio.prototype.options = {
normalizeWhitespace: false,
xmlMode: false,
lowerCaseTags: false
decodeEntities: false
};

/*
Expand Down Expand Up @@ -139,7 +140,7 @@ var isHtml = function(str) {
*/

Cheerio.prototype._make = function(dom) {
var cheerio = new Cheerio(dom);
var cheerio = new Cheerio(dom, undefined, undefined, this.options);
cheerio.prevObject = this;
return cheerio;
};
Expand Down
15 changes: 10 additions & 5 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
var select = require('CSSselect'),
parse = require('./parse'),
render = require('./render'),
decode = require('./utils').decode;
decode = require('./utils').decode,
_ = require('lodash');

/**
* $.load(str)
*/

var load = exports.load = function(content, options) {
var Cheerio = require('./cheerio'),
root = parse(content, options);
var Cheerio = require('./cheerio');

var initialize = function(selector, context, r) {
return new Cheerio(selector, context, r || root);
options = _.defaults(options || {}, Cheerio.prototype.options);

var root = parse(content, options);

var initialize = function(selector, context, r, opts) {
opts = _.defaults(opts || {}, options);
return new Cheerio(selector, context, r || root, opts);
};

// Add in the static methods
Expand Down
27 changes: 27 additions & 0 deletions test/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ var xml = function(str, options) {
return dom.xml();
};

var dom = function(str, options) {
var $ = cheerio.load('', options);
return $(str).html();
};

describe('render', function() {

describe('(xml)', function() {
Expand All @@ -24,4 +29,26 @@ describe('render', function() {

});

describe('(dom)', function () {

it('should keep camelCase for new nodes', function() {
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
expect(dom(str, {xmlMode: false})).to.equal('<someelem someattribute="something">hello</someelem>');
});

it('should keep camelCase for new nodes', function() {
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
expect(dom(str, {xmlMode: true})).to.equal('<someElem someAttribute="something">hello</someElem>');
});

it('should maintain the parsing options of distinct contexts independently', function() {
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
var $x = cheerio.load('', { xmlMode: false });
var $h = cheerio.load('', { xmlMode: true });

expect($x(str).html()).to.equal('<someelem someattribute="something">hello</someelem>');
});

});

});

0 comments on commit e478de2

Please sign in to comment.