This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

20
node_modules/w3c-xmlserializer/README.md generated vendored Normal file
View File

@ -0,0 +1,20 @@
# w3c-xmlserializer
An XML serializer that follows the [W3C specification](https://w3c.github.io/DOM-Parsing/).
This module is mostly used as an internal part of [jsdom](https://github.com/jsdom/jsdom). However, you can use it independently with some care. That isn't very well-documented yet, but hopefully the below sample can give you an idea.
## Basic usage
```js
const { XMLSerializer } = require("w3c-xmlserializer");
const { JSDOM } = require("jsdom");
const { document } = new JSDOM().window;
const XMLSerializer = XMLSerializer.interface;
const serializer = new XMLSerializer();
const doc = document.createElement("akomaNtoso");
console.log(serializer.serializeToString(doc));
// => '<akomantoso xmlns="http://www.w3.org/1999/xhtml"></akomantoso>'
```

View File

@ -0,0 +1,9 @@
"use strict";
const { produceXMLSerialization } = require("./serialization");
exports.implementation = class XMLSerializerImpl {
serializeToString(root) {
return produceXMLSerialization(root, false);
}
};

113
node_modules/w3c-xmlserializer/lib/XMLSerializer.js generated vendored Normal file
View File

@ -0,0 +1,113 @@
"use strict";
const conversions = require("webidl-conversions");
const utils = require("./utils.js");
const impl = utils.implSymbol;
class XMLSerializer {
constructor() {
return iface.setup(Object.create(new.target.prototype));
}
serializeToString(root) {
if (!this || !module.exports.is(this)) {
throw new TypeError("Illegal invocation");
}
if (arguments.length < 1) {
throw new TypeError(
"Failed to execute 'serializeToString' on 'XMLSerializer': 1 argument required, but only " +
arguments.length +
" present."
);
}
const args = [];
{
let curArg = arguments[0];
curArg = utils.tryImplForWrapper(curArg);
args.push(curArg);
}
return this[impl].serializeToString(...args);
}
}
Object.defineProperties(XMLSerializer.prototype, {
serializeToString: { enumerable: true },
[Symbol.toStringTag]: { value: "XMLSerializer", configurable: true }
});
const iface = {
// When an interface-module that implements this interface as a mixin is loaded, it will append its own `.is()`
// method into this array. It allows objects that directly implements *those* interfaces to be recognized as
// implementing this mixin interface.
_mixedIntoPredicates: [],
is(obj) {
if (obj) {
if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation) {
return true;
}
for (const isMixedInto of module.exports._mixedIntoPredicates) {
if (isMixedInto(obj)) {
return true;
}
}
}
return false;
},
isImpl(obj) {
if (obj) {
if (obj instanceof Impl.implementation) {
return true;
}
const wrapper = utils.wrapperForImpl(obj);
for (const isMixedInto of module.exports._mixedIntoPredicates) {
if (isMixedInto(wrapper)) {
return true;
}
}
}
return false;
},
convert(obj, { context = "The provided value" } = {}) {
if (module.exports.is(obj)) {
return utils.implForWrapper(obj);
}
throw new TypeError(`${context} is not of type 'XMLSerializer'.`);
},
create(constructorArgs, privateData) {
let obj = Object.create(XMLSerializer.prototype);
obj = this.setup(obj, constructorArgs, privateData);
return obj;
},
createImpl(constructorArgs, privateData) {
let obj = Object.create(XMLSerializer.prototype);
obj = this.setup(obj, constructorArgs, privateData);
return utils.implForWrapper(obj);
},
_internalSetup(obj) {},
setup(obj, constructorArgs, privateData) {
if (!privateData) privateData = {};
privateData.wrapper = obj;
this._internalSetup(obj);
Object.defineProperty(obj, impl, {
value: new Impl.implementation(constructorArgs, privateData),
configurable: true
});
obj[impl][utils.wrapperSymbol] = obj;
if (Impl.init) {
Impl.init(obj[impl], privateData);
}
return obj;
},
interface: XMLSerializer,
expose: {
Window: { XMLSerializer }
}
}; // iface
module.exports = iface;
const Impl = require("./XMLSerializer-impl.js");

128
node_modules/w3c-xmlserializer/lib/attributes.js generated vendored Normal file
View File

@ -0,0 +1,128 @@
"use strict";
const xnv = require("xml-name-validator");
const { NAMESPACES } = require("./constants");
function generatePrefix(map, newNamespace, prefixIndex) {
const generatedPrefix = "ns" + prefixIndex;
map[newNamespace] = [generatedPrefix];
return generatedPrefix;
}
function preferredPrefixString(map, ns, preferredPrefix) {
const candidateList = map[ns];
if (!candidateList) {
return null;
}
if (candidateList.includes(preferredPrefix)) {
return preferredPrefix;
}
return candidateList[candidateList.length - 1];
}
function serializeAttributeValue(value/* , requireWellFormed*/) {
if (value === null) {
return "";
}
// TODO: Check well-formedness
return value
.replace(/&/g, "&amp;")
.replace(/"/g, "&quot;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\t/g, "&#x9;")
.replace(/\n/g, "&#xA;")
.replace(/\r/g, "&#xD;");
}
function serializeAttributes(
element,
map,
localPrefixes,
ignoreNamespaceDefAttr,
requireWellFormed,
refs
) {
let result = "";
const namespaceLocalnames = Object.create(null);
for (const attr of element.attributes) {
if (
requireWellFormed &&
namespaceLocalnames[attr.namespaceURI] &&
namespaceLocalnames[attr.namespaceURI].has(attr.localName)
) {
throw new Error("Found duplicated attribute");
}
if (!namespaceLocalnames[attr.namespaceURI]) {
namespaceLocalnames[attr.namespaceURI] = new Set();
}
namespaceLocalnames[attr.namespaceURI].add(attr.localName);
const attributeNamespace = attr.namespaceURI;
let candidatePrefix = null;
if (attributeNamespace !== null) {
candidatePrefix = preferredPrefixString(
map,
attributeNamespace,
attr.prefix
);
if (attributeNamespace === NAMESPACES.XMLNS) {
if (
attr.value === NAMESPACES.XML ||
(attr.prefix === null && ignoreNamespaceDefAttr) ||
(attr.prefix !== null &&
localPrefixes[attr.localName] !== attr.value &&
map[attr.value].includes(attr.localName))
) {
continue;
}
if (requireWellFormed && attr.value === NAMESPACES.XMLNS) {
throw new Error(
"The XMLNS namespace is reserved and cannot be applied as an element's namespace via XML parsing"
);
}
if (requireWellFormed && attr.value === "") {
throw new Error(
"Namespace prefix declarations cannot be used to undeclare a namespace"
);
}
if (attr.prefix === "xmlns") {
candidatePrefix = "xmlns";
}
} else if (candidatePrefix === null) {
candidatePrefix = generatePrefix(
map,
attributeNamespace,
refs.prefixIndex++
);
result += ` xmlns:${candidatePrefix}="${serializeAttributeValue(
attributeNamespace,
requireWellFormed
)}"`;
}
}
result += " ";
if (candidatePrefix !== null) {
result += candidatePrefix + ":";
}
if (
requireWellFormed &&
(attr.localName.includes(":") ||
!xnv.name(attr.localName) ||
(attr.localName === "xmlns" && attributeNamespace === null))
) {
throw new Error("Invalid attribute localName value");
}
result += `${attr.localName}="${serializeAttributeValue(
attr.value,
requireWellFormed
)}"`;
}
return result;
}
module.exports.preferredPrefixString = preferredPrefixString;
module.exports.generatePrefix = generatePrefix;
module.exports.serializeAttributeValue = serializeAttributeValue;
module.exports.serializeAttributes = serializeAttributes;

44
node_modules/w3c-xmlserializer/lib/constants.js generated vendored Normal file
View File

@ -0,0 +1,44 @@
"use strict";
module.exports.NAMESPACES = {
HTML: "http://www.w3.org/1999/xhtml",
XML: "http://www.w3.org/XML/1998/namespace",
XMLNS: "http://www.w3.org/2000/xmlns/"
};
module.exports.NODE_TYPES = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2, // historical
TEXT_NODE: 3,
CDATA_SECTION_NODE: 4,
ENTITY_REFERENCE_NODE: 5, // historical
ENTITY_NODE: 6, // historical
PROCESSING_INSTRUCTION_NODE: 7,
COMMENT_NODE: 8,
DOCUMENT_NODE: 9,
DOCUMENT_TYPE_NODE: 10,
DOCUMENT_FRAGMENT_NODE: 11,
NOTATION_NODE: 12 // historical
};
module.exports.VOID_ELEMENTS = new Set([
"area",
"base",
"basefont",
"bgsound",
"br",
"col",
"embed",
"frame",
"hr",
"img",
"input",
"keygen",
"link",
"menuitem",
"meta",
"param",
"source",
"track",
"wbr"
]);

9
node_modules/w3c-xmlserializer/lib/index.js generated vendored Normal file
View File

@ -0,0 +1,9 @@
"use strict";
const { produceXMLSerialization } = require("./serialization");
const XMLSerializer = require("./XMLSerializer");
module.exports = {
XMLSerializer,
produceXMLSerialization
};

379
node_modules/w3c-xmlserializer/lib/serialization.js generated vendored Normal file
View File

@ -0,0 +1,379 @@
"use strict";
const DOMException = require("domexception");
const xnv = require("xml-name-validator");
const attributeUtils = require("./attributes");
const { NAMESPACES, VOID_ELEMENTS, NODE_TYPES } = require("./constants");
const XML_CHAR = /^(\x09|\x0A|\x0D|[\x20-\uD7FF]|[\uE000-\uFFFD]|(?:[\uD800-\uDBFF][\uDC00-\uDFFF]))*$/;
const PUBID_CHAR = /^(\x20|\x0D|\x0A|[a-zA-Z0-9]|[-'()+,./:=?;!*#@$_%])*$/;
function asciiCaseInsensitiveMatch(a, b) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; ++i) {
if ((a.charCodeAt(i) | 32) !== (b.charCodeAt(i) | 32)) {
return false;
}
}
return true;
}
function recordNamespaceInformation(element, map, prefixMap) {
let defaultNamespaceAttrValue = null;
for (let i = 0; i < element.attributes.length; ++i) {
const attr = element.attributes[i];
if (attr.namespaceURI === NAMESPACES.XMLNS) {
if (attr.prefix === null) {
defaultNamespaceAttrValue = attr.value;
continue;
}
let namespaceDefinition = attr.value;
if (namespaceDefinition === NAMESPACES.XML) {
continue;
}
// This is exactly the other way than the spec says, but that's intended.
// All the maps coalesce null to the empty string (explained in the
// spec), so instead of doing that every time, just do it once here.
if (namespaceDefinition === null) {
namespaceDefinition = "";
}
if (
namespaceDefinition in map &&
map[namespaceDefinition].includes(attr.localName)
) {
continue;
}
if (!(namespaceDefinition in map)) {
map[namespaceDefinition] = [];
}
map[namespaceDefinition].push(attr.localName);
prefixMap[attr.localName] = namespaceDefinition;
}
}
return defaultNamespaceAttrValue;
}
function serializeDocumentType(node, namespace, prefixMap, requireWellFormed) {
if (requireWellFormed && !PUBID_CHAR.test(node.publicId)) {
throw new Error("Node publicId is not well formed");
}
if (
requireWellFormed &&
(!XML_CHAR.test(node.systemId) ||
(node.systemId.includes('"') && node.systemId.includes("'")))
) {
throw new Error("Node systemId is not well formed");
}
let markup = `<!DOCTYPE ${node.name}`;
if (node.publicId !== "") {
markup += ` PUBLIC "${node.publicId}"`;
} else if (node.systemId !== "") {
markup += " SYSTEM";
}
if (node.systemId !== "") {
markup += ` "${node.systemId}"`;
}
return markup + ">";
}
function serializeProcessingInstruction(
node,
namespace,
prefixMap,
requireWellFormed
) {
if (
requireWellFormed &&
(node.target.includes(":") || asciiCaseInsensitiveMatch(node.target, "xml"))
) {
throw new Error("Node target is not well formed");
}
if (
requireWellFormed &&
(!XML_CHAR.test(node.data) || node.data.includes("?>"))
) {
throw new Error("Node data is not well formed");
}
return `<?${node.target} ${node.data}?>`;
}
function serializeDocument(
node,
namespace,
prefixMap,
requireWellFormed,
refs
) {
if (requireWellFormed && node.documentElement === null) {
throw new Error("Document does not have a document element");
}
let serializedDocument = "";
for (const child of node.childNodes) {
serializedDocument += xmlSerialization(
child,
namespace,
prefixMap,
requireWellFormed,
refs
);
}
return serializedDocument;
}
function serializeDocumentFragment(
node,
namespace,
prefixMap,
requireWellFormed,
refs
) {
let markup = "";
for (const child of node.childNodes) {
markup += xmlSerialization(
child,
namespace,
prefixMap,
requireWellFormed,
refs
);
}
return markup;
}
function serializeText(node, namespace, prefixMap, requireWellFormed) {
if (requireWellFormed && !XML_CHAR.test(node.data)) {
throw new Error("Node data is not well formed");
}
return node.data
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}
function serializeComment(node, namespace, prefixMap, requireWellFormed) {
if (requireWellFormed && !XML_CHAR.test(node.data)) {
throw new Error("Node data is not well formed");
}
if (
requireWellFormed &&
(node.data.includes("--") || node.data.endsWith("-"))
) {
throw new Error("Found hyphens in illegal places");
}
return `<!--${node.data}-->`;
}
function serializeElement(node, namespace, prefixMap, requireWellFormed, refs) {
if (
requireWellFormed &&
(node.localName.includes(":") || !xnv.name(node.localName))
) {
throw new Error("localName is not a valid XML name");
}
let markup = "<";
let qualifiedName = "";
let skipEndTag = false;
let ignoreNamespaceDefinitionAttr = false;
const map = Object.assign({}, prefixMap);
const localPrefixesMap = Object.create(null);
const localDefaultNamespace = recordNamespaceInformation(
node,
map,
localPrefixesMap
);
let inheritedNs = namespace;
const ns = node.namespaceURI;
if (inheritedNs === ns) {
if (localDefaultNamespace !== null) {
ignoreNamespaceDefinitionAttr = true;
}
if (ns === NAMESPACES.XML) {
qualifiedName = "xml:" + node.localName;
} else {
qualifiedName = node.localName;
}
markup += qualifiedName;
} else {
let { prefix } = node;
let candidatePrefix = attributeUtils.preferredPrefixString(map, ns, prefix);
if (prefix === "xmlns") {
if (requireWellFormed) {
throw new Error("Elements can't have xmlns prefix");
}
candidatePrefix = "xmlns";
}
if (candidatePrefix !== null) {
qualifiedName = candidatePrefix + ":" + node.localName;
if (
localDefaultNamespace !== null &&
localDefaultNamespace !== NAMESPACES.XML
) {
inheritedNs =
localDefaultNamespace === "" ? null : localDefaultNamespace;
}
markup += qualifiedName;
} else if (prefix !== null) {
if (prefix in localPrefixesMap) {
prefix = attributeUtils.generatePrefix(map, ns, refs.prefixIndex++);
}
if (map[ns]) {
map[ns].push(prefix);
} else {
map[ns] = [prefix];
}
qualifiedName = prefix + ":" + node.localName;
markup += `${qualifiedName} xmlns:${prefix}="${attributeUtils.serializeAttributeValue(
ns,
requireWellFormed
)}"`;
if (localDefaultNamespace !== null) {
inheritedNs =
localDefaultNamespace === "" ? null : localDefaultNamespace;
}
} else if (localDefaultNamespace === null || localDefaultNamespace !== ns) {
ignoreNamespaceDefinitionAttr = true;
qualifiedName = node.localName;
inheritedNs = ns;
markup += `${qualifiedName} xmlns="${attributeUtils.serializeAttributeValue(
ns,
requireWellFormed
)}"`;
} else {
qualifiedName = node.localName;
inheritedNs = ns;
markup += qualifiedName;
}
}
markup += attributeUtils.serializeAttributes(
node,
map,
localPrefixesMap,
ignoreNamespaceDefinitionAttr,
requireWellFormed,
refs
);
if (
ns === NAMESPACES.HTML &&
node.childNodes.length === 0 &&
VOID_ELEMENTS.has(node.localName)
) {
markup += " /";
skipEndTag = true;
} else if (ns !== NAMESPACES.HTML && node.childNodes.length === 0) {
markup += "/";
skipEndTag = true;
}
markup += ">";
if (skipEndTag) {
return markup;
}
if (ns === NAMESPACES.HTML && node.localName === "template") {
markup += xmlSerialization(
node.content,
inheritedNs,
map,
requireWellFormed,
refs
);
} else {
for (const child of node.childNodes) {
markup += xmlSerialization(
child,
inheritedNs,
map,
requireWellFormed,
refs
);
}
}
markup += `</${qualifiedName}>`;
return markup;
}
function serializeCDATASection(node) {
return "<![CDATA[" + node.data + "]]>";
}
/**
* @param {{prefixIndex: number}} refs
*/
function xmlSerialization(node, namespace, prefixMap, requireWellFormed, refs) {
switch (node.nodeType) {
case NODE_TYPES.ELEMENT_NODE:
return serializeElement(
node,
namespace,
prefixMap,
requireWellFormed,
refs
);
case NODE_TYPES.DOCUMENT_NODE:
return serializeDocument(
node,
namespace,
prefixMap,
requireWellFormed,
refs
);
case NODE_TYPES.COMMENT_NODE:
return serializeComment(node, namespace, prefixMap, requireWellFormed);
case NODE_TYPES.TEXT_NODE:
return serializeText(node, namespace, prefixMap, requireWellFormed);
case NODE_TYPES.DOCUMENT_FRAGMENT_NODE:
return serializeDocumentFragment(
node,
namespace,
prefixMap,
requireWellFormed,
refs
);
case NODE_TYPES.DOCUMENT_TYPE_NODE:
return serializeDocumentType(
node,
namespace,
prefixMap,
requireWellFormed
);
case NODE_TYPES.PROCESSING_INSTRUCTION_NODE:
return serializeProcessingInstruction(
node,
namespace,
prefixMap,
requireWellFormed
);
case NODE_TYPES.ATTRIBUTE_NODE:
return "";
case NODE_TYPES.CDATA_SECTION_NODE:
return serializeCDATASection(node);
default:
throw new TypeError("Only Nodes and Attr objects can be serialized");
}
}
module.exports.produceXMLSerialization = (root, requireWellFormed) => {
const namespacePrefixMap = Object.create(null);
namespacePrefixMap["http://www.w3.org/XML/1998/namespace"] = ["xml"];
try {
return xmlSerialization(root, null, namespacePrefixMap, requireWellFormed, {
prefixIndex: 1
});
} catch (e) {
throw new DOMException(
"Failed to serialize XML: " + e.message,
"InvalidStateError"
);
}
};

127
node_modules/w3c-xmlserializer/lib/utils.js generated vendored Normal file
View File

@ -0,0 +1,127 @@
"use strict";
// Returns "Type(value) is Object" in ES terminology.
function isObject(value) {
return typeof value === "object" && value !== null || typeof value === "function";
}
function hasOwn(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
const getOwnPropertyDescriptors = typeof Object.getOwnPropertyDescriptors === "function" ?
Object.getOwnPropertyDescriptors :
// Polyfill exists until we require Node.js v8.x
// https://tc39.github.io/ecma262/#sec-object.getownpropertydescriptors
obj => {
if (obj === undefined || obj === null) {
throw new TypeError("Cannot convert undefined or null to object");
}
obj = Object(obj);
const ownKeys = Reflect.ownKeys(obj);
const descriptors = {};
for (const key of ownKeys) {
const descriptor = Reflect.getOwnPropertyDescriptor(obj, key);
if (descriptor !== undefined) {
Reflect.defineProperty(descriptors, key, {
value: descriptor,
writable: true,
enumerable: true,
configurable: true
});
}
}
return descriptors;
};
const wrapperSymbol = Symbol("wrapper");
const implSymbol = Symbol("impl");
const sameObjectCaches = Symbol("SameObject caches");
function getSameObject(wrapper, prop, creator) {
if (!wrapper[sameObjectCaches]) {
wrapper[sameObjectCaches] = Object.create(null);
}
if (prop in wrapper[sameObjectCaches]) {
return wrapper[sameObjectCaches][prop];
}
wrapper[sameObjectCaches][prop] = creator();
return wrapper[sameObjectCaches][prop];
}
function wrapperForImpl(impl) {
return impl ? impl[wrapperSymbol] : null;
}
function implForWrapper(wrapper) {
return wrapper ? wrapper[implSymbol] : null;
}
function tryWrapperForImpl(impl) {
const wrapper = wrapperForImpl(impl);
return wrapper ? wrapper : impl;
}
function tryImplForWrapper(wrapper) {
const impl = implForWrapper(wrapper);
return impl ? impl : wrapper;
}
const iterInternalSymbol = Symbol("internal");
const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
function isArrayIndexPropName(P) {
if (typeof P !== "string") {
return false;
}
const i = P >>> 0;
if (i === Math.pow(2, 32) - 1) {
return false;
}
const s = `${i}`;
if (P !== s) {
return false;
}
return true;
}
const supportsPropertyIndex = Symbol("supports property index");
const supportedPropertyIndices = Symbol("supported property indices");
const supportsPropertyName = Symbol("supports property name");
const supportedPropertyNames = Symbol("supported property names");
const indexedGet = Symbol("indexed property get");
const indexedSetNew = Symbol("indexed property set new");
const indexedSetExisting = Symbol("indexed property set existing");
const namedGet = Symbol("named property get");
const namedSetNew = Symbol("named property set new");
const namedSetExisting = Symbol("named property set existing");
const namedDelete = Symbol("named property delete");
module.exports = exports = {
isObject,
hasOwn,
getOwnPropertyDescriptors,
wrapperSymbol,
implSymbol,
getSameObject,
wrapperForImpl,
implForWrapper,
tryWrapperForImpl,
tryImplForWrapper,
iterInternalSymbol,
IteratorPrototype,
isArrayIndexPropName,
supportsPropertyIndex,
supportedPropertyIndices,
supportsPropertyName,
supportedPropertyNames,
indexedGet,
indexedSetNew,
indexedSetExisting,
namedGet,
namedSetNew,
namedSetExisting,
namedDelete
};

34
node_modules/w3c-xmlserializer/package.json generated vendored Normal file
View File

@ -0,0 +1,34 @@
{
"name": "w3c-xmlserializer",
"description": "A per-spec XML serializer implementation",
"keywords": [
"dom",
"w3c",
"xml",
"xmlserializer"
],
"version": "1.1.2",
"license": "MIT",
"dependencies": {
"domexception": "^1.0.1",
"webidl-conversions": "^4.0.2",
"xml-name-validator": "^3.0.0"
},
"devDependencies": {
"eslint": "^5.15.2",
"jest": "^24.5.0",
"jsdom": "^14.0.0",
"webidl2js": "^9.2.0"
},
"repository": "jsdom/w3c-xmlserializer",
"files": [
"lib/"
],
"main": "lib/index.js",
"scripts": {
"prepare": "node scripts/convert-idl.js",
"pretest": "node scripts/convert-idl.js",
"test": "jest",
"lint": "eslint ."
}
}