1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | class _Visitor {
|
---|
9 | visitTag(tag) {
|
---|
10 | const strAttrs = this._serializeAttributes(tag.attrs);
|
---|
11 | if (tag.children.length == 0) {
|
---|
12 | return `<${tag.name}${strAttrs}/>`;
|
---|
13 | }
|
---|
14 | const strChildren = tag.children.map(node => node.visit(this));
|
---|
15 | return `<${tag.name}${strAttrs}>${strChildren.join('')}</${tag.name}>`;
|
---|
16 | }
|
---|
17 | visitText(text) {
|
---|
18 | return text.value;
|
---|
19 | }
|
---|
20 | visitDeclaration(decl) {
|
---|
21 | return `<?xml${this._serializeAttributes(decl.attrs)} ?>`;
|
---|
22 | }
|
---|
23 | _serializeAttributes(attrs) {
|
---|
24 | const strAttrs = Object.keys(attrs).map((name) => `${name}="${attrs[name]}"`).join(' ');
|
---|
25 | return strAttrs.length > 0 ? ' ' + strAttrs : '';
|
---|
26 | }
|
---|
27 | visitDoctype(doctype) {
|
---|
28 | return `<!DOCTYPE ${doctype.rootTag} [\n${doctype.dtd}\n]>`;
|
---|
29 | }
|
---|
30 | }
|
---|
31 | const _visitor = new _Visitor();
|
---|
32 | export function serialize(nodes) {
|
---|
33 | return nodes.map((node) => node.visit(_visitor)).join('');
|
---|
34 | }
|
---|
35 | export class Declaration {
|
---|
36 | constructor(unescapedAttrs) {
|
---|
37 | this.attrs = {};
|
---|
38 | Object.keys(unescapedAttrs).forEach((k) => {
|
---|
39 | this.attrs[k] = escapeXml(unescapedAttrs[k]);
|
---|
40 | });
|
---|
41 | }
|
---|
42 | visit(visitor) {
|
---|
43 | return visitor.visitDeclaration(this);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | export class Doctype {
|
---|
47 | constructor(rootTag, dtd) {
|
---|
48 | this.rootTag = rootTag;
|
---|
49 | this.dtd = dtd;
|
---|
50 | }
|
---|
51 | visit(visitor) {
|
---|
52 | return visitor.visitDoctype(this);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | export class Tag {
|
---|
56 | constructor(name, unescapedAttrs = {}, children = []) {
|
---|
57 | this.name = name;
|
---|
58 | this.children = children;
|
---|
59 | this.attrs = {};
|
---|
60 | Object.keys(unescapedAttrs).forEach((k) => {
|
---|
61 | this.attrs[k] = escapeXml(unescapedAttrs[k]);
|
---|
62 | });
|
---|
63 | }
|
---|
64 | visit(visitor) {
|
---|
65 | return visitor.visitTag(this);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | export class Text {
|
---|
69 | constructor(unescapedValue) {
|
---|
70 | this.value = escapeXml(unescapedValue);
|
---|
71 | }
|
---|
72 | visit(visitor) {
|
---|
73 | return visitor.visitText(this);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | export class CR extends Text {
|
---|
77 | constructor(ws = 0) {
|
---|
78 | super(`\n${new Array(ws + 1).join(' ')}`);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | const _ESCAPED_CHARS = [
|
---|
82 | [/&/g, '&'],
|
---|
83 | [/"/g, '"'],
|
---|
84 | [/'/g, '''],
|
---|
85 | [/</g, '<'],
|
---|
86 | [/>/g, '>'],
|
---|
87 | ];
|
---|
88 | // Escape `_ESCAPED_CHARS` characters in the given text with encoded entities
|
---|
89 | export function escapeXml(text) {
|
---|
90 | return _ESCAPED_CHARS.reduce((text, entry) => text.replace(entry[0], entry[1]), text);
|
---|
91 | }
|
---|
92 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoieG1sX2hlbHBlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvbXBpbGVyL3NyYy9pMThuL3NlcmlhbGl6ZXJzL3htbF9oZWxwZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6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