Skip to content

Commit

Permalink
Implemented document.title property and HTMLTitleElement. (Was in pro…
Browse files Browse the repository at this point in the history
…cess

of creating test cases for some iframe-related work when I realized these
were missing, which is why the test case is in "window.js" and there's
the extra new material in "iframe.html".)
  • Loading branch information
gleneivey committed Jun 24, 2009
1 parent 0b0a1a7 commit 512fe39
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.xml
Expand Up @@ -105,6 +105,7 @@
<fileset dir="${SRC_DIR}" includes="html/table.js" />
<fileset dir="${SRC_DIR}" includes="html/tbody-thead-tfoot.js" />
<fileset dir="${SRC_DIR}" includes="html/td-th.js" />
<fileset dir="${SRC_DIR}" includes="html/title.js" />
<fileset dir="${SRC_DIR}" includes="html/tr.js" />
<fileset dir="${SRC_DIR}" includes="serializer/xml.js" />
<fileset dir="${SRC_DIR}" includes="xpath/expression.js" />
Expand Down
61 changes: 58 additions & 3 deletions dist/env.js
Expand Up @@ -4810,7 +4810,6 @@ var HTMLDocument = function(implementation) {
this.DOMDocument = DOMDocument;
this.DOMDocument(implementation);

this.title = "";
this._refferer = "";
this._domain;
this._open = false;
Expand Down Expand Up @@ -4869,7 +4868,7 @@ __extend__(HTMLDocument.prototype, {
else if(tagName.match(/TBODY|TFOOT|THEAD/)) {node = new HTMLSectionElement(this);}
else if(tagName.match(/TD|TH/)) {node = new HTMLTableCellElement(this);}
else if(tagName.match(/TEXTAREA/)) {node = new HTMLElement(this);}
else if(tagName.match(/TITLE/)) {node = new HTMLElement(this);}
else if(tagName.match(/TITLE/)) {node = new HTMLTitleElement(this);}
else if(tagName.match(/TR/)) {node = new HTMLTableRowElement(this);}
else if(tagName.match(/UL/)) {node = new HTMLElement(this);}
else{
Expand Down Expand Up @@ -4897,6 +4896,29 @@ __extend__(HTMLDocument.prototype, {
return this.replaceNode(this.body,html);

},

get title(){
var titleArray = this.getElementsByTagName('title');
if (titleArray.length < 1)
return "";
return titleArray[0].text;
},
set title(titleStr){
titleArray = this.getElementsByTagName('title');
if (titleArray.length < 1){
// need to make a new element and add it to "head"
var titleElem = new HTMLTitleElement(this);
titleElem.text = titleStr;
var headArray = this.getElementsByTagName('head');
if (headArray.length < 1)
return; // ill-formed, just give up.....
headArray[0].appendChild(titleElem);
}
else {
titleArray[0].text = titleStr;
}
},

//set/get cookie see cookie.js
get domain(){
return this._domain||window.location.domain;
Expand Down Expand Up @@ -7030,7 +7052,40 @@ __extend__(HTMLTableCellElement.prototype, {

});

$w.HTMLTableCellElement = HTMLTableCellElement;$debug("Defining HTMLTableRowElement");
$w.HTMLTableCellElement = HTMLTableCellElement;$debug("Defining HTMLTitleElement");
/*
* HTMLTitleElement - DOM Level 2
*/
var HTMLTitleElement = function(ownerDocument) {
this.HTMLElement = HTMLElement;
this.HTMLElement(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);
},

set text(titleStr) {
this.innerHTML = titleStr; // if paranoid, would error on embedded HTML
}
});

$w.HTMLTitleElement = HTMLTitleElement;
$debug("Defining HTMLTableRowElement");
/*
* HTMLRowElement - DOM Level 2
* Implementation Provided by Steven Wood
Expand Down
61 changes: 58 additions & 3 deletions dist/env.rhino.js
Expand Up @@ -5203,7 +5203,6 @@ var HTMLDocument = function(implementation) {
this.DOMDocument = DOMDocument;
this.DOMDocument(implementation);

this.title = "";
this._refferer = "";
this._domain;
this._open = false;
Expand Down Expand Up @@ -5262,7 +5261,7 @@ __extend__(HTMLDocument.prototype, {
else if(tagName.match(/TBODY|TFOOT|THEAD/)) {node = new HTMLSectionElement(this);}
else if(tagName.match(/TD|TH/)) {node = new HTMLTableCellElement(this);}
else if(tagName.match(/TEXTAREA/)) {node = new HTMLElement(this);}
else if(tagName.match(/TITLE/)) {node = new HTMLElement(this);}
else if(tagName.match(/TITLE/)) {node = new HTMLTitleElement(this);}
else if(tagName.match(/TR/)) {node = new HTMLTableRowElement(this);}
else if(tagName.match(/UL/)) {node = new HTMLElement(this);}
else{
Expand Down Expand Up @@ -5290,6 +5289,29 @@ __extend__(HTMLDocument.prototype, {
return this.replaceNode(this.body,html);

},

get title(){
var titleArray = this.getElementsByTagName('title');
if (titleArray.length < 1)
return "";
return titleArray[0].text;
},
set title(titleStr){
titleArray = this.getElementsByTagName('title');
if (titleArray.length < 1){
// need to make a new element and add it to "head"
var titleElem = new HTMLTitleElement(this);
titleElem.text = titleStr;
var headArray = this.getElementsByTagName('head');
if (headArray.length < 1)
return; // ill-formed, just give up.....
headArray[0].appendChild(titleElem);
}
else {
titleArray[0].text = titleStr;
}
},

//set/get cookie see cookie.js
get domain(){
return this._domain||window.location.domain;
Expand Down Expand Up @@ -7423,7 +7445,40 @@ __extend__(HTMLTableCellElement.prototype, {

});

$w.HTMLTableCellElement = HTMLTableCellElement;$debug("Defining HTMLTableRowElement");
$w.HTMLTableCellElement = HTMLTableCellElement;$debug("Defining HTMLTitleElement");
/*
* HTMLTitleElement - DOM Level 2
*/
var HTMLTitleElement = function(ownerDocument) {
this.HTMLElement = HTMLElement;
this.HTMLElement(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);
},

set text(titleStr) {
this.innerHTML = titleStr; // if paranoid, would error on embedded HTML
}
});

$w.HTMLTitleElement = HTMLTitleElement;
$debug("Defining HTMLTableRowElement");
/*
* HTMLRowElement - DOM Level 2
* Implementation Provided by Steven Wood
Expand Down
26 changes: 24 additions & 2 deletions src/html/document.js
Expand Up @@ -12,7 +12,6 @@ var HTMLDocument = function(implementation) {
this.DOMDocument = DOMDocument;
this.DOMDocument(implementation);

this.title = "";
this._refferer = "";
this._domain;
this._open = false;
Expand Down Expand Up @@ -71,7 +70,7 @@ __extend__(HTMLDocument.prototype, {
else if(tagName.match(/TBODY|TFOOT|THEAD/)) {node = new HTMLSectionElement(this);}
else if(tagName.match(/TD|TH/)) {node = new HTMLTableCellElement(this);}
else if(tagName.match(/TEXTAREA/)) {node = new HTMLElement(this);}
else if(tagName.match(/TITLE/)) {node = new HTMLElement(this);}
else if(tagName.match(/TITLE/)) {node = new HTMLTitleElement(this);}
else if(tagName.match(/TR/)) {node = new HTMLTableRowElement(this);}
else if(tagName.match(/UL/)) {node = new HTMLElement(this);}
else{
Expand Down Expand Up @@ -99,6 +98,29 @@ __extend__(HTMLDocument.prototype, {
return this.replaceNode(this.body,html);

},

get title(){
var titleArray = this.getElementsByTagName('title');
if (titleArray.length < 1)
return "";
return titleArray[0].text;
},
set title(titleStr){
titleArray = this.getElementsByTagName('title');
if (titleArray.length < 1){
// need to make a new element and add it to "head"
var titleElem = new HTMLTitleElement(this);
titleElem.text = titleStr;
var headArray = this.getElementsByTagName('head');
if (headArray.length < 1)
return; // ill-formed, just give up.....
headArray[0].appendChild(titleElem);
}
else {
titleArray[0].text = titleStr;
}
},

//set/get cookie see cookie.js
get domain(){
return this._domain||window.location.domain;
Expand Down
23 changes: 21 additions & 2 deletions src/html/title.js
Expand Up @@ -8,7 +8,26 @@ var HTMLTitleElement = function(ownerDocument) {
};
HTMLTitleElement.prototype = new HTMLElement;
__extend__(HTMLTitleElement.prototype, {
//TODO
$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);
},

set text(titleStr) {
this.innerHTML = titleStr; // if paranoid, would error on embedded HTML
}
});

$w.HTMLTitleElement = HTMLTitleElement;
$w.HTMLTitleElement = HTMLTitleElement;
27 changes: 26 additions & 1 deletion test/html/iframe.html
Expand Up @@ -4,9 +4,34 @@
<head>
<title>Content document for IFRAME element in env.js unit test suite</title>
</head>
<body>
<body onload="checkEventsWithinIframe(this);">
<p id="anElementWithText">
This is the text content of a paragraph element.
</p>

<!-- elements and scripting here match test cases in ../unit/window.js -->
<script>//ensure that we can execute JS inline while loading
document.write(
'\x3cp id="js_generated_p"\x3eDynamically-generated\x3c/p\x3e');
</script>

<script>//verify that our "document" object actually points to this page
document.write('\x3cp id="internalDocRefResult"\x3e');
if (document.getElementById('anElementWithText'))
document.write("First paragraph element exists-found.");
else
document.write("Eeek! Didn't find paragraph id=anElementWithText.");
document.write('\x3c/p\x3e');
</script>

<script>// append 'p id=appended "An appended paragraph"' to doc....
function checkEventsWithinIframe(body_elem){
var t = document.createTextNode("An appended paragraph");
var p = document.createElement("p");
p.setAttribute("id", "appended");
p.appendChild(t);
body_elem.appendChild(p);
}
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions test/unit/window.js
Expand Up @@ -18,3 +18,16 @@ test("Window Global Scope Equivalence", function() {
ok( window.$$$$$ === "12345", "Property is in window scope." );

});


test("Window scope in iframe isolated", function() {
expect(1);

// test cases here rely on JS in ../html/iframe.html
var idoc = document.getElementById('loadediframe').contentDocument;
var mtch = idoc.title.match(/IFRAME/);
try{ok (mtch && mtch.length > 0,
"Can get 'document' object from test iframe");
}catch(e){print(e);}
});

0 comments on commit 512fe39

Please sign in to comment.