1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TrimSpacesTransform = void 0;
|
4 | var nodes_1 = require("../nodes");
|
5 |
|
6 |
|
7 |
|
8 | var TrimSpacesTransform = (function () {
|
9 | function TrimSpacesTransform() {
|
10 | }
|
11 | TrimSpacesTransform.transform = function (docParagraph) {
|
12 | var transformedNodes = [];
|
13 |
|
14 | var pendingSpace = false;
|
15 |
|
16 | var accumulatedTextChunks = [];
|
17 | var accumulatedNodes = [];
|
18 |
|
19 |
|
20 | var finishedSkippingLeadingSpaces = false;
|
21 | for (var _i = 0, _a = docParagraph.nodes; _i < _a.length; _i++) {
|
22 | var node = _a[_i];
|
23 | switch (node.kind) {
|
24 | case nodes_1.DocNodeKind.PlainText:
|
25 | var docPlainText = node;
|
26 | var text = docPlainText.text;
|
27 | var startedWithSpace = /^\s/.test(text);
|
28 | var endedWithSpace = /\s$/.test(text);
|
29 | var collapsedText = text.replace(/\s+/g, ' ').trim();
|
30 | if (startedWithSpace && finishedSkippingLeadingSpaces) {
|
31 | pendingSpace = true;
|
32 | }
|
33 | if (collapsedText.length > 0) {
|
34 | if (pendingSpace) {
|
35 | accumulatedTextChunks.push(' ');
|
36 | pendingSpace = false;
|
37 | }
|
38 | accumulatedTextChunks.push(collapsedText);
|
39 | accumulatedNodes.push(node);
|
40 | finishedSkippingLeadingSpaces = true;
|
41 | }
|
42 | if (endedWithSpace && finishedSkippingLeadingSpaces) {
|
43 | pendingSpace = true;
|
44 | }
|
45 | break;
|
46 | case nodes_1.DocNodeKind.SoftBreak:
|
47 | if (finishedSkippingLeadingSpaces) {
|
48 | pendingSpace = true;
|
49 | }
|
50 | accumulatedNodes.push(node);
|
51 | break;
|
52 | default:
|
53 | if (pendingSpace) {
|
54 | accumulatedTextChunks.push(' ');
|
55 | pendingSpace = false;
|
56 | }
|
57 |
|
58 | if (accumulatedTextChunks.length > 0) {
|
59 |
|
60 |
|
61 | transformedNodes.push(new nodes_1.DocPlainText({
|
62 | configuration: docParagraph.configuration,
|
63 | text: accumulatedTextChunks.join('')
|
64 | }));
|
65 | accumulatedTextChunks.length = 0;
|
66 | accumulatedNodes.length = 0;
|
67 | }
|
68 | transformedNodes.push(node);
|
69 | finishedSkippingLeadingSpaces = true;
|
70 | }
|
71 | }
|
72 |
|
73 | if (accumulatedTextChunks.length > 0) {
|
74 | transformedNodes.push(new nodes_1.DocPlainText({
|
75 | configuration: docParagraph.configuration,
|
76 | text: accumulatedTextChunks.join('')
|
77 | }));
|
78 | accumulatedTextChunks.length = 0;
|
79 | accumulatedNodes.length = 0;
|
80 | }
|
81 | var transformedParagraph = new nodes_1.DocParagraph({
|
82 | configuration: docParagraph.configuration
|
83 | });
|
84 | transformedParagraph.appendNodes(transformedNodes);
|
85 | return transformedParagraph;
|
86 | };
|
87 | return TrimSpacesTransform;
|
88 | }());
|
89 | exports.TrimSpacesTransform = TrimSpacesTransform;
|
90 |
|
\ | No newline at end of file |