serialization.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. "use strict";
  2. const DOMException = require("domexception");
  3. const xnv = require("xml-name-validator");
  4. const attributeUtils = require("./attributes");
  5. const { NAMESPACES, VOID_ELEMENTS, NODE_TYPES } = require("./constants");
  6. const XML_CHAR = /^(\x09|\x0A|\x0D|[\x20-\uD7FF]|[\uE000-\uFFFD]|(?:[\uD800-\uDBFF][\uDC00-\uDFFF]))*$/;
  7. const PUBID_CHAR = /^(\x20|\x0D|\x0A|[a-zA-Z0-9]|[-'()+,./:=?;!*#@$_%])*$/;
  8. function asciiCaseInsensitiveMatch(a, b) {
  9. if (a.length !== b.length) {
  10. return false;
  11. }
  12. for (let i = 0; i < a.length; ++i) {
  13. if ((a.charCodeAt(i) | 32) !== (b.charCodeAt(i) | 32)) {
  14. return false;
  15. }
  16. }
  17. return true;
  18. }
  19. function recordNamespaceInformation(element, map, prefixMap) {
  20. let defaultNamespaceAttrValue = null;
  21. for (let i = 0; i < element.attributes.length; ++i) {
  22. const attr = element.attributes[i];
  23. if (attr.namespaceURI === NAMESPACES.XMLNS) {
  24. if (attr.prefix === null) {
  25. defaultNamespaceAttrValue = attr.value;
  26. continue;
  27. }
  28. let namespaceDefinition = attr.value;
  29. if (namespaceDefinition === NAMESPACES.XML) {
  30. continue;
  31. }
  32. // This is exactly the other way than the spec says, but that's intended.
  33. // All the maps coalesce null to the empty string (explained in the
  34. // spec), so instead of doing that every time, just do it once here.
  35. if (namespaceDefinition === null) {
  36. namespaceDefinition = "";
  37. }
  38. if (
  39. namespaceDefinition in map &&
  40. map[namespaceDefinition].includes(attr.localName)
  41. ) {
  42. continue;
  43. }
  44. if (!(namespaceDefinition in map)) {
  45. map[namespaceDefinition] = [];
  46. }
  47. map[namespaceDefinition].push(attr.localName);
  48. prefixMap[attr.localName] = namespaceDefinition;
  49. }
  50. }
  51. return defaultNamespaceAttrValue;
  52. }
  53. function serializeDocumentType(node, namespace, prefixMap, requireWellFormed) {
  54. if (requireWellFormed && !PUBID_CHAR.test(node.publicId)) {
  55. throw new Error("Node publicId is not well formed");
  56. }
  57. if (
  58. requireWellFormed &&
  59. (!XML_CHAR.test(node.systemId) ||
  60. (node.systemId.includes('"') && node.systemId.includes("'")))
  61. ) {
  62. throw new Error("Node systemId is not well formed");
  63. }
  64. let markup = `<!DOCTYPE ${node.name}`;
  65. if (node.publicId !== "") {
  66. markup += ` PUBLIC "${node.publicId}"`;
  67. } else if (node.systemId !== "") {
  68. markup += " SYSTEM";
  69. }
  70. if (node.systemId !== "") {
  71. markup += ` "${node.systemId}"`;
  72. }
  73. return markup + ">";
  74. }
  75. function serializeProcessingInstruction(
  76. node,
  77. namespace,
  78. prefixMap,
  79. requireWellFormed
  80. ) {
  81. if (
  82. requireWellFormed &&
  83. (node.target.includes(":") || asciiCaseInsensitiveMatch(node.target, "xml"))
  84. ) {
  85. throw new Error("Node target is not well formed");
  86. }
  87. if (
  88. requireWellFormed &&
  89. (!XML_CHAR.test(node.data) || node.data.includes("?>"))
  90. ) {
  91. throw new Error("Node data is not well formed");
  92. }
  93. return `<?${node.target} ${node.data}?>`;
  94. }
  95. function serializeDocument(
  96. node,
  97. namespace,
  98. prefixMap,
  99. requireWellFormed,
  100. refs
  101. ) {
  102. if (requireWellFormed && node.documentElement === null) {
  103. throw new Error("Document does not have a document element");
  104. }
  105. let serializedDocument = "";
  106. for (const child of node.childNodes) {
  107. serializedDocument += xmlSerialization(
  108. child,
  109. namespace,
  110. prefixMap,
  111. requireWellFormed,
  112. refs
  113. );
  114. }
  115. return serializedDocument;
  116. }
  117. function serializeDocumentFragment(
  118. node,
  119. namespace,
  120. prefixMap,
  121. requireWellFormed,
  122. refs
  123. ) {
  124. let markup = "";
  125. for (const child of node.childNodes) {
  126. markup += xmlSerialization(
  127. child,
  128. namespace,
  129. prefixMap,
  130. requireWellFormed,
  131. refs
  132. );
  133. }
  134. return markup;
  135. }
  136. function serializeText(node, namespace, prefixMap, requireWellFormed) {
  137. if (requireWellFormed && !XML_CHAR.test(node.data)) {
  138. throw new Error("Node data is not well formed");
  139. }
  140. return node.data
  141. .replace(/&/g, "&amp;")
  142. .replace(/</g, "&lt;")
  143. .replace(/>/g, "&gt;");
  144. }
  145. function serializeComment(node, namespace, prefixMap, requireWellFormed) {
  146. if (requireWellFormed && !XML_CHAR.test(node.data)) {
  147. throw new Error("Node data is not well formed");
  148. }
  149. if (
  150. requireWellFormed &&
  151. (node.data.includes("--") || node.data.endsWith("-"))
  152. ) {
  153. throw new Error("Found hyphens in illegal places");
  154. }
  155. return `<!--${node.data}-->`;
  156. }
  157. function serializeElement(node, namespace, prefixMap, requireWellFormed, refs) {
  158. if (
  159. requireWellFormed &&
  160. (node.localName.includes(":") || !xnv.name(node.localName))
  161. ) {
  162. throw new Error("localName is not a valid XML name");
  163. }
  164. let markup = "<";
  165. let qualifiedName = "";
  166. let skipEndTag = false;
  167. let ignoreNamespaceDefinitionAttr = false;
  168. const map = Object.assign({}, prefixMap);
  169. const localPrefixesMap = Object.create(null);
  170. const localDefaultNamespace = recordNamespaceInformation(
  171. node,
  172. map,
  173. localPrefixesMap
  174. );
  175. let inheritedNs = namespace;
  176. const ns = node.namespaceURI;
  177. if (inheritedNs === ns) {
  178. if (localDefaultNamespace !== null) {
  179. ignoreNamespaceDefinitionAttr = true;
  180. }
  181. if (ns === NAMESPACES.XML) {
  182. qualifiedName = "xml:" + node.localName;
  183. } else {
  184. qualifiedName = node.localName;
  185. }
  186. markup += qualifiedName;
  187. } else {
  188. let { prefix } = node;
  189. let candidatePrefix = attributeUtils.preferredPrefixString(map, ns, prefix);
  190. if (prefix === "xmlns") {
  191. if (requireWellFormed) {
  192. throw new Error("Elements can't have xmlns prefix");
  193. }
  194. candidatePrefix = "xmlns";
  195. }
  196. if (candidatePrefix !== null) {
  197. qualifiedName = candidatePrefix + ":" + node.localName;
  198. if (
  199. localDefaultNamespace !== null &&
  200. localDefaultNamespace !== NAMESPACES.XML
  201. ) {
  202. inheritedNs =
  203. localDefaultNamespace === "" ? null : localDefaultNamespace;
  204. }
  205. markup += qualifiedName;
  206. } else if (prefix !== null) {
  207. if (prefix in localPrefixesMap) {
  208. prefix = attributeUtils.generatePrefix(map, ns, refs.prefixIndex++);
  209. }
  210. if (map[ns]) {
  211. map[ns].push(prefix);
  212. } else {
  213. map[ns] = [prefix];
  214. }
  215. qualifiedName = prefix + ":" + node.localName;
  216. markup += `${qualifiedName} xmlns:${prefix}="${attributeUtils.serializeAttributeValue(
  217. ns,
  218. requireWellFormed
  219. )}"`;
  220. if (localDefaultNamespace !== null) {
  221. inheritedNs =
  222. localDefaultNamespace === "" ? null : localDefaultNamespace;
  223. }
  224. } else if (localDefaultNamespace === null || localDefaultNamespace !== ns) {
  225. ignoreNamespaceDefinitionAttr = true;
  226. qualifiedName = node.localName;
  227. inheritedNs = ns;
  228. markup += `${qualifiedName} xmlns="${attributeUtils.serializeAttributeValue(
  229. ns,
  230. requireWellFormed
  231. )}"`;
  232. } else {
  233. qualifiedName = node.localName;
  234. inheritedNs = ns;
  235. markup += qualifiedName;
  236. }
  237. }
  238. markup += attributeUtils.serializeAttributes(
  239. node,
  240. map,
  241. localPrefixesMap,
  242. ignoreNamespaceDefinitionAttr,
  243. requireWellFormed,
  244. refs
  245. );
  246. if (
  247. ns === NAMESPACES.HTML &&
  248. node.childNodes.length === 0 &&
  249. VOID_ELEMENTS.has(node.localName)
  250. ) {
  251. markup += " /";
  252. skipEndTag = true;
  253. } else if (ns !== NAMESPACES.HTML && node.childNodes.length === 0) {
  254. markup += "/";
  255. skipEndTag = true;
  256. }
  257. markup += ">";
  258. if (skipEndTag) {
  259. return markup;
  260. }
  261. if (ns === NAMESPACES.HTML && node.localName === "template") {
  262. markup += xmlSerialization(
  263. node.content,
  264. inheritedNs,
  265. map,
  266. requireWellFormed,
  267. refs
  268. );
  269. } else {
  270. for (const child of node.childNodes) {
  271. markup += xmlSerialization(
  272. child,
  273. inheritedNs,
  274. map,
  275. requireWellFormed,
  276. refs
  277. );
  278. }
  279. }
  280. markup += `</${qualifiedName}>`;
  281. return markup;
  282. }
  283. function serializeCDATASection(node) {
  284. return "<![CDATA[" + node.data + "]]>";
  285. }
  286. /**
  287. * @param {{prefixIndex: number}} refs
  288. */
  289. function xmlSerialization(node, namespace, prefixMap, requireWellFormed, refs) {
  290. switch (node.nodeType) {
  291. case NODE_TYPES.ELEMENT_NODE:
  292. return serializeElement(
  293. node,
  294. namespace,
  295. prefixMap,
  296. requireWellFormed,
  297. refs
  298. );
  299. case NODE_TYPES.DOCUMENT_NODE:
  300. return serializeDocument(
  301. node,
  302. namespace,
  303. prefixMap,
  304. requireWellFormed,
  305. refs
  306. );
  307. case NODE_TYPES.COMMENT_NODE:
  308. return serializeComment(node, namespace, prefixMap, requireWellFormed);
  309. case NODE_TYPES.TEXT_NODE:
  310. return serializeText(node, namespace, prefixMap, requireWellFormed);
  311. case NODE_TYPES.DOCUMENT_FRAGMENT_NODE:
  312. return serializeDocumentFragment(
  313. node,
  314. namespace,
  315. prefixMap,
  316. requireWellFormed,
  317. refs
  318. );
  319. case NODE_TYPES.DOCUMENT_TYPE_NODE:
  320. return serializeDocumentType(
  321. node,
  322. namespace,
  323. prefixMap,
  324. requireWellFormed
  325. );
  326. case NODE_TYPES.PROCESSING_INSTRUCTION_NODE:
  327. return serializeProcessingInstruction(
  328. node,
  329. namespace,
  330. prefixMap,
  331. requireWellFormed
  332. );
  333. case NODE_TYPES.ATTRIBUTE_NODE:
  334. return "";
  335. case NODE_TYPES.CDATA_SECTION_NODE:
  336. return serializeCDATASection(node);
  337. default:
  338. throw new TypeError("Only Nodes and Attr objects can be serialized");
  339. }
  340. }
  341. module.exports.produceXMLSerialization = (root, requireWellFormed) => {
  342. const namespacePrefixMap = Object.create(null);
  343. namespacePrefixMap["http://www.w3.org/XML/1998/namespace"] = ["xml"];
  344. try {
  345. return xmlSerialization(root, null, namespacePrefixMap, requireWellFormed, {
  346. prefixIndex: 1
  347. });
  348. } catch (e) {
  349. throw new DOMException(
  350. "Failed to serialize XML: " + e.message,
  351. "InvalidStateError"
  352. );
  353. }
  354. };