Skip to content

Commit

Permalink
Implemented .innerText getter and setter for HTML elements. <title>.text
Browse files Browse the repository at this point in the history
now implemented in terms of this.  New unit tests.
  • Loading branch information
gleneivey committed Sep 13, 2009
1 parent 58a5df4 commit 16a4dcd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 43 deletions.
Binary file modified dist/env-js.jar
Binary file not shown.
35 changes: 21 additions & 14 deletions dist/env.js
Expand Up @@ -5143,6 +5143,20 @@ var HTMLElement = function(ownerDocument) {
};
HTMLElement.prototype = new DOMElement;
__extend__(HTMLElement.prototype, {
$recursivelyGatherTextFromNodeTree: function(aNode) {
var accumulateText = "";
var idx; var n;
for (idx=0;idx < aNode.childNodes.length;idx++){
n = aNode.childNodes.item(idx);
if (n.nodeType == DOMNode.TEXT_NODE)
accumulateText += n.data;
else
accumulateText += this.$recursivelyGatherTextFromNodeTree(n);
}

return accumulateText;
},

get className() {
return this.getAttribute("class")||'';
},
Expand Down Expand Up @@ -5188,6 +5202,12 @@ __extend__(HTMLElement.prototype, {
//Mark for garbage collection
doc = null;
},
get innerText(){
return this.$recursivelyGatherTextFromNodeTree(this);
},
set innerText(newText){
this.innerHTML = newText; // a paranoid would HTML-escape, but...
},
get lang() {
return this.getAttribute("lang");

Expand Down Expand Up @@ -7449,21 +7469,8 @@ var HTMLTitleElement = function(ownerDocument) {
};
HTMLTitleElement.prototype = new HTMLElement;
__extend__(HTMLTitleElement.prototype, {
$recursivelyGatherTextFromNodeTree: function(aNode) {
var accumulateText = "";
var idx; var n;
for (idx=0;idx < aNode.childNodes.length;idx++){
n = aNode.childNodes.item(idx);
if (n.nodeType == DOMNode.TEXT_NODE)
accumulateText += n.data;
else
accumulateText += this.$recursivelyGatherTextFromNodeTree(n);
}

return accumulateText;
},
get text() {
return this.$recursivelyGatherTextFromNodeTree(this);
return this.innerText;
},

set text(titleStr) {
Expand Down
35 changes: 21 additions & 14 deletions dist/env.rhino.js
Expand Up @@ -5781,6 +5781,20 @@ var HTMLElement = function(ownerDocument) {
};
HTMLElement.prototype = new DOMElement;
__extend__(HTMLElement.prototype, {
$recursivelyGatherTextFromNodeTree: function(aNode) {
var accumulateText = "";
var idx; var n;
for (idx=0;idx < aNode.childNodes.length;idx++){
n = aNode.childNodes.item(idx);
if (n.nodeType == DOMNode.TEXT_NODE)
accumulateText += n.data;
else
accumulateText += this.$recursivelyGatherTextFromNodeTree(n);
}

return accumulateText;
},

get className() {
return this.getAttribute("class")||'';
},
Expand Down Expand Up @@ -5826,6 +5840,12 @@ __extend__(HTMLElement.prototype, {
//Mark for garbage collection
doc = null;
},
get innerText(){
return this.$recursivelyGatherTextFromNodeTree(this);
},
set innerText(newText){
this.innerHTML = newText; // a paranoid would HTML-escape, but...
},
get lang() {
return this.getAttribute("lang");

Expand Down Expand Up @@ -8087,21 +8107,8 @@ var HTMLTitleElement = function(ownerDocument) {
};
HTMLTitleElement.prototype = new HTMLElement;
__extend__(HTMLTitleElement.prototype, {
$recursivelyGatherTextFromNodeTree: function(aNode) {
var accumulateText = "";
var idx; var n;
for (idx=0;idx < aNode.childNodes.length;idx++){
n = aNode.childNodes.item(idx);
if (n.nodeType == DOMNode.TEXT_NODE)
accumulateText += n.data;
else
accumulateText += this.$recursivelyGatherTextFromNodeTree(n);
}

return accumulateText;
},
get text() {
return this.$recursivelyGatherTextFromNodeTree(this);
return this.innerText;
},

set text(titleStr) {
Expand Down
20 changes: 20 additions & 0 deletions src/html/element.js
Expand Up @@ -10,6 +10,20 @@ var HTMLElement = function(ownerDocument) {
};
HTMLElement.prototype = new DOMElement;
__extend__(HTMLElement.prototype, {
$recursivelyGatherTextFromNodeTree: function(aNode) {
var accumulateText = "";
var idx; var n;
for (idx=0;idx < aNode.childNodes.length;idx++){
n = aNode.childNodes.item(idx);
if (n.nodeType == DOMNode.TEXT_NODE)
accumulateText += n.data;
else
accumulateText += this.$recursivelyGatherTextFromNodeTree(n);
}

return accumulateText;
},

get className() {
return this.getAttribute("class")||'';
},
Expand Down Expand Up @@ -55,6 +69,12 @@ __extend__(HTMLElement.prototype, {
//Mark for garbage collection
doc = null;
},
get innerText(){
return this.$recursivelyGatherTextFromNodeTree(this);
},
set innerText(newText){
this.innerHTML = newText; // a paranoid would HTML-escape, but...
},
get lang() {
return this.getAttribute("lang");

Expand Down
15 changes: 1 addition & 14 deletions src/html/title.js
Expand Up @@ -8,21 +8,8 @@ var HTMLTitleElement = function(ownerDocument) {
};
HTMLTitleElement.prototype = new HTMLElement;
__extend__(HTMLTitleElement.prototype, {
$recursivelyGatherTextFromNodeTree: function(aNode) {
var accumulateText = "";
var idx; var n;
for (idx=0;idx < aNode.childNodes.length;idx++){
n = aNode.childNodes.item(idx);
if (n.nodeType == DOMNode.TEXT_NODE)
accumulateText += n.data;
else
accumulateText += this.$recursivelyGatherTextFromNodeTree(n);
}

return accumulateText;
},
get text() {
return this.$recursivelyGatherTextFromNodeTree(this);
return this.innerText;
},

set text(titleStr) {
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Expand Up @@ -26,7 +26,8 @@ window.onload = function(){
// Load the tests
load(
"test/unit/dom.js",
"test/unit/window.js"
"test/unit/window.js",
"test/unit/elementmembers.js"
);
if (whichJarFile == "envjs")
load(
Expand Down
29 changes: 29 additions & 0 deletions test/unit/elementmembers.js
@@ -0,0 +1,29 @@
module("elementmembers");

// We ought to have test coverage for all members of all DOM objects, but
// until then, add test cases here for members as they are created

test("attributes common to all HTML elements", function() {
expect(4);

// tests for .innerText
var mtch = document.getElementById('dl').innerText.match(
/^See this blog entry for more information.\s+Here are/);
try{ ok(mtch && mtch.length > 0,
"dl.innerText returns the correct content");
}catch(e){print(e);}

mtch = document.getElementById('sndp').innerText.match(/^Everything insid/);
try{ ok(mtch && mtch.length > 0,
"p.innerText returns the correct content");
}catch(e){print(e);}

try{ ok(document.getElementById('sndp').innerText = "test text" || true,
"p.innerText= operates without exception");
}catch(e){print(e);}

try{ ok(document.getElementById('sndp').innerText == "test text",
"p.innerText= changes content of element");
}catch(e){print(e);}
});

0 comments on commit 16a4dcd

Please sign in to comment.