All files / xml / stringify.ts

100.00% Branches 33/33
100.00% Lines 100/100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
x3
x3
 
x38
 
 
x38
x45
x45
x50
x50
x52
x52
x50
x45
x45
x45
 
 
x66
x38
 
 
x3
x8
x8
x8
x8
x8
x8
x8
x8
 
 
 
 
x3
x3
 
x38
x132
x44
x38
 
 
 
 
x3
x3
x3
x3
x3
 
 
x46
x46
x46
 
 
x46
x46
x46
 
 
x46
x46
x53
x53
 
 
x46
x64
x64
 
 
x71
x71
 
 
x46
 
x59
x59
x59
x59
x59
 
 
x71
x71
x71
 
x71
x46
 
 
 
 
x3
x3
x3
x3
x3
 
x31
x31
x39
x31
x40
x31
x37
x67
x36
x36
x36
x31
x31
 
 
 
 
 
 
 
 
x3
 
x9
x11
x11
 
 
 
x13
x13
x9
 
 
 
 
 
x3
x8
x9
x9
 
x9
x8
x9
x9
 
x9
x11
x8



































































































































































































// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

/**
 * XML serialization module.
 *
 * @module
 */

import type {
  StringifyOptions,
  XmlDeclarationEvent,
  XmlDocument,
  XmlElement,
  XmlNode,
} from "./types.ts";
import { encodeAttributeValue, encodeEntities } from "./_entities.ts";

export type { StringifyOptions } from "./types.ts";

/**
 * Converts an XML document or element to an XML string.
 *
 * @example Usage
 * ```ts
 * import { stringify } from "@std/xml/stringify";
 * import { assertEquals } from "@std/assert";
 *
 * const element = {
 *   type: "element" as const,
 *   name: { raw: "root", local: "root" },
 *   attributes: {},
 *   children: [{ type: "text" as const, text: "Hello!" }],
 * };
 *
 * assertEquals(stringify(element), "<root>Hello!</root>");
 * ```
 *
 * @param node The XML document or element to serialize.
 * @param options Options to control serialization behavior.
 * @returns The serialized XML string.
 */
export function stringify(
  node: XmlDocument | XmlElement,
  options?: StringifyOptions,
): string {
  const { indent, declaration = true } = options ?? {};

  // Check if it's a document (has 'root' property) or an element
  if ("root" in node) {
    let result = "";
    if (declaration && node.declaration) {
      result += serializeDeclaration(node.declaration);
      if (indent !== undefined) {
        result += "\n";
      }
    }
    result += serializeElement(node.root, indent, 0);
    return result;
  }

  // It's an element
  return serializeElement(node, indent, 0);
}

/** Serializes an XML declaration to a string. */
function serializeDeclaration(decl: XmlDeclarationEvent): string {
  const encoding = decl.encoding !== undefined
    ? ` encoding="${decl.encoding}"`
    : "";
  const standalone = decl.standalone !== undefined
    ? ` standalone="${decl.standalone}"`
    : "";
  return `<?xml version="${decl.version}"${encoding}${standalone}?>`;
}

/**
 * Creates a memoized indent getter to avoid recomputing indent.repeat(depth).
 */
function createIndentCache(
  indent: string | undefined,
): (depth: number) => string {
  if (indent === undefined) return () => "";
  const cache: string[] = [""];
  return (depth: number): string => (cache[depth] ??= indent.repeat(depth));
}

/**
 * Serializes an XML element and its children to a string.
 */
function serializeElement(
  element: XmlElement,
  indent: string | undefined,
  depth: number,
  getIndent?: (depth: number) => string,
): string {
  // Initialize indent cache on first call
  const indentFn = getIndent ?? createIndentCache(indent);
  const prefix = indentFn(depth);
  const newline = indent !== undefined ? "\n" : "";

  // Build tag name (with optional namespace prefix)
  const tagName = element.name.prefix
    ? `${element.name.prefix}:${element.name.local}`
    : element.name.local;

  // Build attributes string
  let attrsStr = "";
  for (const [name, value] of Object.entries(element.attributes)) {
    attrsStr += ` ${name}="${encodeAttributeValue(value)}"`;
  }

  // Self-closing tag if no children
  if (element.children.length === 0) {
    return `${prefix}<${tagName}${attrsStr}/>`;
  }

  // Check if all children are inline content (text or cdata only)
  const hasOnlyInlineContent = element.children.every(
    (child) => child.type === "text" || child.type === "cdata",
  );

  if (hasOnlyInlineContent) {
    // Inline: <tag>content</tag> (no indentation for content)
    const content = element.children
      .map((child) => serializeNode(child, undefined, 0, indentFn))
      .join("");
    return `${prefix}<${tagName}${attrsStr}>${content}</${tagName}>`;
  }

  // Block: children on separate lines (when indenting)
  const childContent = element.children
    .map((child) => serializeNode(child, indent, depth + 1, indentFn))
    .join(newline);

  return `${prefix}<${tagName}${attrsStr}>${newline}${childContent}${newline}${prefix}</${tagName}>`;
}

/**
 * Serializes any XML node to a string.
 */
function serializeNode(
  node: XmlNode,
  indent: string | undefined,
  depth: number,
  getIndent: (depth: number) => string,
): string {
  switch (node.type) {
    case "element":
      return serializeElement(node, indent, depth, getIndent);
    case "text":
      return encodeEntities(node.text);
    case "cdata":
      return serializeCData(node.text);
    case "comment": {
      const prefix = getIndent(depth);
      return `${prefix}<!--${validateCommentText(node.text)}-->`;
    }
  }
}

/**
 * Serializes CDATA content, escaping any `]]>` sequences.
 *
 * Per XML 1.0 §2.7, CDATA sections cannot contain `]]>`.
 * The standard approach is to split at each occurrence:
 * `a]]>b` becomes `<![CDATA[a]]]]><![CDATA[>b]]>`
 */
function serializeCData(text: string): string {
  // Fast path: no ]]> means no escaping needed
  if (!text.includes("]]>")) {
    return `<![CDATA[${text}]]>`;
  }

  // Replace each ]]> with ]]]]><![CDATA[>
  // This ends the current CDATA at ]] and starts a new one with >
  const escaped = text.replaceAll("]]>", "]]]]><![CDATA[>");
  return `<![CDATA[${escaped}]]>`;
}

/**
 * Validates comment text per XML 1.0 §2.5: cannot contain `--` or end with `-`.
 * @throws {TypeError} If the comment text contains invalid sequences.
 */
function validateCommentText(text: string): string {
  if (text.includes("--")) {
    throw new TypeError(
      `Invalid comment: contains "--" which is forbidden in XML comments`,
    );
  }
  if (text.endsWith("-")) {
    throw new TypeError(
      `Invalid comment: ends with "-" which would produce invalid "--->"`,
    );
  }
  return text;
}