Skip to content

Commit

Permalink
seperate DOM and HTML parsers/creates
Browse files Browse the repository at this point in the history
  • Loading branch information
smparkes committed Nov 24, 2009
1 parent 3a50a6f commit 9f1124d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
25 changes: 21 additions & 4 deletions src/dom/implementation.js
Expand Up @@ -35,9 +35,20 @@ __extend__(DOMImplementation.prototype,{
return new DOMDocumentType();
},
createDocument : function(nsuri, qname, doctype){
//TODO - this currently returns an empty doc
//but needs to handle the args
return new HTMLDocument($implementation, null, "");
//TODO - this currently returns an empty doc
//but needs to handle the args
return new Document($implementation, null);
},
createHTMLDocument : function(title){
var doc = new HTMLDocument($implementation, null, "");
var html = doc.createElement("html"); doc.appendChild(html);
var head = doc.createElement("head"); html.appendChild(head);
var body = doc.createElement("body"); html.appendChild(body);
var t = doc.createElement("title"); head.appendChild(t);
if( title) {
t.appendChild(doc.createTextNode(title));
}
return doc;
},
translateErrCode : function(code) {
//convert DOMException Code to human readable error message;
Expand Down Expand Up @@ -571,4 +582,10 @@ function __parseQName__(qualifiedName) {
$debug("Initializing document.implementation");
var $implementation = new DOMImplementation();
$implementation.namespaceAware = false;
$implementation.errorChecking = false;
$implementation.errorChecking = false;

// Local Variables:
// espresso-indent-level:4
// c-basic-offset:4
// tab-width:4
// End:
21 changes: 16 additions & 5 deletions src/html/element.js
Expand Up @@ -41,9 +41,9 @@ __extend__(HTMLElement.prototype, {
set innerHTML(html){
//Should be replaced with HTMLPARSER usage
//$debug('SETTING INNER HTML ('+this+'+'+html.substring(0,64));
var doc = new DOMParser().
parseFromString(html);
var parent = doc.body;
var tmp = new HTMLDocument($implementation,null,"");
$w.parseHtmlDocument(html,tmp,null,null);
var parent = tmp.body;
while(this.firstChild != null){
this.removeChild( this.firstChild );
}
Expand All @@ -54,13 +54,18 @@ __extend__(HTMLElement.prototype, {
this.appendChild( importedNode );
}
//Mark for garbage collection
doc = null;
tmp = null;
},
get innerText(){
return __recursivelyGatherText__(this);
},
set innerText(newText){
this.innerHTML = newText; // a paranoid would HTML-escape, but...
this.innerHTML = "<div>"+newText+"</div>";
while(this.firstChild != null){
this.removeChild( this.firstChild );
}
var text = this.ownerDocument.createTextNode(newText);
this.appendChild(text);
},
get lang() {
return this.getAttribute("lang");
Expand Down Expand Up @@ -289,3 +294,9 @@ var __blur__ = function(element){
};

$w.HTMLElement = HTMLElement;

// Local Variables:
// espresso-indent-level:4
// c-basic-offset:4
// tab-width:4
// End:
28 changes: 18 additions & 10 deletions test/unit/parser.js
Expand Up @@ -30,9 +30,10 @@ test("XML Standard Entities: Spot Check", function() {
var htmlstr =
"<div id='xmlentity' \
style='&lt;Hello&gt;, &quot;W&apos;rld&quot;!'\
>&lt;Hello&gt;, &quot;W&apos;rld&quot;!</div>",
domParser = new DOMParser(),
doc = domParser.parseFromString(htmlstr);
>&lt;Hello&gt;, &quot;W&apos;rld&quot;!</div>";

var doc = document.implementation.createHTMLDocument();
doc.body.innerHTML = htmlstr;

should("Replace entities at nodeValue",{
be:'equal',
Expand All @@ -59,10 +60,11 @@ test("HTML Standard Entities: Spot Check", function() {
"&nbsp; &copy; &reg; &yen; &para; " +
"&Ecirc; &Otilde; &aelig; &divide; &Kappa; &theta; "+
"&bull; &hellip; &trade; &rArr; &sum; &clubs; " +
"&ensp; &mdash;</div>",
domParser = new DOMParser(),
doc = domParser.parseFromString(htmlstr);
"&ensp; &mdash;</div>";

var doc = document.implementation.createHTMLDocument();
doc.body.innerHTML = htmlstr;

should("serialize only &amp;, &lt; and &gt; for TextNode with innerHTML",{
be:'equal',
actual:doc.
Expand All @@ -84,10 +86,10 @@ test("Serialization Conventions", function(){
test("Ugly HTML Parsing", function() {

expect(1);
var domParser = new DOMParser(),
html = '<div id="pig"><p>this is a pig... &apos;oink! oink!&apos;</div>',
doc = domParser.parseFromString(html);

var doc = document.implementation.createHTMLDocument();
doc.body.innerHTML = '<div id="pig"><p>this is a pig... &apos;oink! oink!&apos;</div>';

should('correct the unclosed p tag',{
be:'equal',
actual:doc.
Expand All @@ -110,3 +112,9 @@ test("Really Ugly HTML Parsing", function() {
});

});

// Local Variables:
// espresso-indent-level:4
// c-basic-offset:4
// tab-width:4
// End:

0 comments on commit 9f1124d

Please sign in to comment.