Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.

Commit 5dc9ad1

Browse files
committed
Merge pull request #281 from webcomponents/self-closing-tags-in-xhtml
Handle XHTML self-closing void tag behavior
2 parents 9590a03 + ffba906 commit 5dc9ad1

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/ShadowDOM/wrappers/Document.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@
338338
setWrapper(impl, this);
339339
}
340340

341+
var originalCreateDocument = document.implementation.createDocument;
342+
DOMImplementation.prototype.createDocument = function() {
343+
arguments[2] = unwrap(arguments[2]);
344+
return wrap(originalCreateDocument.apply(unsafeUnwrap(this), arguments));
345+
};
346+
341347
function wrapImplMethod(constructor, name) {
342348
var original = document.implementation[name];
343349
constructor.prototype[name] = function() {
@@ -353,7 +359,6 @@
353359
}
354360

355361
wrapImplMethod(DOMImplementation, 'createDocumentType');
356-
wrapImplMethod(DOMImplementation, 'createDocument');
357362
wrapImplMethod(DOMImplementation, 'createHTMLDocument');
358363
forwardImplMethod(DOMImplementation, 'hasFeature');
359364

@@ -362,8 +367,8 @@
362367
forwardMethodsToWrapper([
363368
window.DOMImplementation,
364369
], [
365-
'createDocumentType',
366370
'createDocument',
371+
'createDocumentType',
367372
'createHTMLDocument',
368373
'hasFeature',
369374
]);

src/ShadowDOM/wrappers/HTMLElement.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@
9393
'noscript'
9494
]);
9595

96+
var XHTML_NS = 'http://www.w3.org/1999/xhtml';
97+
98+
function needsSelfClosingSlash(node) {
99+
// if the namespace is not XHTML_NS, this is probably an XML or SVG Document
100+
// and will need a closing slash on void elements
101+
if (node.namespaceURI !== XHTML_NS)
102+
return true;
103+
104+
var doctype = node.ownerDocument.doctype;
105+
// doctype is null for quirksmode documents
106+
// publicId and systemId are required for XHTML, and are null for HTML5
107+
return doctype && doctype.publicId && doctype.systemId;
108+
}
109+
96110
function getOuterHTML(node, parentNode) {
97111
switch (node.nodeType) {
98112
case Node.ELEMENT_NODE:
@@ -102,11 +116,14 @@
102116
for (var i = 0, attr; attr = attrs[i]; i++) {
103117
s += ' ' + attr.name + '="' + escapeAttr(attr.value) + '"';
104118
}
105-
s += '>';
106-
if (voidElements[tagName])
107-
return s;
108119

109-
return s + getInnerHTML(node) + '</' + tagName + '>';
120+
if (voidElements[tagName]) {
121+
if (needsSelfClosingSlash(node))
122+
s += '/';
123+
return s + '>';
124+
}
125+
126+
return s + '>' + getInnerHTML(node) + '</' + tagName + '>';
110127

111128
case Node.TEXT_NODE:
112129
var data = node.data;

tests/ShadowDOM/js/Document.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ htmlSuite('Document', function() {
3333
assert.equal(doc2.lastElementChild.tagName, 'HTML');
3434
});
3535

36+
test('Create XHTML Document', function() {
37+
var docType = wrap(document).implementation.createDocumentType('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
38+
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd');
39+
var doc = wrap(document).implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', docType);
40+
assert(doc);
41+
});
42+
3643
test('document.documentElement', function() {
3744
var doc = wrap(document);
3845
assert.equal(doc.documentElement.ownerDocument, doc);

tests/ShadowDOM/js/HTMLElement.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,12 @@ suite('HTMLElement', function() {
114114
div.hidden = false;
115115
assert.isFalse(div.hasAttribute('hidden'));
116116
});
117+
118+
test('img outerHTML in XHTML documents', function() {
119+
var docType = document.implementation.createDocumentType('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
120+
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd');
121+
var doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', docType);
122+
var img = doc.createElement('img');
123+
assert.equal(img.outerHTML, '<img/>');
124+
});
117125
});

0 commit comments

Comments
 (0)