forked from jeresig/env-js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git@github.com:thatcher/env-js
- Loading branch information
Showing
4 changed files
with
93 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,112 @@ | ||
// environment mocking for parser | ||
$w = { } | ||
$env = { debug: function() {} } | ||
$openingWindow = $parentWindow = $initTop = null; | ||
module("Html5Parser"); | ||
|
||
load("src/window/window.js", "src/dom/parser.js", "src/dom/entities.js"); | ||
var should = function(msg, options){ | ||
try{ | ||
if(options.be&&options.be=='equal'){ | ||
equals( | ||
options.expected, | ||
options.actual, | ||
msg | ||
); | ||
}else if(options.be&&options.be==='safe'){ | ||
options.test(); | ||
ok(true, msg); | ||
}else{ | ||
ok(false, 'unknown test '+options.be); | ||
} | ||
}catch(e){ | ||
//no nothing | ||
equals(true, false, options.msg||'This test failed.'); | ||
}finally{ | ||
//TODO: might as well keep score here | ||
return this; | ||
} | ||
}; | ||
|
||
module("parser"); | ||
|
||
test("HTML Standard Entities: Spot Check", function() { | ||
|
||
expect(3); | ||
|
||
test("XML Standard Entities: Spot Check", function() { | ||
|
||
expect(2); | ||
var htmlstr = | ||
"<div id='xmlentity'><Hello>, "W'rld"!</div>", | ||
"<div id='xmlentity' \ | ||
style='<Hello>, "W'rld"!'\ | ||
><Hello>, "W'rld"!</div>", | ||
domParser = new DOMParser(), | ||
doc = domParser.parseFromString(htmlstr), | ||
actual, | ||
expected; | ||
doc = domParser.parseFromString(htmlstr); | ||
|
||
actual = doc. | ||
getElementById('xmlentity'). | ||
childNodes[0]. | ||
nodeValue; | ||
expected = '<Hello>, "W\'rld"!'; | ||
equals( | ||
actual, | ||
expected, | ||
"parser replaces entities" | ||
); | ||
|
||
actual = doc. | ||
getElementById('xmlentity'). | ||
innerHTML; | ||
expected = '<Hello>, "W\'rld"!'; | ||
equals( | ||
actual, | ||
expected, | ||
"innerHTML serializes back only &, < and > for TextNode" | ||
); | ||
should("Replace entities at nodeValue",{ | ||
be:'equal', | ||
actual : doc. | ||
getElementById('xmlentity'). | ||
childNodes[0]. | ||
nodeValue, | ||
expected : '<Hello>, "W\'rld"!' | ||
}). | ||
should("serialize only &, < and > for TextNode with innerHTML",{ | ||
be: 'equal', | ||
actual : doc. | ||
getElementById('xmlentity'). | ||
innerHTML, | ||
expected : '<Hello>, "W\'rld"!' | ||
}); | ||
|
||
}); | ||
|
||
test("HTML Standard Entities: Spot Check", function() { | ||
|
||
htmlstr = "<div id='htmlentity'>" & < > "+ | ||
expect(1); | ||
var htmlstr = "<div id='htmlentity'>" & < > "+ | ||
" © ® ¥ ¶ " + | ||
"Ê Õ æ ÷ Κ θ "+ | ||
"• … ™ ⇒ ∑ ♣ " + | ||
"  —</body></html>"; | ||
expected = '" & < > '+ | ||
'\xA0 \xA9 \xAE \xA5 \xB6 '+ | ||
'\xCA \xD5 \xE6 \xF7 \u039A \u03B8 '+ | ||
'\u2022 \u2026 \u2122 \u21D2 \u2211 \u2663 '+ | ||
'\u2002 \u2014'; | ||
|
||
domParser = new DOMParser(); | ||
doc = domParser.parseFromString(htmlstr); | ||
actual = doc. | ||
getElementById('htmlentity'). | ||
innerHTML; | ||
"  —</div>", | ||
domParser = new DOMParser(), | ||
doc = domParser.parseFromString(htmlstr); | ||
|
||
equals( | ||
actual, | ||
expected, | ||
"html entities are not serialized back with innerHTML" | ||
); | ||
should("serialize only &, < and > for TextNode with innerHTML",{ | ||
be:'equal', | ||
actual:doc. | ||
getElementById('htmlentity'). | ||
innerHTML, | ||
expected : '" & < > '+ | ||
'\xA0 \xA9 \xAE \xA5 \xB6 '+ | ||
'\xCA \xD5 \xE6 \xF7 \u039A \u03B8 '+ | ||
'\u2022 \u2026 \u2122 \u21D2 \u2211 \u2663 '+ | ||
'\u2002 \u2014' | ||
}); | ||
|
||
}); | ||
|
||
test("HTML Serialization Convention", function(){ | ||
test("Serialization Conventions", function(){ | ||
|
||
}); | ||
|
||
test("Ugly HTML Parsing", function() { | ||
|
||
test("Ugly HTML", function() { | ||
expect(1); | ||
//setup | ||
var domParser = new DOMParser(), | ||
html = '<div id="pig"><p>this is a pig... 'oink! oink!'</div>', | ||
doc = domParser.parseFromString(html), | ||
expected = '<div id="pig"><p>this is a pig... \'oink! oink!\'</p></div>', | ||
actual = doc.getElementById('pig').xml; | ||
|
||
equals( | ||
actual, | ||
expected, | ||
'got expected well formed html' | ||
); | ||
doc = domParser.parseFromString(html); | ||
|
||
should('correct the unclosed p tag',{ | ||
be:'equal', | ||
actual:doc. | ||
getElementById('pig'). | ||
xml, | ||
expected:'<div id="pig"><p>this is a pig... \'oink! oink!\'</p></div>' | ||
}); | ||
|
||
}); | ||
|
||
test("Really Ugly HTML Parsing", function() { | ||
|
||
expect(1); | ||
|
||
should('parse the document without error',{ | ||
be:'safe', | ||
test:function(){ | ||
window.open('html/malformed.html'); | ||
} | ||
}); | ||
|
||
}); |