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

Added appendTo and prependTo with tests see #641 #726

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ var uniqueSplice = function(array, spliceIdx, spliceCount, newElems, parent) {
return array.splice.apply(array, spliceArgs);
};

exports.appendTo = function(target) {
if (typeof target === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project uses two-space soft tabs. Could you change the code style here to match?

target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}

target.append(this);

return this;
};

exports.prependTo = function(target) {
if (typeof target === 'string') {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}

target.prepend(this);

return this;
};

exports.append = _insert(function(dom, children, parent) {
uniqueSplice(children, children.length, 0, dom, parent);
});
Expand Down
18 changes: 18 additions & 0 deletions test/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,24 @@ describe('$(...)', function() {
});
});

describe('.appendTo', function() {

it('(Node) : should add element as last child', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test description is incorrect. It describes a Node-accepting signature, but the test invokes appendTo with a Cheerio instance (which is elsewhere described as "$(...)")

$('<li class="plum">Plum</li>').appendTo($fruits);
expect($fruits.children().eq(3).hasClass('plum')).to.be.ok();
});

});

describe('.prependTo', function() {

it('(html) : should add element as first child', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test description is incorrect. It describes an HTML-accepting signature, but the test invokes prependTo with a Cheerio instance (which is elsewhere described as "$(...)")

$('<li class="plum">Plum</li>').prependTo($fruits);
expect($fruits.children().eq(0).hasClass('plum')).to.be.ok();
});

});

describe('.after', function() {

it('() : should do nothing', function() {
Expand Down