Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #17 from PolymerLabs/replace
Browse files Browse the repository at this point in the history
Add replace nodes
  • Loading branch information
dfreedm committed Apr 23, 2015
2 parents 0650779 + ad97986 commit a77c352
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dom5.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ function newElement(tagName, namespace) {
};
}

function replace(oldNode, newNode) {
insertBefore(oldNode.parentNode, oldNode, newNode);
remove(oldNode);
}

function remove(node) {
var parent = node.parentNode;
if (parent) {
Expand Down Expand Up @@ -379,6 +384,7 @@ module.exports = {
getTextContent: getTextContent,
setTextContent: setTextContent,
remove: remove,
replace: replace,
append: append,
insertBefore: insertBefore,
normalize: normalize,
Expand Down
11 changes: 11 additions & 0 deletions test/dom5_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ suite('dom5', function() {
});
});

suite('Replace node', function() {
test('New node replaces old node', function() {
var divA = doc.childNodes[1].childNodes[1].childNodes[0];
var newNode = dom5.constructors.element('ul');
dom5.replace(divA, newNode);
assert.equal(divA.parentNode, null);
assert.equal(doc.childNodes[1].childNodes[1].childNodes.indexOf(divA), -1);
assert.equal(doc.childNodes[1].childNodes[1].childNodes.indexOf(newNode), 0);
});
});

suite('Remove node', function() {
test('node is removed from parentNode', function() {
var divA = doc.childNodes[1].childNodes[1].childNodes[0];
Expand Down

0 comments on commit a77c352

Please sign in to comment.