-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
222 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,177 @@ | ||
"use strict"; | ||
|
||
const attributes = require("../living/attributes"); | ||
const DocumentType = require("../living/generated/DocumentType"); | ||
const idlUtils = require("../living/generated/utils"); | ||
|
||
// Tree traversing | ||
exports.getFirstChild = function (node) { | ||
return node.childNodes[0]; | ||
}; | ||
module.exports = function (document, fragment) { | ||
const exports = {}; | ||
|
||
// Node construction | ||
exports.createDocument = function () { | ||
return document; | ||
}; | ||
|
||
exports.createDocumentFragment = function () { | ||
return document.createDocumentFragment(); | ||
}; | ||
|
||
exports.createElement = function (tagName, namespaceURI, attrs) { | ||
const elem = document._createElementWithCorrectElementInterface(tagName, namespaceURI); | ||
if (tagName === "script" && !fragment) { | ||
elem._parserInserted = true; | ||
elem._nonBlocking = false; | ||
} | ||
|
||
elem._localName = tagName; | ||
elem._namespaceURI = namespaceURI || null; | ||
for (let i = 0; i < attrs.length; ++i) { | ||
attributes.setAttributeValue(elem, attrs[i].name, attrs[i].value, | ||
attrs[i].prefix || null, attrs[i].namespace || null); | ||
} | ||
return elem; | ||
}; | ||
|
||
exports.createCommentNode = function (data) { | ||
return document.createComment(data); | ||
}; | ||
|
||
// Tree mutation | ||
exports.appendChild = function (parentNode, newNode) { | ||
parentNode.appendChild(newNode); | ||
}; | ||
|
||
exports.insertBefore = function (parentNode, newNode, referenceNode) { | ||
parentNode.insertBefore(newNode, referenceNode); | ||
}; | ||
|
||
exports.setTemplateContent = function (templateElement, contentElement) { | ||
templateElement.content = contentElement; | ||
}; | ||
|
||
exports.setDocumentType = function (doc, name, publicId, systemId) { | ||
const doctypeNode = doc.doctype; | ||
if (doctypeNode) { | ||
doctypeNode.parentNode.removeChild(doctypeNode); | ||
} | ||
const newType = DocumentType.createImpl([], { core: document.defaultView, ownerDocument: doc, name, publicId, systemId }); | ||
exports.appendChild(doc, newType); | ||
}; | ||
|
||
exports.setQuirksMode = function (doc) { | ||
doc._mode = "quirks"; | ||
}; | ||
|
||
exports.isQuirksMode = function (doc) { | ||
return doc._mode === "quirks"; | ||
}; | ||
|
||
exports.detachNode = function (node) { | ||
if (node.parentNode) { | ||
node.parentNode.removeChild(node); | ||
} | ||
}; | ||
|
||
exports.insertText = function (parentNode, text) { | ||
const lastChild = parentNode.lastChild; | ||
if (lastChild && lastChild.nodeName === "#text") { | ||
lastChild.nodeValue += text; | ||
} else { | ||
exports.appendChild(parentNode, document.createTextNode(text)); | ||
} | ||
}; | ||
|
||
exports.insertTextBefore = function (parentNode, text, referenceNode) { | ||
const prevNode = referenceNode.previousSibling; | ||
if (prevNode && prevNode.nodeName === "#text") { | ||
prevNode.nodeValue += text; | ||
} else { | ||
exports.insertBefore(parentNode, document.createTextNode(text), referenceNode); | ||
} | ||
}; | ||
|
||
exports.adoptAttributes = function (recipientNode, attrs) { | ||
for (let i = 0; i < attrs.length; ++i) { | ||
if (recipientNode.hasAttribute(attrs[i].name)) { | ||
recipientNode.attributes.setNamedItemNS(attrs[i]); | ||
} | ||
} | ||
}; | ||
|
||
// Tree traversing | ||
exports.getFirstChild = function (node) { | ||
return idlUtils.implForWrapper(node.childNodes[0]); | ||
}; | ||
|
||
exports.getChildNodes = function (node) { | ||
// parse5 treats template elements specially, assuming you return an array whose single item is the document fragment | ||
const children = node._templateContents ? [node._templateContents] : []; | ||
if (children.length === 0) { | ||
exports.getChildNodes = function (node) { | ||
const impls = []; | ||
for (let i = 0; i < node.childNodes.length; ++i) { | ||
children.push(idlUtils.implForWrapper(node.childNodes[i])); | ||
impls.push(idlUtils.implForWrapper(node.childNodes[i])); | ||
} | ||
} | ||
return children; | ||
}; | ||
return impls; | ||
}; | ||
|
||
exports.getParentNode = function (node) { | ||
return node.parentNode; | ||
}; | ||
exports.getParentNode = function (node) { | ||
return node.parentNode; | ||
}; | ||
|
||
exports.getAttrList = function (node) { | ||
return node.attributes; | ||
}; | ||
exports.getAttrList = function (node) { | ||
return node.attributes; | ||
}; | ||
|
||
// Node data | ||
exports.getTagName = function (element) { | ||
return element.tagName.toLowerCase(); | ||
}; | ||
// Node data | ||
exports.getTagName = function (element) { | ||
if (!element.tagName) { | ||
return null; | ||
} | ||
|
||
exports.getNamespaceURI = function (element) { | ||
return element.namespaceURI || "http://www.w3.org/1999/xhtml"; | ||
}; | ||
return element.tagName.toLowerCase(); | ||
}; | ||
|
||
exports.getTextNodeContent = function (textNode) { | ||
return textNode.nodeValue; | ||
}; | ||
exports.getNamespaceURI = function (element) { | ||
return element.namespaceURI || "http://www.w3.org/1999/xhtml"; | ||
}; | ||
|
||
exports.getCommentNodeContent = function (commentNode) { | ||
return commentNode.nodeValue; | ||
}; | ||
exports.getTextNodeContent = function (textNode) { | ||
return textNode.nodeValue; | ||
}; | ||
|
||
exports.getDocumentTypeNodeName = function (doctypeNode) { | ||
return doctypeNode.name; | ||
}; | ||
exports.getCommentNodeContent = function (commentNode) { | ||
return commentNode.nodeValue; | ||
}; | ||
|
||
exports.getDocumentTypeNodePublicId = function (doctypeNode) { | ||
return doctypeNode.publicId || null; | ||
}; | ||
exports.getDocumentTypeNodeName = function (doctypeNode) { | ||
return doctypeNode.name; | ||
}; | ||
|
||
exports.getDocumentTypeNodeSystemId = function (doctypeNode) { | ||
return doctypeNode.systemId || null; | ||
}; | ||
exports.getDocumentTypeNodePublicId = function (doctypeNode) { | ||
return doctypeNode.publicId || null; | ||
}; | ||
|
||
// Node types | ||
exports.isTextNode = function (node) { | ||
return node.nodeName === "#text"; | ||
}; | ||
exports.getDocumentTypeNodeSystemId = function (doctypeNode) { | ||
return doctypeNode.systemId || null; | ||
}; | ||
|
||
exports.isCommentNode = function (node) { | ||
return node.nodeName === "#comment"; | ||
}; | ||
exports.getTemplateContent = function (node) { | ||
return node.content; | ||
}; | ||
|
||
exports.isDocumentTypeNode = function (node) { | ||
return node.nodeType === 10; | ||
}; | ||
// Node types | ||
exports.isTextNode = function (node) { | ||
return node.nodeName === "#text"; | ||
}; | ||
|
||
exports.isCommentNode = function (node) { | ||
return node.nodeName === "#comment"; | ||
}; | ||
|
||
exports.isDocumentTypeNode = function (node) { | ||
return node.nodeType === 10; | ||
}; | ||
|
||
exports.isElementNode = function (node) { | ||
return Boolean(node.tagName); | ||
}; | ||
|
||
exports.isElementNode = function (node) { | ||
return Boolean(node.tagName); | ||
return exports; | ||
}; |
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
Oops, something went wrong.