Skip to content

Commit

Permalink
Add Node's wholeText property
Browse files Browse the repository at this point in the history
Fixes #1397.
  • Loading branch information
jdanyow authored and domenic committed Dec 18, 2016
1 parent 3c50533 commit 45f4458
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Jason Priestley <[email protected]>
jden <[email protected]>
Jean-Francois Remy <[email protected]>
Jeff Carpenter <[email protected]>
Jeremy Danyow <[email protected]>
Jérémy Lal <[email protected]>
Jerry Sievert <[email protected]>
Jimmy Mabey <[email protected]>
Expand Down
16 changes: 15 additions & 1 deletion lib/jsdom/living/nodes/Text-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@ class TextImpl extends CharacterDataImpl {
// TODO: range stuff
}

// TODO: wholeText property
get wholeText() {
let wholeText = this.textContent;
let next;
let current = this;
while ((next = domSymbolTree.previousSibling(current)) && next.nodeType === NODE_TYPE.TEXT_NODE) {
wholeText = next.textContent + wholeText;
current = next;
}
current = this;
while ((next = domSymbolTree.nextSibling(current)) && next.nodeType === NODE_TYPE.TEXT_NODE) {
wholeText += next.textContent;
current = next;
}
return wholeText;
}
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/nodes/Text.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Exposed=Window]
interface Text : CharacterData {
[NewObject] Text splitText(unsigned long offset);
// readonly attribute DOMString wholeText;
readonly attribute DOMString wholeText;
};
1 change: 1 addition & 0 deletions test/web-platform-tests/to-upstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("Local tests in Web Platform Test format (to-upstream)", () => {
"dom/nodes/Node-cloneNode-svg.html",
"dom/nodes/Node-isEqualNode.html",
"dom/nodes/Node-mutation-adoptNode.html",
"dom/nodes/Text-wholeText.html",
"domparsing/DOMParser-dont-upstream.html",
"domparsing/insert-adjacent.html",
"FileAPI/blob/Blob-isClosed.html",
Expand Down
46 changes: 46 additions & 0 deletions test/web-platform-tests/to-upstream/dom/nodes/Text-wholeText.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Text - wholeText</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-text-wholetext">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
"use strict";

test(() => {
const parent = document.createElement("div");

const t1 = document.createTextNode("a");
const t2 = document.createTextNode("b");
const t3 = document.createTextNode("c");

assert_equals(t1.wholeText, t1.textContent);

parent.appendChild(t1);

assert_equals(t1.wholeText, t1.textContent);

parent.appendChild(t2);

assert_equals(t1.wholeText, t1.textContent + t2.textContent);
assert_equals(t2.wholeText, t1.textContent + t2.textContent);

parent.appendChild(t3);

assert_equals(t1.wholeText, t1.textContent + t2.textContent + t3.textContent);
assert_equals(t2.wholeText, t1.textContent + t2.textContent + t3.textContent);
assert_equals(t3.wholeText, t1.textContent + t2.textContent + t3.textContent);

const a = document.createElement("a");
a.textContent = "I'm an Anchor";
parent.insertBefore(a, t3);

const span = document.createElement("span");
span.textContent = "I'm a Span";
parent.appendChild(document.createElement("span"));

assert_equals(t1.wholeText, t1.textContent + t2.textContent);
assert_equals(t2.wholeText, t1.textContent + t2.textContent);
assert_equals(t3.wholeText, t3.textContent);
}, "wholeText returns text of all Text nodes logically adjacent to the node, in document order.");
</script>

0 comments on commit 45f4458

Please sign in to comment.