Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove "root" node #341

Merged
merged 1 commit into from
Dec 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var val = exports.val = function(value) {
// Go up until we hit a form or root
parentEl = this.closest('form');
if (parentEl.length === 0) {
root = (this.parents().last()[0] || this[0]).parent;
root = (this.parents().last()[0] || this[0]).root;
parentEl = this._make(root);
}

Expand Down
25 changes: 14 additions & 11 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var after = exports.after = function() {
dom = makeDomArray(elems);

this.each(function(i, el) {
var siblings = el.parent.children,
var parent = el.parent || el.root,
siblings = parent.children,
index = siblings.indexOf(el);

// If not found, move on
Expand All @@ -63,8 +64,8 @@ var after = exports.after = function() {
siblings.splice.apply(siblings, [++index, 0].concat(dom));

// Update next, prev, and parent pointers
updateDOM(siblings, el.parent);
el.parent.children = siblings;
updateDOM(siblings, parent);
parent.children = siblings;

});

Expand All @@ -76,7 +77,8 @@ var before = exports.before = function() {
dom = makeDomArray(elems);

this.each(function(i, el) {
var siblings = el.parent.children,
var parent = el.parent || el.root,
siblings = parent.children,
index = siblings.indexOf(el);

// If not found, move on
Expand All @@ -90,8 +92,8 @@ var before = exports.before = function() {
siblings.splice.apply(siblings, [index, 0].concat(dom));

// Update next, prev, and parent pointers
updateDOM(siblings, el.parent);
el.parent.children = siblings;
updateDOM(siblings, parent);
parent.children = siblings;

});

Expand All @@ -109,16 +111,17 @@ var remove = exports.remove = function(selector) {
elems = elems.filter(selector);

elems.each(function(i, el) {
var siblings = el.parent.children,
var parent = el.parent || el.root,
siblings = parent.children,
index = siblings.indexOf(el);

if (!~index) return;

siblings.splice(index, 1);

// Update next, prev, and parent pointers
updateDOM(siblings, el.parent);
el.parent.children = siblings;
updateDOM(siblings, parent);
parent.children = siblings;
});

return this;
Expand All @@ -128,8 +131,8 @@ var replaceWith = exports.replaceWith = function(content) {
var dom = makeDomArray(content);

this.each(function(i, el) {
var siblings = el.parent.children,
parent = el.parent,
var parent = el.parent || el.root,
siblings = parent.children,
index;

if (_.isFunction(content)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var parent = exports.parent = function(selector) {

this.each(function(idx, elem) {
var parentElem = elem.parent;
if (set.indexOf(parentElem) < 0 && parentElem.type !== 'root') {
if (parentElem && set.indexOf(parentElem) < 0) {
set.push(parentElem);
}
});
Expand Down Expand Up @@ -236,7 +236,7 @@ var slice = exports.slice = function() {

function traverseParents(self, elem, selector, limit) {
var elems = [];
while (elems.length < limit && elem.type !== 'root') {
while (elem && elems.length < limit) {
if (!selector || self._make(elem).filter(selector).length) {
elems.push(elem);
}
Expand Down
1 change: 0 additions & 1 deletion lib/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var path = require('path'),
parse = require('./parse'),
evaluate = parse.evaluate,
updateDOM = parse.update,
_ = require('underscore');

/*
Expand Down
9 changes: 8 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ var update = exports.update = function(arr, parent) {

node.prev = arr[i - 1] || null;
node.next = arr[i + 1] || null;
node.parent = parent;

if (parent && parent.type === 'root') {
node.root = parent;
node.parent = null;
} else {
delete node.root;
node.parent = parent;
}
}

return parent;
Expand Down
5 changes: 5 additions & 0 deletions test/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ describe('cheerio', function() {
expect($a[0].children[0].data).to.equal('Save');
});

it('should not create a top-level node', function() {
var $elem = $('* div', '<div>');
expect($elem).to.have.length(0);
});

it('should be able to select multiple elements: $(".apple, #fruits")', function() {
var $elems = $('.apple, #fruits', fruits);
expect($elems).to.have.length(2);
Expand Down
4 changes: 2 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('parse', function() {
expect(root.parent).to.be(null);

var child = root.children[0];
expect(child.parent).to.equal(root);
expect(child.parent).to.be(null);
}

it('should add root to: ' + basic, function() {
Expand All @@ -174,7 +174,7 @@ describe('parse', function() {
expect(root.children).to.have.length(2);
expect(root.children[0].name).to.equal('h2');
expect(root.children[1].name).to.equal('p');
expect(root.children[1].parent.name).to.equal('root');
expect(root.children[1].parent).to.equal(null);
});

it('should add root to: ' + comment, function() {
Expand Down