• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / jdita / a7766ea3-980a-4afe-844e-9324f17748d0

18 Apr 2024 01:08PM UTC coverage: 84.65% (-1.2%) from 85.85%
a7766ea3-980a-4afe-844e-9324f17748d0

Pull #136

circleci

marmoure
Revert "[doc] update `serializeToXML` info in the README"

This reverts commit 0fcee1b26.
Pull Request #136: Feature "XDita Serialization of the AST" (#2731)

426 of 539 branches covered (79.04%)

Branch coverage included in aggregate %.

49 of 76 new or added lines in 6 files covered. (64.47%)

13 existing lines in 1 file now uncovered.

1096 of 1259 relevant lines covered (87.05%)

20.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/packages/lwdita-xdita/xditaSerializer.ts
1
import { BaseNode, DocumentNode, TextNode } from "@evolvedbinary/lwdita-ast";
2
import { JDita } from "./classes";
3

4
/**
5
 * Stream interface
6
 */
7
interface SimpleStream<T> {
8
  emit(event: T): void;
9
  close(): void;
10
}
11

12
/**
13
 * Output stream for text
14
 */
15
export interface TextOutputStream extends SimpleStream<string> {}
16

17
/**
18
 * In-memory text output stream implementation
19
 */
20
export class InMemoryTextOutputStream implements TextOutputStream {
1✔
21
  private text = '';
4✔
22

23
  emit(event: string): void {
24
    this.text += event;
36✔
25
  }
26

27
  close(): void {
28
    // Do nothing
29
  }
30

31
  getText(): string {
32
    return this.text;
4✔
33
  }
34
}
35

36
/**
37
 * Serializer for XDITA
38
 * Takes an AST and serializes it to XDITA
39
 */
40
export class XditaSerializer {
1✔
41
  outStream: TextOutputStream;
42
  indent: boolean;
43
  tabSize: number;
44
  indentation: string;
45
  EOL: string;
46
  depth = 0;
5✔
47

48
  /**
49
   * Constructor
50
   *
51
   * @param outStream - The output array
52
   * @param indent - enable indentation
53
   * @param tabSize - size of the tab
54
   */
55
  constructor(outStream: TextOutputStream, indent = false, indentation = " ", tabSize = 4) {
12✔
56
    this.outStream = outStream;
5✔
57
    this.indent = indent;
5✔
58
    this.tabSize = tabSize;
5✔
59
    this.indentation = indentation;
5✔
60
    if(indentation === '\t') {
5!
NEW
61
      this.tabSize = 1;
×
62
    }
63
    this.EOL = '\n';
5✔
64
  }
65

66
  /**
67
   * Emit the indentation to the output stream
68
   */
69
  printIndentation(): void {
70
    if (this.indent) {
14✔
71
      this.outStream.emit(this.indentation.repeat(this.depth * this.tabSize));
3✔
72
    }
73
  }
74

75
  /**
76
   * Emit the End of Line character to the output stream
77
   */
78
  printEOL(): void {
79
    if (this.indent) {
14✔
80
      this.outStream.emit(this.EOL);
3✔
81
    }
82
  }
83

84
  /**
85
   * Emit the attributes to the output stream
86
   */
87
  printAttributes(node: JDita): void {
88
    let attrsPrint = '';
8✔
89
    const props = node.attributes;
8✔
90
    if (props) {
8✔
91
      const attr = props as Record<string, string>;
1✔
92
      attrsPrint = Object.keys(props).filter(key => attr[key]).map(key => `${key}="${attr[key]}"`).join(' ');
5✔
93
    }
94
    if (attrsPrint.length) attrsPrint = ` ${attrsPrint}`;
8✔
95
    this.outStream.emit(attrsPrint);
8✔
96
  }
97

98
  /**
99
   * Emit the text content of text nodes to the output stream
100
   */
101
  printText(text?: string): void {
102
    if (text) {
1!
103
      this.outStream.emit(text);
1✔
104
    }
105
  }
106

107
  /**
108
   * Visit a node and emit its printable tab to the output stream
109
   */
110
  visit(node: JDita): void {
111
    // do not emit anything if the node is a document node
112
    if (node.nodeName === 'document') {
13✔
113
      node.children?.forEach(child => this.visit(child));
4!
114
      // close the output stream after visiting all of the children
115
      this.outStream.close();
4✔
116
    } else {
117
      // print the indentation
118
      this.printIndentation();
9✔
119

120
      // if the node is a text node, print the text content
121
      if (node.nodeName === 'text') {
9✔
122
        this.printText(node.content);
1✔
123
      } else {
124
        // print the node tag
125
        this.outStream.emit(`<${node.nodeName}`);
8✔
126
        // print the attributes
127
        this.printAttributes(node);
8✔
128
        // increment the depth after printing the tag
129
        this.depth++;
8✔
130
        if (node.children?.length) {
8✔
131
          // print the closing tag and visit the children
132
          this.outStream.emit(`>`);
5✔
133
          this.printEOL();
5✔
134
          node.children.forEach(child => this.visit(child));
5✔
135
          this.depth--;
5✔
136
          this.printIndentation();
5✔
137
          this.outStream.emit(`</${node.nodeName}>`);
5✔
138
        } else {
139
          // print the self closing tag
140
          this.outStream.emit(`/>`);
3✔
141
          this.depth--;
3✔
142
        }
143
      }
144
      this.printEOL();
9✔
145
    }
146
  }
147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc