| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 | 'use strict';const { DOCUMENT_MODE } = require('../common/html');//Node constructionexports.createDocument = function() {    return {        nodeName: '#document',        mode: DOCUMENT_MODE.NO_QUIRKS,        childNodes: []    };};exports.createDocumentFragment = function() {    return {        nodeName: '#document-fragment',        childNodes: []    };};exports.createElement = function(tagName, namespaceURI, attrs) {    return {        nodeName: tagName,        tagName: tagName,        attrs: attrs,        namespaceURI: namespaceURI,        childNodes: [],        parentNode: null    };};exports.createCommentNode = function(data) {    return {        nodeName: '#comment',        data: data,        parentNode: null    };};const createTextNode = function(value) {    return {        nodeName: '#text',        value: value,        parentNode: null    };};//Tree mutationconst appendChild = (exports.appendChild = function(parentNode, newNode) {    parentNode.childNodes.push(newNode);    newNode.parentNode = parentNode;});const insertBefore = (exports.insertBefore = function(parentNode, newNode, referenceNode) {    const insertionIdx = parentNode.childNodes.indexOf(referenceNode);    parentNode.childNodes.splice(insertionIdx, 0, newNode);    newNode.parentNode = parentNode;});exports.setTemplateContent = function(templateElement, contentElement) {    templateElement.content = contentElement;};exports.getTemplateContent = function(templateElement) {    return templateElement.content;};exports.setDocumentType = function(document, name, publicId, systemId) {    let doctypeNode = null;    for (let i = 0; i < document.childNodes.length; i++) {        if (document.childNodes[i].nodeName === '#documentType') {            doctypeNode = document.childNodes[i];            break;        }    }    if (doctypeNode) {        doctypeNode.name = name;        doctypeNode.publicId = publicId;        doctypeNode.systemId = systemId;    } else {        appendChild(document, {            nodeName: '#documentType',            name: name,            publicId: publicId,            systemId: systemId        });    }};exports.setDocumentMode = function(document, mode) {    document.mode = mode;};exports.getDocumentMode = function(document) {    return document.mode;};exports.detachNode = function(node) {    if (node.parentNode) {        const idx = node.parentNode.childNodes.indexOf(node);        node.parentNode.childNodes.splice(idx, 1);        node.parentNode = null;    }};exports.insertText = function(parentNode, text) {    if (parentNode.childNodes.length) {        const prevNode = parentNode.childNodes[parentNode.childNodes.length - 1];        if (prevNode.nodeName === '#text') {            prevNode.value += text;            return;        }    }    appendChild(parentNode, createTextNode(text));};exports.insertTextBefore = function(parentNode, text, referenceNode) {    const prevNode = parentNode.childNodes[parentNode.childNodes.indexOf(referenceNode) - 1];    if (prevNode && prevNode.nodeName === '#text') {        prevNode.value += text;    } else {        insertBefore(parentNode, createTextNode(text), referenceNode);    }};exports.adoptAttributes = function(recipient, attrs) {    const recipientAttrsMap = [];    for (let i = 0; i < recipient.attrs.length; i++) {        recipientAttrsMap.push(recipient.attrs[i].name);    }    for (let j = 0; j < attrs.length; j++) {        if (recipientAttrsMap.indexOf(attrs[j].name) === -1) {            recipient.attrs.push(attrs[j]);        }    }};//Tree traversingexports.getFirstChild = function(node) {    return node.childNodes[0];};exports.getChildNodes = function(node) {    return node.childNodes;};exports.getParentNode = function(node) {    return node.parentNode;};exports.getAttrList = function(element) {    return element.attrs;};//Node dataexports.getTagName = function(element) {    return element.tagName;};exports.getNamespaceURI = function(element) {    return element.namespaceURI;};exports.getTextNodeContent = function(textNode) {    return textNode.value;};exports.getCommentNodeContent = function(commentNode) {    return commentNode.data;};exports.getDocumentTypeNodeName = function(doctypeNode) {    return doctypeNode.name;};exports.getDocumentTypeNodePublicId = function(doctypeNode) {    return doctypeNode.publicId;};exports.getDocumentTypeNodeSystemId = function(doctypeNode) {    return doctypeNode.systemId;};//Node typesexports.isTextNode = function(node) {    return node.nodeName === '#text';};exports.isCommentNode = function(node) {    return node.nodeName === '#comment';};exports.isDocumentTypeNode = function(node) {    return node.nodeName === '#documentType';};exports.isElementNode = function(node) {    return !!node.tagName;};// Source code locationexports.setNodeSourceCodeLocation = function(node, location) {    node.sourceCodeLocation = location;};exports.getNodeSourceCodeLocation = function(node) {    return node.sourceCodeLocation;};
 |