Skip to content

Commit

Permalink
Merge pull request #1311 from Ms2ger/createHTMLDocument
Browse files Browse the repository at this point in the history
Cleanup and improve DOMImplementation#createHTMLDocument tests; r=zcorpan
  • Loading branch information
Ms2ger committed Nov 18, 2014
2 parents 361fefc + e5f5511 commit e5c5762
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 58 deletions.
88 changes: 49 additions & 39 deletions dom/nodes/DOMImplementation-createHTMLDocument.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,58 @@
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-documentelement">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="DOMImplementation-createHTMLDocument.js"></script>
<div id="log"></div>
<script>
function checkDoc(title, expectedtitle, normalizedtitle) {
test(function() {
var doc = document.implementation.createHTMLDocument(title);
assert_equals(doc.doctype.name, "html")
assert_equals(doc.doctype.publicId, "")
assert_equals(doc.doctype.systemId, "")
assert_equals(doc.documentElement.localName, "html")
assert_equals(doc.documentElement.firstChild.localName, "head")
if (title !== undefined) {
assert_equals(doc.documentElement.firstChild.childNodes.length, 1)
assert_equals(doc.documentElement.firstChild.firstChild.localName, "title")
assert_equals(doc.documentElement.firstChild.firstChild.firstChild.data,
expectedtitle)
} else {
assert_equals(doc.documentElement.firstChild.childNodes.length, 0)
}
assert_equals(doc.documentElement.lastChild.localName, "body")
assert_equals(doc.documentElement.lastChild.childNodes.length, 0)
})
}
checkDoc("", "", "")
checkDoc(null, "null", "null")
checkDoc(undefined, "", "")
checkDoc("foo bar baz", "foo bar baz", "foo bar baz")
checkDoc("foo\t\tbar baz", "foo\t\tbar baz", "foo bar baz")
checkDoc("foo\n\nbar baz", "foo\n\nbar baz", "foo bar baz")
checkDoc("foo\f\fbar baz", "foo\f\fbar baz", "foo bar baz")
checkDoc("foo\r\rbar baz", "foo\r\rbar baz", "foo bar baz")
createHTMLDocuments(function(doc, expectedtitle, normalizedtitle) {
assert_true(doc instanceof Document, "Should be a Document")
assert_true(doc instanceof Node, "Should be a Node")
assert_equals(doc.childNodes.length, 2,
"Document should have two child nodes")

test(function() {
var doc = document.implementation.createHTMLDocument();
assert_equals(doc.doctype.name, "html")
assert_equals(doc.doctype.publicId, "")
assert_equals(doc.doctype.systemId, "")
assert_equals(doc.documentElement.localName, "html")
assert_equals(doc.documentElement.firstChild.localName, "head")
assert_equals(doc.documentElement.firstChild.childNodes.length, 0)
assert_equals(doc.documentElement.lastChild.localName, "body")
assert_equals(doc.documentElement.lastChild.childNodes.length, 0)
}, "Missing title argument");
var doctype = doc.doctype
assert_true(doctype instanceof DocumentType,
"Doctype should be a DocumentType")
assert_true(doctype instanceof Node, "Doctype should be a Node")
assert_equals(doctype.name, "html")
assert_equals(doctype.publicId, "")
assert_equals(doctype.systemId, "")

var documentElement = doc.documentElement
assert_true(documentElement instanceof HTMLHtmlElement,
"Document element should be a HTMLHtmlElement")
assert_equals(documentElement.childNodes.length, 2,
"Document element should have two child nodes")
assert_equals(documentElement.localName, "html")
assert_equals(documentElement.tagName, "HTML")

var head = documentElement.firstChild
assert_true(head instanceof HTMLHeadElement,
"Head should be a HTMLHeadElement")
assert_equals(head.localName, "head")
assert_equals(head.tagName, "HEAD")

if (expectedtitle !== undefined) {
assert_equals(head.childNodes.length, 1)

var title = head.firstChild
assert_true(title instanceof HTMLTitleElement,
"Title should be a HTMLTitleElement")
assert_equals(title.localName, "title")
assert_equals(title.tagName, "TITLE")
assert_equals(title.childNodes.length, 1)
assert_equals(title.firstChild.data, expectedtitle)
} else {
assert_equals(head.childNodes.length, 0)
}

var body = documentElement.lastChild
assert_true(body instanceof HTMLBodyElement,
"Body should be a HTMLBodyElement")
assert_equals(body.localName, "body")
assert_equals(body.tagName, "BODY")
assert_equals(body.childNodes.length, 0)
})

test(function() {
var doc = document.implementation.createHTMLDocument("test");
Expand Down
25 changes: 25 additions & 0 deletions dom/nodes/DOMImplementation-createHTMLDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function createHTMLDocuments(checkDoc) {
var tests = [
["", "", ""],
[null, "null", "null"],
[undefined, undefined, ""],
["foo bar baz", "foo bar baz", "foo bar baz"],
["foo\t\tbar baz", "foo\t\tbar baz", "foo bar baz"],
["foo\n\nbar baz", "foo\n\nbar baz", "foo bar baz"],
["foo\f\fbar baz", "foo\f\fbar baz", "foo bar baz"],
["foo\r\rbar baz", "foo\r\rbar baz", "foo bar baz"],
]

tests.forEach(function(t, i) {
var title = t[0], expectedtitle = t[1], normalizedtitle = t[2]
test(function() {
var doc = document.implementation.createHTMLDocument(title);
checkDoc(doc, expectedtitle, normalizedtitle)
}, "createHTMLDocument test " + i + ": " + t.map(function(el) { return format_value(el) }))
})

test(function() {
var doc = document.implementation.createHTMLDocument();
checkDoc(doc, undefined, "")
}, "Missing title argument");
}
23 changes: 4 additions & 19 deletions html/dom/documents/dom-tree-accessors/document.title-07.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@
<title>Document.title and DOMImplementation.createHTMLDocument</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/dom/nodes/DOMImplementation-createHTMLDocument.js"></script>
<div id="log"></div>
<script>
function checkDoc(title, expectedtitle, normalizedtitle) {
test(function() {
var doc = document.implementation.createHTMLDocument(title);
assert_equals(doc.title, normalizedtitle)
})
}
checkDoc("", "", "")
checkDoc(null, "null", "null")
checkDoc(undefined, "", "")
checkDoc("foo bar baz", "foo bar baz", "foo bar baz")
checkDoc("foo\t\tbar baz", "foo\t\tbar baz", "foo bar baz")
checkDoc("foo\n\nbar baz", "foo\n\nbar baz", "foo bar baz")
checkDoc("foo\f\fbar baz", "foo\f\fbar baz", "foo bar baz")
checkDoc("foo\r\rbar baz", "foo\r\rbar baz", "foo bar baz")

test(function() {
var doc = document.implementation.createHTMLDocument()
assert_equals(doc.title, "")
}, "Missing title argument");
createHTMLDocuments(function(doc, expectedtitle, normalizedtitle) {
assert_equals(doc.title, normalizedtitle)
})
</script>

0 comments on commit e5c5762

Please sign in to comment.