From 28da68b474ac60e90e71e60c0e250669ec70f221 Mon Sep 17 00:00:00 2001 From: Pete Blois Date: Mon, 13 Jan 2014 14:57:18 -0800 Subject: [PATCH] Make Node.textContent ignore comment nodes Currently it is incorrectly including the text from comment nodes in generating textContent. --- src/wrappers/Node.js | 4 +++- test/js/Node.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/wrappers/Node.js b/src/wrappers/Node.js index ea7498c..fac1b17 100644 --- a/src/wrappers/Node.js +++ b/src/wrappers/Node.js @@ -543,7 +543,9 @@ // are no shadow trees below or above the context node. var s = ''; for (var child = this.firstChild; child; child = child.nextSibling) { - s += child.textContent; + if (child.nodeType != Node.COMMENT_NODE) { + s += child.textContent; + } } return s; }, diff --git a/test/js/Node.js b/test/js/Node.js index d170c82..1488abb 100644 --- a/test/js/Node.js +++ b/test/js/Node.js @@ -298,10 +298,21 @@ suite('Node', function() { parent.insertBefore(c3); parent.insertBefore(c2, c3); parent.insertBefore(c1, c2); - + assert.equal(parent.firstChild, c1); assert.equal(c1.nextElementSibling, c2); assert.equal(c2.nextElementSibling, c3); }); + test('textContent of comment', function() { + var comment = document.createComment('abc'); + assert.equal(comment.textContent, 'abc'); + }); + + test('textContent ignores comments', function() { + var div = document.createElement('div'); + div.innerHTML = 'abef'; + assert.equal(div.textContent, 'abef'); + }); + });