新增调试信息

This commit is contained in:
2025-10-27 22:15:25 +08:00
parent ae62457d8c
commit 04642cb2f0
5479 changed files with 683397 additions and 3450 deletions
+19
View File
@@ -0,0 +1,19 @@
/**
* Copyright (C) 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.
*/
/**
* Returns a context string for the specified node, for use in error messages.
*/
export declare function getContext(obj: unknown): string;
+47
View File
@@ -0,0 +1,47 @@
"use strict";
/**
* Copyright (C) 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContext = void 0;
var XmlAttribute_1 = __importDefault(require("./nodes/XmlAttribute"));
var XmlDocument_1 = __importDefault(require("./nodes/XmlDocument"));
var XmlDtd_1 = __importDefault(require("./nodes/XmlDtd"));
var XmlElement_1 = __importDefault(require("./nodes/XmlElement"));
/**
* Returns a context string for the specified node, for use in error messages.
*/
function getContext(obj) {
if (obj instanceof XmlAttribute_1.default) {
return getContext(obj.up()) + (" > attribute \"" + obj.name + "\"");
}
else if (obj instanceof XmlDocument_1.default) {
return "in XML document";
}
else if (obj instanceof XmlDtd_1.default) {
return getContext(obj.up()) + " > DTD";
}
else if (obj instanceof XmlElement_1.default) {
return getContext(obj.up()) + (" > element \"" + obj.name + "\"");
}
else {
throw new Error("Unrecognized object of type "
+ Object.prototype.toString.call(obj));
}
}
exports.getContext = getContext;
+37
View File
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2016-2018 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.
*/
/**
* Replaces ampersands (&) with the appropriate XML character reference.
*/
export declare function escapeAmpersands(str: string): string;
/**
* Replaces left angle brackets (<) with the appropriate XML character
* reference.
*/
export declare function escapeLeftAngleBrackets(str: string): string;
/**
* Replaces right angle brackets (>) with the appropriate XML character
* reference when part of the string "]]>".
*/
export declare function escapeRightAngleBracketsInCdataTerminator(str: string): string;
/**
* Replaces single quotes (") with the appropriate XML character reference.
*/
export declare function escapeSingleQuotes(str: string): string;
/**
* Replaces double quotes (") with the appropriate XML character reference.
*/
export declare function escapeDoubleQuotes(str: string): string;
+55
View File
@@ -0,0 +1,55 @@
"use strict";
/**
* Copyright (C) 2016-2018 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.escapeDoubleQuotes = exports.escapeSingleQuotes = exports.escapeRightAngleBracketsInCdataTerminator = exports.escapeLeftAngleBrackets = exports.escapeAmpersands = void 0;
/**
* Replaces ampersands (&) with the appropriate XML character reference.
*/
function escapeAmpersands(str) {
return str.replace(/&/g, "&");
}
exports.escapeAmpersands = escapeAmpersands;
/**
* Replaces left angle brackets (<) with the appropriate XML character
* reference.
*/
function escapeLeftAngleBrackets(str) {
return str.replace(/</g, "&lt;");
}
exports.escapeLeftAngleBrackets = escapeLeftAngleBrackets;
/**
* Replaces right angle brackets (&gt;) with the appropriate XML character
* reference when part of the string "]]>".
*/
function escapeRightAngleBracketsInCdataTerminator(str) {
return str.replace(/]]>/g, "]]&gt;");
}
exports.escapeRightAngleBracketsInCdataTerminator = escapeRightAngleBracketsInCdataTerminator;
/**
* Replaces single quotes (") with the appropriate XML character reference.
*/
function escapeSingleQuotes(str) {
return str.replace(/'/g, "&apos;");
}
exports.escapeSingleQuotes = escapeSingleQuotes;
/**
* Replaces double quotes (") with the appropriate XML character reference.
*/
function escapeDoubleQuotes(str) {
return str.replace(/"/g, "&quot;");
}
exports.escapeDoubleQuotes = escapeDoubleQuotes;
+38
View File
@@ -0,0 +1,38 @@
/**
* 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 XmlDocument, { IXmlDocumentOptions } from "./nodes/XmlDocument";
export { default as XmlAttribute, IXmlAttributeOptions } from "./nodes/XmlAttribute";
export { default as XmlAttributeText, IXmlAttributeTextOptions } from "./nodes/XmlAttributeText";
export { default as XmlCdata, IXmlCdataOptions } from "./nodes/XmlCdata";
export { default as XmlCharData, IXmlCharDataOptions } from "./nodes/XmlCharData";
export { default as XmlCharRef, IXmlCharRefOptions } from "./nodes/XmlCharRef";
export { default as XmlComment, IXmlCommentOptions } from "./nodes/XmlComment";
export { default as XmlDecl, IXmlDeclOptions } from "./nodes/XmlDecl";
export { default as XmlDocument, IXmlDocumentOptions } from "./nodes/XmlDocument";
export { default as XmlDtd, IXmlDtdOptions } from "./nodes/XmlDtd";
export { default as XmlDtdAttlist, IXmlDtdAttlistOptions } from "./nodes/XmlDtdAttlist";
export { default as XmlDtdElement, IXmlDtdElementOptions } from "./nodes/XmlDtdElement";
export { default as XmlDtdEntity, IXmlDtdEntityOptions } from "./nodes/XmlDtdEntity";
export { default as XmlDtdNotation, IXmlDtdNotationOptions } from "./nodes/XmlDtdNotation";
export { default as XmlDtdParamEntityRef, IXmlDtdParamEntityRefOptions } from "./nodes/XmlDtdParamEntityRef";
export { default as XmlElement, IXmlElementOptions } from "./nodes/XmlElement";
export { default as XmlEntityRef, IXmlEntityRefOptions } from "./nodes/XmlEntityRef";
export { default as XmlProcInst, IXmlProcInstOptions } from "./nodes/XmlProcInst";
export { IStringOptions } from "./options";
/**
* Returns a new XML document with the specified options.
*/
export declare function document(options?: IXmlDocumentOptions): XmlDocument;
+64
View File
@@ -0,0 +1,64 @@
"use strict";
/**
* 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.document = exports.XmlProcInst = exports.XmlEntityRef = exports.XmlElement = exports.XmlDtdParamEntityRef = exports.XmlDtdNotation = exports.XmlDtdEntity = exports.XmlDtdElement = exports.XmlDtdAttlist = exports.XmlDtd = exports.XmlDocument = exports.XmlDecl = exports.XmlComment = exports.XmlCharRef = exports.XmlCharData = exports.XmlCdata = exports.XmlAttributeText = exports.XmlAttribute = void 0;
var XmlDocument_1 = __importDefault(require("./nodes/XmlDocument"));
var XmlAttribute_1 = require("./nodes/XmlAttribute");
Object.defineProperty(exports, "XmlAttribute", { enumerable: true, get: function () { return __importDefault(XmlAttribute_1).default; } });
var XmlAttributeText_1 = require("./nodes/XmlAttributeText");
Object.defineProperty(exports, "XmlAttributeText", { enumerable: true, get: function () { return __importDefault(XmlAttributeText_1).default; } });
var XmlCdata_1 = require("./nodes/XmlCdata");
Object.defineProperty(exports, "XmlCdata", { enumerable: true, get: function () { return __importDefault(XmlCdata_1).default; } });
var XmlCharData_1 = require("./nodes/XmlCharData");
Object.defineProperty(exports, "XmlCharData", { enumerable: true, get: function () { return __importDefault(XmlCharData_1).default; } });
var XmlCharRef_1 = require("./nodes/XmlCharRef");
Object.defineProperty(exports, "XmlCharRef", { enumerable: true, get: function () { return __importDefault(XmlCharRef_1).default; } });
var XmlComment_1 = require("./nodes/XmlComment");
Object.defineProperty(exports, "XmlComment", { enumerable: true, get: function () { return __importDefault(XmlComment_1).default; } });
var XmlDecl_1 = require("./nodes/XmlDecl");
Object.defineProperty(exports, "XmlDecl", { enumerable: true, get: function () { return __importDefault(XmlDecl_1).default; } });
var XmlDocument_2 = require("./nodes/XmlDocument");
Object.defineProperty(exports, "XmlDocument", { enumerable: true, get: function () { return __importDefault(XmlDocument_2).default; } });
var XmlDtd_1 = require("./nodes/XmlDtd");
Object.defineProperty(exports, "XmlDtd", { enumerable: true, get: function () { return __importDefault(XmlDtd_1).default; } });
var XmlDtdAttlist_1 = require("./nodes/XmlDtdAttlist");
Object.defineProperty(exports, "XmlDtdAttlist", { enumerable: true, get: function () { return __importDefault(XmlDtdAttlist_1).default; } });
var XmlDtdElement_1 = require("./nodes/XmlDtdElement");
Object.defineProperty(exports, "XmlDtdElement", { enumerable: true, get: function () { return __importDefault(XmlDtdElement_1).default; } });
var XmlDtdEntity_1 = require("./nodes/XmlDtdEntity");
Object.defineProperty(exports, "XmlDtdEntity", { enumerable: true, get: function () { return __importDefault(XmlDtdEntity_1).default; } });
var XmlDtdNotation_1 = require("./nodes/XmlDtdNotation");
Object.defineProperty(exports, "XmlDtdNotation", { enumerable: true, get: function () { return __importDefault(XmlDtdNotation_1).default; } });
var XmlDtdParamEntityRef_1 = require("./nodes/XmlDtdParamEntityRef");
Object.defineProperty(exports, "XmlDtdParamEntityRef", { enumerable: true, get: function () { return __importDefault(XmlDtdParamEntityRef_1).default; } });
var XmlElement_1 = require("./nodes/XmlElement");
Object.defineProperty(exports, "XmlElement", { enumerable: true, get: function () { return __importDefault(XmlElement_1).default; } });
var XmlEntityRef_1 = require("./nodes/XmlEntityRef");
Object.defineProperty(exports, "XmlEntityRef", { enumerable: true, get: function () { return __importDefault(XmlEntityRef_1).default; } });
var XmlProcInst_1 = require("./nodes/XmlProcInst");
Object.defineProperty(exports, "XmlProcInst", { enumerable: true, get: function () { return __importDefault(XmlProcInst_1).default; } });
/**
* Returns a new XML document with the specified options.
*/
function document(options) {
if (options === void 0) { options = {}; }
return new XmlDocument_1.default(options);
}
exports.document = document;
+88
View File
@@ -0,0 +1,88 @@
/**
* 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 { IStringOptions } from "../options";
import XmlAttributeText, { IXmlAttributeTextOptions } from "./XmlAttributeText";
import XmlCharRef, { IXmlCharRefOptions } from "./XmlCharRef";
import XmlEntityRef, { IXmlEntityRefOptions } from "./XmlEntityRef";
/**
* The options used to create a new attribute.
*/
export interface IXmlAttributeOptions {
/**
* The name of the attribute.
*/
name: string;
/**
* Whether to replace any invalid characters in the name of the attribute
* with the Unicode replacement character. By default, this is disabled.
*/
replaceInvalidCharsInName?: boolean;
}
/**
* Represents an attribute.
*
* An attribute is part of the start tag of an element and is
* structured as follows, where `{name}` is the name of the attribute and
* `{value}` is the value of the attribute:
*
* ```xml
* <element {name}="{value}">
* ```
*
* The `{name}` value is a property of this node, while the `{value}` property
* consists of the children of this node.
*
* Attributes can have an unlimited number of attribute text, character
* references, and entity references.
*/
export default class XmlAttribute<Parent> {
private readonly _children;
private readonly _replaceInvalidCharsInName;
private readonly _parent;
private readonly _validation;
private _name;
constructor(parent: Parent, validation: boolean, options: IXmlAttributeOptions);
/**
* Gets the name of this attribute.
*/
get name(): string;
/**
* Sets the name of this attribute.
*/
set name(name: string);
/**
* Adds a character reference to this attribute and returns the new
* character reference.
*/
charRef(options: IXmlCharRefOptions): XmlCharRef<this>;
/**
* Adds an entity reference to this attribute and returns the new entity
* reference.
*/
entityRef(options: IXmlEntityRefOptions): XmlEntityRef<this>;
/**
* Adds attribute text to this attribute and returns the new text.
*/
text(options: IXmlAttributeTextOptions): XmlAttributeText<this>;
/**
* Returns an XML string representation of this attribute.
*/
toString(options?: IStringOptions): string;
/**
* Returns the parent of this attribute.
*/
up(): Parent;
}
+146
View File
@@ -0,0 +1,146 @@
"use strict";
/**
* 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var escape_1 = require("../escape");
var options_1 = require("../options");
var validate_1 = require("../validate");
var XmlAttributeText_1 = __importDefault(require("./XmlAttributeText"));
var XmlCharRef_1 = __importDefault(require("./XmlCharRef"));
var XmlEntityRef_1 = __importDefault(require("./XmlEntityRef"));
/**
* Represents an attribute.
*
* An attribute is part of the start tag of an element and is
* structured as follows, where `{name}` is the name of the attribute and
* `{value}` is the value of the attribute:
*
* ```xml
* <element {name}="{value}">
* ```
*
* The `{name}` value is a property of this node, while the `{value}` property
* consists of the children of this node.
*
* Attributes can have an unlimited number of attribute text, character
* references, and entity references.
*/
var XmlAttribute = /** @class */ (function () {
function XmlAttribute(parent, validation, options) {
this._validation = validation;
if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInName)) {
this._replaceInvalidCharsInName = options.replaceInvalidCharsInName;
}
else {
this._replaceInvalidCharsInName = false;
}
this._children = [];
this._parent = parent;
this.name = options.name;
}
Object.defineProperty(XmlAttribute.prototype, "name", {
/**
* Gets the name of this attribute.
*/
get: function () {
return this._name;
},
/**
* Sets the name of this attribute.
*/
set: function (name) {
if (this._replaceInvalidCharsInName) {
name = (0, validate_1.fixName)(name);
if (name.length === 0) {
throw new Error((0, error_1.getContext)(this.up()) + ": attribute name"
+ " should not be empty");
}
}
else if (this._validation && !(0, validate_1.validateName)(name)) {
if (name.length === 0) {
throw new Error((0, error_1.getContext)(this.up()) + ": attribute name"
+ " should not be empty");
}
else {
throw new Error((0, error_1.getContext)(this.up()) + ": attribute name"
+ (" \"" + name + "\" should not contain characters not")
+ " allowed in XML names");
}
}
this._name = name;
},
enumerable: false,
configurable: true
});
/**
* Adds a character reference to this attribute and returns the new
* character reference.
*/
XmlAttribute.prototype.charRef = function (options) {
var charRef = new XmlCharRef_1.default(this, this._validation, options);
this._children.push(charRef);
return charRef;
};
/**
* Adds an entity reference to this attribute and returns the new entity
* reference.
*/
XmlAttribute.prototype.entityRef = function (options) {
var charRef = new XmlEntityRef_1.default(this, this._validation, options);
this._children.push(charRef);
return charRef;
};
/**
* Adds attribute text to this attribute and returns the new text.
*/
XmlAttribute.prototype.text = function (options) {
var textNode = new XmlAttributeText_1.default(this, this._validation, options);
this._children.push(textNode);
return textNode;
};
/**
* Returns an XML string representation of this attribute.
*/
XmlAttribute.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
var optionsObj = new options_1.StringOptions(options);
var quote = optionsObj.doubleQuotes ? "\"" : "'";
var str = this._name + "=" + quote;
for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
var child = _a[_i];
if (optionsObj.doubleQuotes) {
str += (0, escape_1.escapeDoubleQuotes)(child.toString());
}
else {
str += (0, escape_1.escapeSingleQuotes)(child.toString());
}
}
str += quote;
return str;
};
/**
* Returns the parent of this attribute.
*/
XmlAttribute.prototype.up = function () {
return this._parent;
};
return XmlAttribute;
}());
exports.default = XmlAttribute;
+58
View File
@@ -0,0 +1,58 @@
/**
* 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.
*/
/**
* The options used to create attribute text.
*/
export interface IXmlAttributeTextOptions {
/**
* The attribute text.
*/
charData: string;
/**
* Whether to replace any invalid characters in the attribute text with the
* Unicode replacement character. By default, this is disabled.
*/
replaceInvalidCharsInCharData?: boolean;
}
/**
* Represents text in an attribute value.
*
* Restricted characters, such as the ampersand (`&`) and the opening angle
* bracket (`<`), are all automatically escaped.
*/
export default class XmlAttributeText<Parent> {
private readonly _replaceInvalidCharsInCharData;
private readonly _parent;
private readonly _validation;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlAttributeTextOptions);
/**
* Gets this attribute text.
*/
get charData(): string;
/**
* Sets this attribute text.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this attribute text.
*/
toString(): string;
/**
* Returns the parent of this attribute text.
*/
up(): Parent;
}
+80
View File
@@ -0,0 +1,80 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var escape_1 = require("../escape");
var validate_1 = require("../validate");
/**
* Represents text in an attribute value.
*
* Restricted characters, such as the ampersand (`&`) and the opening angle
* bracket (`<`), are all automatically escaped.
*/
var XmlAttributeText = /** @class */ (function () {
function XmlAttributeText(parent, validation, options) {
this._validation = validation;
if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInCharData)) {
this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData);
}
else {
this._replaceInvalidCharsInCharData = false;
}
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlAttributeText.prototype, "charData", {
/**
* Gets this attribute text.
*/
get: function () {
return this._charData;
},
/**
* Sets this attribute text.
*/
set: function (charData) {
if (this._replaceInvalidCharsInCharData) {
charData = (0, validate_1.fixChar)(charData);
}
else if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.getContext)(this.up()) + ": attribute text"
+ (" \"" + charData + "\" should not contain characters not")
+ " allowed in XML");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this attribute text.
*/
XmlAttributeText.prototype.toString = function () {
var str = this._charData;
str = (0, escape_1.escapeAmpersands)(str);
str = (0, escape_1.escapeLeftAngleBrackets)(str);
return str;
};
/**
* Returns the parent of this attribute text.
*/
XmlAttributeText.prototype.up = function () {
return this._parent;
};
return XmlAttributeText;
}());
exports.default = XmlAttributeText;
+63
View File
@@ -0,0 +1,63 @@
/**
* 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.
*/
/**
* The options used to create a CDATA section.
*/
export interface IXmlCdataOptions {
/**
* The character data of the CDATA section.
*/
charData: string;
/**
* Whether to replace any invalid characters in the character data of the
* CDATA section with the Unicode replacement character. By default, this
* is disabled.
*/
replaceInvalidCharsInCharData?: boolean;
}
/**
* Represents a CDATA section.
*
* A CDATA section is structured as follows, where `{data}` is the
* character data of the section:
*
* ```xml
* <![CDATA[{data}]]>
* ```
*/
export default class XmlCdata<Parent> {
private readonly _replaceInvalidCharsInCharData;
private readonly _parent;
private readonly _validation;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlCdataOptions);
/**
* Gets the character data of this CDATA section.
*/
get charData(): string;
/**
* Sets the character data of this CDATA section.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this CDATA section.
*/
toString(): string;
/**
* Returns the parent of this CDATA section.
*/
up(): Parent;
}
+88
View File
@@ -0,0 +1,88 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents a CDATA section.
*
* A CDATA section is structured as follows, where `{data}` is the
* character data of the section:
*
* ```xml
* <![CDATA[{data}]]>
* ```
*/
var XmlCdata = /** @class */ (function () {
function XmlCdata(parent, validation, options) {
this._validation = validation;
if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInCharData)) {
this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData);
}
else {
this._replaceInvalidCharsInCharData = false;
}
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlCdata.prototype, "charData", {
/**
* Gets the character data of this CDATA section.
*/
get: function () {
return this._charData;
},
/**
* Sets the character data of this CDATA section.
*/
set: function (charData) {
if (this._replaceInvalidCharsInCharData) {
charData = (0, validate_1.fixChar)(charData);
}
else if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.getContext)(this.up()) + ": CDATA section"
+ (" \"" + charData + "\" should not contain characters")
+ " not allowed in XML");
}
if (this._replaceInvalidCharsInCharData) {
charData = charData.replace("]]>", "\uFFFD\uFFFD\uFFFD");
}
else if (this._validation && charData.indexOf("]]>") !== -1) {
throw new Error((0, error_1.getContext)(this.up()) + ": CDATA section"
+ (" \"" + charData + "\" should not contain the string")
+ " ']]>'");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this CDATA section.
*/
XmlCdata.prototype.toString = function () {
return "<![CDATA[" + this._charData + "]]>";
};
/**
* Returns the parent of this CDATA section.
*/
XmlCdata.prototype.up = function () {
return this._parent;
};
return XmlCdata;
}());
exports.default = XmlCdata;
+59
View File
@@ -0,0 +1,59 @@
/**
* 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.
*/
/**
* The options used to create new character data.
*/
export interface IXmlCharDataOptions {
/**
* The character data.
*/
charData: string;
/**
* Whether to replace any invalid characters in the character data with the
* Unicode replacement character. By default, this is disabled.
*/
replaceInvalidCharsInCharData?: boolean;
}
/**
* Represents character data.
*
* Restricted characters, such as the ampersand (`&`), the opening angle
* bracket (`<`), and the closing angle bracket (`>`) when it appears in the
* string `]]>`, are all automatically escaped.
*/
export default class XmlCharData<Parent> {
private readonly _replaceInvalidCharsInCharData;
private readonly _parent;
private readonly _validation;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlCharDataOptions);
/**
* Gets the text of this character data.
*/
get charData(): string;
/**
* Sets the text of this character data.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this character data.
*/
toString(): string;
/**
* Returns the parent of this character data.
*/
up(): Parent;
}
+82
View File
@@ -0,0 +1,82 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var escape_1 = require("../escape");
var validate_1 = require("../validate");
/**
* Represents character data.
*
* Restricted characters, such as the ampersand (`&`), the opening angle
* bracket (`<`), and the closing angle bracket (`>`) when it appears in the
* string `]]>`, are all automatically escaped.
*/
var XmlCharData = /** @class */ (function () {
function XmlCharData(parent, validation, options) {
this._validation = validation;
if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInCharData)) {
this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData);
}
else {
this._replaceInvalidCharsInCharData = false;
}
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlCharData.prototype, "charData", {
/**
* Gets the text of this character data.
*/
get: function () {
return this._charData;
},
/**
* Sets the text of this character data.
*/
set: function (charData) {
if (this._replaceInvalidCharsInCharData) {
charData = (0, validate_1.fixChar)(charData);
}
else if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.getContext)(this.up()) + ": character data"
+ ("\"" + charData + "\" should not contain characters not")
+ " allowed in XML");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this character data.
*/
XmlCharData.prototype.toString = function () {
var str = this._charData;
str = (0, escape_1.escapeAmpersands)(str);
str = (0, escape_1.escapeLeftAngleBrackets)(str);
str = (0, escape_1.escapeRightAngleBracketsInCdataTerminator)(str);
return str;
};
/**
* Returns the parent of this character data.
*/
XmlCharData.prototype.up = function () {
return this._parent;
};
return XmlCharData;
}());
exports.default = XmlCharData;
+89
View File
@@ -0,0 +1,89 @@
/**
* 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.
*/
/**
* The options used to create a new character reference.
*/
export interface IXmlCharRefOptions {
/**
* The character to represent using the reference.
*/
char: string;
/**
* Whether to use the hexadecimal or decimal representation for the
* reference. Defaults to false.
*/
hex?: boolean;
}
/**
* Represents a character reference.
*
* A character reference is structured as follows, where `{dec}` is the
* decimal representation code point corresponding to a particular Unicode
* character:
*
* ```xml
* &#{dec};
* ```
*
* The corresponding hexadecimal version is structured as follows, where
* `{hex}` is the hexadecimal representation code point corresponding to a
* particular Unicode character:
*
* ```xml
* &#x{hex};
* ```
*
* Unicode characters outside of the Basic Multilingual Plane are represented
* using a surrogate pair consisting of two character references.
*
* The `{dec}` and `{hex}` values are defined by the `char` and `hex`
* properties of this node; the former is the character to be represented while
* the latter indicates whether the decimal or hexadecimal representation
* should be used.
*/
export default class XmlCharRef<Parent> {
private readonly _validation;
private readonly _parent;
private _char;
private _hex;
constructor(parent: Parent, validation: boolean, options: IXmlCharRefOptions);
/**
* Gets the character of this character reference.
*/
get char(): string;
/**
* Sets the character of this character reference.
*/
set char(char: string);
/**
* Gets whether the decimal or hexadecimal representation should be used
* for this character reference.
*/
get hex(): boolean;
/**
* Sets whether the decimal or hexadecimal representation should be used
* for this character reference.
*/
set hex(hex: boolean);
/**
* Returns an XML string representation of this character reference.
*/
toString(): string;
/**
* Returns the parent of this character reference.
*/
up(): Parent;
}
+136
View File
@@ -0,0 +1,136 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents a character reference.
*
* A character reference is structured as follows, where `{dec}` is the
* decimal representation code point corresponding to a particular Unicode
* character:
*
* ```xml
* &#{dec};
* ```
*
* The corresponding hexadecimal version is structured as follows, where
* `{hex}` is the hexadecimal representation code point corresponding to a
* particular Unicode character:
*
* ```xml
* &#x{hex};
* ```
*
* Unicode characters outside of the Basic Multilingual Plane are represented
* using a surrogate pair consisting of two character references.
*
* The `{dec}` and `{hex}` values are defined by the `char` and `hex`
* properties of this node; the former is the character to be represented while
* the latter indicates whether the decimal or hexadecimal representation
* should be used.
*/
var XmlCharRef = /** @class */ (function () {
function XmlCharRef(parent, validation, options) {
this._hex = false;
this._validation = validation;
this._parent = parent;
this.char = options.char;
if (!(0, validate_1.isUndefined)(options.hex)) {
this.hex = options.hex;
}
}
Object.defineProperty(XmlCharRef.prototype, "char", {
/**
* Gets the character of this character reference.
*/
get: function () {
return this._char;
},
/**
* Sets the character of this character reference.
*/
set: function (char) {
if (this._validation && !(0, validate_1.validateSingleChar)(char)) {
throw new Error((0, error_1.getContext)(this.up()) + ": character reference"
+ (" \"" + char + "\" should reference a single character,")
+ " and this character should be allowed in XML");
}
this._char = char;
},
enumerable: false,
configurable: true
});
Object.defineProperty(XmlCharRef.prototype, "hex", {
/**
* Gets whether the decimal or hexadecimal representation should be used
* for this character reference.
*/
get: function () {
return this._hex;
},
/**
* Sets whether the decimal or hexadecimal representation should be used
* for this character reference.
*/
set: function (hex) {
this._hex = hex;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this character reference.
*/
XmlCharRef.prototype.toString = function () {
var char;
if (this._char.length === 1) {
char = this._char.charCodeAt(0);
}
else {
var first = this._char.charCodeAt(0);
if (first >= 0xD800 && first <= 0xDBFF && this._char.length > 1) {
var second = this._char.charCodeAt(1);
if (second >= 0xDC00 && second <= 0xDFFF) {
char = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
}
else {
throw new Error((0, error_1.getContext)(this.up()) + ": character"
+ (" reference \"" + this.char + "\" should")
+ " reference a valid Unicode character");
}
}
else {
char = first;
}
}
if (this._hex) {
return "&#x" + char.toString(16) + ";";
}
else {
return "&#" + char + ";";
}
};
/**
* Returns the parent of this character reference.
*/
XmlCharRef.prototype.up = function () {
return this._parent;
};
return XmlCharRef;
}());
exports.default = XmlCharRef;
+62
View File
@@ -0,0 +1,62 @@
/**
* 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.
*/
/**
* 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;
private readonly _parent;
private readonly _validation;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlCommentOptions);
/**
* Gets the text of this comment.
*/
get charData(): string;
/**
* Sets the text of this comment.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this comment.
*/
toString(): string;
/**
* Returns the parent of this comment.
*/
up(): Parent;
}
+99
View File
@@ -0,0 +1,99 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents a comment.
*
* A comment is structured as follows, where `{content}` is the text of the
* comment:
*
* ```xml
* <!--{content}-->
* ```
*/
var XmlComment = /** @class */ (function () {
function XmlComment(parent, validation, options) {
this._validation = validation;
if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInCharData)) {
this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData);
}
else {
this._replaceInvalidCharsInCharData = false;
}
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlComment.prototype, "charData", {
/**
* Gets the text of this comment.
*/
get: function () {
return this._charData;
},
/**
* Sets the text of this comment.
*/
set: function (charData) {
if (this._replaceInvalidCharsInCharData) {
charData = (0, validate_1.fixChar)(charData);
}
else if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.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((0, error_1.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((0, error_1.getContext)(this.up()) + ": comment content"
+ (" \"" + charData + "\" should not end with the string")
+ " '-'");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this comment.
*/
XmlComment.prototype.toString = function () {
return "<!--" + this._charData + "-->";
};
/**
* Returns the parent of this comment.
*/
XmlComment.prototype.up = function () {
return this._parent;
};
return XmlComment;
}());
exports.default = XmlComment;
+92
View File
@@ -0,0 +1,92 @@
/**
* 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 { IStringOptions } from "../options";
/**
* The options used to create a new declaration.
*/
export interface IXmlDeclOptions {
/**
* The encoding attribute to be included in the declaration. If defined,
* this value must be a valid encoding. By default, no encoding attribute
* is included.
*/
encoding?: string;
/**
* The value of the standalone attribute to be included in the declaration.
* If defined, this value must be "yes" or "no". By default, no standalone
* attribute is included.
*/
standalone?: string;
/**
* The XML version to be included in the declaration. If defined, this
* value must be a valid XML version number. Defaults to "1.0".
*/
version?: string;
}
/**
* Represents a declaration.
*
* A declaration is structured as follows, where `{version}` is the XML
* version, `{encoding}` is the encoding of the document, and `{standalone}`
* is either "yes" or "no", depending on whether the document may contain
* external markup declarations:
*
* ```xml
* <?xml version="{version}" encoding="{encoding}" standalone="{standalone}"?>
* ```
*/
export default class XmlDecl<Parent> {
private readonly _validation;
private _encoding;
private readonly _parent;
private _standalone;
private _version;
constructor(parent: Parent, validation: boolean, options: IXmlDeclOptions);
/**
* Gets the encoding associated with this declaration.
*/
get encoding(): string | undefined;
/**
* Sets the encoding associated with this declaration.
*/
set encoding(encoding: string | undefined);
/**
* Gets the value of the standalone attribute associated with this
* declaration.
*/
get standalone(): string | undefined;
/**
* Sets the value of the standalone attribute associated with this
* declaration.
*/
set standalone(standalone: string | undefined);
/**
* Gets the XML version associated with this declaration.
*/
get version(): string;
/**
* Sets the XML version associated with this declaration.
*/
set version(version: string);
/**
* Returns an XML string representation of this declaration.
*/
toString(options?: IStringOptions): string;
/**
* Returns the parent of this declaration.
*/
up(): Parent;
}
+180
View File
@@ -0,0 +1,180 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var options_1 = require("../options");
var validate_1 = require("../validate");
/**
* Represents a declaration.
*
* A declaration is structured as follows, where `{version}` is the XML
* version, `{encoding}` is the encoding of the document, and `{standalone}`
* is either "yes" or "no", depending on whether the document may contain
* external markup declarations:
*
* ```xml
* <?xml version="{version}" encoding="{encoding}" standalone="{standalone}"?>
* ```
*/
var XmlDecl = /** @class */ (function () {
function XmlDecl(parent, validation, options) {
this._version = "1.0";
this._validation = validation;
this._parent = parent;
this.encoding = options.encoding;
this.standalone = options.standalone;
if (!(0, validate_1.isUndefined)(options.version)) {
this.version = options.version;
}
}
Object.defineProperty(XmlDecl.prototype, "encoding", {
/**
* Gets the encoding associated with this declaration.
*/
get: function () {
return this._encoding;
},
/**
* Sets the encoding associated with this declaration.
*/
set: function (encoding) {
if (this._validation && !(0, validate_1.isUndefined)(encoding)) {
if (!validateEncoding(encoding)) {
throw new Error((0, error_1.getContext)(this.up()) + ": declaration"
+ (" encoding attribute " + encoding + " should be a")
+ " valid encoding");
}
}
this._encoding = encoding;
},
enumerable: false,
configurable: true
});
Object.defineProperty(XmlDecl.prototype, "standalone", {
/**
* Gets the value of the standalone attribute associated with this
* declaration.
*/
get: function () {
return this._standalone;
},
/**
* Sets the value of the standalone attribute associated with this
* declaration.
*/
set: function (standalone) {
if (this._validation && !(0, validate_1.isUndefined)(standalone)) {
if (standalone !== "yes" && standalone !== "no") {
throw new Error((0, error_1.getContext)(this.up()) + ": declaration"
+ (" standalone attribute " + standalone + " should")
+ " be the string 'yes' or the string 'no'");
}
}
this._standalone = standalone;
},
enumerable: false,
configurable: true
});
Object.defineProperty(XmlDecl.prototype, "version", {
/**
* Gets the XML version associated with this declaration.
*/
get: function () {
return this._version;
},
/**
* Sets the XML version associated with this declaration.
*/
set: function (version) {
if (this._validation && !validateVersion(version)) {
throw new Error((0, error_1.getContext)(this.up()) + ": declaration version"
+ (" attribute " + version + " should be a valid XML")
+ " version");
}
this._version = version;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this declaration.
*/
XmlDecl.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
var optionsObj = new options_1.StringOptions(options);
var quote = optionsObj.doubleQuotes ? '"' : "'";
var str = "<?xml version=" + quote + this._version + quote;
if (!(0, validate_1.isUndefined)(this._encoding)) {
str += " encoding=" + quote + this._encoding + quote;
}
if (!(0, validate_1.isUndefined)(this._standalone)) {
str += " standalone=" + quote + this._standalone + quote;
}
str += "?>";
return str;
};
/**
* Returns the parent of this declaration.
*/
XmlDecl.prototype.up = function () {
return this._parent;
};
return XmlDecl;
}());
exports.default = XmlDecl;
/**
* Returns true if the specified encoding only contains characters permitted by
* the XML specification.
*/
function validateEncoding(str) {
if (str.length === 0) {
return false;
}
var initialChar = str.charCodeAt(0);
if (!((initialChar >= 0x41 && initialChar <= 0x5A)
|| (initialChar >= 0x61 && initialChar <= 0x7A))) {
return false;
}
for (var i = 1; i < str.length; i++) {
var char = str.charCodeAt(i);
if (char === 0x5F
|| char === 0x2D
|| char === 0x2E
|| (char >= 0x30 && char <= 0x39)
|| (char >= 0x41 && char <= 0x5A)
|| (char >= 0x61 && char <= 0x7A)) {
continue;
}
if (i + 1 === str.length) {
return false;
}
return false;
}
return true;
}
/**
* Returns true if the specified version only contains characters permitted by
* the XML specification.
*/
function validateVersion(str) {
for (var i = 0; i <= 9; i++) {
if (str === "1." + i) {
return true;
}
}
return false;
}
+96
View File
@@ -0,0 +1,96 @@
/**
* 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 { IStringOptions } from "../options";
import XmlComment, { IXmlCommentOptions } from "./XmlComment";
import XmlDecl, { IXmlDeclOptions } from "./XmlDecl";
import XmlDtd, { IXmlDtdOptions } from "./XmlDtd";
import XmlElement, { IXmlElementOptions } from "./XmlElement";
import XmlProcInst, { IXmlProcInstOptions } from "./XmlProcInst";
/**
* The options used to create a new document.
*/
export interface IXmlDocumentOptions {
/**
* Whether to throw an exception if basic XML validation fails while
* building the document.
*/
validation?: boolean;
}
/**
* Represents a document.
*
* A sample document is structured as follows:
*
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
* "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
* <html>
* <head>
* <title>My page title</title>
* </head>
* <body>
* <h1>Welcome!</h1>
* <p>I hope you enjoy visiting my website.</p>
* <img src="picture.png"/>
* </body>
* </html>
* ```
*
* Each component of the document, such as the declaration, document type
* definition, and root element, are children of this node.
*
* Documents must have exactly one element, which is the document's root
* element.
*
* Documents can have exactly one declaration and one document type definition
* in that order, so long as they precede the element.
*
* Documents can have an unlimited number of comments or processing
* instructions, so long as they follow the declaration, if one exists.
*/
export default class XmlDocument {
private readonly _children;
private readonly _validation;
constructor(options: IXmlDocumentOptions);
/**
* Adds a comment to this document and returns the new comment.
*/
comment(options: IXmlCommentOptions): XmlComment<this>;
/**
* Adds a declaration to this document and returns the new declaration.
*/
decl(options?: IXmlDeclOptions): XmlDecl<this>;
/**
* Adds a document type definition to this document and returns the new
* document type definition.
*/
dtd(options: IXmlDtdOptions): XmlDtd<this>;
/**
* Adds the root element to this document and returns the element.
*/
element(options: IXmlElementOptions): XmlElement<this>;
/**
* Adds a processing instruction to this document and returns the new
* processing instruction.
*/
procInst(options: IXmlProcInstOptions): XmlProcInst<this>;
/**
* Returns an XML string representation of this document using the
* specified options.
*/
toString(options?: IStringOptions): string;
}
+166
View File
@@ -0,0 +1,166 @@
"use strict";
/**
* 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var options_1 = require("../options");
var validate_1 = require("../validate");
var XmlComment_1 = __importDefault(require("./XmlComment"));
var XmlDecl_1 = __importDefault(require("./XmlDecl"));
var XmlDtd_1 = __importDefault(require("./XmlDtd"));
var XmlElement_1 = __importDefault(require("./XmlElement"));
var XmlProcInst_1 = __importDefault(require("./XmlProcInst"));
/**
* Represents a document.
*
* A sample document is structured as follows:
*
* ```xml
* <?xml version="1.0" encoding="UTF-8"?>
* <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
* "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
* <html>
* <head>
* <title>My page title</title>
* </head>
* <body>
* <h1>Welcome!</h1>
* <p>I hope you enjoy visiting my website.</p>
* <img src="picture.png"/>
* </body>
* </html>
* ```
*
* Each component of the document, such as the declaration, document type
* definition, and root element, are children of this node.
*
* Documents must have exactly one element, which is the document's root
* element.
*
* Documents can have exactly one declaration and one document type definition
* in that order, so long as they precede the element.
*
* Documents can have an unlimited number of comments or processing
* instructions, so long as they follow the declaration, if one exists.
*/
var XmlDocument = /** @class */ (function () {
function XmlDocument(options) {
this._children = [];
this._validation = !(0, validate_1.isUndefined)(options.validation)
? options.validation
: true;
}
/**
* Adds a comment to this document and returns the new comment.
*/
XmlDocument.prototype.comment = function (options) {
var comment = new XmlComment_1.default(this, this._validation, options);
this._children.push(comment);
return comment;
};
/**
* Adds a declaration to this document and returns the new declaration.
*/
XmlDocument.prototype.decl = function (options) {
if (options === void 0) { options = {}; }
if (this._validation && this._children.length !== 0) {
throw new Error("in XML document: declaration must be the first"
+ " child");
}
var declaration = new XmlDecl_1.default(this, this._validation, options);
this._children.push(declaration);
return declaration;
};
/**
* Adds a document type definition to this document and returns the new
* document type definition.
*/
XmlDocument.prototype.dtd = function (options) {
var filteredChildren = this._children.filter(function (value) {
return value instanceof XmlElement_1.default;
});
if (this._validation && filteredChildren.length !== 0) {
throw new Error("in XML document: DTD must precede the root"
+ " element");
}
var dtd = new XmlDtd_1.default(this, this._validation, options);
this._children.push(dtd);
return dtd;
};
/**
* Adds the root element to this document and returns the element.
*/
XmlDocument.prototype.element = function (options) {
var filteredChildren = this._children.filter(function (value) {
return value instanceof XmlElement_1.default;
});
if (this._validation && filteredChildren.length !== 0) {
throw new Error("in XML document: only one root element is"
+ " permitted");
}
var element = new XmlElement_1.default(this, this._validation, options);
this._children.push(element);
return element;
};
/**
* Adds a processing instruction to this document and returns the new
* processing instruction.
*/
XmlDocument.prototype.procInst = function (options) {
var procInst = new XmlProcInst_1.default(this, this._validation, options);
this._children.push(procInst);
return procInst;
};
/**
* Returns an XML string representation of this document using the
* specified options.
*/
XmlDocument.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
var filteredChildren = this._children.filter(function (value) {
return value instanceof XmlElement_1.default;
});
if (this._validation && filteredChildren.length !== 1) {
throw new Error("in XML document: no more than one root element"
+ " is permitted");
}
var optionsObj = new options_1.StringOptions(options);
var str = "";
for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
var node = _a[_i];
if (node instanceof XmlDecl_1.default
|| node instanceof XmlDtd_1.default
|| node instanceof XmlElement_1.default) {
str += node.toString(options);
}
else {
str += node.toString();
}
if (optionsObj.pretty) {
str += optionsObj.newline;
}
}
var len = str.length - optionsObj.newline.length;
if (str.substr(len) === optionsObj.newline) {
str = str.substr(0, len);
}
return str;
};
return XmlDocument;
}());
exports.default = XmlDocument;
+147
View File
@@ -0,0 +1,147 @@
/**
* 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 { IStringOptions } from "../options";
import XmlComment, { IXmlCommentOptions } from "./XmlComment";
import XmlDtdAttlist, { IXmlDtdAttlistOptions } from "./XmlDtdAttlist";
import XmlDtdElement, { IXmlDtdElementOptions } from "./XmlDtdElement";
import XmlDtdEntity, { IXmlDtdEntityOptions } from "./XmlDtdEntity";
import XmlDtdNotation, { IXmlDtdNotationOptions } from "./XmlDtdNotation";
import { default as XmlDtdParamEntityRef, IXmlDtdParamEntityRefOptions } from "./XmlDtdParamEntityRef";
import XmlProcInst, { IXmlProcInstOptions } from "./XmlProcInst";
/**
* The options used to create a new document type definition.
*/
export interface IXmlDtdOptions {
/**
* The name of the DTD.
*/
name: string;
/**
* The system identifier of the DTD, excluding quotation marks. By default,
* no system identifier is included.
*/
sysId?: string;
/**
* The public identifier of the DTD, excluding quotation marks. If a public
* identifier is provided, a system identifier must be provided as well.
* By default, no public identifier is included.
*/
pubId?: string;
}
/**
* Represents an XML document type definition (DTD).
*
* A document type definition is structured as follows, where `{name}` is
* the name of the DTD, `{sysId}` is the system identifier of the DTD,
* `{pubId}` is the public identifier of the DTD, and `{intSubset}` is the
* internal subset of the DTD:
*
* ```xml
* <!DOCTYPE {name} SYSTEM "{sysId}" PUBLIC "{pubId}" [
* {intSubset}
* ]>
* ```
*
* DTDs can have an unlimited number of comments, attribute-list declarations,
* element declarations, entity declarations, notation declarations, parameter
* entity references, and processing instructions.
*/
export default class XmlDtd<Parent> {
private readonly _children;
private readonly _parent;
private _name;
private readonly _validation;
private _pubId;
private _sysId;
constructor(parent: Parent, validation: boolean, options: IXmlDtdOptions);
/**
* Gets the name of the DTD.
*/
get name(): string;
/**
* Sets the name of the DTD.
*/
set name(name: string);
/**
* Gets the public identifier of the DTD.
*/
get pubId(): string | undefined;
/**
* Sets the public identifier of the DTD.
*/
set pubId(pubId: string | undefined);
/**
* Gets the system identifier of the DTD.
*/
get sysId(): string | undefined;
/**
* Sets the system identifier of the DTD.
*/
set sysId(sysId: string | undefined);
/**
* Adds an attribute-list declaration to this document type declaration
* and returns the new attribute-list declaration.
*/
attlist(options: IXmlDtdAttlistOptions): XmlDtdAttlist<this>;
/**
* Adds a comment to this document type declaration and returns the
* new comment.
*/
comment(options: IXmlCommentOptions): XmlComment<this>;
/**
* Adds an element declaration to this document type declaration
* and returns the new element declaration.
*/
element(options: IXmlDtdElementOptions): XmlDtdElement<this>;
/**
* Adds an entity declaration to this document type declaration
* and returns the new entity declaration.
*/
entity(options: IXmlDtdEntityOptions): XmlDtdEntity<this>;
/**
* Adds a notation declaration to this document type declaration
* and returns the new notation declaration.
*/
notation(options: IXmlDtdNotationOptions): XmlDtdNotation<this>;
/**
* Adds a parameter entity reference to this document type declaration
* and returns the new parameter entity reference.
*/
paramEntityRef(options: IXmlDtdParamEntityRefOptions): XmlDtdParamEntityRef<this>;
/**
* Adds a processing instruction to this document type declaration
* and returns the new processing instruction.
*/
procInst(options: IXmlProcInstOptions): XmlProcInst<this>;
/**
* Returns an XML string representation of this document type declaration.
*/
toString(options?: IStringOptions): string;
/**
* Returns the parent of this attribute.
*/
up(): Parent;
/**
* Appends the XML string representation of a public or system identifier
* to an existing string.
*/
private appendId;
}
/**
* Returns true if the specified public identifier only contains characters
* permitted by the XML specification.
*/
export declare function validatePubId(str: string): boolean;
+312
View File
@@ -0,0 +1,312 @@
"use strict";
/**
* 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validatePubId = void 0;
var error_1 = require("../error");
var options_1 = require("../options");
var validate_1 = require("../validate");
var XmlComment_1 = __importDefault(require("./XmlComment"));
var XmlDtdAttlist_1 = __importDefault(require("./XmlDtdAttlist"));
var XmlDtdElement_1 = __importDefault(require("./XmlDtdElement"));
var XmlDtdEntity_1 = __importDefault(require("./XmlDtdEntity"));
var XmlDtdNotation_1 = __importDefault(require("./XmlDtdNotation"));
var XmlDtdParamEntityRef_1 = __importDefault(require("./XmlDtdParamEntityRef"));
var XmlProcInst_1 = __importDefault(require("./XmlProcInst"));
/**
* Represents an XML document type definition (DTD).
*
* A document type definition is structured as follows, where `{name}` is
* the name of the DTD, `{sysId}` is the system identifier of the DTD,
* `{pubId}` is the public identifier of the DTD, and `{intSubset}` is the
* internal subset of the DTD:
*
* ```xml
* <!DOCTYPE {name} SYSTEM "{sysId}" PUBLIC "{pubId}" [
* {intSubset}
* ]>
* ```
*
* DTDs can have an unlimited number of comments, attribute-list declarations,
* element declarations, entity declarations, notation declarations, parameter
* entity references, and processing instructions.
*/
var XmlDtd = /** @class */ (function () {
function XmlDtd(parent, validation, options) {
this._pubId = undefined;
this._sysId = undefined;
this._validation = validation;
this._children = [];
this._parent = parent;
this.name = options.name;
if (!(0, validate_1.isUndefined)(options.sysId)) {
this.sysId = options.sysId;
}
if (!(0, validate_1.isUndefined)(options.pubId)) {
this.pubId = options.pubId;
}
}
Object.defineProperty(XmlDtd.prototype, "name", {
/**
* Gets the name of the DTD.
*/
get: function () {
return this._name;
},
/**
* Sets the name of the DTD.
*/
set: function (name) {
if (this._validation && !(0, validate_1.validateName)(name)) {
throw new Error((0, error_1.getContext)(this.up()) + ": DTD name \"" + name + "\""
+ " should not contain characters not allowed in"
+ " XML names");
}
this._name = name;
},
enumerable: false,
configurable: true
});
Object.defineProperty(XmlDtd.prototype, "pubId", {
/**
* Gets the public identifier of the DTD.
*/
get: function () {
return this._pubId;
},
/**
* Sets the public identifier of the DTD.
*/
set: function (pubId) {
if (!(0, validate_1.isUndefined)(pubId)) {
if (this._validation && !validatePubId(pubId)) {
throw new Error((0, error_1.getContext)(this.up()) + ": DTD public"
+ (" identifier \"" + pubId + "\" should not contain")
+ " characters not allowed in public"
+ " identifiers");
}
if (this._validation && (0, validate_1.isUndefined)(this._sysId)) {
throw new Error((0, error_1.getContext)(this.up()) + ": DTD public"
+ (" identifier \"" + pubId + "\" should not be defined")
+ " if system identifier is undefined");
}
}
this._pubId = pubId;
},
enumerable: false,
configurable: true
});
Object.defineProperty(XmlDtd.prototype, "sysId", {
/**
* Gets the system identifier of the DTD.
*/
get: function () {
return this._sysId;
},
/**
* Sets the system identifier of the DTD.
*/
set: function (sysId) {
if (!(0, validate_1.isUndefined)(sysId)) {
if (this._validation && !(0, validate_1.validateChar)(sysId)) {
throw new Error((0, error_1.getContext)(this.up()) + ": DTD system"
+ (" identifier \"" + sysId + "\" should not contain")
+ " characters not allowed in XML");
}
else if (this._validation
&& sysId.indexOf("'") !== -1
&& sysId.indexOf("\"") !== -1) {
throw new Error((0, error_1.getContext)(this.up()) + ": DTD system"
+ (" identifier \"" + sysId + "\" should not contain")
+ " both single quotes and double quotes");
}
}
this._sysId = sysId;
},
enumerable: false,
configurable: true
});
/**
* Adds an attribute-list declaration to this document type declaration
* and returns the new attribute-list declaration.
*/
XmlDtd.prototype.attlist = function (options) {
var attlist = new XmlDtdAttlist_1.default(this, this._validation, options);
this._children.push(attlist);
return attlist;
};
/**
* Adds a comment to this document type declaration and returns the
* new comment.
*/
XmlDtd.prototype.comment = function (options) {
var comment = new XmlComment_1.default(this, this._validation, options);
this._children.push(comment);
return comment;
};
/**
* Adds an element declaration to this document type declaration
* and returns the new element declaration.
*/
XmlDtd.prototype.element = function (options) {
var element = new XmlDtdElement_1.default(this, this._validation, options);
this._children.push(element);
return element;
};
/**
* Adds an entity declaration to this document type declaration
* and returns the new entity declaration.
*/
XmlDtd.prototype.entity = function (options) {
var entity = new XmlDtdEntity_1.default(this, this._validation, options);
this._children.push(entity);
return entity;
};
/**
* Adds a notation declaration to this document type declaration
* and returns the new notation declaration.
*/
XmlDtd.prototype.notation = function (options) {
var notation = new XmlDtdNotation_1.default(this, this._validation, options);
this._children.push(notation);
return notation;
};
/**
* Adds a parameter entity reference to this document type declaration
* and returns the new parameter entity reference.
*/
XmlDtd.prototype.paramEntityRef = function (options) {
var paramEntity = new XmlDtdParamEntityRef_1.default(this, this._validation, options);
this._children.push(paramEntity);
return paramEntity;
};
/**
* Adds a processing instruction to this document type declaration
* and returns the new processing instruction.
*/
XmlDtd.prototype.procInst = function (options) {
var procInst = new XmlProcInst_1.default(this, this._validation, options);
this._children.push(procInst);
return procInst;
};
/**
* Returns an XML string representation of this document type declaration.
*/
XmlDtd.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
var optionsObj = new options_1.StringOptions(options);
var str = "<!DOCTYPE " + this._name;
if ((0, validate_1.isUndefined)(this._pubId)) {
if (!(0, validate_1.isUndefined)(this._sysId)) {
str += " ";
str = this.appendId("SYSTEM", this._sysId, str, optionsObj);
}
}
else {
if ((0, validate_1.isUndefined)(this._sysId)) {
throw new Error((0, error_1.getContext)(this.up()) + ": DTD system"
+ " identifier is not undefined");
}
str += " ";
str = this.appendId("PUBLIC", this._pubId, str, optionsObj);
str = this.appendId("", this._sysId, str, optionsObj);
}
if (this._children.length !== 0) {
str += " [";
for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
var node = _a[_i];
if (optionsObj.pretty) {
str += optionsObj.newline + optionsObj.indent;
}
str += node.toString();
}
if (optionsObj.pretty) {
str += optionsObj.newline;
}
str += "]>";
}
else {
str += ">";
}
return str;
};
/**
* Returns the parent of this attribute.
*/
XmlDtd.prototype.up = function () {
return this._parent;
};
/**
* Appends the XML string representation of a public or system identifier
* to an existing string.
*/
XmlDtd.prototype.appendId = function (type, value, str, options) {
str += type + " ";
if (options.doubleQuotes) {
if (this._validation && value.indexOf("\"") !== -1) {
throw new Error((0, error_1.getContext)(this.up()) + ": doubleQuotes option"
+ " inconsistent with DTD system identifier or"
+ " public identifier");
}
str += "\"" + value + "\"";
}
else {
if (this._validation && value.indexOf("'") !== -1) {
throw new Error((0, error_1.getContext)(this) + ": doubleQuotes option"
+ " inconsistent with DTD system identifier or"
+ " public identifier");
}
str += "'" + value + "'";
}
return str;
};
return XmlDtd;
}());
exports.default = XmlDtd;
/**
* Returns true if the specified public identifier only contains characters
* permitted by the XML specification.
*/
function validatePubId(str) {
for (var i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
if (char === 0xA
|| char === 0xD
|| char === 0x20
|| char === 0x21
|| (char >= 0x23 && char <= 0x25)
|| (char >= 0x27 && char <= 0x2F)
|| (char >= 0x30 && char <= 0x39)
|| char === 0x3A
|| char === 0x3B
|| char === 0x3D
|| char === 0x3F
|| (char >= 0x40 && char <= 0x5A)
|| char === 0x5F
|| (char >= 0x61 && char <= 0x7A)) {
continue;
}
if (i + 1 === str.length) {
return false;
}
return false;
}
return true;
}
exports.validatePubId = validatePubId;
+56
View File
@@ -0,0 +1,56 @@
/**
* 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.
*/
/**
* The options used to create a new attribute-list declaration.
*/
export interface IXmlDtdAttlistOptions {
/**
* The text of the declaration.
*/
charData: string;
}
/**
* Represents an attribute-list declaration in a document type definition.
*
* An attribute-list declaration is structured as follows, where `{text}`
* is the text of the declaration:
*
* ```xml
* <!ATTLIST {text}>
* ```
*/
export default class XmlDtdAttlist<Parent> {
private readonly _validation;
private readonly _parent;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlDtdAttlistOptions);
/**
* Gets the text of this entity declaration.
*/
get charData(): string;
/**
* Sets the text of this entity declaration.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this entity declaration.
*/
toString(): string;
/**
* Returns the parent of this entity declaration.
*/
up(): Parent;
}
+71
View File
@@ -0,0 +1,71 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents an attribute-list declaration in a document type definition.
*
* An attribute-list declaration is structured as follows, where `{text}`
* is the text of the declaration:
*
* ```xml
* <!ATTLIST {text}>
* ```
*/
var XmlDtdAttlist = /** @class */ (function () {
function XmlDtdAttlist(parent, validation, options) {
this._validation = validation;
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlDtdAttlist.prototype, "charData", {
/**
* Gets the text of this entity declaration.
*/
get: function () {
return this._charData;
},
/**
* Sets the text of this entity declaration.
*/
set: function (charData) {
if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.getContext)(this.up()) + ": attribute-list"
+ (" declaration \"" + charData + "\" should not contain")
+ " characters not allowed in XML");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this entity declaration.
*/
XmlDtdAttlist.prototype.toString = function () {
return "<!ATTLIST " + this._charData + ">";
};
/**
* Returns the parent of this entity declaration.
*/
XmlDtdAttlist.prototype.up = function () {
return this._parent;
};
return XmlDtdAttlist;
}());
exports.default = XmlDtdAttlist;
+56
View File
@@ -0,0 +1,56 @@
/**
* 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.
*/
/**
* The options used to create a new element declaration.
*/
export interface IXmlDtdElementOptions {
/**
* The text of the declaration.
*/
charData: string;
}
/**
* Represents an element declaration in a document type definition.
*
* An element declaration is structured as follows, where `{text}` is the
* text of the declaration:
*
* ```xml
* <!ELEMENT {text}>
* ```
*/
export default class XmlDtdElement<Parent> {
private readonly _validation;
private readonly _parent;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlDtdElementOptions);
/**
* Gets the text of this element declaration.
*/
get charData(): string;
/**
* Sets the text of this element declaration.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this element declaration.
*/
toString(): string;
/**
* Returns the parent of this element declaration.
*/
up(): Parent;
}
+71
View File
@@ -0,0 +1,71 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents an element declaration in a document type definition.
*
* An element declaration is structured as follows, where `{text}` is the
* text of the declaration:
*
* ```xml
* <!ELEMENT {text}>
* ```
*/
var XmlDtdElement = /** @class */ (function () {
function XmlDtdElement(parent, validation, options) {
this._validation = validation;
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlDtdElement.prototype, "charData", {
/**
* Gets the text of this element declaration.
*/
get: function () {
return this._charData;
},
/**
* Sets the text of this element declaration.
*/
set: function (charData) {
if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.getContext)(this.up()) + ": element declaration"
+ (" \"" + charData + "\" should not contain characters")
+ " not allowed in XML");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this element declaration.
*/
XmlDtdElement.prototype.toString = function () {
return "<!ELEMENT " + this._charData + ">";
};
/**
* Returns the parent of this element declaration.
*/
XmlDtdElement.prototype.up = function () {
return this._parent;
};
return XmlDtdElement;
}());
exports.default = XmlDtdElement;
+56
View File
@@ -0,0 +1,56 @@
/**
* 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.
*/
/**
* The options used to create a new entity declaration.
*/
export interface IXmlDtdEntityOptions {
/**
* The text of the declaration.
*/
charData: string;
}
/**
* Represents an entity declaration in a document type definition.
*
* An entity declaration is structured as follows, where `{text}` is the
* text of the declaration:
*
* ```xml
* <!ENTITY {text}>
* ```
*/
export default class XmlDtdEntity<Parent> {
private readonly _validation;
private readonly _parent;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlDtdEntityOptions);
/**
* Gets the text of this entity declaration.
*/
get charData(): string;
/**
* Sets the text of this entity declaration.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this entity declaration.
*/
toString(): string;
/**
* Returns the parent of this entity declaration.
*/
up(): Parent;
}
+71
View File
@@ -0,0 +1,71 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents an entity declaration in a document type definition.
*
* An entity declaration is structured as follows, where `{text}` is the
* text of the declaration:
*
* ```xml
* <!ENTITY {text}>
* ```
*/
var XmlDtdEntity = /** @class */ (function () {
function XmlDtdEntity(parent, validation, options) {
this._validation = validation;
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlDtdEntity.prototype, "charData", {
/**
* Gets the text of this entity declaration.
*/
get: function () {
return this._charData;
},
/**
* Sets the text of this entity declaration.
*/
set: function (charData) {
if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.getContext)(this.up()) + ": entity declaration"
+ (" \"" + charData + "\" should not contain characters")
+ " not allowed in XML");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this entity declaration.
*/
XmlDtdEntity.prototype.toString = function () {
return "<!ENTITY " + this._charData + ">";
};
/**
* Returns the parent of this entity declaration.
*/
XmlDtdEntity.prototype.up = function () {
return this._parent;
};
return XmlDtdEntity;
}());
exports.default = XmlDtdEntity;
+56
View File
@@ -0,0 +1,56 @@
/**
* 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.
*/
/**
* The options used to create a new notation declaration.
*/
export interface IXmlDtdNotationOptions {
/**
* The text of the declaration.
*/
charData: string;
}
/**
* Represents a notation declaration in a document type definition.
*
* A notation declaration is structured as follows, where `{text}` is the
* text of the declaration:
*
* ```xml
* <!NOTATION {text}>
* ```
*/
export default class XmlDtdNotation<Parent> {
private readonly _validation;
private readonly _parent;
private _charData;
constructor(parent: Parent, validation: boolean, options: IXmlDtdNotationOptions);
/**
* Gets the text of this notation declaration.
*/
get charData(): string;
/**
* Sets the text of this notation declaration.
*/
set charData(charData: string);
/**
* Returns an XML string representation of this notation declaration.
*/
toString(): string;
/**
* Returns the parent of this notation declaration.
*/
up(): Parent;
}
+71
View File
@@ -0,0 +1,71 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents a notation declaration in a document type definition.
*
* A notation declaration is structured as follows, where `{text}` is the
* text of the declaration:
*
* ```xml
* <!NOTATION {text}>
* ```
*/
var XmlDtdNotation = /** @class */ (function () {
function XmlDtdNotation(parent, validation, options) {
this._validation = validation;
this._parent = parent;
this.charData = options.charData;
}
Object.defineProperty(XmlDtdNotation.prototype, "charData", {
/**
* Gets the text of this notation declaration.
*/
get: function () {
return this._charData;
},
/**
* Sets the text of this notation declaration.
*/
set: function (charData) {
if (this._validation && !(0, validate_1.validateChar)(charData)) {
throw new Error((0, error_1.getContext)(this.up()) + ": notation declaration"
+ (" \"" + charData + "\" should not contain characters")
+ " not allowed in XML");
}
this._charData = charData;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this notation declaration.
*/
XmlDtdNotation.prototype.toString = function () {
return "<!NOTATION " + this._charData + ">";
};
/**
* Returns the parent of this notation declaration.
*/
XmlDtdNotation.prototype.up = function () {
return this._parent;
};
return XmlDtdNotation;
}());
exports.default = XmlDtdNotation;
+56
View File
@@ -0,0 +1,56 @@
/**
* 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.
*/
/**
* The options used to create a new parameter entity reference.
*/
export interface IXmlDtdParamEntityRefOptions {
/**
* The name of the entity to be referenced.
*/
name: string;
}
/**
* Represents a parameter entity reference in a document type definition.
*
* A parameter entity reference is structured as follows, where `{entity}`
* is the name of the entity:
*
* ```xml
* %{entity};
* ```
*/
export default class XmlDtdParamEntityRef<Parent> {
private readonly _validation;
private readonly _parent;
private _name;
constructor(parent: Parent, validation: boolean, options: IXmlDtdParamEntityRefOptions);
/**
* Gets the name of this parameter entity reference.
*/
get name(): string;
/**
* Sets the name of this parameter entity reference.
*/
set name(name: string);
/**
* Returns an XML string representation of this parameter entity reference.
*/
toString(): string;
/**
* Returns the parent of this parameter entity reference.
*/
up(): Parent;
}
+71
View File
@@ -0,0 +1,71 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents a parameter entity reference in a document type definition.
*
* A parameter entity reference is structured as follows, where `{entity}`
* is the name of the entity:
*
* ```xml
* %{entity};
* ```
*/
var XmlDtdParamEntityRef = /** @class */ (function () {
function XmlDtdParamEntityRef(parent, validation, options) {
this._validation = validation;
this._parent = parent;
this.name = options.name;
}
Object.defineProperty(XmlDtdParamEntityRef.prototype, "name", {
/**
* Gets the name of this parameter entity reference.
*/
get: function () {
return this._name;
},
/**
* Sets the name of this parameter entity reference.
*/
set: function (name) {
if (this._validation && !(0, validate_1.validateName)(name)) {
throw new Error((0, error_1.getContext)(this.up()) + ": parameter entity"
+ (" reference name \"" + name + "\" should not contain")
+ " characters not allowed in XML names");
}
this._name = name;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this parameter entity reference.
*/
XmlDtdParamEntityRef.prototype.toString = function () {
return "%" + this._name + ";";
};
/**
* Returns the parent of this parameter entity reference.
*/
XmlDtdParamEntityRef.prototype.up = function () {
return this._parent;
};
return XmlDtdParamEntityRef;
}());
exports.default = XmlDtdParamEntityRef;
+153
View File
@@ -0,0 +1,153 @@
/**
* 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 { IStringOptions } from "../options";
import { default as XmlAttribute, IXmlAttributeOptions as IXmlAttributeOptions } from "./XmlAttribute";
import XmlCdata, { IXmlCdataOptions } from "./XmlCdata";
import XmlCharData, { IXmlCharDataOptions } from "./XmlCharData";
import XmlCharRef, { IXmlCharRefOptions } from "./XmlCharRef";
import XmlComment, { IXmlCommentOptions } from "./XmlComment";
import XmlEntityRef, { IXmlEntityRefOptions } from "./XmlEntityRef";
import XmlProcInst, { IXmlProcInstOptions } from "./XmlProcInst";
/**
* The options used to create a new element.
*/
export interface IXmlElementOptions {
/**
* The name of the element.
*/
name: string;
/**
* Whether to replace any invalid characters in the name of the element
* with the Unicode replacement character. By default, this is disabled.
*/
replaceInvalidCharsInName?: boolean;
/**
* Whether to use a self-closing tag if this element is empty.
*
* For example, use:
* ```xml
* <element/>
* ```
* instead of:
* ```xml
* <element></element>
* ```
*
* By default, this is enabled.
*/
useSelfClosingTagIfEmpty?: boolean;
}
/**
* Represents an XML element.
*
* A sample element is structured as follows, where `{name}` is the name
* of the element:
*
* ```xml
* <{name} attname="attvalue">
* <subelem/>
* <?pitarget picontent?>
* text
* </{name}></pre>
* ```
*
* XML elements can have an unlimited number of attributes, CDATA sections,
* character references, comments, elements, entity references, processing
* instructions, and character data.
*
* An element with no content will be represented using an empty element tag:
*
* ```xml
* <{name}/>
* ```
*/
export default class XmlElement<Parent> {
private readonly _validation;
private readonly _children;
private readonly _attributeNames;
private readonly _parent;
private readonly _replaceInvalidCharsInName;
private readonly _useSelfClosingTagIfEmpty;
private _name;
constructor(parent: Parent, validation: boolean, options: IXmlElementOptions);
/**
* Gets the name of this element.
*/
get name(): string;
/**
* Sets the name of this element.
*/
set name(name: string);
/**
* Adds an attribute to this element and returns the new attribute.
*/
attribute(options: IXmlAttributeOptions): XmlAttribute<this>;
/**
* Adds a CDATA section to this element and returns the new CDATA section.
*/
cdata(options: IXmlCdataOptions): XmlCdata<this>;
/**
* Adds character data to this element and returns the new character data.
*/
charData(options: IXmlCharDataOptions): XmlCharData<this>;
/**
* Adds a character reference to this element and returns the new
* character reference.
*/
charRef(options: IXmlCharRefOptions): XmlCharRef<this>;
/**
* Adds a comment to this element and returns the new comment.
*/
comment(options: IXmlCommentOptions): XmlComment<this>;
/**
* Adds an element to this element and returns the new element.
*/
element(options: IXmlElementOptions): XmlElement<this>;
/**
* Adds an entity reference to this element and returns the new entity
* reference.
*/
entityRef(options: IXmlEntityRefOptions): XmlEntityRef<this>;
/**
* Adds a processing instruction to this element and returns the new
* processing instruction.
*/
procInst(options: IXmlProcInstOptions): XmlProcInst<this>;
/**
* Returns an XML string representation of this element using the specified
* options.
*/
toString(options?: IStringOptions): string;
/**
* Returns the parent of this element.
*/
up(): Parent;
/**
* Returns an XML string representation of this element using the specified
* options and initial indent.
*/
private toStringWithIndent;
/**
* Returns true if the specified nodes are all character references,
* entity references, or character data.
*/
private allSameLineNodes;
/**
* Returns true if the specified nodes are all character references,
* entity references, or character data.
*/
private onSameLine;
}
+302
View File
@@ -0,0 +1,302 @@
"use strict";
/**
* 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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var options_1 = require("../options");
var validate_1 = require("../validate");
var XmlAttribute_1 = __importDefault(require("./XmlAttribute"));
var XmlCdata_1 = __importDefault(require("./XmlCdata"));
var XmlCharData_1 = __importDefault(require("./XmlCharData"));
var XmlCharRef_1 = __importDefault(require("./XmlCharRef"));
var XmlComment_1 = __importDefault(require("./XmlComment"));
var XmlEntityRef_1 = __importDefault(require("./XmlEntityRef"));
var XmlProcInst_1 = __importDefault(require("./XmlProcInst"));
/**
* Represents an XML element.
*
* A sample element is structured as follows, where `{name}` is the name
* of the element:
*
* ```xml
* <{name} attname="attvalue">
* <subelem/>
* <?pitarget picontent?>
* text
* </{name}></pre>
* ```
*
* XML elements can have an unlimited number of attributes, CDATA sections,
* character references, comments, elements, entity references, processing
* instructions, and character data.
*
* An element with no content will be represented using an empty element tag:
*
* ```xml
* <{name}/>
* ```
*/
var XmlElement = /** @class */ (function () {
function XmlElement(parent, validation, options) {
this._validation = validation;
if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInName)) {
this._replaceInvalidCharsInName = options.replaceInvalidCharsInName;
}
else {
this._replaceInvalidCharsInName = false;
}
if (!(0, validate_1.isUndefined)(options.useSelfClosingTagIfEmpty)) {
this._useSelfClosingTagIfEmpty = options.useSelfClosingTagIfEmpty;
}
else {
this._useSelfClosingTagIfEmpty = true;
}
this._children = [];
this._attributeNames = [];
this._parent = parent;
this.name = options.name;
}
Object.defineProperty(XmlElement.prototype, "name", {
/**
* Gets the name of this element.
*/
get: function () {
return this._name;
},
/**
* Sets the name of this element.
*/
set: function (name) {
if (this._replaceInvalidCharsInName) {
name = (0, validate_1.fixName)(name);
if (name.length === 0) {
throw new Error((0, error_1.getContext)(this.up()) + ": element name should"
+ " not be empty");
}
}
else if (this._validation && !(0, validate_1.validateName)(name)) {
if (name.length === 0) {
throw new Error((0, error_1.getContext)(this.up()) + ": element name should"
+ " not be empty");
}
else {
throw new Error((0, error_1.getContext)(this.up()) + ": element name"
+ (" \"" + name + "\" should not contain characters not")
+ " allowed in XML names");
}
}
this._name = name;
},
enumerable: false,
configurable: true
});
/**
* Adds an attribute to this element and returns the new attribute.
*/
XmlElement.prototype.attribute = function (options) {
if (this._validation
&& this._attributeNames.indexOf(options.name) !== -1) {
throw new Error((0, error_1.getContext)(this.up()) + ": element \"" + this.name + "\""
+ " already contains an attribute with the"
+ (" name \"" + options.name + "\""));
}
var attribute = new XmlAttribute_1.default(this, this._validation, options);
this._children.push(attribute);
this._attributeNames.push(options.name);
return attribute;
};
/**
* Adds a CDATA section to this element and returns the new CDATA section.
*/
XmlElement.prototype.cdata = function (options) {
var cdata = new XmlCdata_1.default(this, this._validation, options);
this._children.push(cdata);
return cdata;
};
/**
* Adds character data to this element and returns the new character data.
*/
XmlElement.prototype.charData = function (options) {
var charDataNode = new XmlCharData_1.default(this, this._validation, options);
this._children.push(charDataNode);
return charDataNode;
};
/**
* Adds a character reference to this element and returns the new
* character reference.
*/
XmlElement.prototype.charRef = function (options) {
var charRef = new XmlCharRef_1.default(this, this._validation, options);
this._children.push(charRef);
return charRef;
};
/**
* Adds a comment to this element and returns the new comment.
*/
XmlElement.prototype.comment = function (options) {
var comment = new XmlComment_1.default(this, this._validation, options);
this._children.push(comment);
return comment;
};
/**
* Adds an element to this element and returns the new element.
*/
XmlElement.prototype.element = function (options) {
var element = new XmlElement(this, this._validation, options);
this._children.push(element);
return element;
};
/**
* Adds an entity reference to this element and returns the new entity
* reference.
*/
XmlElement.prototype.entityRef = function (options) {
var entityRef = new XmlEntityRef_1.default(this, this._validation, options);
this._children.push(entityRef);
return entityRef;
};
/**
* Adds a processing instruction to this element and returns the new
* processing instruction.
*/
XmlElement.prototype.procInst = function (options) {
var procInst = new XmlProcInst_1.default(this, this._validation, options);
this._children.push(procInst);
return procInst;
};
/**
* Returns an XML string representation of this element using the specified
* options.
*/
XmlElement.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
return this.toStringWithIndent(options, "");
};
/**
* Returns the parent of this element.
*/
XmlElement.prototype.up = function () {
return this._parent;
};
/**
* Returns an XML string representation of this element using the specified
* options and initial indent.
*/
XmlElement.prototype.toStringWithIndent = function (options, indent) {
var optionsObj = new options_1.StringOptions(options);
var newIndent = indent + optionsObj.indent;
// Element tag start
var str = "<" + this._name;
// Attributes and other nodes
var nodes = [];
for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
var node = _a[_i];
if (node instanceof XmlAttribute_1.default) {
str += " " + node.toString(options);
}
else {
nodes.push(node);
}
}
// Child nodes
if (nodes.length > 0) {
var childStr = "";
for (var i = 0; i < nodes.length; i++) {
var next = nodes[i];
var nextStr = "";
if (next instanceof XmlElement) {
nextStr += next.toStringWithIndent(optionsObj, newIndent);
}
else {
nextStr += next.toString();
}
var prev = i > 0 ? nodes[i - 1] : undefined;
// Skip empty nodes
if (next instanceof XmlCharData_1.default && next.toString() === "") {
continue;
}
// Line break before child nodes unless all nodes, or at least
// the most recent two, are of type XmlCharacterReference,
// XmlEntityReference, or XmlCharData
if (optionsObj.pretty) {
if (!this.allSameLineNodes(nodes)) {
if (!(i > 0 && this.onSameLine(next, prev))) {
nextStr = optionsObj.newline + newIndent + nextStr;
}
}
}
childStr += nextStr;
}
// Line break before end tag unless all nodes are of type
// XmlCharacterReference, XmlEntityReference, or XmlCharData
if (optionsObj.pretty) {
if (!this.allSameLineNodes(nodes)) {
childStr += optionsObj.newline + indent;
}
}
if (childStr.length === 0 && this._useSelfClosingTagIfEmpty) {
// Element empty tag end
str += "/>";
}
else {
// Element start and end tags
str += ">" + childStr + "</" + this._name + ">";
}
}
else if (this._useSelfClosingTagIfEmpty) {
// Element empty tag end
str += "/>";
}
else {
// Element start and end tags
str += "></" + this._name + ">";
}
return str;
};
/**
* Returns true if the specified nodes are all character references,
* entity references, or character data.
*/
XmlElement.prototype.allSameLineNodes = function (nodes) {
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
var node = nodes_1[_i];
if (!((node instanceof XmlCharRef_1.default
|| node instanceof XmlEntityRef_1.default
|| node instanceof XmlCharData_1.default))) {
return false;
}
}
return true;
};
/**
* Returns true if the specified nodes are all character references,
* entity references, or character data.
*/
XmlElement.prototype.onSameLine = function (prev, next) {
return (prev instanceof XmlCharRef_1.default
|| prev instanceof XmlEntityRef_1.default
|| prev instanceof XmlCharData_1.default)
&& (!(0, validate_1.isUndefined)(next)
&& (next instanceof XmlCharRef_1.default
|| next instanceof XmlEntityRef_1.default
|| next instanceof XmlCharData_1.default));
};
return XmlElement;
}());
exports.default = XmlElement;
+56
View File
@@ -0,0 +1,56 @@
/**
* 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.
*/
/**
* The options used to create a new entity reference.
*/
export interface IXmlEntityRefOptions {
/**
* The name of the entity to be referenced.
*/
name: string;
}
/**
* Represents an entity reference.
*
* An entity reference is structured as follows, where `{name}` is the name of
* the entity to be referenced:
*
* ```xml
* &{entity};
* ```
*/
export default class XmlEntityRef<Parent> {
private readonly _validation;
private readonly _parent;
private _name;
constructor(parent: Parent, validation: boolean, options: IXmlEntityRefOptions);
/**
* Gets the name of this entity reference.
*/
get name(): string;
/**
* Sets the name of this entity reference.
*/
set name(name: string);
/**
* Returns an XML string representation of this entity reference.
*/
toString(): string;
/**
* Returns the parent of this entity reference.
*/
up(): Parent;
}
+71
View File
@@ -0,0 +1,71 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents an entity reference.
*
* An entity reference is structured as follows, where `{name}` is the name of
* the entity to be referenced:
*
* ```xml
* &{entity};
* ```
*/
var XmlEntityRef = /** @class */ (function () {
function XmlEntityRef(parent, validation, options) {
this._validation = validation;
this._parent = parent;
this.name = options.name;
}
Object.defineProperty(XmlEntityRef.prototype, "name", {
/**
* Gets the name of this entity reference.
*/
get: function () {
return this._name;
},
/**
* Sets the name of this entity reference.
*/
set: function (name) {
if (this._validation && !(0, validate_1.validateName)(name)) {
throw new Error((0, error_1.getContext)(this.up()) + ": entity reference name"
+ (" \"" + name + "\" should not contain characters not")
+ " allowed in XML names");
}
this._name = name;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this entity reference.
*/
XmlEntityRef.prototype.toString = function () {
return "&" + this._name + ";";
};
/**
* Returns the parent of this entity reference.
*/
XmlEntityRef.prototype.up = function () {
return this._parent;
};
return XmlEntityRef;
}());
exports.default = XmlEntityRef;
+71
View File
@@ -0,0 +1,71 @@
/**
* 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.
*/
/**
* The options used to create a new processing instruction.
*/
export interface IXmlProcInstOptions {
/**
* The target of the processing instruction.
*/
target: string;
/**
* The data of the processing instruction, or undefined if there is no
* content.
*/
content?: string;
}
/**
* Represents a processing instruction.
*
* A processing instruction is structured as follows, where `{target}` and
* `{content}` are the target and content of the processing instruction
* respectively:
*
* ```xml
* <?{target} {content}?>
* ```
*/
export default class XmlProcInst<Parent> {
private readonly _validation;
private readonly _parent;
private _content;
private _target;
constructor(parent: Parent, validation: boolean, options: IXmlProcInstOptions);
/**
* Gets the content of this processing instruction.
*/
get content(): string | undefined;
/**
* Sets the content of this processing instruction.
*/
set content(content: string | undefined);
/**
* Gets the target of this processing instruction.
*/
get target(): string;
/**
* Sets the content of this processing instruction.
*/
set target(target: string);
/**
* Returns an XML string representation of this processing instruction.
*/
toString(): string;
/**
* Returns the parent of this processing instruction.
*/
up(): Parent;
}
+112
View File
@@ -0,0 +1,112 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var error_1 = require("../error");
var validate_1 = require("../validate");
/**
* Represents a processing instruction.
*
* A processing instruction is structured as follows, where `{target}` and
* `{content}` are the target and content of the processing instruction
* respectively:
*
* ```xml
* <?{target} {content}?>
* ```
*/
var XmlProcInst = /** @class */ (function () {
function XmlProcInst(parent, validation, options) {
this._validation = validation;
this._parent = parent;
this.content = options.content;
this.target = options.target;
}
Object.defineProperty(XmlProcInst.prototype, "content", {
/**
* Gets the content of this processing instruction.
*/
get: function () {
return this._content;
},
/**
* Sets the content of this processing instruction.
*/
set: function (content) {
if (!(0, validate_1.isUndefined)(content)) {
if (this._validation && !(0, validate_1.validateChar)(content)) {
throw new Error((0, error_1.getContext)(this.up()) + ": processing"
+ (" instruction content \"" + content + "\" should")
+ " not contain characters not allowed in XML");
}
else if (this._validation && content.indexOf("?>") !== -1) {
throw new Error((0, error_1.getContext)(this.up()) + ": processing"
+ (" instruction content \"" + content + "\" should")
+ " not contain the string '?>'");
}
}
this._content = content;
},
enumerable: false,
configurable: true
});
Object.defineProperty(XmlProcInst.prototype, "target", {
/**
* Gets the target of this processing instruction.
*/
get: function () {
return this._target;
},
/**
* Sets the content of this processing instruction.
*/
set: function (target) {
if (this._validation && !(0, validate_1.validateName)(target)) {
throw new Error((0, error_1.getContext)(this.up()) + ": processing"
+ (" instruction target \"" + target + "\" should")
+ " not contain characters not allowed in XML"
+ " names");
}
if (this._validation && target === "xml") {
throw new Error((0, error_1.getContext)(this.up()) + ": processing"
+ (" instruction target \"" + target + "\" should")
+ " not be the string 'xml'");
}
this._target = target;
},
enumerable: false,
configurable: true
});
/**
* Returns an XML string representation of this processing instruction.
*/
XmlProcInst.prototype.toString = function () {
if ((0, validate_1.isUndefined)(this._content)) {
return "<?" + this._target + "?>";
}
else {
return "<?" + this._target + " " + this._content + "?>";
}
};
/**
* Returns the parent of this processing instruction.
*/
XmlProcInst.prototype.up = function () {
return this._parent;
};
return XmlProcInst;
}());
exports.default = XmlProcInst;
+51
View File
@@ -0,0 +1,51 @@
/**
* 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.
*/
/**
* Formatting options for the string representation of an XML node.
*/
export interface IStringOptions {
/**
* Whether double quotes or single quotes should be used in XML attributes.
* By default, single quotes are used.
*/
doubleQuotes?: boolean;
/**
* The indent string used for pretty-printing. The default indent string is
* four spaces.
*/
indent?: string;
/**
* The newline string used for pretty-printing. The default newline string
* is "\n".
*/
newline?: string;
/**
* Whether pretty-printing is enabled. By default, pretty-printing is
* enabled.
*/
pretty?: boolean;
}
/**
* Implementation of the IStringOptions interface used to provide default
* values to fields.
*/
export declare class StringOptions implements IStringOptions {
doubleQuotes: boolean;
indent: string;
newline: string;
pretty: boolean;
constructor(options: IStringOptions);
}
+45
View File
@@ -0,0 +1,45 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringOptions = void 0;
var validate_1 = require("./validate");
/**
* Implementation of the IStringOptions interface used to provide default
* values to fields.
*/
var StringOptions = /** @class */ (function () {
function StringOptions(options) {
this.doubleQuotes = false;
this.indent = " ";
this.newline = "\n";
this.pretty = true;
if (!(0, validate_1.isUndefined)(options.doubleQuotes)) {
this.doubleQuotes = options.doubleQuotes;
}
if (!(0, validate_1.isUndefined)(options.indent)) {
this.indent = options.indent;
}
if (!(0, validate_1.isUndefined)(options.newline)) {
this.newline = options.newline;
}
if (!(0, validate_1.isUndefined)(options.pretty)) {
this.pretty = options.pretty;
}
}
return StringOptions;
}());
exports.StringOptions = StringOptions;
+46
View File
@@ -0,0 +1,46 @@
/**
* 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.
*/
/**
* Returns true if the specified string only contains characters permitted by
* the XML specification.
*/
export declare function validateChar(str: string): boolean;
/**
* Returns a version of the specified string that only contains characters
* permitted by the XML specification, with invalid characters replaced
* by the replacement character U+FFFD.
*/
export declare function fixChar(str: string): string;
/**
* Returns true if the specified string only contains a single character, and
* that this character is permitted by the XML specification.
*/
export declare function validateSingleChar(str: string): boolean;
/**
* Returns true if the specified string only contains characters permitted by
* the XML specification for names.
*/
export declare function validateName(str: string): boolean;
/**
* Returns a version of the specified string that only contains characters
* permitted by the XML specification for names, with invalid characters
* replaced by the replacement character U+FFFD.
*/
export declare function fixName(str: string): string;
/**
* Returns true if the specified value is undefined.
*/
export declare function isUndefined(val: unknown): val is undefined;
+274
View File
@@ -0,0 +1,274 @@
"use strict";
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isUndefined = exports.fixName = exports.validateName = exports.validateSingleChar = exports.fixChar = exports.validateChar = void 0;
/**
* Returns true if the specified string only contains characters permitted by
* the XML specification.
*/
function validateChar(str) {
for (var i = 0; i < str.length; i++) {
var firstChar = str.charCodeAt(i);
if (firstChar === 0x9 || firstChar === 0xA || firstChar === 0xD
|| (firstChar >= 0x20 && firstChar <= 0xD7FF)
|| (firstChar >= 0xE000 && firstChar <= 0xFFFD)) {
continue;
}
if (i + 1 === str.length) {
return false;
}
// UTF-16 surrogate characters
var secondChar = str.charCodeAt(i + 1);
if ((firstChar >= 0xD800 && firstChar <= 0xDBFF)
&& (secondChar >= 0xDC00 && secondChar <= 0xDFFF)) {
i++;
continue;
}
return false;
}
return true;
}
exports.validateChar = validateChar;
/**
* Returns a version of the specified string that only contains characters
* permitted by the XML specification, with invalid characters replaced
* by the replacement character U+FFFD.
*/
function fixChar(str) {
var newStr = "";
for (var i = 0; i < str.length; i++) {
var firstChar = str.charCodeAt(i);
if (firstChar === 0x9 || firstChar === 0xA || firstChar === 0xD
|| (firstChar >= 0x20 && firstChar <= 0xD7FF)
|| (firstChar >= 0xE000 && firstChar <= 0xFFFD)) {
newStr += str[i];
continue;
}
if (i + 1 === str.length) {
newStr += "\uFFFD";
return newStr;
}
// UTF-16 surrogate characters
var secondChar = str.charCodeAt(i + 1);
if ((firstChar >= 0xD800 && firstChar <= 0xDBFF)
&& (secondChar >= 0xDC00 && secondChar <= 0xDFFF)) {
newStr += str[i] + str[i + 1];
i++;
continue;
}
newStr += "\uFFFD";
}
return newStr;
}
exports.fixChar = fixChar;
/**
* Returns true if the specified string only contains a single character, and
* that this character is permitted by the XML specification.
*/
function validateSingleChar(str) {
if (str.length === 0) {
return false;
}
var firstChar = str.charCodeAt(0);
if (str.length === 1) {
return (firstChar === 0x9 || firstChar === 0xA || firstChar === 0xD
|| (firstChar >= 0x20 && firstChar <= 0xD7FF)
|| (firstChar >= 0xE000 && firstChar <= 0xFFFD));
}
if (str.length !== 2) {
return false;
}
// UTF-16 surrogate characters
var secondChar = str.charCodeAt(1);
return ((firstChar >= 0xD800 && firstChar <= 0xDBFF)
&& (secondChar >= 0xDC00 && secondChar <= 0xDFFF));
}
exports.validateSingleChar = validateSingleChar;
/**
* Returns true if the specified string only contains characters permitted by
* the XML specification for names.
*/
function validateName(str) {
if (str.length === 0) {
return false;
}
var initialFirstChar = str.charCodeAt(0);
var initialFirstCharMatch = (initialFirstChar === 0x3A
|| initialFirstChar === 0x5F
|| (initialFirstChar >= 0x41 && initialFirstChar <= 0x5A)
|| (initialFirstChar >= 0x61 && initialFirstChar <= 0x7A)
|| (initialFirstChar >= 0xC0 && initialFirstChar <= 0xD6)
|| (initialFirstChar >= 0xD8 && initialFirstChar <= 0xF6)
|| (initialFirstChar >= 0XF8 && initialFirstChar <= 0X2FF)
|| (initialFirstChar >= 0x370 && initialFirstChar <= 0x37D)
|| (initialFirstChar >= 0x37F && initialFirstChar <= 0X1FFF)
|| (initialFirstChar >= 0x200C && initialFirstChar <= 0x200D)
|| (initialFirstChar >= 0x2070 && initialFirstChar <= 0x218F)
|| (initialFirstChar >= 0x2C00 && initialFirstChar <= 0x2FEF)
|| (initialFirstChar >= 0x3001 && initialFirstChar <= 0xD7FF)
|| (initialFirstChar >= 0xF900 && initialFirstChar <= 0xFDCF)
|| (initialFirstChar >= 0xFDF0 && initialFirstChar <= 0xFFFD));
if (str.length === 1) {
return initialFirstCharMatch;
}
// UTF-16 surrogate characters
var initialSecondChar = str.charCodeAt(1);
var initialSecondCharMatch = ((initialFirstChar >= 0xD800 && initialFirstChar <= 0xDB7F)
&& (initialSecondChar >= 0xDC00 && initialSecondChar <= 0xDFFF));
if (!initialFirstCharMatch && !initialSecondCharMatch) {
return false;
}
var start = initialSecondCharMatch ? 2 : 1;
for (var i = start; i < str.length; i++) {
var firstChar = str.charCodeAt(i);
if (firstChar === 0x3A
|| firstChar === 0x5F
|| firstChar === 0x2D
|| firstChar === 0x2E
|| firstChar === 0xB7
|| (firstChar >= 0x30 && firstChar <= 0x39)
|| (firstChar >= 0x41 && firstChar <= 0x5A)
|| (firstChar >= 0x61 && firstChar <= 0x7A)
|| (firstChar >= 0xC0 && firstChar <= 0xD6)
|| (firstChar >= 0xD8 && firstChar <= 0xF6)
|| (firstChar >= 0XF8 && firstChar <= 0X2FF)
|| (firstChar >= 0x300 && firstChar <= 0x36F)
|| (firstChar >= 0x370 && firstChar <= 0x37D)
|| (firstChar >= 0x37F && firstChar <= 0X1FFF)
|| (firstChar >= 0x200C && firstChar <= 0x200D)
|| (firstChar >= 0x203F && firstChar <= 0x2040)
|| (firstChar >= 0x2070 && firstChar <= 0x218F)
|| (firstChar >= 0x2C00 && firstChar <= 0x2FEF)
|| (firstChar >= 0x3001 && firstChar <= 0xD7FF)
|| (firstChar >= 0xF900 && firstChar <= 0xFDCF)
|| (firstChar >= 0xFDF0 && firstChar <= 0xFFFD)) {
continue;
}
if (i + 1 === str.length) {
return false;
}
// UTF-16 surrogate characters
var secondChar = str.charCodeAt(i + 1);
if ((firstChar >= 0xD800 && firstChar <= 0xDB7F)
&& (secondChar >= 0xDC00 && secondChar <= 0xDFFF)) {
i++;
continue;
}
return false;
}
return true;
}
exports.validateName = validateName;
/**
* Returns a version of the specified string that only contains characters
* permitted by the XML specification for names, with invalid characters
* replaced by the replacement character U+FFFD.
*/
function fixName(str) {
var newStr = "";
if (str.length === 0) {
return newStr;
}
var initialFirstChar = str.charCodeAt(0);
var initialFirstCharMatch = (initialFirstChar === 0x3A
|| initialFirstChar === 0x5F
|| (initialFirstChar >= 0x41 && initialFirstChar <= 0x5A)
|| (initialFirstChar >= 0x61 && initialFirstChar <= 0x7A)
|| (initialFirstChar >= 0xC0 && initialFirstChar <= 0xD6)
|| (initialFirstChar >= 0xD8 && initialFirstChar <= 0xF6)
|| (initialFirstChar >= 0XF8 && initialFirstChar <= 0X2FF)
|| (initialFirstChar >= 0x370 && initialFirstChar <= 0x37D)
|| (initialFirstChar >= 0x37F && initialFirstChar <= 0X1FFF)
|| (initialFirstChar >= 0x200C && initialFirstChar <= 0x200D)
|| (initialFirstChar >= 0x2070 && initialFirstChar <= 0x218F)
|| (initialFirstChar >= 0x2C00 && initialFirstChar <= 0x2FEF)
|| (initialFirstChar >= 0x3001 && initialFirstChar <= 0xD7FF)
|| (initialFirstChar >= 0xF900 && initialFirstChar <= 0xFDCF)
|| (initialFirstChar >= 0xFDF0 && initialFirstChar <= 0xFFFD));
if (str.length === 1) {
if (initialFirstCharMatch) {
newStr = str[0];
}
else {
newStr = "\uFFFD";
}
return newStr;
}
// UTF-16 surrogate characters
var initialSecondChar = str.charCodeAt(1);
var initialSecondCharMatch = ((initialFirstChar >= 0xD800 && initialFirstChar <= 0xDB7F)
&& (initialSecondChar >= 0xDC00 && initialSecondChar <= 0xDFFF));
if (initialSecondCharMatch) {
newStr = str[0] + str[1];
}
else if (initialFirstCharMatch) {
newStr = str[0];
}
else {
newStr = "\uFFFD";
}
var start = initialSecondCharMatch ? 2 : 1;
for (var i = start; i < str.length; i++) {
var firstChar = str.charCodeAt(i);
if (firstChar === 0x3A
|| firstChar === 0x5F
|| firstChar === 0x2D
|| firstChar === 0x2E
|| firstChar === 0xB7
|| (firstChar >= 0x30 && firstChar <= 0x39)
|| (firstChar >= 0x41 && firstChar <= 0x5A)
|| (firstChar >= 0x61 && firstChar <= 0x7A)
|| (firstChar >= 0xC0 && firstChar <= 0xD6)
|| (firstChar >= 0xD8 && firstChar <= 0xF6)
|| (firstChar >= 0XF8 && firstChar <= 0X2FF)
|| (firstChar >= 0x300 && firstChar <= 0x36F)
|| (firstChar >= 0x370 && firstChar <= 0x37D)
|| (firstChar >= 0x37F && firstChar <= 0X1FFF)
|| (firstChar >= 0x200C && firstChar <= 0x200D)
|| (firstChar >= 0x203F && firstChar <= 0x2040)
|| (firstChar >= 0x2070 && firstChar <= 0x218F)
|| (firstChar >= 0x2C00 && firstChar <= 0x2FEF)
|| (firstChar >= 0x3001 && firstChar <= 0xD7FF)
|| (firstChar >= 0xF900 && firstChar <= 0xFDCF)
|| (firstChar >= 0xFDF0 && firstChar <= 0xFFFD)) {
newStr += str[i];
continue;
}
if (i + 1 === str.length) {
newStr += "\uFFFD";
return newStr;
}
// UTF-16 surrogate characters
var secondChar = str.charCodeAt(i + 1);
if ((firstChar >= 0xD800 && firstChar <= 0xDB7F)
&& (secondChar >= 0xDC00 && secondChar <= 0xDFFF)) {
newStr += str[i] + str[i + 1];
i++;
continue;
}
newStr += "\uFFFD";
}
return newStr;
}
exports.fixName = fixName;
/**
* Returns true if the specified value is undefined.
*/
function isUndefined(val) {
return Object.prototype.toString.call(val) === "[object Undefined]";
}
exports.isUndefined = isUndefined;