/
XmlComment.ts
117 lines (109 loc) · 3.69 KB
/
XmlComment.ts
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
/**
* Copyright (C) 2016-2019 Michael Kourlas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {getContext} from "../error";
import {fixChar, isUndefined, validateChar} from "../validate";
/**
* The options used to create a new comment.
*/
export interface IXmlCommentOptions {
/**
* The content of the comment.
*/
charData: string;
/**
* Whether to replace any invalid characters in the content of the comment
* with the Unicode replacement character. By default, this is disabled.
*/
replaceInvalidCharsInCharData?: boolean;
}
/**
* Represents a comment.
*
* A comment is structured as follows, where `{content}` is the text of the
* comment:
*
* ```xml
* <!--{content}-->
* ```
*/
export default class XmlComment<Parent> {
private readonly _replaceInvalidCharsInCharData: boolean;
private readonly _parent: Parent;
private readonly _validation: boolean;
private _charData!: string;
constructor(parent: Parent, validation: boolean,
options: IXmlCommentOptions)
{
this._validation = validation;
if (!isUndefined(options.replaceInvalidCharsInCharData)) {
this._replaceInvalidCharsInCharData = (
options.replaceInvalidCharsInCharData);
} else {
this._replaceInvalidCharsInCharData = false;
}
this._parent = parent;
this.charData = options.charData;
}
/**
* Gets the text of this comment.
*/
public get charData() {
return this._charData;
}
/**
* Sets the text of this comment.
*/
public set charData(charData: string) {
if (this._replaceInvalidCharsInCharData) {
charData = fixChar(charData);
} else if (this._validation && !validateChar(charData)) {
throw new Error(`${getContext(this.up())}: comment content`
+ ` "${charData}" should not contain characters`
+ " not allowed in XML");
}
if (this._replaceInvalidCharsInCharData) {
charData = charData.replace("--", "\uFFFD\uFFFD");
} else if (this._validation && charData.indexOf("--") !== -1) {
throw new Error(`${getContext(this.up())}: comment content`
+ ` "${charData}" should not contain the string`
+ " '--'");
}
if (this._replaceInvalidCharsInCharData) {
if (charData.lastIndexOf("-") === charData.length - 1) {
charData = charData.substr(0, charData.length - 1) + "\uFFFD";
}
} else if (this._validation
&& charData.lastIndexOf("-") === charData.length - 1)
{
throw new Error(`${getContext(this.up())}: comment content`
+ ` "${charData}" should not end with the string`
+ " '-'");
}
this._charData = charData;
}
/**
* Returns an XML string representation of this comment.
*/
public toString() {
return "<!--" + this._charData + "-->";
}
/**
* Returns the parent of this comment.
*/
public up() {
return this._parent;
}
}