-
-
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
183 additions
and
123 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,168 @@ | ||
"use strict"; | ||
|
||
const idlUtils = require("../living/generated/utils"); | ||
const attributes = require("../living/attributes"); | ||
const createDocumentTypeInternal = require("../living/document-type").create; | ||
|
||
module.exports = function (document) { | ||
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); | ||
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); | ||
} | ||
|
||
// Tree traversing | ||
exports.getFirstChild = function (node) { | ||
return node.childNodes[0]; | ||
}; | ||
const newType = createDocumentTypeInternal(document.defaultView, document, name, publicId, systemId); | ||
exports.appendChild(doc, newType); | ||
}; | ||
|
||
exports.setQuirksMode = function (doc) { | ||
doc._mode = "quirks"; | ||
}; | ||
|
||
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) { | ||
for (let i = 0; i < node.childNodes.length; ++i) { | ||
children.push(idlUtils.implForWrapper(node.childNodes[i])); | ||
exports.isQuirksMode = function (doc) { | ||
return doc._mode === "quirks"; | ||
}; | ||
|
||
exports.detachNode = function (node) { | ||
if (node.parentNode) { | ||
node.parentNode.removeChild(node); | ||
} | ||
} | ||
return children; | ||
}; | ||
}; | ||
|
||
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.getParentNode = function (node) { | ||
return node.parentNode; | ||
}; | ||
exports.adoptAttributes = function (recipientNode, attrs) { | ||
for (let i = 0; i < attrs.length; ++i) { | ||
if (recipientNode.hasAttribute(attrs[i].name)) { | ||
recipientNode.attributes.setNamedItemNS(attrs[i]); | ||
} | ||
} | ||
}; | ||
|
||
exports.getAttrList = function (node) { | ||
return node.attributes; | ||
}; | ||
// Tree traversing | ||
exports.getFirstChild = function (node) { | ||
return node.childNodes[0]; | ||
}; | ||
|
||
// Node data | ||
exports.getTagName = function (element) { | ||
return element.tagName.toLowerCase(); | ||
}; | ||
exports.getChildNodes = function (node) { | ||
return node.childNodes; | ||
}; | ||
|
||
exports.getNamespaceURI = function (element) { | ||
return element.namespaceURI || "http://www.w3.org/1999/xhtml"; | ||
}; | ||
exports.getParentNode = function (node) { | ||
return node.parentNode; | ||
}; | ||
|
||
exports.getTextNodeContent = function (textNode) { | ||
return textNode.nodeValue; | ||
}; | ||
exports.getAttrList = function (node) { | ||
return node.attributes; | ||
}; | ||
|
||
exports.getCommentNodeContent = function (commentNode) { | ||
return commentNode.nodeValue; | ||
}; | ||
// Node data | ||
exports.getTagName = function (element) { | ||
if (!element.tagName) { | ||
return null; | ||
} | ||
|
||
exports.getDocumentTypeNodeName = function (doctypeNode) { | ||
return doctypeNode.name; | ||
}; | ||
return element.tagName.toLowerCase(); | ||
}; | ||
|
||
exports.getDocumentTypeNodePublicId = function (doctypeNode) { | ||
return doctypeNode.publicId || null; | ||
}; | ||
exports.getNamespaceURI = function (element) { | ||
return element.namespaceURI || "http://www.w3.org/1999/xhtml"; | ||
}; | ||
|
||
exports.getDocumentTypeNodeSystemId = function (doctypeNode) { | ||
return doctypeNode.systemId || null; | ||
}; | ||
exports.getTextNodeContent = function (textNode) { | ||
return textNode.nodeValue; | ||
}; | ||
|
||
// Node types | ||
exports.isTextNode = function (node) { | ||
return node.nodeName === "#text"; | ||
}; | ||
exports.getCommentNodeContent = function (commentNode) { | ||
return commentNode.nodeValue; | ||
}; | ||
|
||
exports.isCommentNode = function (node) { | ||
return node.nodeName === "#comment"; | ||
}; | ||
exports.getDocumentTypeNodeName = function (doctypeNode) { | ||
return doctypeNode.name; | ||
}; | ||
|
||
exports.isDocumentTypeNode = function (node) { | ||
return node.nodeType === 10; | ||
}; | ||
exports.getDocumentTypeNodePublicId = function (doctypeNode) { | ||
return doctypeNode.publicId || null; | ||
}; | ||
|
||
exports.getDocumentTypeNodeSystemId = function (doctypeNode) { | ||
return doctypeNode.systemId || null; | ||
}; | ||
|
||
exports.getTemplateContent = function (node) { | ||
return node.content; | ||
}; | ||
|
||
// 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
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
Oops, something went wrong.