'use strict'; /** * @typedef {Object} TreeAdapter */ //Node construction /** * Creates a document node. * * @function createDocument * @memberof TreeAdapter * * @returns {ASTNode} document * * @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L19|default implementation.} */ exports.createDocument = function () { return { nodeName: '#document', quirksMode: false, childNodes: [] }; }; /** * Creates a document fragment node. * * @function createDocumentFragment * @memberof TreeAdapter * * @returns {ASTNode} fragment * * @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L37|default implementation.} */ exports.createDocumentFragment = function () { return { nodeName: '#document-fragment', quirksMode: false, childNodes: [] }; }; /** * Creates an element node. * * @function createElement * @memberof TreeAdapter * * @param {String} tagName - Tag name of the element. * @param {String} namespaceURI - Namespace of the element. * @param {Array} attrs - Attribute name-value pair array. * Foreign attributes may contain `namespace` and `prefix` fields as well. * * @returns {ASTNode} element * * @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L61|default implementation.} */ exports.createElement = function (tagName, namespaceURI, attrs) { return { nodeName: tagName, tagName: tagName, attrs: attrs, namespaceURI: namespaceURI, childNodes: [], parentNode: null }; }; /** * Creates a comment node. * * @function createCommentNode * @memberof TreeAdapter * * @param {String} data - Comment text. * * @returns {ASTNode} comment * * @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L85|default implementation.} */ exports.createCommentNode = function (data) { return { nodeName: '#comment', data: data, parentNode: null }; }; var createTextNode = function (value) { return { nodeName: '#text', value: value, parentNode: null }; }; //Tree mutation /** * Appends a child node to the given parent node. * * @function appendChild * @memberof TreeAdapter * * @param {ASTNode} parentNode - Parent node. * @param {ASTNode} newNode - Child node. * * @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L114|default implementation.} */ var appendChild = exports.appendChild = function (parentNode, newNode) { parentNode.childNodes.push(newNode); newNode.parentNode = parentNode; }; /** * Inserts a child node to the given parent node before the given reference node. * * @function insertBefore * @memberof TreeAdapter * * @param {ASTNode} parentNode - Parent node. * @param {ASTNode} newNode - Child node. * @param {ASTNode} referenceNode - Reference node. * * @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L131|default implementation.} */ var insertBefore = exports.insertBefore = function (parentNode, newNode, referenceNode) { var insertionIdx = parentNode.childNodes.indexOf(referenceNode); parentNode.childNodes.splice(insertionIdx, 0, newNode); newNode.parentNode = parentNode; }; /** * Sets the `