-
-
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
7 changed files
with
201 additions
and
116 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,65 +1,165 @@ | ||
"use strict"; | ||
|
||
// Tree traversing | ||
exports.getFirstChild = function (node) { | ||
return node.childNodes[0]; | ||
}; | ||
const attributes = require("../living/attributes"); | ||
|
||
exports.getChildNodes = function (node) { | ||
// parse5 treats template elements specially, assuming you return an array whose single item is the document fragment | ||
return node._templateContents ? [node._templateContents] : node.childNodes; | ||
}; | ||
module.exports = function (document) { | ||
const exports = {}; | ||
|
||
exports.getParentNode = function (node) { | ||
return node.parentNode; | ||
}; | ||
// Node construction | ||
exports.createDocument = function () { | ||
return document; | ||
}; | ||
|
||
exports.getAttrList = function (node) { | ||
return node.attributes; | ||
}; | ||
exports.createDocumentFragment = function () { | ||
return document.createDocumentFragment(); | ||
}; | ||
|
||
// Node data | ||
exports.getTagName = function (element) { | ||
return element.tagName.toLowerCase(); | ||
}; | ||
exports.createElement = function (tagName, namespaceURI, attrs) { | ||
const elem = document.createElementNS(namespaceURI, tagName); | ||
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.getNamespaceURI = function (element) { | ||
return element.namespaceURI || "http://www.w3.org/1999/xhtml"; | ||
}; | ||
exports.createCommentNode = function (data) { | ||
return document.createComment(data); | ||
}; | ||
|
||
exports.getTextNodeContent = function (textNode) { | ||
return textNode.nodeValue; | ||
}; | ||
// Tree mutation | ||
exports.appendChild = function (parentNode, newNode) { | ||
parentNode.appendChild(newNode); | ||
}; | ||
|
||
exports.getCommentNodeContent = function (commentNode) { | ||
return commentNode.nodeValue; | ||
}; | ||
exports.insertBefore = function (parentNode, newNode, referenceNode) { | ||
parentNode.insertBefore(newNode, referenceNode); | ||
}; | ||
|
||
exports.getDocumentTypeNodeName = function (doctypeNode) { | ||
return doctypeNode.name; | ||
}; | ||
exports.setTemplateContent = function (templateElement, contentElement) { | ||
templateElement.content = contentElement; | ||
}; | ||
|
||
exports.getDocumentTypeNodePublicId = function (doctypeNode) { | ||
return doctypeNode.publicId || null; | ||
}; | ||
exports.setDocumentType = function (doc, name, publicId, systemId) { | ||
const doctypeNode = doc.doctype; | ||
|
||
exports.getDocumentTypeNodeSystemId = function (doctypeNode) { | ||
return doctypeNode.systemId || null; | ||
}; | ||
if (doctypeNode) { | ||
doctypeNode.name = name; | ||
doctypeNode.publicId = publicId; | ||
doctypeNode.systemId = systemId; | ||
} else { | ||
const newType = doc.implementation.createDocumentType(name, publicId, systemId); | ||
exports.appendChild(doc, newType); | ||
} | ||
}; | ||
|
||
// Node types | ||
exports.isTextNode = function (node) { | ||
return node.nodeName === "#text"; | ||
}; | ||
exports.setQuirksMode = function (doc) { | ||
doc.quirksMode = true; | ||
}; | ||
|
||
exports.isCommentNode = function (node) { | ||
return node.nodeName === "#comment"; | ||
}; | ||
exports.isQuirksMode = function (doc) { | ||
return doc.quirksMode; | ||
}; | ||
|
||
exports.isDocumentTypeNode = function (node) { | ||
return node.nodeType === 10; | ||
}; | ||
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 node.childNodes[0]; | ||
}; | ||
|
||
exports.getChildNodes = function (node) { | ||
// parse5 treats template elements specially, assuming you return an array whose single item is the document fragment | ||
return node.childNodes; | ||
}; | ||
|
||
exports.getParentNode = function (node) { | ||
return node.parentNode; | ||
}; | ||
|
||
exports.getAttrList = function (node) { | ||
return node.attributes; | ||
}; | ||
|
||
// Node data | ||
exports.getTagName = function (element) { | ||
return element.tagName.toLowerCase(); | ||
}; | ||
|
||
exports.getNamespaceURI = function (element) { | ||
return element.namespaceURI || "http://www.w3.org/1999/xhtml"; | ||
}; | ||
|
||
exports.getTextNodeContent = function (textNode) { | ||
return textNode.nodeValue; | ||
}; | ||
|
||
exports.getCommentNodeContent = function (commentNode) { | ||
return commentNode.nodeValue; | ||
}; | ||
|
||
exports.getDocumentTypeNodeName = function (doctypeNode) { | ||
return doctypeNode.name; | ||
}; | ||
|
||
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
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.