diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index 4b443184c1..cbe352d710 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -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]; } @@ -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 { @@ -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, @@ -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 @@ -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, @@ -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 @@ -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 @@ -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) { diff --git a/lib/cheerio.js b/lib/cheerio.js index 18d0d1d126..750ecc2293 100644 --- a/lib/cheerio.js +++ b/lib/cheerio.js @@ -4,7 +4,6 @@ var path = require('path'), parse = require('./parse'), - evaluate = parse.evaluate, _ = require('lodash'); /* @@ -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); } @@ -63,7 +64,7 @@ var Cheerio = module.exports = function(selector, context, root) { // $() 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 @@ -72,7 +73,7 @@ var Cheerio = module.exports = function(selector, context, root) { } else if (typeof context === 'string') { if (isHtml(context)) { // $('li', '') - context = parse(context); + context = parse(context, this.options); context = Cheerio.call(this, context); } else { // $('li', 'ul') @@ -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 @@ -110,7 +111,7 @@ Cheerio.prototype.cheerio = '[cheerio object]'; Cheerio.prototype.options = { normalizeWhitespace: false, xmlMode: false, - lowerCaseTags: false + decodeEntities: false }; /* @@ -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; }; diff --git a/lib/static.js b/lib/static.js index a8ce11a159..aedf6e0a30 100644 --- a/lib/static.js +++ b/lib/static.js @@ -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 diff --git a/test/xml.js b/test/xml.js index 0f9ce19926..2a1f2197e8 100644 --- a/test/xml.js +++ b/test/xml.js @@ -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() { @@ -24,4 +29,26 @@ describe('render', function() { }); + describe('(dom)', function () { + + it('should keep camelCase for new nodes', function() { + var str = 'hello'; + expect(dom(str, {xmlMode: false})).to.equal('hello'); + }); + + it('should keep camelCase for new nodes', function() { + var str = 'hello'; + expect(dom(str, {xmlMode: true})).to.equal('hello'); + }); + + it('should maintain the parsing options of distinct contexts independently', function() { + var str = 'hello'; + var $x = cheerio.load('', { xmlMode: false }); + var $h = cheerio.load('', { xmlMode: true }); + + expect($x(str).html()).to.equal('hello'); + }); + + }); + });