Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Override Node.normalize to avoid processing shadowroots
Browse files Browse the repository at this point in the history
Add tests for Node.normalize

Also with shadowroot

Follow spec to modify the first of a contiguous TextNode range
  • Loading branch information
dfreedm committed Jan 29, 2014
1 parent c87e879 commit 7ed4c13
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/wrappers/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@
return p && p.invalidateShadowRenderer();
}

function cleanupNodes(nodes) {
for (var i = 0, n; i < nodes.length; i++) {
n = nodes[i];
n.parentNode.removeChild(n);
}
}

var OriginalNode = window.Node;

/**
Expand Down Expand Up @@ -629,6 +636,43 @@
// This only wraps, it therefore only operates on the composed DOM and not
// the logical DOM.
return originalCompareDocumentPosition.call(this.impl, unwrap(otherNode));
},

normalize: function() {
var nodes = snapshotNodeList(this.childNodes);
var remNodes = [];
var s = '';
var modNode;

for (var i = 0, n; i < nodes.length; i++) {
n = nodes[i];
if (n.nodeType === Node.TEXT_NODE) {
if (!modNode && !n.data.length)
this.removeNode(n);
else if (!modNode)
modNode = n;
else {
s += n.data;
remNodes.push(n);
}
} else {
if (modNode && remNodes.length) {
modNode.data += s;
cleanUpNodes(remNodes);
}
remNodes = [];
s = '';
modNode = null;
if (n.childNodes.length)
n.normalize();
}
}

// handle case where >1 text nodes are the last children
if (modNode && remNodes.length) {
modNode.data += s;
cleanupNodes(remNodes);
}
}
});

Expand Down
44 changes: 44 additions & 0 deletions test/js/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,48 @@ suite('Node', function() {
assert.equal(div.textContent, 'abef');
});

test('normalize', function() {
var div = document.createElement('div');
div.appendChild(document.createTextNode('foo\n'));
var span = document.createElement('span');
span.appendChild(document.createTextNode('buzz'));
span.appendChild(document.createTextNode('quux'));
div.appendChild(span);
div.appendChild(document.createTextNode('bar\n'));
assert.equal(div.textContent, 'foo\nbuzzquuxbar\n');

div.normalize();

assert.equal(div.textContent, 'foo\nbuzzquuxbar\n');
assert.equal(div.childNodes.length, 3);
assert.equal(div.firstChild.textContent, 'foo\n');
assert.equal(div.firstChild.nextSibling, span);
assert.equal(span.childNodes.length, 1);
assert.equal(span.firstChild.textContent, 'buzzquux');
assert.equal(span.nextSibling, div.lastChild);
assert.equal(div.lastChild.textContent, 'bar\n');

});

test('normalize with shadowroot', function() {
var div = document.createElement('div');
div.appendChild(document.createTextNode('foo\n'));
var sr = div.createShadowRoot();
sr.appendChild(document.createTextNode('buzz'));
sr.appendChild(document.createTextNode('quux'));
div.appendChild(document.createTextNode('bar\n'));
assert.equal(div.textContent, 'foo\nbar\n');
assert.equal(sr.textContent, 'buzzquux');

div.normalize();

assert.equal(div.textContent, 'foo\nbar\n');
assert.equal(sr.textContent, 'buzzquux');
assert.equal(div.childNodes.length, 1);
assert.equal(div.firstChild.textContent, 'foo\nbar\n');
assert.equal(sr.childNodes.length, 2);
assert.equal(sr.firstChild.textContent, 'buzz');
assert.equal(sr.firstChild.nextSibling.textContent, 'quux');
});

});

0 comments on commit 7ed4c13

Please sign in to comment.