Comparing version 1.0.2 to 2.0.0
@@ -0,1 +1,11 @@ | ||
## 2.0.0 ## | ||
* API rewrite and simplification | ||
* Add option to use self-closing tags if an element is empty | ||
* Stop indenting multi-line strings | ||
* Add option to automatically fix strings that are not valid XML in certain | ||
cases | ||
* More detailed error messages | ||
* Other bug fixes | ||
## 1.0.2 ## | ||
@@ -2,0 +12,0 @@ |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2018 Michael Kourlas | ||
* | ||
@@ -19,6 +19,2 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with ampersands escaped. | ||
* | ||
* @private | ||
@@ -31,6 +27,2 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with left angle brackets escaped. | ||
* | ||
* @private | ||
@@ -43,7 +35,2 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with right angle brackets escaped | ||
* when part of the string "]]>". | ||
* | ||
* @private | ||
@@ -55,6 +42,2 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with single quotes escaped. | ||
* | ||
* @private | ||
@@ -66,8 +49,4 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with double quotes escaped. | ||
* | ||
* @private | ||
*/ | ||
export declare function escapeDoubleQuotes(str: string): string; |
@@ -0,3 +1,4 @@ | ||
"use strict"; | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2018 Michael Kourlas | ||
* | ||
@@ -16,3 +17,2 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -22,6 +22,2 @@ /** | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with ampersands escaped. | ||
* | ||
* @private | ||
@@ -37,6 +33,2 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with left angle brackets escaped. | ||
* | ||
* @private | ||
@@ -52,7 +44,2 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with right angle brackets escaped | ||
* when part of the string "]]>". | ||
* | ||
* @private | ||
@@ -67,6 +54,2 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with single quotes escaped. | ||
* | ||
* @private | ||
@@ -81,6 +64,2 @@ */ | ||
* | ||
* @param str The string to escape. | ||
* | ||
* @returns A copy of the specified string with double quotes escaped. | ||
* | ||
* @private | ||
@@ -87,0 +66,0 @@ */ |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,28 +16,23 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import XmlAttribute from "./nodes/XmlAttribute"; | ||
import XmlAttributeText from "./nodes/XmlAttributeText"; | ||
import XmlCdata from "./nodes/XmlCdata"; | ||
import XmlCharData from "./nodes/XmlCharData"; | ||
import XmlCharRef from "./nodes/XmlCharRef"; | ||
import XmlComment from "./nodes/XmlComment"; | ||
import XmlDecl from "./nodes/XmlDecl"; | ||
import XmlDocument from "./nodes/XmlDocument"; | ||
import XmlDtd from "./nodes/XmlDtd"; | ||
import XmlDtdAttlist from "./nodes/XmlDtdAttlist"; | ||
import XmlDtdElement from "./nodes/XmlDtdElement"; | ||
import XmlDtdEntity from "./nodes/XmlDtdEntity"; | ||
import XmlDtdNotation from "./nodes/XmlDtdNotation"; | ||
import XmlDtdParamEntityRef from "./nodes/XmlDtdParamEntityRef"; | ||
import XmlElement from "./nodes/XmlElement"; | ||
import XmlEntityRef from "./nodes/XmlEntityRef"; | ||
import XmlNode from "./nodes/XmlNode"; | ||
import XmlProcInst from "./nodes/XmlProcInst"; | ||
export { XmlAttribute, XmlAttributeText, XmlCdata, XmlCharData, XmlCharRef, XmlComment, XmlDecl, XmlDocument, XmlDtd, XmlDtdAttlist, XmlDtdElement, XmlDtdEntity, XmlDtdNotation, XmlDtdParamEntityRef, XmlElement, XmlEntityRef, XmlNode, XmlProcInst }; | ||
import XmlDocument, { IXmlDocumentOptions } from "./nodes/XmlDocument"; | ||
export { default as XmlAttribute } from "./nodes/XmlAttribute"; | ||
export { default as XmlAttributeText } from "./nodes/XmlAttributeText"; | ||
export { default as XmlCdata } from "./nodes/XmlCdata"; | ||
export { default as XmlCharData } from "./nodes/XmlCharData"; | ||
export { default as XmlCharRef } from "./nodes/XmlCharRef"; | ||
export { default as XmlComment } from "./nodes/XmlComment"; | ||
export { default as XmlDecl } from "./nodes/XmlDecl"; | ||
export { default as XmlDocument } from "./nodes/XmlDocument"; | ||
export { default as XmlDtd } from "./nodes/XmlDtd"; | ||
export { default as XmlDtdAttlist } from "./nodes/XmlDtdAttlist"; | ||
export { default as XmlDtdElement } from "./nodes/XmlDtdElement"; | ||
export { default as XmlDtdEntity } from "./nodes/XmlDtdEntity"; | ||
export { default as XmlDtdNotation } from "./nodes/XmlDtdNotation"; | ||
export { default as XmlDtdParamEntityRef } from "./nodes/XmlDtdParamEntityRef"; | ||
export { default as XmlElement } from "./nodes/XmlElement"; | ||
export { default as XmlEntityRef } from "./nodes/XmlEntityRef"; | ||
export { default as XmlProcInst } from "./nodes/XmlProcInst"; | ||
/** | ||
* Creates a new XML document. | ||
* | ||
* @param root The name of the root element of the document. | ||
* | ||
* @returns The new XML document. | ||
* Returns a new XML document with the specified options. | ||
*/ | ||
export declare function document(root: string): XmlDocument; | ||
export declare function document(options?: IXmlDocumentOptions): XmlDocument; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -18,2 +17,7 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var XmlDocument_1 = __importDefault(require("./nodes/XmlDocument")); | ||
var XmlAttribute_1 = require("./nodes/XmlAttribute"); | ||
@@ -33,4 +37,4 @@ exports.XmlAttribute = XmlAttribute_1.default; | ||
exports.XmlDecl = XmlDecl_1.default; | ||
var XmlDocument_1 = require("./nodes/XmlDocument"); | ||
exports.XmlDocument = XmlDocument_1.default; | ||
var XmlDocument_2 = require("./nodes/XmlDocument"); | ||
exports.XmlDocument = XmlDocument_2.default; | ||
var XmlDtd_1 = require("./nodes/XmlDtd"); | ||
@@ -52,16 +56,11 @@ exports.XmlDtd = XmlDtd_1.default; | ||
exports.XmlEntityRef = XmlEntityRef_1.default; | ||
var XmlNode_1 = require("./nodes/XmlNode"); | ||
exports.XmlNode = XmlNode_1.default; | ||
var XmlProcInst_1 = require("./nodes/XmlProcInst"); | ||
exports.XmlProcInst = XmlProcInst_1.default; | ||
/** | ||
* Creates a new XML document. | ||
* | ||
* @param root The name of the root element of the document. | ||
* | ||
* @returns The new XML document. | ||
* Returns a new XML document with the specified options. | ||
*/ | ||
function document(root) { | ||
return new XmlDocument_1.default(root); | ||
function document(options) { | ||
if (options === void 0) { options = {}; } | ||
return new XmlDocument_1.default(options); | ||
} | ||
exports.document = document; |
@@ -0,10 +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 { IStringOptions } from "../options"; | ||
import XmlAttributeText from "./XmlAttributeText"; | ||
import XmlCharRef from "./XmlCharRef"; | ||
import XmlEntityRef from "./XmlEntityRef"; | ||
import XmlNode from "./XmlNode"; | ||
import XmlAttributeText, { IXmlAttributeTextOptions } from "./XmlAttributeText"; | ||
import XmlCharRef, { IXmlCharRefOptions } from "./XmlCharRef"; | ||
import XmlEntityRef, { IXmlEntityRefOptions } from "./XmlEntityRef"; | ||
/** | ||
* Represents an XML element attribute. | ||
* 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 XML element attribute is part of the start tag of an element and is | ||
* 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 | ||
@@ -20,112 +48,41 @@ * `{value}` is the value of the attribute: | ||
* | ||
* XmlAttribute nodes must have at least one child, and can have an unlimited | ||
* number of {@link XmlAttributeText}, {@link XmlCharRef}, and | ||
* {@link XmlEntityRef} nodes as children. | ||
* Attributes can have an unlimited number of attribute text, character | ||
* references, and entity references. | ||
*/ | ||
export default class XmlAttribute extends XmlNode { | ||
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); | ||
/** | ||
* Initializes a new instance of the {@link XmlAttribute} class. | ||
* | ||
* @param name The name of the XML attribute. | ||
* @param value The initial value of the XML attribute. Additional children | ||
* can be added later. Only {@link XmlAttributeText}, | ||
* {@link XmlCharRef}, and {@link XmlEntityRef} nodes are | ||
* permitted. | ||
*/ | ||
constructor(name: string, value: XmlNode | XmlNode[]); | ||
/** | ||
* Gets the name of this attribute. | ||
* | ||
* @returns The name of this attribute. | ||
*/ | ||
/** | ||
* Sets the name of this attribute. | ||
* | ||
* @param name The name of this attribute. | ||
*/ | ||
* Sets the name of this attribute. | ||
*/ | ||
name: string; | ||
/** | ||
* Inserts a new XML character reference at the specified index. | ||
* | ||
* @param char The character to represent using the reference. | ||
* @param hex Whether to use the hexadecimal or decimal representation for | ||
* the reference. If left undefined, decimal is the default. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created XML declaration. | ||
* Adds a character reference to this attribute and returns the new | ||
* character reference. | ||
*/ | ||
charRef(char: string, hex: boolean, index?: number): XmlCharRef; | ||
charRef(options: IXmlCharRefOptions): XmlCharRef<this>; | ||
/** | ||
* Inserts a new XML entity reference at the specified index. | ||
* | ||
* @param entity The entity to be referenced. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created XML declaration. | ||
* Adds an entity reference to this attribute and returns the new entity | ||
* reference. | ||
*/ | ||
entityRef(entity: string, index?: number): XmlEntityRef; | ||
entityRef(options: IXmlEntityRefOptions): XmlEntityRef<this>; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Note that only {@link XmlCharRef}, {@link XmlEntityRef}, and | ||
* {@link XmlCharData} nodes can be inserted; otherwise, an exception will | ||
* be thrown. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after the | ||
* index are shifted to the right. If no index is specified, | ||
* the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Adds attribute text to this attribute and returns the new text. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
text(options: IXmlAttributeTextOptions): XmlAttributeText<this>; | ||
/** | ||
* Removes the specified node from this node's children. | ||
* | ||
* Note that this node must have at least one child. Attempts to remove | ||
* the last child node will result in an exception. | ||
* | ||
* @param node The node to remove. | ||
* | ||
* @returns Whether a node was removed. | ||
* Returns an XML string representation of this attribute. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
toString(options?: IStringOptions): string; | ||
/** | ||
* Removes the node at the specified index from this node's children. | ||
* | ||
* Note that this node must have at least one child. Attempts to remove | ||
* the last child node will result in an exception. | ||
* | ||
* @param index The index at which the node to be removed is located. | ||
* | ||
* @returns The node that was removed, or undefined if no node was removed. | ||
* Returns the parent of this attribute. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Inserts a new XML text node at the specified index. | ||
* | ||
* @param text Arbitrary character data. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created XML declaration. | ||
*/ | ||
text(text: string, index?: number): XmlAttributeText; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -28,14 +17,17 @@ * Licensed under the Apache License, Version 2.0 (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 utils_1 = require("../utils"); | ||
var validate_1 = require("../validate"); | ||
var XmlAttributeText_1 = require("./XmlAttributeText"); | ||
var XmlCharRef_1 = require("./XmlCharRef"); | ||
var XmlEntityRef_1 = require("./XmlEntityRef"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
var XmlAttributeText_1 = __importDefault(require("./XmlAttributeText")); | ||
var XmlCharRef_1 = __importDefault(require("./XmlCharRef")); | ||
var XmlEntityRef_1 = __importDefault(require("./XmlEntityRef")); | ||
/** | ||
* Represents an XML element attribute. | ||
* Represents an attribute. | ||
* | ||
* An XML element attribute is part of the start tag of an element and is | ||
* 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 | ||
@@ -51,30 +43,17 @@ * `{value}` is the value of the attribute: | ||
* | ||
* XmlAttribute nodes must have at least one child, and can have an unlimited | ||
* number of {@link XmlAttributeText}, {@link XmlCharRef}, and | ||
* {@link XmlEntityRef} nodes as children. | ||
* Attributes can have an unlimited number of attribute text, character | ||
* references, and entity references. | ||
*/ | ||
var XmlAttribute = (function (_super) { | ||
__extends(XmlAttribute, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlAttribute} class. | ||
* | ||
* @param name The name of the XML attribute. | ||
* @param value The initial value of the XML attribute. Additional children | ||
* can be added later. Only {@link XmlAttributeText}, | ||
* {@link XmlCharRef}, and {@link XmlEntityRef} nodes are | ||
* permitted. | ||
*/ | ||
function XmlAttribute(name, value) { | ||
var _this = _super.call(this) || this; | ||
_this.name = name; | ||
if (utils_1.isArray(value)) { | ||
for (var _i = 0, value_1 = value; _i < value_1.length; _i++) { | ||
var node = value_1[_i]; | ||
_this.insertChild(node); | ||
} | ||
var XmlAttribute = /** @class */ (function () { | ||
function XmlAttribute(parent, validation, options) { | ||
this._validation = validation; | ||
if (!validate_1.isUndefined(options.replaceInvalidCharsInName)) { | ||
this._replaceInvalidCharsInName = options.replaceInvalidCharsInName; | ||
} | ||
else { | ||
_this.insertChild(value); | ||
this._replaceInvalidCharsInName = false; | ||
} | ||
return _this; | ||
this._children = []; | ||
this._parent = parent; | ||
this.name = options.name; | ||
} | ||
@@ -84,4 +63,2 @@ Object.defineProperty(XmlAttribute.prototype, "name", { | ||
* Gets the name of this attribute. | ||
* | ||
* @returns The name of this attribute. | ||
*/ | ||
@@ -93,12 +70,21 @@ get: function () { | ||
* Sets the name of this attribute. | ||
* | ||
* @param name The name of this attribute. | ||
*/ | ||
set: function (name) { | ||
if (!utils_1.isString(name)) { | ||
throw new TypeError("name should be a string"); | ||
if (this._replaceInvalidCharsInName) { | ||
name = validate_1.fixName(name); | ||
if (name.length === 0) { | ||
throw new Error(error_1.getContext(this.up()) + ": attribute name" | ||
+ " should not be empty"); | ||
} | ||
} | ||
else if (!validate_1.validateName(name)) { | ||
throw new Error("name should not contain characters not" | ||
+ " allowed in XML names"); | ||
else if (this._validation && !validate_1.validateName(name)) { | ||
if (name.length === 0) { | ||
throw new Error(error_1.getContext(this.up()) + ": attribute name" | ||
+ " should not be empty"); | ||
} | ||
else { | ||
throw new Error(error_1.getContext(this.up()) + ": attribute name" | ||
+ (" \"" + name + "\" should not contain characters not") | ||
+ " allowed in XML names"); | ||
} | ||
} | ||
@@ -111,111 +97,29 @@ this._name = name; | ||
/** | ||
* Inserts a new XML character reference at the specified index. | ||
* | ||
* @param char The character to represent using the reference. | ||
* @param hex Whether to use the hexadecimal or decimal representation for | ||
* the reference. If left undefined, decimal is the default. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created XML declaration. | ||
* Adds a character reference to this attribute and returns the new | ||
* character reference. | ||
*/ | ||
XmlAttribute.prototype.charRef = function (char, hex, index) { | ||
var charRef = new XmlCharRef_1.default(char, hex); | ||
this.insertChild(charRef, index); | ||
XmlAttribute.prototype.charRef = function (options) { | ||
var charRef = new XmlCharRef_1.default(this, this._validation, options); | ||
this._children.push(charRef); | ||
return charRef; | ||
}; | ||
/** | ||
* Inserts a new XML entity reference at the specified index. | ||
* | ||
* @param entity The entity to be referenced. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created XML declaration. | ||
* Adds an entity reference to this attribute and returns the new entity | ||
* reference. | ||
*/ | ||
XmlAttribute.prototype.entityRef = function (entity, index) { | ||
var charRef = new XmlEntityRef_1.default(entity); | ||
this.insertChild(charRef, index); | ||
XmlAttribute.prototype.entityRef = function (options) { | ||
var charRef = new XmlEntityRef_1.default(this, this._validation, options); | ||
this._children.push(charRef); | ||
return charRef; | ||
}; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Note that only {@link XmlCharRef}, {@link XmlEntityRef}, and | ||
* {@link XmlCharData} nodes can be inserted; otherwise, an exception will | ||
* be thrown. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after the | ||
* index are shifted to the right. If no index is specified, | ||
* the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Adds attribute text to this attribute and returns the new text. | ||
*/ | ||
XmlAttribute.prototype.insertChild = function (node, index) { | ||
if (!(node instanceof XmlCharRef_1.default || node instanceof XmlEntityRef_1.default || | ||
node instanceof XmlAttributeText_1.default)) { | ||
throw new TypeError("node should be an instance of XmlCharRef," | ||
+ " XmlEntityRef, or XmlAttributeText"); | ||
} | ||
return _super.prototype.insertChild.call(this, node, index); | ||
}; | ||
/** | ||
* Removes the specified node from this node's children. | ||
* | ||
* Note that this node must have at least one child. Attempts to remove | ||
* the last child node will result in an exception. | ||
* | ||
* @param node The node to remove. | ||
* | ||
* @returns Whether a node was removed. | ||
*/ | ||
XmlAttribute.prototype.removeChild = function (node) { | ||
if (this._children.length === 1) { | ||
throw new Error("XmlAttribute nodes must have at least one child"); | ||
} | ||
return _super.prototype.removeChild.call(this, node); | ||
}; | ||
/** | ||
* Removes the node at the specified index from this node's children. | ||
* | ||
* Note that this node must have at least one child. Attempts to remove | ||
* the last child node will result in an exception. | ||
* | ||
* @param index The index at which the node to be removed is located. | ||
* | ||
* @returns The node that was removed, or undefined if no node was removed. | ||
*/ | ||
XmlAttribute.prototype.removeChildAtIndex = function (index) { | ||
if (this._children.length === 1) { | ||
throw new Error("XmlAttribute nodes must have at least one child"); | ||
} | ||
return _super.prototype.removeChildAtIndex.call(this, index); | ||
}; | ||
/** | ||
* Inserts a new XML text node at the specified index. | ||
* | ||
* @param text Arbitrary character data. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created XML declaration. | ||
*/ | ||
XmlAttribute.prototype.text = function (text, index) { | ||
var textNode = new XmlAttributeText_1.default(text); | ||
this.insertChild(textNode, index); | ||
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 node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
* Returns an XML string representation of this attribute. | ||
*/ | ||
@@ -226,10 +130,10 @@ XmlAttribute.prototype.toString = function (options) { | ||
var quote = optionsObj.doubleQuotes ? "\"" : "'"; | ||
var str = this.name + "=" + quote; | ||
var str = this._name + "=" + quote; | ||
for (var _i = 0, _a = this._children; _i < _a.length; _i++) { | ||
var child = _a[_i]; | ||
if (optionsObj.doubleQuotes) { | ||
str += escape_1.escapeDoubleQuotes(child.toString(options)); | ||
str += escape_1.escapeDoubleQuotes(child.toString()); | ||
} | ||
else { | ||
str += escape_1.escapeSingleQuotes(child.toString(options)); | ||
str += escape_1.escapeSingleQuotes(child.toString()); | ||
} | ||
@@ -240,4 +144,10 @@ } | ||
}; | ||
/** | ||
* Returns the parent of this attribute. | ||
*/ | ||
XmlAttribute.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
return XmlAttribute; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlAttribute; |
@@ -1,76 +0,57 @@ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents text in an XML attribute value. | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
* Restricted characters, such as the ampersand (`&`) and the opening angle | ||
* bracket (`<`), are all automatically escaped. | ||
* 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 | ||
* | ||
* To create an character reference or entity reference, you should use | ||
* {@link XmlCharRef} or {@link XmlEntityRef} respectively instead. | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* XmlAttributeText nodes cannot have any children. | ||
* 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. | ||
*/ | ||
export default class XmlAttributeText extends XmlNode { | ||
private _text; | ||
/** | ||
* The options used to create attribute text. | ||
*/ | ||
export interface IXmlAttributeTextOptions { | ||
/** | ||
* Initializes a new instance of the {@link XmlAttributeText} class. | ||
* | ||
* @param text Text. | ||
* The attribute text. | ||
*/ | ||
constructor(text: string); | ||
charData: string; | ||
/** | ||
* Gets the text associated with this node. | ||
* | ||
* @returns The text associated with this node. | ||
* 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); | ||
/** | ||
* Sets the text associated with this node. | ||
* | ||
* @param text Text. | ||
* Gets this attribute text. | ||
*/ | ||
text: string; | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
children(): XmlNode[]; | ||
* Sets this attribute text. | ||
*/ | ||
charData: string; | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this attribute text. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this attribute text. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -28,52 +17,44 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var error_1 = require("../error"); | ||
var escape_1 = require("../escape"); | ||
var utils_1 = require("../utils"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents text in an XML attribute value. | ||
* Represents text in an attribute value. | ||
* | ||
* Restricted characters, such as the ampersand (`&`) and the opening angle | ||
* bracket (`<`), are all automatically escaped. | ||
* | ||
* To create an character reference or entity reference, you should use | ||
* {@link XmlCharRef} or {@link XmlEntityRef} respectively instead. | ||
* | ||
* XmlAttributeText nodes cannot have any children. | ||
*/ | ||
var XmlAttributeText = (function (_super) { | ||
__extends(XmlAttributeText, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlAttributeText} class. | ||
* | ||
* @param text Text. | ||
*/ | ||
function XmlAttributeText(text) { | ||
var _this = _super.call(this) || this; | ||
_this.text = text; | ||
return _this; | ||
var XmlAttributeText = /** @class */ (function () { | ||
function XmlAttributeText(parent, validation, options) { | ||
this._validation = validation; | ||
if (!validate_1.isUndefined(options.replaceInvalidCharsInCharData)) { | ||
this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData); | ||
} | ||
else { | ||
this._replaceInvalidCharsInCharData = false; | ||
} | ||
this._parent = parent; | ||
this.charData = options.charData; | ||
} | ||
Object.defineProperty(XmlAttributeText.prototype, "text", { | ||
Object.defineProperty(XmlAttributeText.prototype, "charData", { | ||
/** | ||
* Gets the text associated with this node. | ||
* | ||
* @returns The text associated with this node. | ||
* Gets this attribute text. | ||
*/ | ||
get: function () { | ||
return this._text; | ||
return this._charData; | ||
}, | ||
/** | ||
* Sets the text associated with this node. | ||
* | ||
* @param text Text. | ||
* Sets this attribute text. | ||
*/ | ||
set: function (text) { | ||
if (!utils_1.isString(text)) { | ||
throw new TypeError("text should be a string"); | ||
set: function (charData) { | ||
if (this._replaceInvalidCharsInCharData) { | ||
charData = validate_1.fixChar(charData); | ||
} | ||
else if (!validate_1.validateChar(text)) { | ||
throw new Error("text should not contain characters not allowed" | ||
+ " in XML"); | ||
else if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": attribute text" | ||
+ (" \"" + charData + "\" should not contain characters not") | ||
+ " allowed in XML"); | ||
} | ||
this._text = text; | ||
this._charData = charData; | ||
}, | ||
@@ -84,60 +65,18 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this attribute text. | ||
*/ | ||
XmlAttributeText.prototype.children = function () { | ||
throw new Error("XmlAttributeText nodes cannot have children"); | ||
XmlAttributeText.prototype.toString = function () { | ||
var str = this._charData; | ||
str = escape_1.escapeAmpersands(str); | ||
str = escape_1.escapeLeftAngleBrackets(str); | ||
return str; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this attribute text. | ||
*/ | ||
XmlAttributeText.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlAttributeText nodes cannot have children"); | ||
XmlAttributeText.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlAttributeText.prototype.removeChild = function (node) { | ||
throw new Error("XmlAttributeText nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlAttributeText} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlAttributeText.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlAttributeText nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
XmlAttributeText.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
var str = this.text; | ||
str = escape_1.escapeAmpersands(str); | ||
str = escape_1.escapeLeftAngleBrackets(str); | ||
return str; | ||
}; | ||
return XmlAttributeText; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlAttributeText; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,21 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML CDATA section. | ||
* 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. | ||
* | ||
* An XML CDATA section is structured as follows, where `{data}` is the | ||
* A CDATA section is structured as follows, where `{data}` is the | ||
* character data of the section: | ||
@@ -28,69 +41,24 @@ * | ||
* ``` | ||
* | ||
* The `{data}` value is a property of this node. | ||
* | ||
* XmlCdata nodes cannot have any children. | ||
*/ | ||
export default class XmlCdata extends XmlNode { | ||
private _data; | ||
export default class XmlCdata<Parent> { | ||
private readonly _replaceInvalidCharsInCharData; | ||
private readonly _parent; | ||
private readonly _validation; | ||
private _charData; | ||
constructor(parent: Parent, validation: boolean, options: IXmlCdataOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlCdata} class. | ||
* | ||
* @param data The character data of the CDATA section. | ||
* Gets the character data of this CDATA section. | ||
*/ | ||
constructor(data: string); | ||
/** | ||
* Gets the character data of the CDATA section. | ||
* | ||
* @returns The character data of the CDATA section. | ||
*/ | ||
* Sets the character data of this CDATA section. | ||
*/ | ||
charData: string; | ||
/** | ||
* Sets the character data of the CDATA section. | ||
* | ||
* @param data The character data of the CDATA section. | ||
* Returns an XML string representation of this CDATA section. | ||
*/ | ||
data: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this CDATA section. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML CDATA section. | ||
* Represents a CDATA section. | ||
* | ||
* An XML CDATA section is structured as follows, where `{data}` is the | ||
* A CDATA section is structured as follows, where `{data}` is the | ||
* character data of the section: | ||
@@ -25,45 +29,43 @@ * | ||
* ``` | ||
* | ||
* The `{data}` value is a property of this node. | ||
* | ||
* XmlCdata nodes cannot have any children. | ||
*/ | ||
var XmlCdata = (function (_super) { | ||
__extends(XmlCdata, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlCdata} class. | ||
* | ||
* @param data The character data of the CDATA section. | ||
*/ | ||
function XmlCdata(data) { | ||
var _this = _super.call(this) || this; | ||
_this.data = data; | ||
return _this; | ||
var XmlCdata = /** @class */ (function () { | ||
function XmlCdata(parent, validation, options) { | ||
this._validation = validation; | ||
if (!validate_1.isUndefined(options.replaceInvalidCharsInCharData)) { | ||
this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData); | ||
} | ||
else { | ||
this._replaceInvalidCharsInCharData = false; | ||
} | ||
this._parent = parent; | ||
this.charData = options.charData; | ||
} | ||
Object.defineProperty(XmlCdata.prototype, "data", { | ||
Object.defineProperty(XmlCdata.prototype, "charData", { | ||
/** | ||
* Gets the character data of the CDATA section. | ||
* | ||
* @returns The character data of the CDATA section. | ||
* Gets the character data of this CDATA section. | ||
*/ | ||
get: function () { | ||
return this._data; | ||
return this._charData; | ||
}, | ||
/** | ||
* Sets the character data of the CDATA section. | ||
* | ||
* @param data The character data of the CDATA section. | ||
* Sets the character data of this CDATA section. | ||
*/ | ||
set: function (data) { | ||
if (!utils_1.isString(data)) { | ||
throw new TypeError("character data should be a string"); | ||
set: function (charData) { | ||
if (this._replaceInvalidCharsInCharData) { | ||
charData = validate_1.fixChar(charData); | ||
} | ||
else if (!validate_1.validateChar(data)) { | ||
throw new Error("character data should not contain characters not" | ||
+ " allowed in XML"); | ||
else if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": CDATA section" | ||
+ (" \"" + charData + "\" should not contain characters") | ||
+ " not allowed in XML"); | ||
} | ||
else if (/]]>/.test(data)) { | ||
throw new Error("data should not contain the string ']]>'"); | ||
if (this._replaceInvalidCharsInCharData) { | ||
charData = charData.replace("]]>", "\uFFFD\uFFFD\uFFFD"); | ||
} | ||
this._data = data; | ||
else if (this._validation && charData.indexOf("]]>") !== -1) { | ||
throw new Error(error_1.getContext(this.up()) + ": CDATA section" | ||
+ (" \"" + charData + "\" should not contain the string") | ||
+ " ']]>'"); | ||
} | ||
this._charData = charData; | ||
}, | ||
@@ -74,57 +76,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this CDATA section. | ||
*/ | ||
XmlCdata.prototype.children = function () { | ||
throw new Error("XmlCdata nodes cannot have children"); | ||
XmlCdata.prototype.toString = function () { | ||
return "<![CDATA[" + this._charData + "]]>"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this CDATA section. | ||
*/ | ||
XmlCdata.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlCdata nodes cannot have children"); | ||
XmlCdata.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCdata.prototype.removeChild = function (node) { | ||
throw new Error("XmlCdata nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCdata} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCdata.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlCdata nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlCdata.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "<![CDATA[" + this.data + "]]>"; | ||
}; | ||
return XmlCdata; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlCdata; |
@@ -1,77 +0,58 @@ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents character data in an XML document. | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
* 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. | ||
* 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 | ||
* | ||
* To create an character reference or entity reference, you should use | ||
* {@link XmlCharRef} or {@link XmlEntityRef} respectively instead. | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* XmlCharData nodes cannot have any children. | ||
* 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. | ||
*/ | ||
export default class XmlCharData extends XmlNode { | ||
private _charData; | ||
/** | ||
* The options used to create new character data. | ||
*/ | ||
export interface IXmlCharDataOptions { | ||
/** | ||
* Initializes a new instance of the {@link XmlCharData} class. | ||
* | ||
* @param charData Character data. | ||
* The character data. | ||
*/ | ||
constructor(charData: string); | ||
/** | ||
* Gets the character data associated with this node. | ||
* | ||
* @returns The character data associated with this node. | ||
*/ | ||
/** | ||
* Sets the character data associated with this node. | ||
* | ||
* @param charData Character data. | ||
*/ | ||
charData: string; | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Whether to replace any invalid characters in the character data with the | ||
* Unicode replacement character. By default, this is disabled. | ||
*/ | ||
children(): XmlNode[]; | ||
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); | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Gets the text of this character data. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
* Sets the text of this character data. | ||
*/ | ||
charData: string; | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this character data. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
toString(): string; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
* Returns the parent of this character data. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -28,8 +17,8 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var error_1 = require("../error"); | ||
var escape_1 = require("../escape"); | ||
var utils_1 = require("../utils"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents character data in an XML document. | ||
* Represents character data. | ||
* | ||
@@ -39,25 +28,18 @@ * Restricted characters, such as the ampersand (`&`), the opening angle | ||
* string `]]>`, are all automatically escaped. | ||
* | ||
* To create an character reference or entity reference, you should use | ||
* {@link XmlCharRef} or {@link XmlEntityRef} respectively instead. | ||
* | ||
* XmlCharData nodes cannot have any children. | ||
*/ | ||
var XmlCharData = (function (_super) { | ||
__extends(XmlCharData, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlCharData} class. | ||
* | ||
* @param charData Character data. | ||
*/ | ||
function XmlCharData(charData) { | ||
var _this = _super.call(this) || this; | ||
_this.charData = charData; | ||
return _this; | ||
var XmlCharData = /** @class */ (function () { | ||
function XmlCharData(parent, validation, options) { | ||
this._validation = validation; | ||
if (!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 character data associated with this node. | ||
* | ||
* @returns The character data associated with this node. | ||
* Gets the text of this character data. | ||
*/ | ||
@@ -68,13 +50,12 @@ get: function () { | ||
/** | ||
* Sets the character data associated with this node. | ||
* | ||
* @param charData Character data. | ||
* Sets the text of this character data. | ||
*/ | ||
set: function (charData) { | ||
if (!utils_1.isString(charData)) { | ||
throw new TypeError("charData should be a string"); | ||
if (this._replaceInvalidCharsInCharData) { | ||
charData = validate_1.fixChar(charData); | ||
} | ||
else if (!validate_1.validateChar(charData)) { | ||
throw new Error("charData should not contain characters not allowed" | ||
+ " in XML"); | ||
else if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": character data" | ||
+ ("\"" + charData + "\" should not contain characters not") | ||
+ " allowed in XML"); | ||
} | ||
@@ -87,54 +68,6 @@ this._charData = charData; | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this character data. | ||
*/ | ||
XmlCharData.prototype.children = function () { | ||
throw new Error("XmlCharData nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCharData.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlCharData nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCharData.prototype.removeChild = function (node) { | ||
throw new Error("XmlCharData nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCharData} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCharData.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlCharData nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
XmlCharData.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
var str = this.charData; | ||
XmlCharData.prototype.toString = function () { | ||
var str = this._charData; | ||
str = escape_1.escapeAmpersands(str); | ||
@@ -145,4 +78,10 @@ str = escape_1.escapeLeftAngleBrackets(str); | ||
}; | ||
/** | ||
* Returns the parent of this character data. | ||
*/ | ||
XmlCharData.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
return XmlCharData; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlCharData; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,20 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML character reference. | ||
* 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. | ||
* | ||
* An XML character reference is structured as follows, where `{dec}` is the | ||
* A character reference is structured as follows, where `{dec}` is the | ||
* decimal representation code point corresponding to a particular Unicode | ||
@@ -45,85 +57,33 @@ * character: | ||
* should be used. | ||
* | ||
* XmlCharRef nodes cannot have any children. | ||
*/ | ||
export default class XmlCharRef extends XmlNode { | ||
export default class XmlCharRef<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _char; | ||
private _hex; | ||
constructor(parent: Parent, validation: boolean, options: IXmlCharRefOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlCharRef} class. | ||
* | ||
* @param char The character to represent using the reference. | ||
* @param hex Whether to use the hexadecimal or decimal representation for | ||
* the reference. If left undefined, decimal is the default. | ||
* Gets the character of this character reference. | ||
*/ | ||
constructor(char: string, hex?: boolean); | ||
/** | ||
* Gets the character to represent using the reference. | ||
* | ||
* @returns The character to represent using the reference. | ||
*/ | ||
/** | ||
* Sets the character to represent using the reference. | ||
* | ||
* @param char The character to represent using the reference. | ||
*/ | ||
* Sets the character of this character reference. | ||
*/ | ||
char: string; | ||
/** | ||
* Gets whether or not to use the hexadecimal or decimal representation for | ||
* the reference. | ||
* | ||
* @returns Whether or not to use the hexadecimal or decimal representation | ||
* for the reference. | ||
* Gets whether the decimal or hexadecimal representation should be used | ||
* for this character reference. | ||
*/ | ||
/** | ||
* Sets whether or not to use the hexadecimal or decimal representation for | ||
* the reference. | ||
* | ||
* @param hex Whether or not to use the hexadecimal or decimal | ||
* representation for the reference. | ||
*/ | ||
* Sets whether the decimal or hexadecimal representation should be used | ||
* for this character reference. | ||
*/ | ||
hex: boolean; | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this character reference. | ||
*/ | ||
children(): XmlNode[]; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this character reference. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML character reference. | ||
* Represents a character reference. | ||
* | ||
* An XML character reference is structured as follows, where `{dec}` is the | ||
* A character reference is structured as follows, where `{dec}` is the | ||
* decimal representation code point corresponding to a particular Unicode | ||
@@ -42,26 +46,16 @@ * character: | ||
* should be used. | ||
* | ||
* XmlCharRef nodes cannot have any children. | ||
*/ | ||
var XmlCharRef = (function (_super) { | ||
__extends(XmlCharRef, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlCharRef} class. | ||
* | ||
* @param char The character to represent using the reference. | ||
* @param hex Whether to use the hexadecimal or decimal representation for | ||
* the reference. If left undefined, decimal is the default. | ||
*/ | ||
function XmlCharRef(char, hex) { | ||
if (hex === void 0) { hex = false; } | ||
var _this = _super.call(this) || this; | ||
_this.char = char; | ||
_this.hex = hex; | ||
return _this; | ||
var XmlCharRef = /** @class */ (function () { | ||
function XmlCharRef(parent, validation, options) { | ||
this._hex = false; | ||
this._validation = validation; | ||
this._parent = parent; | ||
this.char = options.char; | ||
if (!validate_1.isUndefined(options.hex)) { | ||
this.hex = options.hex; | ||
} | ||
} | ||
Object.defineProperty(XmlCharRef.prototype, "char", { | ||
/** | ||
* Gets the character to represent using the reference. | ||
* | ||
* @returns The character to represent using the reference. | ||
* Gets the character of this character reference. | ||
*/ | ||
@@ -72,14 +66,10 @@ get: function () { | ||
/** | ||
* Sets the character to represent using the reference. | ||
* | ||
* @param char The character to represent using the reference. | ||
* Sets the character of this character reference. | ||
*/ | ||
set: function (char) { | ||
if (!utils_1.isString(char)) { | ||
throw new TypeError("char should be a string"); | ||
if (this._validation && !validate_1.validateSingleChar(char)) { | ||
throw new Error(error_1.getContext(this.up()) + ": character reference" | ||
+ (" \"" + char + "\" should reference a single character,") | ||
+ " and this character should be allowed in XML"); | ||
} | ||
else if (!validate_1.validateSingleChar(char)) { | ||
throw new Error("char should contain a single character, and this" | ||
+ " character should be allowed in XML"); | ||
} | ||
this._char = char; | ||
@@ -92,7 +82,4 @@ }, | ||
/** | ||
* Gets whether or not to use the hexadecimal or decimal representation for | ||
* the reference. | ||
* | ||
* @returns Whether or not to use the hexadecimal or decimal representation | ||
* for the reference. | ||
* Gets whether the decimal or hexadecimal representation should be used | ||
* for this character reference. | ||
*/ | ||
@@ -103,12 +90,6 @@ get: function () { | ||
/** | ||
* Sets whether or not to use the hexadecimal or decimal representation for | ||
* the reference. | ||
* | ||
* @param hex Whether or not to use the hexadecimal or decimal | ||
* representation for the reference. | ||
* Sets whether the decimal or hexadecimal representation should be used | ||
* for this character reference. | ||
*/ | ||
set: function (hex) { | ||
if (!utils_1.isBoolean(hex)) { | ||
throw new TypeError("hex should be a boolean"); | ||
} | ||
this._hex = hex; | ||
@@ -120,61 +101,27 @@ }, | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this character reference. | ||
*/ | ||
XmlCharRef.prototype.children = function () { | ||
throw new Error("XmlCharRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCharRef.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlCharRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCharRef.prototype.removeChild = function (node) { | ||
throw new Error("XmlCharRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlCharRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlCharRef.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlCharRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
XmlCharRef.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
XmlCharRef.prototype.toString = function () { | ||
var char; | ||
if (this.char.length === 1) { | ||
char = this.char.charCodeAt(0); | ||
if (this._char.length === 1) { | ||
char = this._char.charCodeAt(0); | ||
} | ||
else { | ||
char = utils_1.getCodePoint(this.char, 0); | ||
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(error_1.getContext(this.up()) + ": character" | ||
+ (" reference \"" + this.char + "\" should") | ||
+ " reference a valid Unicode character"); | ||
} | ||
} | ||
else { | ||
char = first; | ||
} | ||
} | ||
if (this.hex) { | ||
if (this._hex) { | ||
return "&#x" + char.toString(16) + ";"; | ||
@@ -186,4 +133,10 @@ } | ||
}; | ||
/** | ||
* Returns the parent of this character reference. | ||
*/ | ||
XmlCharRef.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
return XmlCharRef; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlCharRef; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,9 +16,21 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML comment. | ||
* 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. | ||
* | ||
* An XML character reference is structured as follows, where `{content}` is | ||
* the text of the comment. | ||
* A comment is structured as follows, where `{content}` is the text of the | ||
* comment: | ||
* | ||
@@ -28,69 +40,24 @@ * ```xml | ||
* ``` | ||
* | ||
* The `{content}` value is a property of this node. | ||
* | ||
* XmlComment nodes cannot have any children. | ||
*/ | ||
export default class XmlComment extends XmlNode { | ||
private _content; | ||
export default class XmlComment<Parent> { | ||
private readonly _replaceInvalidCharsInCharData; | ||
private readonly _parent; | ||
private readonly _validation; | ||
private _charData; | ||
constructor(parent: Parent, validation: boolean, options: IXmlCommentOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlComment} class. | ||
* | ||
* @param content The content of the comment. | ||
* Gets the text of this comment. | ||
*/ | ||
constructor(content: string); | ||
/** | ||
* Gets the content of the comment. | ||
* | ||
* @returns The content of the comment. | ||
*/ | ||
* Sets the text of this comment. | ||
*/ | ||
charData: string; | ||
/** | ||
* Sets the content of the comment. | ||
* | ||
* @param content The content of the comment. | ||
* Returns an XML string representation of this comment. | ||
*/ | ||
content: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this comment. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML comment. | ||
* Represents a comment. | ||
* | ||
* An XML character reference is structured as follows, where `{content}` is | ||
* the text of the comment. | ||
* A comment is structured as follows, where `{content}` is the text of the | ||
* comment: | ||
* | ||
@@ -25,46 +29,54 @@ * ```xml | ||
* ``` | ||
* | ||
* The `{content}` value is a property of this node. | ||
* | ||
* XmlComment nodes cannot have any children. | ||
*/ | ||
var XmlComment = (function (_super) { | ||
__extends(XmlComment, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlComment} class. | ||
* | ||
* @param content The content of the comment. | ||
*/ | ||
function XmlComment(content) { | ||
var _this = _super.call(this) || this; | ||
_this.content = content; | ||
return _this; | ||
var XmlComment = /** @class */ (function () { | ||
function XmlComment(parent, validation, options) { | ||
this._validation = validation; | ||
if (!validate_1.isUndefined(options.replaceInvalidCharsInCharData)) { | ||
this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData); | ||
} | ||
else { | ||
this._replaceInvalidCharsInCharData = false; | ||
} | ||
this._parent = parent; | ||
this.charData = options.charData; | ||
} | ||
Object.defineProperty(XmlComment.prototype, "content", { | ||
Object.defineProperty(XmlComment.prototype, "charData", { | ||
/** | ||
* Gets the content of the comment. | ||
* | ||
* @returns The content of the comment. | ||
* Gets the text of this comment. | ||
*/ | ||
get: function () { | ||
return this._content; | ||
return this._charData; | ||
}, | ||
/** | ||
* Sets the content of the comment. | ||
* | ||
* @param content The content of the comment. | ||
* Sets the text of this comment. | ||
*/ | ||
set: function (content) { | ||
if (!utils_1.isString(content)) { | ||
throw new TypeError("content should be a string"); | ||
set: function (charData) { | ||
if (this._replaceInvalidCharsInCharData) { | ||
charData = validate_1.fixChar(charData); | ||
} | ||
else if (!validate_1.validateChar(content)) { | ||
throw new Error("content should not contain characters" | ||
else if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": comment content" | ||
+ (" \"" + charData + "\" should not contain characters") | ||
+ " not allowed in XML"); | ||
} | ||
else if (!/^([^-]|-[^-])*$/.test(content)) { | ||
throw new Error("content should not contain the string '--' or" | ||
+ " end with '-'"); | ||
if (this._replaceInvalidCharsInCharData) { | ||
charData = charData.replace("--", "\uFFFD\uFFFD"); | ||
} | ||
this._content = content; | ||
else if (this._validation && charData.indexOf("--") !== -1) { | ||
throw new Error(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(error_1.getContext(this.up()) + ": comment content" | ||
+ (" \"" + charData + "\" should not end with the string") | ||
+ " '-'"); | ||
} | ||
this._charData = charData; | ||
}, | ||
@@ -75,57 +87,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this comment. | ||
*/ | ||
XmlComment.prototype.children = function () { | ||
throw new Error("XmlComment nodes cannot have children"); | ||
XmlComment.prototype.toString = function () { | ||
return "<!--" + this._charData + "-->"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this comment. | ||
*/ | ||
XmlComment.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlComment nodes cannot have children"); | ||
XmlComment.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlComment.prototype.removeChild = function (node) { | ||
throw new Error("XmlComment nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlComment} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlComment.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlComment nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlComment.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "<!--" + this.content + "-->"; | ||
}; | ||
return XmlComment; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlComment; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,29 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IDeclarationOptions, IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
import { IStringOptions } from "../options"; | ||
/** | ||
* Represents an XML declaration. | ||
* 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. | ||
* | ||
* An XML declaration is structured as follows, where `{version}` is the XML | ||
* A declaration is structured as follows, where `{version}` is the XML | ||
* version, `{encoding}` is the encoding of the document, and `{standalone}` | ||
@@ -30,102 +51,41 @@ * is either "yes" or "no", depending on whether the document may contain | ||
* ``` | ||
* | ||
* The `{version}`, `{encoding}`, and `{standalone}` values are properties of | ||
* this node. | ||
* | ||
* XmlDecl nodes cannot have any children. | ||
*/ | ||
export default class XmlDecl extends XmlNode { | ||
private _encoding?; | ||
private _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); | ||
/** | ||
* Initializes a new instance of the {@link XmlDecl} class. | ||
* | ||
* @param options The options associated with the XML declaration. | ||
* Gets the encoding associated with this declaration. | ||
*/ | ||
constructor(options?: IDeclarationOptions); | ||
/** | ||
* Gets the XML encoding to be included in the declaration. | ||
* | ||
* @returns The XML encoding to be included in the declaration. This value | ||
* may be undefined. | ||
*/ | ||
/** | ||
* Sets the XML encoding to be included in the declaration. | ||
* | ||
* @param encoding The XML encoding to be included in the declaration. This | ||
* value must be a valid encoding. If left undefined, no | ||
* encoding is included. | ||
*/ | ||
* Sets the encoding associated with this declaration. | ||
*/ | ||
encoding: string | undefined; | ||
/** | ||
* Gets the XML standalone attribute to be included in the declaration. | ||
* | ||
* @returns The XML standalone attribute to be included in the declaration. | ||
* This value may be undefined. | ||
* Gets the value of the standalone attribute associated with this | ||
* declaration. | ||
*/ | ||
/** | ||
* Sets the XML standalone attribute to be included in the declaration. | ||
* | ||
* @param standalone The XML standalone attribute to be included. This | ||
* value must be "yes" or "no". If left undefined, no | ||
* standalone attribute is included. | ||
*/ | ||
* Sets the value of the standalone attribute associated with this | ||
* declaration. | ||
*/ | ||
standalone: string | undefined; | ||
/** | ||
* Gets the XML version to be included in the declaration. | ||
* | ||
* @returns The XML version to tbe included in the declaration. | ||
* Gets the XML version associated with this declaration. | ||
*/ | ||
/** | ||
* Sets the XML version to be included in the declaration. | ||
* | ||
* @param version The XML version to be included in the declaration. This | ||
* value must be a valid XML version number. If left | ||
* undefined, the default version is "1.0". | ||
*/ | ||
* Sets the XML version associated with this declaration. | ||
*/ | ||
version: string; | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this declaration. | ||
*/ | ||
children(): XmlNode[]; | ||
toString(options?: IStringOptions): string; | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this declaration. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -28,9 +17,10 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var error_1 = require("../error"); | ||
var options_1 = require("../options"); | ||
var utils_1 = require("../utils"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
var validate_1 = require("../validate"); | ||
/** | ||
* Represents an XML declaration. | ||
* Represents a declaration. | ||
* | ||
* An XML declaration is structured as follows, where `{version}` is the XML | ||
* A declaration is structured as follows, where `{version}` is the XML | ||
* version, `{encoding}` is the encoding of the document, and `{standalone}` | ||
@@ -43,30 +33,17 @@ * is either "yes" or "no", depending on whether the document may contain | ||
* ``` | ||
* | ||
* The `{version}`, `{encoding}`, and `{standalone}` values are properties of | ||
* this node. | ||
* | ||
* XmlDecl nodes cannot have any children. | ||
*/ | ||
var XmlDecl = (function (_super) { | ||
__extends(XmlDecl, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDecl} class. | ||
* | ||
* @param options The options associated with the XML declaration. | ||
*/ | ||
function XmlDecl(options) { | ||
if (options === void 0) { options = {}; } | ||
var _this = _super.call(this) || this; | ||
var optionsObj = new options_1.DeclarationOptions(options); | ||
_this.encoding = optionsObj.encoding; | ||
_this.standalone = optionsObj.standalone; | ||
_this.version = optionsObj.version; | ||
return _this; | ||
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 (!validate_1.isUndefined(options.version)) { | ||
this.version = options.version; | ||
} | ||
} | ||
Object.defineProperty(XmlDecl.prototype, "encoding", { | ||
/** | ||
* Gets the XML encoding to be included in the declaration. | ||
* | ||
* @returns The XML encoding to be included in the declaration. This value | ||
* may be undefined. | ||
* Gets the encoding associated with this declaration. | ||
*/ | ||
@@ -77,17 +54,12 @@ get: function () { | ||
/** | ||
* Sets the XML encoding to be included in the declaration. | ||
* | ||
* @param encoding The XML encoding to be included in the declaration. This | ||
* value must be a valid encoding. If left undefined, no | ||
* encoding is included. | ||
* Sets the encoding associated with this declaration. | ||
*/ | ||
set: function (encoding) { | ||
if (utils_1.isString(encoding)) { | ||
if (!/^[A-Za-z][A-Za-z0-9._-]*$/.test(encoding)) { | ||
throw new Error("encoding should be a valid XML encoding"); | ||
if (this._validation && !validate_1.isUndefined(encoding)) { | ||
if (!validateEncoding(encoding)) { | ||
throw new Error(error_1.getContext(this.up()) + ": declaration" | ||
+ (" encoding attribute " + encoding + " should be a") | ||
+ " valid encoding"); | ||
} | ||
} | ||
else if (!utils_1.isUndefined(encoding)) { | ||
throw new TypeError("name should be a string or undefined"); | ||
} | ||
this._encoding = encoding; | ||
@@ -100,6 +72,4 @@ }, | ||
/** | ||
* Gets the XML standalone attribute to be included in the declaration. | ||
* | ||
* @returns The XML standalone attribute to be included in the declaration. | ||
* This value may be undefined. | ||
* Gets the value of the standalone attribute associated with this | ||
* declaration. | ||
*/ | ||
@@ -110,18 +80,13 @@ get: function () { | ||
/** | ||
* Sets the XML standalone attribute to be included in the declaration. | ||
* | ||
* @param standalone The XML standalone attribute to be included. This | ||
* value must be "yes" or "no". If left undefined, no | ||
* standalone attribute is included. | ||
* Sets the value of the standalone attribute associated with this | ||
* declaration. | ||
*/ | ||
set: function (standalone) { | ||
if (utils_1.isString(standalone)) { | ||
if (!/^(yes|no)$/.test(standalone)) { | ||
throw new Error("standalone should be either the string" | ||
+ " 'yes' or the string 'no'"); | ||
if (this._validation && !validate_1.isUndefined(standalone)) { | ||
if (standalone !== "yes" && standalone !== "no") { | ||
throw new Error(error_1.getContext(this.up()) + ": declaration" | ||
+ (" standalone attribute " + standalone + " should") | ||
+ " be the string 'yes' or the string 'no'"); | ||
} | ||
} | ||
else if (!utils_1.isUndefined(standalone)) { | ||
throw new TypeError("standalone should be a string or undefined"); | ||
} | ||
this._standalone = standalone; | ||
@@ -134,5 +99,3 @@ }, | ||
/** | ||
* Gets the XML version to be included in the declaration. | ||
* | ||
* @returns The XML version to tbe included in the declaration. | ||
* Gets the XML version associated with this declaration. | ||
*/ | ||
@@ -143,15 +106,10 @@ get: function () { | ||
/** | ||
* Sets the XML version to be included in the declaration. | ||
* | ||
* @param version The XML version to be included in the declaration. This | ||
* value must be a valid XML version number. If left | ||
* undefined, the default version is "1.0". | ||
* Sets the XML version associated with this declaration. | ||
*/ | ||
set: function (version) { | ||
if (!utils_1.isString(version)) { | ||
throw new TypeError("version should be a string"); | ||
if (this._validation && !validateVersion(version)) { | ||
throw new Error(error_1.getContext(this.up()) + ": declaration version" | ||
+ (" attribute " + version + " should be a valid XML") | ||
+ " version"); | ||
} | ||
else if (!/^1\.[0-9]+$/.test(version)) { | ||
throw new Error("version should be a valid XML version"); | ||
} | ||
this._version = version; | ||
@@ -163,51 +121,4 @@ }, | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this declaration. | ||
*/ | ||
XmlDecl.prototype.children = function () { | ||
throw new Error("XmlDecl nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDecl.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlDecl nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDecl.prototype.removeChild = function (node) { | ||
throw new Error("XmlDecl nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDecl} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDecl.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlDecl nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlDecl.prototype.toString = function (options) { | ||
@@ -217,8 +128,8 @@ if (options === void 0) { options = {}; } | ||
var quote = optionsObj.doubleQuotes ? '"' : "'"; | ||
var str = "<?xml version=" + quote + this.version + quote; | ||
if (utils_1.isString(this.encoding)) { | ||
str += " encoding=" + quote + this.encoding + quote; | ||
var str = "<?xml version=" + quote + this._version + quote; | ||
if (!validate_1.isUndefined(this._encoding)) { | ||
str += " encoding=" + quote + this._encoding + quote; | ||
} | ||
if (utils_1.isString(this.standalone)) { | ||
str += " standalone=" + quote + this.standalone + quote; | ||
if (!validate_1.isUndefined(this._standalone)) { | ||
str += " standalone=" + quote + this._standalone + quote; | ||
} | ||
@@ -228,4 +139,56 @@ str += "?>"; | ||
}; | ||
/** | ||
* Returns the parent of this declaration. | ||
*/ | ||
XmlDecl.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
return XmlDecl; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDecl; | ||
/** | ||
* Returns true if the specified encoding only contains characters permitted by | ||
* the XML specification. | ||
* | ||
* @private | ||
*/ | ||
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. | ||
* | ||
* @private | ||
*/ | ||
function validateVersion(str) { | ||
for (var i = 0; i <= 9; i++) { | ||
if (str === "1." + i) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,13 +16,22 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IDeclarationOptions, IStringOptions } from "../options"; | ||
import XmlComment from "./XmlComment"; | ||
import XmlDecl from "./XmlDecl"; | ||
import XmlDtd from "./XmlDtd"; | ||
import XmlElement from "./XmlElement"; | ||
import XmlNode from "./XmlNode"; | ||
import XmlProcInst from "./XmlProcInst"; | ||
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"; | ||
/** | ||
* Represents an XML document. | ||
* 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 XML document is structured as follows: | ||
* A sample document is structured as follows: | ||
* | ||
@@ -45,133 +54,45 @@ * ```xml | ||
* | ||
* Each component of the document, such as the XML declaration, document type | ||
* Each component of the document, such as the declaration, document type | ||
* definition, and root element, are children of this node. | ||
* | ||
* XmlDocument nodes must have exactly one {@link XmlElement} child, which is | ||
* the document's root element. | ||
* Documents must have exactly one element, which is the document's root | ||
* element. | ||
* | ||
* XmlDocument nodes can have exactly one {@link XmlDecl} and {@link XmlDtd} | ||
* child in that order, so long as they precede the {@link XmlElement} node. | ||
* Documents can have exactly one declaration and one document type definition | ||
* in that order, so long as they precede the element. | ||
* | ||
* XmlDocument nodes can have an unlimited number of {@link XmlComment} or | ||
* {@link XmlProcInst} nodes, so long as they follow the {@link XmlDecl} node, | ||
* if one exists. | ||
* 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 extends XmlNode { | ||
export default class XmlDocument { | ||
private readonly _children; | ||
private readonly _validation; | ||
constructor(options: IXmlDocumentOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlDocument} class. | ||
* | ||
* @param root The name of the root element. | ||
* Adds a comment to this document and returns the new comment. | ||
*/ | ||
constructor(root: string); | ||
comment(options: IXmlCommentOptions): XmlComment<this>; | ||
/** | ||
* Inserts a new comment at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the comment. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created element. | ||
* Adds a declaration to this document and returns the new declaration. | ||
*/ | ||
comment(content: string, index?: number): XmlComment; | ||
decl(options?: IXmlDeclOptions): XmlDecl<this>; | ||
/** | ||
* Inserts a new XML declaration at the beginning of this node's children. | ||
* | ||
* @param options The options associated with the XML declaration. | ||
* | ||
* @returns The newly created XML declaration. | ||
* Adds a document type definition to this document and returns the new | ||
* document type definition. | ||
*/ | ||
decl(options?: IDeclarationOptions): XmlDecl; | ||
dtd(options: IXmlDtdOptions): XmlDtd<this>; | ||
/** | ||
* Inserts a new XML document type definition. Unless a different index is | ||
* specified, the node is inserted immediately after the XML declaration | ||
* if one exists, or at the beginning of this node's children if one does | ||
* not. | ||
* | ||
* @param name The name of the DTD. | ||
* @param sysId The system identifier of the DTD, excluding quotation marks. | ||
* @param pubId The public identifier of the DTD, excluding quotation marks. | ||
* If a public identifier is provided, a system identifier | ||
* must be provided as well. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted immediately after the | ||
* XML declaration if one exists, or at the beginning of this | ||
* node's children if one does not. | ||
* | ||
* @returns The newly created XML document type definition. | ||
* Adds the root element to this document and returns the element. | ||
*/ | ||
dtd(name: string, sysId?: string, pubId?: string, index?: number): XmlDtd; | ||
element(options: IXmlElementOptions): XmlElement<this>; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Only {@link XmlComment}, {@link XmlDecl}, {@link XmlDtd}, or | ||
* {@link XmlProcInst} nodes can be inserted. Furthermore, {@link XmlDecl} | ||
* and {@link XmlDtd} nodes must be inserted in that order and must | ||
* precede the {@link XmlElement} node. In addition, {@link XmlComment} or | ||
* {@link XmlProcInst} nodes must follow the {@link XmlDecl} node. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after | ||
* the index are shifted to the right. If no index is | ||
* specified, the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Adds a processing instruction to this document and returns the new | ||
* processing instruction. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
procInst(options: IXmlProcInstOptions): XmlProcInst<this>; | ||
/** | ||
* Inserts a new processing instruction at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created processing instruction. | ||
* Returns an XML string representation of this document using the | ||
* specified options. | ||
*/ | ||
procInst(target: string, content?: string, index?: number): XmlProcInst; | ||
/** | ||
* Removes the specified node from this node's children. | ||
* | ||
* Note that {@link XmlElement} nodes cannot be removed from this node; | ||
* attempts to do so will result in an exception being thrown. | ||
* | ||
* @param node The node to remove. | ||
* | ||
* @returns Whether a node was removed. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Removes the node at the specified index from this node's children. | ||
* | ||
* Note that {@link XmlElement} nodes cannot be removed from this node; | ||
* attempts to do so will result in an exception being thrown. | ||
* | ||
* @param index The index at which the node to be removed is | ||
* located. | ||
* | ||
* @returns The node that was removed, or undefined if no node was removed. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns the root element of this document. | ||
* | ||
* @returns The root element of this document. | ||
*/ | ||
root(): XmlElement; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param {IStringOptions} [options] Formatting options for the string | ||
* representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -28,14 +17,17 @@ * Licensed under the Apache License, Version 2.0 (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 utils_1 = require("../utils"); | ||
var XmlComment_1 = require("./XmlComment"); | ||
var XmlDecl_1 = require("./XmlDecl"); | ||
var XmlDtd_1 = require("./XmlDtd"); | ||
var XmlElement_1 = require("./XmlElement"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
var XmlProcInst_1 = require("./XmlProcInst"); | ||
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 an XML document. | ||
* Represents a document. | ||
* | ||
* A sample XML document is structured as follows: | ||
* A sample document is structured as follows: | ||
* | ||
@@ -58,235 +50,95 @@ * ```xml | ||
* | ||
* Each component of the document, such as the XML declaration, document type | ||
* Each component of the document, such as the declaration, document type | ||
* definition, and root element, are children of this node. | ||
* | ||
* XmlDocument nodes must have exactly one {@link XmlElement} child, which is | ||
* the document's root element. | ||
* Documents must have exactly one element, which is the document's root | ||
* element. | ||
* | ||
* XmlDocument nodes can have exactly one {@link XmlDecl} and {@link XmlDtd} | ||
* child in that order, so long as they precede the {@link XmlElement} node. | ||
* Documents can have exactly one declaration and one document type definition | ||
* in that order, so long as they precede the element. | ||
* | ||
* XmlDocument nodes can have an unlimited number of {@link XmlComment} or | ||
* {@link XmlProcInst} nodes, so long as they follow the {@link XmlDecl} node, | ||
* if one exists. | ||
* Documents can have an unlimited number of comments or processing | ||
* instructions, so long as they follow the declaration, if one exists. | ||
*/ | ||
var XmlDocument = (function (_super) { | ||
__extends(XmlDocument, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDocument} class. | ||
* | ||
* @param root The name of the root element. | ||
*/ | ||
function XmlDocument(root) { | ||
var _this = _super.call(this) || this; | ||
_super.prototype.insertChild.call(_this, new XmlElement_1.default(root)); | ||
return _this; | ||
var XmlDocument = /** @class */ (function () { | ||
function XmlDocument(options) { | ||
this._children = []; | ||
this._validation = !validate_1.isUndefined(options.validation) | ||
? options.validation | ||
: true; | ||
} | ||
/** | ||
* Inserts a new comment at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the comment. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created element. | ||
* Adds a comment to this document and returns the new comment. | ||
*/ | ||
XmlDocument.prototype.comment = function (content, index) { | ||
if (index === void 0) { index = this._children.length; } | ||
var comment = new XmlComment_1.default(content); | ||
this.insertChild(comment, index); | ||
XmlDocument.prototype.comment = function (options) { | ||
var comment = new XmlComment_1.default(this, this._validation, options); | ||
this._children.push(comment); | ||
return comment; | ||
}; | ||
/** | ||
* Inserts a new XML declaration at the beginning of this node's children. | ||
* | ||
* @param options The options associated with the XML declaration. | ||
* | ||
* @returns The newly created XML declaration. | ||
* Adds a declaration to this document and returns the new declaration. | ||
*/ | ||
XmlDocument.prototype.decl = function (options) { | ||
var declaration = new XmlDecl_1.default(options); | ||
this.insertChild(declaration, 0); | ||
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; | ||
}; | ||
/** | ||
* Inserts a new XML document type definition. Unless a different index is | ||
* specified, the node is inserted immediately after the XML declaration | ||
* if one exists, or at the beginning of this node's children if one does | ||
* not. | ||
* | ||
* @param name The name of the DTD. | ||
* @param sysId The system identifier of the DTD, excluding quotation marks. | ||
* @param pubId The public identifier of the DTD, excluding quotation marks. | ||
* If a public identifier is provided, a system identifier | ||
* must be provided as well. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted immediately after the | ||
* XML declaration if one exists, or at the beginning of this | ||
* node's children if one does not. | ||
* | ||
* @returns The newly created XML document type definition. | ||
* Adds a document type definition to this document and returns the new | ||
* document type definition. | ||
*/ | ||
XmlDocument.prototype.dtd = function (name, sysId, pubId, index) { | ||
var dtd = new XmlDtd_1.default(name, sysId, pubId); | ||
if (utils_1.isUndefined(index)) { | ||
if (this._children[0] instanceof XmlDecl_1.default) { | ||
index = 1; | ||
} | ||
else { | ||
index = 0; | ||
} | ||
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"); | ||
} | ||
this.insertChild(dtd, index); | ||
var dtd = new XmlDtd_1.default(this, this._validation, options); | ||
this._children.push(dtd); | ||
return dtd; | ||
}; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Only {@link XmlComment}, {@link XmlDecl}, {@link XmlDtd}, or | ||
* {@link XmlProcInst} nodes can be inserted. Furthermore, {@link XmlDecl} | ||
* and {@link XmlDtd} nodes must be inserted in that order and must | ||
* precede the {@link XmlElement} node. In addition, {@link XmlComment} or | ||
* {@link XmlProcInst} nodes must follow the {@link XmlDecl} node. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after | ||
* the index are shifted to the right. If no index is | ||
* specified, the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Adds the root element to this document and returns the element. | ||
*/ | ||
XmlDocument.prototype.insertChild = function (node, index) { | ||
if (index === void 0) { index = this._children.length; } | ||
if (!(node instanceof XmlComment_1.default | ||
|| node instanceof XmlDecl_1.default | ||
|| node instanceof XmlDtd_1.default | ||
|| node instanceof XmlProcInst_1.default)) { | ||
throw new TypeError("node should be an instance of" | ||
+ " XmlComment, XmlDecl, XmlDtd, or" | ||
+ " XmlProcInst"); | ||
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"); | ||
} | ||
if (node instanceof XmlComment_1.default || node instanceof XmlProcInst_1.default) { | ||
if (this._children[0] instanceof XmlDecl_1.default) { | ||
if (index === 0) { | ||
throw new Error("XmlComment or XmlProcInst node should be" | ||
+ " inserted after the XmlDecl node"); | ||
} | ||
} | ||
} | ||
else if (node instanceof XmlDecl_1.default) { | ||
if (this._children[0] instanceof XmlDecl_1.default) { | ||
throw new Error("XmlDocument node should only contain one" | ||
+ " XmlDecl node"); | ||
} | ||
if (index !== 0) { | ||
throw new Error("XmlDecl node should be inserted at the" | ||
+ " beginning of an XmlDocument node"); | ||
} | ||
} | ||
else if (node instanceof XmlDtd_1.default) { | ||
if (this._children[0] instanceof XmlDecl_1.default) { | ||
if (index === 0) { | ||
throw new Error("XmlDtd node should be inserted after" | ||
+ " the XmlDecl node"); | ||
} | ||
} | ||
for (var i = 0; i < index && i < this._children.length; i++) { | ||
if (this._children[i] instanceof XmlElement_1.default) { | ||
throw new Error("XmlDtd node should be inserted before" | ||
+ " the XmlElement node"); | ||
} | ||
} | ||
for (var _i = 0, _a = this._children; _i < _a.length; _i++) { | ||
var child = _a[_i]; | ||
if (child instanceof XmlDtd_1.default) { | ||
throw new Error("XmlDocument node should only contain" | ||
+ " one XmlDtd node"); | ||
} | ||
} | ||
} | ||
return _super.prototype.insertChild.call(this, node, index); | ||
var element = new XmlElement_1.default(this, this._validation, options); | ||
this._children.push(element); | ||
return element; | ||
}; | ||
/** | ||
* Inserts a new processing instruction at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created processing instruction. | ||
* Adds a processing instruction to this document and returns the new | ||
* processing instruction. | ||
*/ | ||
XmlDocument.prototype.procInst = function (target, content, index) { | ||
if (index === void 0) { index = this._children.length; } | ||
var procInst = new XmlProcInst_1.default(target, content); | ||
this.insertChild(procInst, index); | ||
XmlDocument.prototype.procInst = function (options) { | ||
var procInst = new XmlProcInst_1.default(this, this._validation, options); | ||
this._children.push(procInst); | ||
return procInst; | ||
}; | ||
/** | ||
* Removes the specified node from this node's children. | ||
* | ||
* Note that {@link XmlElement} nodes cannot be removed from this node; | ||
* attempts to do so will result in an exception being thrown. | ||
* | ||
* @param node The node to remove. | ||
* | ||
* @returns Whether a node was removed. | ||
* Returns an XML string representation of this document using the | ||
* specified options. | ||
*/ | ||
XmlDocument.prototype.removeChild = function (node) { | ||
if (node instanceof XmlElement_1.default) { | ||
throw new Error("XmlElement nodes cannot be removed from" | ||
+ " XmlDocument nodes"); | ||
} | ||
return _super.prototype.removeChild.call(this, node); | ||
}; | ||
/** | ||
* Removes the node at the specified index from this node's children. | ||
* | ||
* Note that {@link XmlElement} nodes cannot be removed from this node; | ||
* attempts to do so will result in an exception being thrown. | ||
* | ||
* @param index The index at which the node to be removed is | ||
* located. | ||
* | ||
* @returns The node that was removed, or undefined if no node was removed. | ||
*/ | ||
XmlDocument.prototype.removeChildAtIndex = function (index) { | ||
if (this._children[index] instanceof XmlElement_1.default) { | ||
throw new Error("XmlElement nodes cannot be removed from" | ||
+ " XmlDocument nodes"); | ||
} | ||
return _super.prototype.removeChildAtIndex.call(this, index); | ||
}; | ||
/** | ||
* Returns the root element of this document. | ||
* | ||
* @returns The root element of this document. | ||
*/ | ||
XmlDocument.prototype.root = function () { | ||
for (var _i = 0, _a = this._children; _i < _a.length; _i++) { | ||
var node = _a[_i]; | ||
if (node instanceof XmlElement_1.default) { | ||
return node; | ||
} | ||
} | ||
throw new Error("XmlDocument does not contain a root node"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param {IStringOptions} [options] Formatting options for the string | ||
* representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
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); | ||
@@ -296,3 +148,10 @@ var str = ""; | ||
var node = _a[_i]; | ||
str += node.toString(options); | ||
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) { | ||
@@ -309,3 +168,3 @@ str += optionsObj.newline; | ||
return XmlDocument; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDocument; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -17,14 +17,33 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
import { IStringOptions } from "../options"; | ||
import XmlComment from "./XmlComment"; | ||
import XmlDtdAttlist from "./XmlDtdAttlist"; | ||
import XmlDtdElement from "./XmlDtdElement"; | ||
import XmlDtdEntity from "./XmlDtdEntity"; | ||
import XmlDtdNotation from "./XmlDtdNotation"; | ||
import XmlDtdParamEntityRef from "./XmlDtdParamEntityRef"; | ||
import XmlNode from "./XmlNode"; | ||
import XmlProcInst from "./XmlProcInst"; | ||
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). | ||
* | ||
* An XML document type definition is structured as follows, where `{name}` is | ||
* A document type definition is structured as follows, where `{name}` is | ||
* the name of the DTD, `{sysId}` is the system identifier of the DTD, | ||
@@ -40,177 +59,90 @@ * `{pubId}` is the public identifier of the DTD, and `{intSubset}` is the | ||
* | ||
* The `{name}`, `{pubId}`, and `{sysId}` values are properties of the node, | ||
* while the `{intSubset}` value consists of the children of this node. | ||
* | ||
* XmlDtd nodes can have an unlimited number of {@link XmlComment}, | ||
* {@link XmlDtdAttlist}, {@link XmlDtdElement}, {@link XmlDtdEntity}, | ||
* {@link XmlDtdNotation}, {@link XmlDtdParamEntityRef}, and | ||
* {@link XmlProcInst} nodes. | ||
* 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 extends XmlNode { | ||
export default class XmlDtd<Parent> { | ||
private readonly _children; | ||
private readonly _parent; | ||
private _name; | ||
private _sysId?; | ||
private _pubId?; | ||
private readonly _validation; | ||
private _pubId; | ||
private _sysId; | ||
constructor(parent: Parent, validation: boolean, options: IXmlDtdOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtd} class. | ||
* | ||
* @param name The name of the DTD. | ||
* @param sysId The system identifier of the DTD, excluding quotation marks. | ||
* @param pubId The public identifier of the DTD, excluding quotation marks. | ||
* If a public identifier is provided, a system identifier | ||
* must be provided as well. | ||
*/ | ||
constructor(name: string, sysId?: string, pubId?: string); | ||
/** | ||
* Gets the name of the DTD. | ||
* | ||
* @returns The name of the DTD. | ||
*/ | ||
/** | ||
* Sets the name of the DTD. | ||
* | ||
* @param name The name of the DTD. | ||
*/ | ||
* Sets the name of the DTD. | ||
*/ | ||
name: string; | ||
/** | ||
* Gets the public identifier of the DTD, excluding quotation marks. | ||
* | ||
* @returns The public identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
* Gets the public identifier of the DTD. | ||
*/ | ||
/** | ||
* Sets the public identifier of the DTD, excluding quotation marks. If a | ||
* public identifier is provided, a system identifier must be provided as | ||
* well. | ||
* | ||
* @param pubId The public identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
*/ | ||
* Sets the public identifier of the DTD. | ||
*/ | ||
pubId: string | undefined; | ||
/** | ||
* Gets the system identifier of the DTD, excluding quotation marks. | ||
* | ||
* @returns The system identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
* Gets the system identifier of the DTD. | ||
*/ | ||
/** | ||
* Sets the system identifier of the DTD, excluding quotation marks. | ||
* | ||
* @param sysId The system identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
*/ | ||
* Sets the system identifier of the DTD. | ||
*/ | ||
sysId: string | undefined; | ||
/** | ||
* Inserts a new attribute-list declaration at the specified index. If no | ||
* index is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @param text The text of the attribute-list declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created attribute-list declaration. | ||
* Adds an attribute-list declaration to this document type declaration | ||
* and returns the new attribute-list declaration. | ||
*/ | ||
attlist(text: string, index?: number): XmlDtdAttlist; | ||
attlist(options: IXmlDtdAttlistOptions): XmlDtdAttlist<this>; | ||
/** | ||
* Inserts a new comment at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the comment. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created comment. | ||
* Adds a comment to this document type declaration and returns the | ||
* new comment. | ||
*/ | ||
comment(content: string, index?: number): XmlComment; | ||
comment(options: IXmlCommentOptions): XmlComment<this>; | ||
/** | ||
* Inserts a new element declaration at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param text The text of the element declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created element declaration. | ||
* Adds an element declaration to this document type declaration | ||
* and returns the new element declaration. | ||
*/ | ||
element(text: string, index?: number): XmlDtdElement; | ||
element(options: IXmlDtdElementOptions): XmlDtdElement<this>; | ||
/** | ||
* Inserts a new entity declaration at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param text The text of the entity declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created entity declaration. | ||
* Adds an entity declaration to this document type declaration | ||
* and returns the new entity declaration. | ||
*/ | ||
entity(text: string, index?: number): XmlDtdEntity; | ||
entity(options: IXmlDtdEntityOptions): XmlDtdEntity<this>; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Only {@link XmlComment}, {@link XmlDtdAttlist}, {@link XmlDtdElement}, | ||
* {@link XmlDtdEntity}, {@link XmlDtdNotation}, and {@link XmlProcInst} | ||
* nodes can be inserted; otherwise an exception will be thrown. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after | ||
* the index are shifted to the right. If no index is | ||
* specified, the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Adds a notation declaration to this document type declaration | ||
* and returns the new notation declaration. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
notation(options: IXmlDtdNotationOptions): XmlDtdNotation<this>; | ||
/** | ||
* Inserts a new notation declaration at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param text The text of the notation declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created notation declaration. | ||
* Adds a parameter entity reference to this document type declaration | ||
* and returns the new parameter entity reference. | ||
*/ | ||
notation(text: string, index?: number): XmlDtdNotation; | ||
paramEntityRef(options: IXmlDtdParamEntityRefOptions): XmlDtdParamEntityRef<this>; | ||
/** | ||
* Inserts a new parameter entity reference at the specified index. If no | ||
* index is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @param entity The entity to reference. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created parameter entity reference. | ||
* Adds a processing instruction to this document type declaration | ||
* and returns the new processing instruction. | ||
*/ | ||
paramEntityRef(entity: string, index?: number): XmlDtdParamEntityRef; | ||
procInst(options: IXmlProcInstOptions): XmlProcInst<this>; | ||
/** | ||
* Inserts a new processing instruction at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created processing instruction. | ||
* Returns an XML string representation of this document type declaration. | ||
*/ | ||
procInst(target: string, content?: string, index?: number): XmlProcInst; | ||
toString(options?: IStringOptions): string; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
* Returns the parent of this attribute. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
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. | ||
* | ||
* @private | ||
*/ | ||
export declare function validatePubId(str: string): boolean; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -28,17 +17,20 @@ * Licensed under the Apache License, Version 2.0 (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 utils_1 = require("../utils"); | ||
var validate_1 = require("../validate"); | ||
var XmlComment_1 = require("./XmlComment"); | ||
var XmlDtdAttlist_1 = require("./XmlDtdAttlist"); | ||
var XmlDtdElement_1 = require("./XmlDtdElement"); | ||
var XmlDtdEntity_1 = require("./XmlDtdEntity"); | ||
var XmlDtdNotation_1 = require("./XmlDtdNotation"); | ||
var XmlDtdParamEntityRef_1 = require("./XmlDtdParamEntityRef"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
var XmlProcInst_1 = require("./XmlProcInst"); | ||
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). | ||
* | ||
* An XML document type definition is structured as follows, where `{name}` is | ||
* A document type definition is structured as follows, where `{name}` is | ||
* the name of the DTD, `{sysId}` is the system identifier of the DTD, | ||
@@ -54,27 +46,20 @@ * `{pubId}` is the public identifier of the DTD, and `{intSubset}` is the | ||
* | ||
* The `{name}`, `{pubId}`, and `{sysId}` values are properties of the node, | ||
* while the `{intSubset}` value consists of the children of this node. | ||
* | ||
* XmlDtd nodes can have an unlimited number of {@link XmlComment}, | ||
* {@link XmlDtdAttlist}, {@link XmlDtdElement}, {@link XmlDtdEntity}, | ||
* {@link XmlDtdNotation}, {@link XmlDtdParamEntityRef}, and | ||
* {@link XmlProcInst} nodes. | ||
* 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 = (function (_super) { | ||
__extends(XmlDtd, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtd} class. | ||
* | ||
* @param name The name of the DTD. | ||
* @param sysId The system identifier of the DTD, excluding quotation marks. | ||
* @param pubId The public identifier of the DTD, excluding quotation marks. | ||
* If a public identifier is provided, a system identifier | ||
* must be provided as well. | ||
*/ | ||
function XmlDtd(name, sysId, pubId) { | ||
var _this = _super.call(this) || this; | ||
_this.name = name; | ||
_this.sysId = sysId; | ||
_this.pubId = pubId; | ||
return _this; | ||
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 (!validate_1.isUndefined(options.sysId)) { | ||
this.sysId = options.sysId; | ||
} | ||
if (!validate_1.isUndefined(options.pubId)) { | ||
this.pubId = options.pubId; | ||
} | ||
} | ||
@@ -84,4 +69,2 @@ Object.defineProperty(XmlDtd.prototype, "name", { | ||
* Gets the name of the DTD. | ||
* | ||
* @returns The name of the DTD. | ||
*/ | ||
@@ -93,13 +76,9 @@ get: function () { | ||
* Sets the name of the DTD. | ||
* | ||
* @param name The name of the DTD. | ||
*/ | ||
set: function (name) { | ||
if (!utils_1.isString(name)) { | ||
throw new TypeError("name should be a string"); | ||
if (this._validation && !validate_1.validateName(name)) { | ||
throw new Error(error_1.getContext(this.up()) + ": DTD name \"" + name + "\"" | ||
+ " should not contain characters not allowed in" | ||
+ " XML names"); | ||
} | ||
else if (!validate_1.validateName(name)) { | ||
throw new Error("name should not contain characters not" | ||
+ " allowed in XML names"); | ||
} | ||
this._name = name; | ||
@@ -112,6 +91,3 @@ }, | ||
/** | ||
* Gets the public identifier of the DTD, excluding quotation marks. | ||
* | ||
* @returns The public identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
* Gets the public identifier of the DTD. | ||
*/ | ||
@@ -122,24 +98,18 @@ get: function () { | ||
/** | ||
* Sets the public identifier of the DTD, excluding quotation marks. If a | ||
* public identifier is provided, a system identifier must be provided as | ||
* well. | ||
* | ||
* @param pubId The public identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
* Sets the public identifier of the DTD. | ||
*/ | ||
set: function (pubId) { | ||
if (utils_1.isString(pubId)) { | ||
if (!/^(\u0020|\u000D|\u000A|[a-zA-Z0-9]|[-'()+,./:=?;!*#@$_%])*$/ | ||
.test(pubId)) { | ||
throw new Error("pubId should not contain characters not" | ||
+ " allowed in public identifiers"); | ||
if (!validate_1.isUndefined(pubId)) { | ||
if (this._validation && !validatePubId(pubId)) { | ||
throw new Error(error_1.getContext(this.up()) + ": DTD public" | ||
+ (" identifier \"" + pubId + "\" should not contain") | ||
+ " characters not allowed in public" | ||
+ " identifiers"); | ||
} | ||
else if (utils_1.isUndefined(this.sysId)) { | ||
throw new Error("pubId should not be defined if sysId is" | ||
+ " undefined"); | ||
if (this._validation && validate_1.isUndefined(this._sysId)) { | ||
throw new Error(error_1.getContext(this.up()) + ": DTD public" | ||
+ (" identifier \"" + pubId + "\" should not be defined") | ||
+ " if system identifier is undefined"); | ||
} | ||
} | ||
else if (!utils_1.isUndefined(pubId)) { | ||
throw new TypeError("pubId should be a string or undefined"); | ||
} | ||
this._pubId = pubId; | ||
@@ -152,6 +122,3 @@ }, | ||
/** | ||
* Gets the system identifier of the DTD, excluding quotation marks. | ||
* | ||
* @returns The system identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
* Gets the system identifier of the DTD. | ||
*/ | ||
@@ -162,28 +129,19 @@ get: function () { | ||
/** | ||
* Sets the system identifier of the DTD, excluding quotation marks. | ||
* | ||
* @param sysId The system identifier of the DTD, excluding quotation marks. | ||
* This value may be undefined. | ||
* Sets the system identifier of the DTD. | ||
*/ | ||
set: function (sysId) { | ||
if (utils_1.isString(sysId)) { | ||
if (!validate_1.validateChar(sysId)) { | ||
throw new Error("sysId should not contain characters not" | ||
+ " allowed in XML"); | ||
if (!validate_1.isUndefined(sysId)) { | ||
if (this._validation && !validate_1.validateChar(sysId)) { | ||
throw new Error(error_1.getContext(this.up()) + ": DTD system" | ||
+ (" identifier \"" + sysId + "\" should not contain") | ||
+ " characters not allowed in XML"); | ||
} | ||
else if (sysId.indexOf("'") !== -1 && | ||
sysId.indexOf("\"") !== -1) { | ||
throw new Error("sysId should not contain both single quotes" | ||
+ " and double quotes"); | ||
else if (this._validation | ||
&& sysId.indexOf("'") !== -1 | ||
&& sysId.indexOf("\"") !== -1) { | ||
throw new Error(error_1.getContext(this.up()) + ": DTD system" | ||
+ (" identifier \"" + sysId + "\" should not contain") | ||
+ " both single quotes and double quotes"); | ||
} | ||
} | ||
else if (utils_1.isUndefined(sysId)) { | ||
if (!utils_1.isUndefined(this.pubId)) { | ||
throw new Error("sysId should not be undefined if pubId is" | ||
+ " defined"); | ||
} | ||
} | ||
else { | ||
throw new TypeError("sysId should be a string or undefined"); | ||
} | ||
this._sysId = sysId; | ||
@@ -195,153 +153,66 @@ }, | ||
/** | ||
* Inserts a new attribute-list declaration at the specified index. If no | ||
* index is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @param text The text of the attribute-list declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created attribute-list declaration. | ||
* Adds an attribute-list declaration to this document type declaration | ||
* and returns the new attribute-list declaration. | ||
*/ | ||
XmlDtd.prototype.attlist = function (text, index) { | ||
var attlist = new XmlDtdAttlist_1.default(text); | ||
this.insertChild(attlist, index); | ||
XmlDtd.prototype.attlist = function (options) { | ||
var attlist = new XmlDtdAttlist_1.default(this, this._validation, options); | ||
this._children.push(attlist); | ||
return attlist; | ||
}; | ||
/** | ||
* Inserts a new comment at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the comment. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created comment. | ||
* Adds a comment to this document type declaration and returns the | ||
* new comment. | ||
*/ | ||
XmlDtd.prototype.comment = function (content, index) { | ||
var comment = new XmlComment_1.default(content); | ||
this.insertChild(comment, index); | ||
XmlDtd.prototype.comment = function (options) { | ||
var comment = new XmlComment_1.default(this, this._validation, options); | ||
this._children.push(comment); | ||
return comment; | ||
}; | ||
/** | ||
* Inserts a new element declaration at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param text The text of the element declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created element declaration. | ||
* Adds an element declaration to this document type declaration | ||
* and returns the new element declaration. | ||
*/ | ||
XmlDtd.prototype.element = function (text, index) { | ||
var element = new XmlDtdElement_1.default(text); | ||
this.insertChild(element, index); | ||
XmlDtd.prototype.element = function (options) { | ||
var element = new XmlDtdElement_1.default(this, this._validation, options); | ||
this._children.push(element); | ||
return element; | ||
}; | ||
/** | ||
* Inserts a new entity declaration at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param text The text of the entity declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created entity declaration. | ||
* Adds an entity declaration to this document type declaration | ||
* and returns the new entity declaration. | ||
*/ | ||
XmlDtd.prototype.entity = function (text, index) { | ||
var entity = new XmlDtdEntity_1.default(text); | ||
this.insertChild(entity, index); | ||
XmlDtd.prototype.entity = function (options) { | ||
var entity = new XmlDtdEntity_1.default(this, this._validation, options); | ||
this._children.push(entity); | ||
return entity; | ||
}; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Only {@link XmlComment}, {@link XmlDtdAttlist}, {@link XmlDtdElement}, | ||
* {@link XmlDtdEntity}, {@link XmlDtdNotation}, and {@link XmlProcInst} | ||
* nodes can be inserted; otherwise an exception will be thrown. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after | ||
* the index are shifted to the right. If no index is | ||
* specified, the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Adds a notation declaration to this document type declaration | ||
* and returns the new notation declaration. | ||
*/ | ||
XmlDtd.prototype.insertChild = function (node, index) { | ||
if (!(node instanceof XmlComment_1.default || node instanceof XmlDtdAttlist_1.default || | ||
node instanceof XmlDtdElement_1.default || node instanceof XmlDtdEntity_1.default || | ||
node instanceof XmlDtdNotation_1.default || | ||
node instanceof XmlDtdParamEntityRef_1.default || | ||
node instanceof XmlProcInst_1.default)) { | ||
throw new TypeError("node should be an instance of XmlComment," | ||
+ " XmlDtdAttlist, XmlDtdElement, XmlDtdEntity," | ||
+ " XmlDtdNotation, XmlDtdParamEntityRef, or" | ||
+ " XmlProcInst"); | ||
} | ||
return _super.prototype.insertChild.call(this, node, index); | ||
}; | ||
/** | ||
* Inserts a new notation declaration at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param text The text of the notation declaration. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created notation declaration. | ||
*/ | ||
XmlDtd.prototype.notation = function (text, index) { | ||
var notation = new XmlDtdNotation_1.default(text); | ||
this.insertChild(notation, index); | ||
XmlDtd.prototype.notation = function (options) { | ||
var notation = new XmlDtdNotation_1.default(this, this._validation, options); | ||
this._children.push(notation); | ||
return notation; | ||
}; | ||
/** | ||
* Inserts a new parameter entity reference at the specified index. If no | ||
* index is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @param entity The entity to reference. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created parameter entity reference. | ||
* Adds a parameter entity reference to this document type declaration | ||
* and returns the new parameter entity reference. | ||
*/ | ||
XmlDtd.prototype.paramEntityRef = function (entity, index) { | ||
var paramEntity = new XmlDtdParamEntityRef_1.default(entity); | ||
this.insertChild(paramEntity, index); | ||
XmlDtd.prototype.paramEntityRef = function (options) { | ||
var paramEntity = new XmlDtdParamEntityRef_1.default(this, this._validation, options); | ||
this._children.push(paramEntity); | ||
return paramEntity; | ||
}; | ||
/** | ||
* Inserts a new processing instruction at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created processing instruction. | ||
* Adds a processing instruction to this document type declaration | ||
* and returns the new processing instruction. | ||
*/ | ||
XmlDtd.prototype.procInst = function (target, content, index) { | ||
var procInst = new XmlProcInst_1.default(target, content); | ||
this.insertChild(procInst, index); | ||
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 node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
* Returns an XML string representation of this document type declaration. | ||
*/ | ||
@@ -351,13 +222,17 @@ XmlDtd.prototype.toString = function (options) { | ||
var optionsObj = new options_1.StringOptions(options); | ||
var str = "<!DOCTYPE " + this.name; | ||
if (utils_1.isUndefined(this.pubId)) { | ||
if (!utils_1.isUndefined(this.sysId)) { | ||
var str = "<!DOCTYPE " + this._name; | ||
if (validate_1.isUndefined(this._pubId)) { | ||
if (!validate_1.isUndefined(this._sysId)) { | ||
str += " "; | ||
str = appendId("SYSTEM", this.sysId, str, optionsObj); | ||
str = this.appendId("SYSTEM", this._sysId, str, optionsObj); | ||
} | ||
} | ||
else { | ||
if (validate_1.isUndefined(this._sysId)) { | ||
throw new Error(error_1.getContext(this.up()) + ": DTD system" | ||
+ " identifier is not undefined"); | ||
} | ||
str += " "; | ||
str = appendId("PUBLIC", this.pubId, str, optionsObj); | ||
str = appendId("", this.sysId, str, optionsObj); | ||
str = this.appendId("PUBLIC", this._pubId, str, optionsObj); | ||
str = this.appendId("", this._sysId, str, optionsObj); | ||
} | ||
@@ -371,3 +246,3 @@ if (this._children.length !== 0) { | ||
} | ||
str += node.toString(options); | ||
str += node.toString(); | ||
} | ||
@@ -384,35 +259,67 @@ if (optionsObj.pretty) { | ||
}; | ||
/** | ||
* 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(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(error_1.getContext(this) + ": doubleQuotes option" | ||
+ " inconsistent with DTD system identifier or" | ||
+ " public identifier"); | ||
} | ||
str += "'" + value + "'"; | ||
} | ||
return str; | ||
}; | ||
return XmlDtd; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDtd; | ||
/** | ||
* Appends the XML string representation of a public or system identifier to | ||
* an existing string. | ||
* Returns true if the specified public identifier only contains characters | ||
* permitted by the XML specification. | ||
* | ||
* @param type "SYSTEM", "PUBLIC", or "". | ||
* @param value The value of the identifier. | ||
* @param str The string to which the string representation should be appended. | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns The updated string. | ||
* | ||
* @private | ||
*/ | ||
function appendId(type, value, str, options) { | ||
str += type + " "; | ||
if (options.doubleQuotes) { | ||
if (value.indexOf("\"") !== -1) { | ||
throw new Error("options.doubleQuotes inconsistent with" | ||
+ " sysId or pubId"); | ||
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; | ||
} | ||
str += "\"" + value + "\""; | ||
} | ||
else { | ||
if (value.indexOf("'") !== -1) { | ||
throw new Error("options.doubleQuotes inconsistent with" | ||
+ " sysId or pubId"); | ||
if (i + 1 === str.length) { | ||
return false; | ||
} | ||
str += "'" + value + "'"; | ||
return false; | ||
} | ||
return str; | ||
return true; | ||
} | ||
exports.validatePubId = validatePubId; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML attribute-list declaration in a document type definition. | ||
* 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 XML attribute-list declaration is structured as follows, where `{text}` | ||
* An attribute-list declaration is structured as follows, where `{text}` | ||
* is the text of the declaration: | ||
@@ -28,69 +35,23 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdAttlist nodes cannot have any children. | ||
*/ | ||
export default class XmlDtdAttlist extends XmlNode { | ||
private _text; | ||
export default class XmlDtdAttlist<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _charData; | ||
constructor(parent: Parent, validation: boolean, options: IXmlDtdAttlistOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdAttlist} class. | ||
* | ||
* @param text The text associated with the XML attribute-list declaration. | ||
* Gets the text of this entity declaration. | ||
*/ | ||
constructor(text: string); | ||
/** | ||
* Gets the text associated with the XML attribute-list declaration. | ||
* | ||
* @return The text associated with the XML attribute-list declaration. | ||
*/ | ||
* Sets the text of this entity declaration. | ||
*/ | ||
charData: string; | ||
/** | ||
* Sets the text associated with the XML attribute-list declaration. | ||
* | ||
* @param text The text associated with the XML attribute-list declaration. | ||
* Returns an XML string representation of this entity declaration. | ||
*/ | ||
text: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this entity declaration. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML attribute-list declaration in a document type definition. | ||
* Represents an attribute-list declaration in a document type definition. | ||
* | ||
* An XML attribute-list declaration is structured as follows, where `{text}` | ||
* An attribute-list declaration is structured as follows, where `{text}` | ||
* is the text of the declaration: | ||
@@ -25,42 +29,26 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdAttlist nodes cannot have any children. | ||
*/ | ||
var XmlDtdAttlist = (function (_super) { | ||
__extends(XmlDtdAttlist, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdAttlist} class. | ||
* | ||
* @param text The text associated with the XML attribute-list declaration. | ||
*/ | ||
function XmlDtdAttlist(text) { | ||
var _this = _super.call(this) || this; | ||
_this.text = text; | ||
return _this; | ||
var XmlDtdAttlist = /** @class */ (function () { | ||
function XmlDtdAttlist(parent, validation, options) { | ||
this._validation = validation; | ||
this._parent = parent; | ||
this.charData = options.charData; | ||
} | ||
Object.defineProperty(XmlDtdAttlist.prototype, "text", { | ||
Object.defineProperty(XmlDtdAttlist.prototype, "charData", { | ||
/** | ||
* Gets the text associated with the XML attribute-list declaration. | ||
* | ||
* @return The text associated with the XML attribute-list declaration. | ||
* Gets the text of this entity declaration. | ||
*/ | ||
get: function () { | ||
return this._text; | ||
return this._charData; | ||
}, | ||
/** | ||
* Sets the text associated with the XML attribute-list declaration. | ||
* | ||
* @param text The text associated with the XML attribute-list declaration. | ||
* Sets the text of this entity declaration. | ||
*/ | ||
set: function (text) { | ||
if (!utils_1.isString(text)) { | ||
throw new TypeError("text should be a string"); | ||
set: function (charData) { | ||
if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": attribute-list" | ||
+ (" declaration \"" + charData + "\" should not contain") | ||
+ " characters not allowed in XML"); | ||
} | ||
else if (!validate_1.validateChar(text)) { | ||
throw new Error("data should not contain characters" | ||
+ " not allowed in XML"); | ||
} | ||
this._text = text; | ||
this._charData = charData; | ||
}, | ||
@@ -71,57 +59,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this entity declaration. | ||
*/ | ||
XmlDtdAttlist.prototype.children = function () { | ||
throw new Error("XmlDtdAttlist nodes cannot have children"); | ||
XmlDtdAttlist.prototype.toString = function () { | ||
return "<!ATTLIST " + this._charData + ">"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this entity declaration. | ||
*/ | ||
XmlDtdAttlist.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlDtdAttlist nodes cannot have children"); | ||
XmlDtdAttlist.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdAttlist.prototype.removeChild = function (node) { | ||
throw new Error("XmlDtdAttlist nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdAttlist} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdAttlist.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlDtdAttlist nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlDtdAttlist.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "<!ATTLIST " + this.text + ">"; | ||
}; | ||
return XmlDtdAttlist; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDtdAttlist; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML element declaration in a document type definition. | ||
* 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 XML element declaration is structured as follows, where `{text}` is the | ||
* An element declaration is structured as follows, where `{text}` is the | ||
* text of the declaration: | ||
@@ -28,69 +35,23 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdElement nodes cannot have any children. | ||
*/ | ||
export default class XmlDtdElement extends XmlNode { | ||
private _text; | ||
export default class XmlDtdElement<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _charData; | ||
constructor(parent: Parent, validation: boolean, options: IXmlDtdElementOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdElement} class. | ||
* | ||
* @param text The text associated with the XML element declaration. | ||
* Gets the text of this element declaration. | ||
*/ | ||
constructor(text: string); | ||
/** | ||
* Gets the text associated with the XML element declaration. | ||
* | ||
* @return The text associated with the XML element declaration. | ||
*/ | ||
* Sets the text of this element declaration. | ||
*/ | ||
charData: string; | ||
/** | ||
* Sets the text associated with the XML element declaration. | ||
* | ||
* @param text The text associated with the XML element declaration. | ||
* Returns an XML string representation of this element declaration. | ||
*/ | ||
text: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this element declaration. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML element declaration in a document type definition. | ||
* Represents an element declaration in a document type definition. | ||
* | ||
* An XML element declaration is structured as follows, where `{text}` is the | ||
* An element declaration is structured as follows, where `{text}` is the | ||
* text of the declaration: | ||
@@ -25,42 +29,26 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdElement nodes cannot have any children. | ||
*/ | ||
var XmlDtdElement = (function (_super) { | ||
__extends(XmlDtdElement, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdElement} class. | ||
* | ||
* @param text The text associated with the XML element declaration. | ||
*/ | ||
function XmlDtdElement(text) { | ||
var _this = _super.call(this) || this; | ||
_this.text = text; | ||
return _this; | ||
var XmlDtdElement = /** @class */ (function () { | ||
function XmlDtdElement(parent, validation, options) { | ||
this._validation = validation; | ||
this._parent = parent; | ||
this.charData = options.charData; | ||
} | ||
Object.defineProperty(XmlDtdElement.prototype, "text", { | ||
Object.defineProperty(XmlDtdElement.prototype, "charData", { | ||
/** | ||
* Gets the text associated with the XML element declaration. | ||
* | ||
* @return The text associated with the XML element declaration. | ||
* Gets the text of this element declaration. | ||
*/ | ||
get: function () { | ||
return this._text; | ||
return this._charData; | ||
}, | ||
/** | ||
* Sets the text associated with the XML element declaration. | ||
* | ||
* @param text The text associated with the XML element declaration. | ||
* Sets the text of this element declaration. | ||
*/ | ||
set: function (text) { | ||
if (!utils_1.isString(text)) { | ||
throw new TypeError("text should be a string"); | ||
} | ||
else if (!validate_1.validateChar(text)) { | ||
throw new Error("data should not contain characters" | ||
set: function (charData) { | ||
if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": element declaration" | ||
+ (" \"" + charData + "\" should not contain characters") | ||
+ " not allowed in XML"); | ||
} | ||
this._text = text; | ||
this._charData = charData; | ||
}, | ||
@@ -71,57 +59,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this element declaration. | ||
*/ | ||
XmlDtdElement.prototype.children = function () { | ||
throw new Error("XmlDtdElement nodes cannot have children"); | ||
XmlDtdElement.prototype.toString = function () { | ||
return "<!ELEMENT " + this._charData + ">"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this element declaration. | ||
*/ | ||
XmlDtdElement.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlDtdElement nodes cannot have children"); | ||
XmlDtdElement.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdElement.prototype.removeChild = function (node) { | ||
throw new Error("XmlDtdElement nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdElement} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdElement.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlDtdElement nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlDtdElement.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "<!ELEMENT " + this.text + ">"; | ||
}; | ||
return XmlDtdElement; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDtdElement; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML entity declaration in a document type definition. | ||
* 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 XML entity declaration is structured as follows, where `{text}` is the | ||
* An entity declaration is structured as follows, where `{text}` is the | ||
* text of the declaration: | ||
@@ -28,69 +35,23 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdEntity nodes cannot have any children. | ||
*/ | ||
export default class XmlDtdEntity extends XmlNode { | ||
private _text; | ||
export default class XmlDtdEntity<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _charData; | ||
constructor(parent: Parent, validation: boolean, options: IXmlDtdEntityOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdEntity} class. | ||
* | ||
* @param text The text associated with the XML entity declaration. | ||
* Gets the text of this entity declaration. | ||
*/ | ||
constructor(text: string); | ||
/** | ||
* Gets the text associated with the XML entity declaration. | ||
* | ||
* @return The text associated with the XML entity declaration. | ||
*/ | ||
* Sets the text of this entity declaration. | ||
*/ | ||
charData: string; | ||
/** | ||
* Sets the text associated with the XML entity declaration. | ||
* | ||
* @param text The text associated with the XML entity declaration. | ||
* Returns an XML string representation of this entity declaration. | ||
*/ | ||
text: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this entity declaration. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML entity declaration in a document type definition. | ||
* Represents an entity declaration in a document type definition. | ||
* | ||
* An XML entity declaration is structured as follows, where `{text}` is the | ||
* An entity declaration is structured as follows, where `{text}` is the | ||
* text of the declaration: | ||
@@ -25,42 +29,26 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdEntity nodes cannot have any children. | ||
*/ | ||
var XmlDtdEntity = (function (_super) { | ||
__extends(XmlDtdEntity, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdEntity} class. | ||
* | ||
* @param text The text associated with the XML entity declaration. | ||
*/ | ||
function XmlDtdEntity(text) { | ||
var _this = _super.call(this) || this; | ||
_this.text = text; | ||
return _this; | ||
var XmlDtdEntity = /** @class */ (function () { | ||
function XmlDtdEntity(parent, validation, options) { | ||
this._validation = validation; | ||
this._parent = parent; | ||
this.charData = options.charData; | ||
} | ||
Object.defineProperty(XmlDtdEntity.prototype, "text", { | ||
Object.defineProperty(XmlDtdEntity.prototype, "charData", { | ||
/** | ||
* Gets the text associated with the XML entity declaration. | ||
* | ||
* @return The text associated with the XML entity declaration. | ||
* Gets the text of this entity declaration. | ||
*/ | ||
get: function () { | ||
return this._text; | ||
return this._charData; | ||
}, | ||
/** | ||
* Sets the text associated with the XML entity declaration. | ||
* | ||
* @param text The text associated with the XML entity declaration. | ||
* Sets the text of this entity declaration. | ||
*/ | ||
set: function (text) { | ||
if (!utils_1.isString(text)) { | ||
throw new TypeError("text should be a string"); | ||
} | ||
else if (!validate_1.validateChar(text)) { | ||
throw new Error("data should not contain characters" | ||
set: function (charData) { | ||
if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": entity declaration" | ||
+ (" \"" + charData + "\" should not contain characters") | ||
+ " not allowed in XML"); | ||
} | ||
this._text = text; | ||
this._charData = charData; | ||
}, | ||
@@ -71,57 +59,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this entity declaration. | ||
*/ | ||
XmlDtdEntity.prototype.children = function () { | ||
throw new Error("XmlDtdEntity nodes cannot have children"); | ||
XmlDtdEntity.prototype.toString = function () { | ||
return "<!ENTITY " + this._charData + ">"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this entity declaration. | ||
*/ | ||
XmlDtdEntity.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlDtdEntity nodes cannot have children"); | ||
XmlDtdEntity.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdEntity.prototype.removeChild = function (node) { | ||
throw new Error("XmlDtdEntity nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdEntity} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdEntity.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlDtdEntity nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlDtdEntity.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "<!ENTITY " + this.text + ">"; | ||
}; | ||
return XmlDtdEntity; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDtdEntity; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML notation declaration in a document type definition. | ||
* 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. | ||
* | ||
* An XML notation declaration is structured as follows, where `{text}` is the | ||
* A notation declaration is structured as follows, where `{text}` is the | ||
* text of the declaration: | ||
@@ -28,69 +35,23 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdNotation nodes cannot have any children. | ||
*/ | ||
export default class XmlDtdNotation extends XmlNode { | ||
private _text; | ||
export default class XmlDtdNotation<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _charData; | ||
constructor(parent: Parent, validation: boolean, options: IXmlDtdNotationOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdNotation} class. | ||
* | ||
* @param text The text associated with the XML notation declaration. | ||
* Gets the text of this notation declaration. | ||
*/ | ||
constructor(text: string); | ||
/** | ||
* Gets the text associated with the XML notation declaration. | ||
* | ||
* @return The text associated with the XML notation declaration. | ||
*/ | ||
* Sets the text of this notation declaration. | ||
*/ | ||
charData: string; | ||
/** | ||
* Sets the text associated with the XML notation declaration. | ||
* | ||
* @param text The text associated with the XML notation declaration. | ||
* Returns an XML string representation of this notation declaration. | ||
*/ | ||
text: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this notation declaration. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML notation declaration in a document type definition. | ||
* Represents a notation declaration in a document type definition. | ||
* | ||
* An XML notation declaration is structured as follows, where `{text}` is the | ||
* A notation declaration is structured as follows, where `{text}` is the | ||
* text of the declaration: | ||
@@ -25,42 +29,26 @@ * | ||
* ``` | ||
* | ||
* The `{text}` value is a property of this node. | ||
* | ||
* XmlDtdNotation nodes cannot have any children. | ||
*/ | ||
var XmlDtdNotation = (function (_super) { | ||
__extends(XmlDtdNotation, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdNotation} class. | ||
* | ||
* @param text The text associated with the XML notation declaration. | ||
*/ | ||
function XmlDtdNotation(text) { | ||
var _this = _super.call(this) || this; | ||
_this.text = text; | ||
return _this; | ||
var XmlDtdNotation = /** @class */ (function () { | ||
function XmlDtdNotation(parent, validation, options) { | ||
this._validation = validation; | ||
this._parent = parent; | ||
this.charData = options.charData; | ||
} | ||
Object.defineProperty(XmlDtdNotation.prototype, "text", { | ||
Object.defineProperty(XmlDtdNotation.prototype, "charData", { | ||
/** | ||
* Gets the text associated with the XML notation declaration. | ||
* | ||
* @return The text associated with the XML notation declaration. | ||
* Gets the text of this notation declaration. | ||
*/ | ||
get: function () { | ||
return this._text; | ||
return this._charData; | ||
}, | ||
/** | ||
* Sets the text associated with the XML notation declaration. | ||
* | ||
* @param text The text associated with the XML notation declaration. | ||
* Sets the text of this notation declaration. | ||
*/ | ||
set: function (text) { | ||
if (!utils_1.isString(text)) { | ||
throw new TypeError("text should be a string"); | ||
} | ||
else if (!validate_1.validateChar(text)) { | ||
throw new Error("data should not contain characters" | ||
set: function (charData) { | ||
if (this._validation && !validate_1.validateChar(charData)) { | ||
throw new Error(error_1.getContext(this.up()) + ": notation declaration" | ||
+ (" \"" + charData + "\" should not contain characters") | ||
+ " not allowed in XML"); | ||
} | ||
this._text = text; | ||
this._charData = charData; | ||
}, | ||
@@ -71,57 +59,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this notation declaration. | ||
*/ | ||
XmlDtdNotation.prototype.children = function () { | ||
throw new Error("XmlDtdNotation nodes cannot have children"); | ||
XmlDtdNotation.prototype.toString = function () { | ||
return "<!NOTATION " + this._charData + ">"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this notation declaration. | ||
*/ | ||
XmlDtdNotation.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlDtdNotation nodes cannot have children"); | ||
XmlDtdNotation.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdNotation.prototype.removeChild = function (node) { | ||
throw new Error("XmlDtdNotation nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdNotation} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdNotation.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlDtdNotation nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlDtdNotation.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "<!NOTATION " + this.text + ">"; | ||
}; | ||
return XmlDtdNotation; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDtdNotation; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,8 +16,15 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML parameter entity reference in a document type definition. | ||
* 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. | ||
* | ||
* An XML parameter entity reference is structured as follows, where `{entity}` | ||
* A parameter entity reference is structured as follows, where `{entity}` | ||
* is the name of the entity: | ||
@@ -28,69 +35,23 @@ * | ||
* ``` | ||
* | ||
* The `{entity}` value is a property of this node. | ||
* | ||
* XmlDtdParamEntityRef nodes cannot have any children. | ||
*/ | ||
export default class XmlDtdParamEntityRef extends XmlNode { | ||
private _entity; | ||
export default class XmlDtdParamEntityRef<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _name; | ||
constructor(parent: Parent, validation: boolean, options: IXmlDtdParamEntityRefOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdParamEntityRef} class. | ||
* | ||
* @param entity The entity to be referenced. | ||
* Gets the name of this parameter entity reference. | ||
*/ | ||
constructor(entity: string); | ||
/** | ||
* Gets the entity to be referenced. | ||
* | ||
* @returns The entity to be referenced. | ||
*/ | ||
* Sets the name of this parameter entity reference. | ||
*/ | ||
name: string; | ||
/** | ||
* Sets the entity to be referenced. | ||
* | ||
* @param entity The entity to be referenced. | ||
* Returns an XML string representation of this parameter entity reference. | ||
*/ | ||
entity: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this parameter entity reference. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML parameter entity reference in a document type definition. | ||
* Represents a parameter entity reference in a document type definition. | ||
* | ||
* An XML parameter entity reference is structured as follows, where `{entity}` | ||
* A parameter entity reference is structured as follows, where `{entity}` | ||
* is the name of the entity: | ||
@@ -25,42 +29,26 @@ * | ||
* ``` | ||
* | ||
* The `{entity}` value is a property of this node. | ||
* | ||
* XmlDtdParamEntityRef nodes cannot have any children. | ||
*/ | ||
var XmlDtdParamEntityRef = (function (_super) { | ||
__extends(XmlDtdParamEntityRef, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlDtdParamEntityRef} class. | ||
* | ||
* @param entity The entity to be referenced. | ||
*/ | ||
function XmlDtdParamEntityRef(entity) { | ||
var _this = _super.call(this) || this; | ||
_this.entity = entity; | ||
return _this; | ||
var XmlDtdParamEntityRef = /** @class */ (function () { | ||
function XmlDtdParamEntityRef(parent, validation, options) { | ||
this._validation = validation; | ||
this._parent = parent; | ||
this.name = options.name; | ||
} | ||
Object.defineProperty(XmlDtdParamEntityRef.prototype, "entity", { | ||
Object.defineProperty(XmlDtdParamEntityRef.prototype, "name", { | ||
/** | ||
* Gets the entity to be referenced. | ||
* | ||
* @returns The entity to be referenced. | ||
* Gets the name of this parameter entity reference. | ||
*/ | ||
get: function () { | ||
return this._entity; | ||
return this._name; | ||
}, | ||
/** | ||
* Sets the entity to be referenced. | ||
* | ||
* @param entity The entity to be referenced. | ||
* Sets the name of this parameter entity reference. | ||
*/ | ||
set: function (entity) { | ||
if (!utils_1.isString(entity)) { | ||
throw new TypeError("entity should be a string"); | ||
set: function (name) { | ||
if (this._validation && !validate_1.validateName(name)) { | ||
throw new Error(error_1.getContext(this.up()) + ": parameter entity" | ||
+ (" reference name \"" + name + "\" should not contain") | ||
+ " characters not allowed in XML names"); | ||
} | ||
else if (!validate_1.validateName(entity)) { | ||
throw new Error("entity should not contain characters" | ||
+ " not allowed in XML names"); | ||
} | ||
this._entity = entity; | ||
this._name = name; | ||
}, | ||
@@ -71,57 +59,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this parameter entity reference. | ||
*/ | ||
XmlDtdParamEntityRef.prototype.children = function () { | ||
throw new Error("XmlDtdParamEntityRef nodes cannot have children"); | ||
XmlDtdParamEntityRef.prototype.toString = function () { | ||
return "%" + this._name + ";"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this parameter entity reference. | ||
*/ | ||
XmlDtdParamEntityRef.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlDtdParamEntityRef nodes cannot have children"); | ||
XmlDtdParamEntityRef.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdParamEntityRef.prototype.removeChild = function (node) { | ||
throw new Error("XmlDtdParamEntityRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlDtdParamEntityRef} nodes cannot have | ||
* any children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlDtdParamEntityRef.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlDtdParamEntityRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
*/ | ||
XmlDtdParamEntityRef.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "%" + this.entity + ";"; | ||
}; | ||
return XmlDtdParamEntityRef; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlDtdParamEntityRef; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -17,14 +17,42 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
import { IStringOptions } from "../options"; | ||
import XmlAttribute from "./XmlAttribute"; | ||
import XmlCdata from "./XmlCdata"; | ||
import XmlCharData from "./XmlCharData"; | ||
import XmlCharRef from "./XmlCharRef"; | ||
import XmlComment from "./XmlComment"; | ||
import XmlEntityRef from "./XmlEntityRef"; | ||
import XmlNode from "./XmlNode"; | ||
import XmlProcInst from "./XmlProcInst"; | ||
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 XML element is structured as follows, where `{name}` is the name | ||
* A sample element is structured as follows, where `{name}` is the name | ||
* of the element: | ||
@@ -40,167 +68,87 @@ * | ||
* | ||
* The `{name}` value is a property of the node, while the attributes and | ||
* children of the element (such as other elements, processing instructions, | ||
* and text) are children of this node. | ||
* XML elements can have an unlimited number of attributes, CDATA sections, | ||
* character references, comments, elements, entity references, processing | ||
* instructions, and character data. | ||
* | ||
* XmlElement nodes can have an unlimited number of {@link XmlAttribute}, | ||
* {@link XmlCdata}, {@link XmlCharRef}, {@link XmlComment}, | ||
* {@link XmlElement}, {@link XmlEntityRef}, {@link XmlProcInst}, or | ||
* {@link XmlCharData} nodes as children. | ||
* An element with no content will be represented using an empty element tag: | ||
* | ||
* ```xml | ||
* <{name}/> | ||
* ``` | ||
*/ | ||
export default class XmlElement extends XmlNode { | ||
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); | ||
/** | ||
* Initializes a new instance of the {@link XmlElement} class. | ||
* | ||
* @param name The name of the element. | ||
* Gets the name of this element. | ||
*/ | ||
constructor(name: string); | ||
/** | ||
* Gets the name of the element. | ||
* | ||
* @returns The name of the element. | ||
* Sets the name of this element. | ||
*/ | ||
name: string; | ||
/** | ||
* Adds an attribute to this element and returns the new attribute. | ||
*/ | ||
attribute(options: IXmlAttributeOptions): XmlAttribute<this>; | ||
/** | ||
* Sets the name of the element. | ||
* | ||
* @param name The name of the element. | ||
* Adds a CDATA section to this element and returns the new CDATA section. | ||
*/ | ||
name: string; | ||
cdata(options: IXmlCdataOptions): XmlCdata<this>; | ||
/** | ||
* Inserts an new attribute at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param name The name of the attribute. | ||
* @param value The value of the attribute. Strings are converted to | ||
* XmlAttributeText nodes. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns {XmlAttribute} The newly created attribute. | ||
* Adds character data to this element and returns the new character data. | ||
*/ | ||
attribute(name: string, value: string | XmlNode | Array<string | XmlNode>, index?: number): XmlAttribute; | ||
charData(options: IXmlCharDataOptions): XmlCharData<this>; | ||
/** | ||
* Returns an array containing all of the children of this node that are | ||
* instances of {@link XmlAttribute}. | ||
* | ||
* @returns An array containing all of the children of this node that are | ||
* instances of {@link XmlAttribute}. | ||
* Adds a character reference to this element and returns the new | ||
* character reference. | ||
*/ | ||
attributes(): XmlAttribute[]; | ||
charRef(options: IXmlCharRefOptions): XmlCharRef<this>; | ||
/** | ||
* Inserts a new CDATA section at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the CDATA section. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created CDATA section. | ||
* Adds a comment to this element and returns the new comment. | ||
*/ | ||
cdata(content: string, index?: number): XmlCdata; | ||
comment(options: IXmlCommentOptions): XmlComment<this>; | ||
/** | ||
* Inserts some character data at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param charData Character data. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created text node. | ||
* Adds an element to this element and returns the new element. | ||
*/ | ||
charData(charData: string, index?: number): XmlCharData; | ||
element(options: IXmlElementOptions): XmlElement<this>; | ||
/** | ||
* Inserts a new character reference at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param char The character to represent using the reference. | ||
* @param hex Whether to use the hexadecimal or decimal representation for | ||
* the reference. If left undefined, decimal is the default. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created character reference. | ||
* Adds an entity reference to this element and returns the new entity | ||
* reference. | ||
*/ | ||
charRef(char: string, hex?: boolean, index?: number): XmlCharRef; | ||
entityRef(options: IXmlEntityRefOptions): XmlEntityRef<this>; | ||
/** | ||
* Inserts a new comment at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the comment. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created comment. | ||
* Adds a processing instruction to this element and returns the new | ||
* processing instruction. | ||
*/ | ||
comment(content: string, index?: number): XmlComment; | ||
procInst(options: IXmlProcInstOptions): XmlProcInst<this>; | ||
/** | ||
* Inserts a new element at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param name The name of the element. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created element. | ||
* Returns an XML string representation of this element using the specified | ||
* options. | ||
*/ | ||
element(name: string, index?: number): XmlElement; | ||
toString(options?: IStringOptions): string; | ||
/** | ||
* Inserts a new entity reference at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param entity The entity to be referenced. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created entity reference. | ||
* Returns the parent of this element. | ||
*/ | ||
entityRef(entity: string, index?: number): XmlEntityRef; | ||
up(): Parent; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Note that only {@link XmlAttribute}, {@link XmlCdata}, | ||
* {@link XmlCharRef}, {@link XmlComment}, {@link XmlElement}, | ||
* {@link XmlEntityRef}, {@link XmlProcInst}, or {@link XmlCharData} nodes | ||
* can be inserted; otherwise, an exception will be thrown. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after | ||
* the index are shifted to the right. If no index is | ||
* specified, the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Returns an XML string representation of this element using the specified | ||
* options and initial indent. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
private toStringWithIndent; | ||
/** | ||
* Inserts a new processing instruction at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created processing instruction. | ||
* Returns true if the specified nodes are all character references, | ||
* entity references, or character data. | ||
*/ | ||
procInst(target: string, content?: string, index?: number): XmlProcInst; | ||
private allSameLineNodes; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
* Returns true if the specified nodes are all character references, | ||
* entity references, or character data. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
private onSameLine; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -28,18 +17,20 @@ * Licensed under the Apache License, Version 2.0 (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 utils_1 = require("../utils"); | ||
var validate_1 = require("../validate"); | ||
var XmlAttribute_1 = require("./XmlAttribute"); | ||
var XmlAttributeText_1 = require("./XmlAttributeText"); | ||
var XmlCdata_1 = require("./XmlCdata"); | ||
var XmlCharData_1 = require("./XmlCharData"); | ||
var XmlCharRef_1 = require("./XmlCharRef"); | ||
var XmlComment_1 = require("./XmlComment"); | ||
var XmlEntityRef_1 = require("./XmlEntityRef"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
var XmlProcInst_1 = require("./XmlProcInst"); | ||
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 XML element is structured as follows, where `{name}` is the name | ||
* A sample element is structured as follows, where `{name}` is the name | ||
* of the element: | ||
@@ -55,28 +46,35 @@ * | ||
* | ||
* The `{name}` value is a property of the node, while the attributes and | ||
* children of the element (such as other elements, processing instructions, | ||
* and text) are children of this node. | ||
* XML elements can have an unlimited number of attributes, CDATA sections, | ||
* character references, comments, elements, entity references, processing | ||
* instructions, and character data. | ||
* | ||
* XmlElement nodes can have an unlimited number of {@link XmlAttribute}, | ||
* {@link XmlCdata}, {@link XmlCharRef}, {@link XmlComment}, | ||
* {@link XmlElement}, {@link XmlEntityRef}, {@link XmlProcInst}, or | ||
* {@link XmlCharData} nodes as children. | ||
* An element with no content will be represented using an empty element tag: | ||
* | ||
* ```xml | ||
* <{name}/> | ||
* ``` | ||
*/ | ||
var XmlElement = (function (_super) { | ||
__extends(XmlElement, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlElement} class. | ||
* | ||
* @param name The name of the element. | ||
*/ | ||
function XmlElement(name) { | ||
var _this = _super.call(this) || this; | ||
_this.name = name; | ||
return _this; | ||
var XmlElement = /** @class */ (function () { | ||
function XmlElement(parent, validation, options) { | ||
this._validation = validation; | ||
if (!validate_1.isUndefined(options.replaceInvalidCharsInName)) { | ||
this._replaceInvalidCharsInName = options.replaceInvalidCharsInName; | ||
} | ||
else { | ||
this._replaceInvalidCharsInName = false; | ||
} | ||
if (!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 the element. | ||
* | ||
* @returns The name of the element. | ||
* Gets the name of this element. | ||
*/ | ||
@@ -87,13 +85,22 @@ get: function () { | ||
/** | ||
* Sets the name of the element. | ||
* | ||
* @param name The name of the element. | ||
* Sets the name of this element. | ||
*/ | ||
set: function (name) { | ||
if (!utils_1.isString(name)) { | ||
throw new TypeError("name should be a string"); | ||
if (this._replaceInvalidCharsInName) { | ||
name = validate_1.fixName(name); | ||
if (name.length === 0) { | ||
throw new Error(error_1.getContext(this.up()) + ": element name should" | ||
+ " not be empty"); | ||
} | ||
} | ||
else if (!validate_1.validateName(name)) { | ||
throw new Error("name should not contain characters not" | ||
+ " allowed in XML names"); | ||
else if (this._validation && !validate_1.validateName(name)) { | ||
if (name.length === 0) { | ||
throw new Error(error_1.getContext(this.up()) + ": element name should" | ||
+ " not be empty"); | ||
} | ||
else { | ||
throw new Error(error_1.getContext(this.up()) + ": element name" | ||
+ (" \"" + name + "\" should not contain characters not") | ||
+ " allowed in XML names"); | ||
} | ||
} | ||
@@ -106,228 +113,125 @@ this._name = name; | ||
/** | ||
* Inserts an new attribute at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param name The name of the attribute. | ||
* @param value The value of the attribute. Strings are converted to | ||
* XmlAttributeText nodes. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns {XmlAttribute} The newly created attribute. | ||
* Adds an attribute to this element and returns the new attribute. | ||
*/ | ||
XmlElement.prototype.attribute = function (name, value, index) { | ||
if (utils_1.isString(value)) { | ||
value = new XmlAttributeText_1.default(value); | ||
XmlElement.prototype.attribute = function (options) { | ||
if (this._validation && options.name in this._attributeNames) { | ||
throw new Error(error_1.getContext(this.up()) + ": element \"" + this.name + "\"" | ||
+ " already contains an attribute with the" | ||
+ (" name \"" + options.name + "\"")); | ||
} | ||
else if (utils_1.isArray(value)) { | ||
for (var i = 0; i < value.length; i++) { | ||
if (utils_1.isString(value[i])) { | ||
value[i] = new XmlAttributeText_1.default(value[i]); | ||
} | ||
} | ||
} | ||
var attribute = new XmlAttribute_1.default(name, value); | ||
this.insertChild(attribute, index); | ||
var attribute = new XmlAttribute_1.default(this, this._validation, options); | ||
this._children.push(attribute); | ||
this._attributeNames.push(options.name); | ||
return attribute; | ||
}; | ||
/** | ||
* Returns an array containing all of the children of this node that are | ||
* instances of {@link XmlAttribute}. | ||
* | ||
* @returns An array containing all of the children of this node that are | ||
* instances of {@link XmlAttribute}. | ||
* Adds a CDATA section to this element and returns the new CDATA section. | ||
*/ | ||
XmlElement.prototype.attributes = function () { | ||
return this._children.filter(function (node) { return node instanceof XmlAttribute_1.default; }); | ||
}; | ||
/** | ||
* Inserts a new CDATA section at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the CDATA section. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created CDATA section. | ||
*/ | ||
XmlElement.prototype.cdata = function (content, index) { | ||
var cdata = new XmlCdata_1.default(content); | ||
this.insertChild(cdata, index); | ||
XmlElement.prototype.cdata = function (options) { | ||
var cdata = new XmlCdata_1.default(this, this._validation, options); | ||
this._children.push(cdata); | ||
return cdata; | ||
}; | ||
/** | ||
* Inserts some character data at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param charData Character data. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created text node. | ||
* Adds character data to this element and returns the new character data. | ||
*/ | ||
XmlElement.prototype.charData = function (charData, index) { | ||
var charDataNode = new XmlCharData_1.default(charData); | ||
this.insertChild(charDataNode, index); | ||
XmlElement.prototype.charData = function (options) { | ||
var charDataNode = new XmlCharData_1.default(this, this._validation, options); | ||
this._children.push(charDataNode); | ||
return charDataNode; | ||
}; | ||
/** | ||
* Inserts a new character reference at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param char The character to represent using the reference. | ||
* @param hex Whether to use the hexadecimal or decimal representation for | ||
* the reference. If left undefined, decimal is the default. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created character reference. | ||
* Adds a character reference to this element and returns the new | ||
* character reference. | ||
*/ | ||
XmlElement.prototype.charRef = function (char, hex, index) { | ||
var charRef = new XmlCharRef_1.default(char, hex); | ||
this.insertChild(charRef, index); | ||
XmlElement.prototype.charRef = function (options) { | ||
var charRef = new XmlCharRef_1.default(this, this._validation, options); | ||
this._children.push(charRef); | ||
return charRef; | ||
}; | ||
/** | ||
* Inserts a new comment at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param content The data of the comment. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created comment. | ||
* Adds a comment to this element and returns the new comment. | ||
*/ | ||
XmlElement.prototype.comment = function (content, index) { | ||
var comment = new XmlComment_1.default(content); | ||
this.insertChild(comment, index); | ||
XmlElement.prototype.comment = function (options) { | ||
var comment = new XmlComment_1.default(this, this._validation, options); | ||
this._children.push(comment); | ||
return comment; | ||
}; | ||
/** | ||
* Inserts a new element at the specified index. If no index is specified, | ||
* the node is inserted at the end of this node's children. | ||
* | ||
* @param name The name of the element. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created element. | ||
* Adds an element to this element and returns the new element. | ||
*/ | ||
XmlElement.prototype.element = function (name, index) { | ||
var element = new XmlElement(name); | ||
this.insertChild(element, index); | ||
XmlElement.prototype.element = function (options) { | ||
var element = new XmlElement(this, this._validation, options); | ||
this._children.push(element); | ||
return element; | ||
}; | ||
/** | ||
* Inserts a new entity reference at the specified index. If no index is | ||
* specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param entity The entity to be referenced. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this | ||
* node's children. | ||
* | ||
* @returns The newly created entity reference. | ||
* Adds an entity reference to this element and returns the new entity | ||
* reference. | ||
*/ | ||
XmlElement.prototype.entityRef = function (entity, index) { | ||
var entityRef = new XmlEntityRef_1.default(entity); | ||
this.insertChild(entityRef, index); | ||
XmlElement.prototype.entityRef = function (options) { | ||
var entityRef = new XmlEntityRef_1.default(this, this._validation, options); | ||
this._children.push(entityRef); | ||
return entityRef; | ||
}; | ||
/** | ||
* Inserts the specified node into this node's children at the specified | ||
* index. The node is not inserted if it is already present. If this node | ||
* already has a parent, it is removed from that parent. | ||
* | ||
* Note that only {@link XmlAttribute}, {@link XmlCdata}, | ||
* {@link XmlCharRef}, {@link XmlComment}, {@link XmlElement}, | ||
* {@link XmlEntityRef}, {@link XmlProcInst}, or {@link XmlCharData} nodes | ||
* can be inserted; otherwise, an exception will be thrown. | ||
* | ||
* @param node The node to insert. | ||
* @param index The index at which to insert the node. Nodes at or after | ||
* the index are shifted to the right. If no index is | ||
* specified, the node is inserted at the end. | ||
* | ||
* @returns The node inserted into this node's children, or undefined if no | ||
* node was inserted. | ||
* Adds a processing instruction to this element and returns the new | ||
* processing instruction. | ||
*/ | ||
XmlElement.prototype.insertChild = function (node, index) { | ||
if (!(node instanceof XmlAttribute_1.default | ||
|| node instanceof XmlCdata_1.default | ||
|| node instanceof XmlCharRef_1.default | ||
|| node instanceof XmlComment_1.default | ||
|| node instanceof XmlElement | ||
|| node instanceof XmlEntityRef_1.default | ||
|| node instanceof XmlProcInst_1.default | ||
|| node instanceof XmlCharData_1.default)) { | ||
throw new TypeError("node should be an instance of XmlAttribute," | ||
+ " XmlCdata, XmlCharRef, XmlComment," | ||
+ " XmlElement, XmlEntityRef, XmlProcInst," | ||
+ " or XmlText"); | ||
} | ||
if (node instanceof XmlAttribute_1.default) { | ||
var attributes = this._children.filter(function (n) { return n instanceof XmlAttribute_1.default; }); | ||
for (var _i = 0, attributes_1 = attributes; _i < attributes_1.length; _i++) { | ||
var attribute = attributes_1[_i]; | ||
if (attribute.name === node.name) { | ||
throw new Error("element already contains an" | ||
+ " XmlAttribute object with name " | ||
+ node.name); | ||
} | ||
} | ||
} | ||
return _super.prototype.insertChild.call(this, node, index); | ||
XmlElement.prototype.procInst = function (options) { | ||
var procInst = new XmlProcInst_1.default(this, this._validation, options); | ||
this._children.push(procInst); | ||
return procInst; | ||
}; | ||
/** | ||
* Inserts a new processing instruction at the specified index. If no index | ||
* is specified, the node is inserted at the end of this node's children. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
* @param index The index at which the node should be inserted. If no index | ||
* is specified, the node is inserted at the end of this node's | ||
* children. | ||
* | ||
* @returns The newly created processing instruction. | ||
* Returns an XML string representation of this element using the specified | ||
* options. | ||
*/ | ||
XmlElement.prototype.procInst = function (target, content, index) { | ||
var procInst = new XmlProcInst_1.default(target, content); | ||
this.insertChild(procInst, index); | ||
return procInst; | ||
XmlElement.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return this.toStringWithIndent(options, ""); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns An XML string representation of this node. | ||
* Returns the parent of this element. | ||
*/ | ||
XmlElement.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
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 attributes = this.attributes(); | ||
var nodes = this._children.filter(function (node) { return attributes.indexOf(node) === -1; }); | ||
var newIndent = indent + optionsObj.indent; | ||
// Element tag start | ||
var str = "<" + this._name; | ||
// Attributes | ||
for (var _i = 0, attributes_2 = attributes; _i < attributes_2.length; _i++) { | ||
var attribute = attributes_2[_i]; | ||
str += " " + attribute.toString(options); | ||
// 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) { | ||
// Element non-empty tag end | ||
str += ">"; | ||
var indenter = function (line) { return optionsObj.indent + line; }; | ||
var childStr = ""; | ||
for (var i = 0; i < nodes.length; i++) { | ||
var next = nodes[i]; | ||
var nextStr = next.toString(options); | ||
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 | ||
@@ -337,12 +241,9 @@ // the most recent two, are of type XmlCharacterReference, | ||
if (optionsObj.pretty) { | ||
if (!allSameLineNodes(nodes)) { | ||
if (!(i > 0 && onSameLine(next, prev))) { | ||
str += optionsObj.newline; | ||
nextStr = nextStr.split(optionsObj.newline) | ||
.map(indenter) | ||
.join(optionsObj.newline); | ||
if (!this.allSameLineNodes(nodes)) { | ||
if (!(i > 0 && this.onSameLine(next, prev))) { | ||
nextStr = optionsObj.newline + newIndent + nextStr; | ||
} | ||
} | ||
} | ||
str += nextStr; | ||
childStr += nextStr; | ||
} | ||
@@ -352,8 +253,14 @@ // Line break before end tag unless all nodes are of type | ||
if (optionsObj.pretty) { | ||
if (!allSameLineNodes(nodes)) { | ||
str += optionsObj.newline; | ||
if (!this.allSameLineNodes(nodes)) { | ||
childStr += optionsObj.newline + indent; | ||
} | ||
} | ||
// Element end tag | ||
str += "</" + this._name + ">"; | ||
if (childStr.length === 0 && this._useSelfClosingTagIfEmpty) { | ||
// Element empty tag end | ||
str += "/>"; | ||
} | ||
else { | ||
// Element start and end tags | ||
str += ">" + childStr + "</" + this._name + ">"; | ||
} | ||
} | ||
@@ -366,46 +273,32 @@ else { | ||
}; | ||
/** | ||
* 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) | ||
&& (!validate_1.isUndefined(next) | ||
&& (next instanceof XmlCharRef_1.default | ||
|| next instanceof XmlEntityRef_1.default | ||
|| next instanceof XmlCharData_1.default)); | ||
}; | ||
return XmlElement; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlElement; | ||
/** | ||
* Returns true if the specified nodes are all of type {@link XmlCharRef}, | ||
* {@link XmlEntityRef}, or {@link XmlCharData}. | ||
* | ||
* @param nodes The specified nodes. | ||
* | ||
* @returns Whether or not the specified nodes are all of type | ||
* {@link XmlCharRef}, {@link XmlEntityRef}, or {@link XmlCharData}. | ||
* | ||
* @private | ||
*/ | ||
function allSameLineNodes(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 of type {@link XmlCharRef}, | ||
* {@link XmlEntityRef}, or {@link XmlCharData}. | ||
* | ||
* @param prev The first specified node. | ||
* @param next The second specified node. | ||
* | ||
* @returns Whether or not the specified nodes are all of type | ||
* {@link XmlCharRef}, {@link XmlEntityRef}, or {@link XmlCharData}. | ||
* | ||
* @private | ||
*/ | ||
function onSameLine(prev, next) { | ||
return (prev instanceof XmlCharRef_1.default | ||
|| prev instanceof XmlEntityRef_1.default | ||
|| prev instanceof XmlCharData_1.default) | ||
&& (next instanceof XmlCharRef_1.default | ||
|| next instanceof XmlEntityRef_1.default | ||
|| next instanceof XmlCharData_1.default); | ||
} |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,9 +16,16 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML entity reference. | ||
* 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 XML entity reference is structured as follows, where | ||
* `{entity}` is name of the entity to be referenced: | ||
* An entity reference is structured as follows, where `{name}` is the name of | ||
* the entity to be referenced: | ||
* | ||
@@ -28,69 +35,23 @@ * ```xml | ||
* ``` | ||
* | ||
* The `{entity}` value is a property of this node. | ||
* | ||
* XmlEntityRef nodes cannot have any children. | ||
*/ | ||
export default class XmlEntityRef extends XmlNode { | ||
private _entity; | ||
export default class XmlEntityRef<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _name; | ||
constructor(parent: Parent, validation: boolean, options: IXmlEntityRefOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlEntityRef} class. | ||
* | ||
* @param entity The entity to be referenced. | ||
* Gets the name of this entity reference. | ||
*/ | ||
constructor(entity: string); | ||
/** | ||
* Gets the entity to be referenced. | ||
* | ||
* @returns The entity to be referenced. | ||
*/ | ||
* Sets the name of this entity reference. | ||
*/ | ||
name: string; | ||
/** | ||
* Sets the entity to be referenced. | ||
* | ||
* @param entity The entity to be referenced. | ||
* Returns an XML string representation of this entity reference. | ||
*/ | ||
entity: string; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this entity reference. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML entity reference. | ||
* Represents an entity reference. | ||
* | ||
* An XML entity reference is structured as follows, where | ||
* `{entity}` is name of the entity to be referenced: | ||
* An entity reference is structured as follows, where `{name}` is the name of | ||
* the entity to be referenced: | ||
* | ||
@@ -25,42 +29,26 @@ * ```xml | ||
* ``` | ||
* | ||
* The `{entity}` value is a property of this node. | ||
* | ||
* XmlEntityRef nodes cannot have any children. | ||
*/ | ||
var XmlEntityRef = (function (_super) { | ||
__extends(XmlEntityRef, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlEntityRef} class. | ||
* | ||
* @param entity The entity to be referenced. | ||
*/ | ||
function XmlEntityRef(entity) { | ||
var _this = _super.call(this) || this; | ||
_this.entity = entity; | ||
return _this; | ||
var XmlEntityRef = /** @class */ (function () { | ||
function XmlEntityRef(parent, validation, options) { | ||
this._validation = validation; | ||
this._parent = parent; | ||
this.name = options.name; | ||
} | ||
Object.defineProperty(XmlEntityRef.prototype, "entity", { | ||
Object.defineProperty(XmlEntityRef.prototype, "name", { | ||
/** | ||
* Gets the entity to be referenced. | ||
* | ||
* @returns The entity to be referenced. | ||
* Gets the name of this entity reference. | ||
*/ | ||
get: function () { | ||
return this._entity; | ||
return this._name; | ||
}, | ||
/** | ||
* Sets the entity to be referenced. | ||
* | ||
* @param entity The entity to be referenced. | ||
* Sets the name of this entity reference. | ||
*/ | ||
set: function (entity) { | ||
if (!utils_1.isString(entity)) { | ||
throw new TypeError("entity should be a string"); | ||
set: function (name) { | ||
if (this._validation && !validate_1.validateName(name)) { | ||
throw new Error(error_1.getContext(this.up()) + ": entity reference name" | ||
+ (" \"" + name + "\" should not contain characters not") | ||
+ " allowed in XML names"); | ||
} | ||
else if (!validate_1.validateName(entity)) { | ||
throw new Error("entity should not contain characters" + | ||
" not allowed in XML names"); | ||
} | ||
this._entity = entity; | ||
this._name = name; | ||
}, | ||
@@ -71,57 +59,15 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this entity reference. | ||
*/ | ||
XmlEntityRef.prototype.children = function () { | ||
throw new Error("XmlEntityRef nodes cannot have children"); | ||
XmlEntityRef.prototype.toString = function () { | ||
return "&" + this._name + ";"; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this entity reference. | ||
*/ | ||
XmlEntityRef.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlEntityRef nodes cannot have children"); | ||
XmlEntityRef.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlEntityRef.prototype.removeChild = function (node) { | ||
throw new Error("XmlEntityRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlEntityRef} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlEntityRef.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlEntityRef nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
XmlEntityRef.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
return "&" + this.entity + ";"; | ||
}; | ||
return XmlEntityRef; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlEntityRef; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,10 +16,22 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import { IStringOptions } from "../options"; | ||
import XmlNode from "./XmlNode"; | ||
/** | ||
* Represents an XML processing instruction. | ||
* 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. | ||
* | ||
* An XML processing instruction is structured as follows, where `{target}` | ||
* and `{content}` are the target and content of the processing instruction | ||
* respectively. | ||
* A processing instruction is structured as follows, where `{target}` and | ||
* `{content}` are the target and content of the processing instruction | ||
* respectively: | ||
* | ||
@@ -29,85 +41,31 @@ * ```xml | ||
* ``` | ||
* | ||
* The `{target}` and `{content}` values are properties of this node. | ||
* | ||
* XmlProcInst nodes cannot have any children. | ||
*/ | ||
export default class XmlProcInst extends XmlNode { | ||
export default class XmlProcInst<Parent> { | ||
private readonly _validation; | ||
private readonly _parent; | ||
private _content; | ||
private _target; | ||
private _content?; | ||
constructor(parent: Parent, validation: boolean, options: IXmlProcInstOptions); | ||
/** | ||
* Initializes a new instance of the {@link XmlProcInst} class. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
* Gets the content of this processing instruction. | ||
*/ | ||
constructor(target: string, content?: string); | ||
/** | ||
* Gets the target of the processing instruction. | ||
* | ||
* @returns The target of the processing instruction. | ||
*/ | ||
/** | ||
* Sets the target of the processing instruction. | ||
* | ||
* @param target The target of the processing instruction. | ||
*/ | ||
target: string; | ||
/** | ||
* Gets the data of the processing instruction. | ||
* | ||
* @returns The data of the processing instruction. This value may be | ||
* undefined. | ||
*/ | ||
/** | ||
* Sets the data of the processing instruction. | ||
* | ||
* @param content The data of the processing instruction. This value may be | ||
* undefined. | ||
*/ | ||
* Sets the content of this processing instruction. | ||
*/ | ||
content: string | undefined; | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Gets the target of this processing instruction. | ||
*/ | ||
children(): XmlNode[]; | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
insertChild(node: XmlNode, index?: number): XmlNode | undefined; | ||
* Sets the content of this processing instruction. | ||
*/ | ||
target: string; | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this processing instruction. | ||
*/ | ||
removeChild(node: XmlNode): boolean; | ||
toString(): string; | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this processing instruction. | ||
*/ | ||
removeChildAtIndex(index: number): XmlNode; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
toString(options?: IStringOptions): string; | ||
up(): Parent; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
/** | ||
* 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 utils_1 = require("../utils"); | ||
var error_1 = require("../error"); | ||
var validate_1 = require("../validate"); | ||
var XmlNode_1 = require("./XmlNode"); | ||
/** | ||
* Represents an XML processing instruction. | ||
* Represents a processing instruction. | ||
* | ||
* An XML processing instruction is structured as follows, where `{target}` | ||
* and `{content}` are the target and content of the processing instruction | ||
* respectively. | ||
* A processing instruction is structured as follows, where `{target}` and | ||
* `{content}` are the target and content of the processing instruction | ||
* respectively: | ||
* | ||
@@ -26,48 +30,34 @@ * ```xml | ||
* ``` | ||
* | ||
* The `{target}` and `{content}` values are properties of this node. | ||
* | ||
* XmlProcInst nodes cannot have any children. | ||
*/ | ||
var XmlProcInst = (function (_super) { | ||
__extends(XmlProcInst, _super); | ||
/** | ||
* Initializes a new instance of the {@link XmlProcInst} class. | ||
* | ||
* @param target The target of the processing instruction. | ||
* @param content The data of the processing instruction, or undefined if | ||
* there is no target. | ||
*/ | ||
function XmlProcInst(target, content) { | ||
var _this = _super.call(this) || this; | ||
_this.target = target; | ||
_this.content = content; | ||
return _this; | ||
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, "target", { | ||
Object.defineProperty(XmlProcInst.prototype, "content", { | ||
/** | ||
* Gets the target of the processing instruction. | ||
* | ||
* @returns The target of the processing instruction. | ||
* Gets the content of this processing instruction. | ||
*/ | ||
get: function () { | ||
return this._target; | ||
return this._content; | ||
}, | ||
/** | ||
* Sets the target of the processing instruction. | ||
* | ||
* @param target The target of the processing instruction. | ||
* Sets the content of this processing instruction. | ||
*/ | ||
set: function (target) { | ||
if (!utils_1.isString(target)) { | ||
throw new TypeError("target should be a string"); | ||
set: function (content) { | ||
if (!validate_1.isUndefined(content)) { | ||
if (this._validation && !validate_1.validateChar(content)) { | ||
throw new Error(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(error_1.getContext(this.up()) + ": processing" | ||
+ (" instruction content \"" + content + "\" should") | ||
+ " not contain the string '?>'"); | ||
} | ||
} | ||
else if (!validate_1.validateChar(target)) { | ||
throw new Error("target should not contain characters" | ||
+ " not allowed in XML"); | ||
} | ||
else if (target === "xml") { | ||
throw new Error("target should not be the string 'xml'"); | ||
} | ||
this._target = target; | ||
this._content = content; | ||
}, | ||
@@ -77,32 +67,25 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(XmlProcInst.prototype, "content", { | ||
Object.defineProperty(XmlProcInst.prototype, "target", { | ||
/** | ||
* Gets the data of the processing instruction. | ||
* | ||
* @returns The data of the processing instruction. This value may be | ||
* undefined. | ||
* Gets the target of this processing instruction. | ||
*/ | ||
get: function () { | ||
return this._content; | ||
return this._target; | ||
}, | ||
/** | ||
* Sets the data of the processing instruction. | ||
* | ||
* @param content The data of the processing instruction. This value may be | ||
* undefined. | ||
* Sets the content of this processing instruction. | ||
*/ | ||
set: function (content) { | ||
if (!utils_1.isString(content) && !utils_1.isUndefined(content)) { | ||
throw new TypeError("data should be a string or undefined"); | ||
set: function (target) { | ||
if (this._validation && !validate_1.validateName(target)) { | ||
throw new Error(error_1.getContext(this.up()) + ": processing" | ||
+ (" instruction target \"" + target + "\" should") | ||
+ " not contain characters not allowed in XML" | ||
+ " names"); | ||
} | ||
if (utils_1.isString(content)) { | ||
if (!validate_1.validateChar(content)) { | ||
throw new Error("data should not contain characters" | ||
+ " not allowed in XML"); | ||
} | ||
else if (/\?>/.test(content)) { | ||
throw new Error("data should not contain the string '?>'"); | ||
} | ||
if (this._validation && target === "xml") { | ||
throw new Error(error_1.getContext(this.up()) + ": processing" | ||
+ (" instruction target \"" + target + "\" should") | ||
+ " not be the string 'xml'"); | ||
} | ||
this._content = content; | ||
this._target = target; | ||
}, | ||
@@ -113,62 +96,20 @@ enumerable: true, | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @returns This method does not return. | ||
* Returns an XML string representation of this processing instruction. | ||
*/ | ||
XmlProcInst.prototype.children = function () { | ||
throw new Error("XmlProcInst nodes cannot have children"); | ||
XmlProcInst.prototype.toString = function () { | ||
if (validate_1.isUndefined(this._content)) { | ||
return "<?" + this._target + "?>"; | ||
} | ||
else { | ||
return "<?" + this._target + " " + this._content + "?>"; | ||
} | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
* Returns the parent of this processing instruction. | ||
*/ | ||
XmlProcInst.prototype.insertChild = function (node, index) { | ||
throw new Error("XmlProcInst nodes cannot have children"); | ||
XmlProcInst.prototype.up = function () { | ||
return this._parent; | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @param node This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlProcInst.prototype.removeChild = function (node) { | ||
throw new Error("XmlProcInst nodes cannot have children"); | ||
}; | ||
/** | ||
* Throws an exception since {@link XmlProcInst} nodes cannot have any | ||
* children. | ||
* | ||
* @param index This parameter is unused. | ||
* | ||
* @returns This method does not return. | ||
*/ | ||
XmlProcInst.prototype.removeChildAtIndex = function (index) { | ||
throw new Error("XmlProcInst nodes cannot have children"); | ||
}; | ||
/** | ||
* Returns an XML string representation of this node. | ||
* | ||
* @param options Formatting options for the string representation. | ||
* | ||
* @returns {string} An XML string representation of this node. | ||
*/ | ||
XmlProcInst.prototype.toString = function (options) { | ||
if (options === void 0) { options = {}; } | ||
if (this.content === undefined) { | ||
return "<?" + this.target + "?>"; | ||
} | ||
else { | ||
return "<?" + this.target + " " + this.content + "?>"; | ||
} | ||
}; | ||
return XmlProcInst; | ||
}(XmlNode_1.default)); | ||
}()); | ||
exports.default = XmlProcInst; |
/** | ||
* Formatting options for the string representation of an {@link XmlNode} and | ||
* its children. This object is used by the `toString` method of | ||
* {@link XmlNode}. | ||
* 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. | ||
* If left undefined, single quotes are used. | ||
* By default, single quotes are used. | ||
*/ | ||
doubleQuotes?: boolean; | ||
/** | ||
* The indent string used for pretty-printing. If left undefined, the | ||
* default indent string is four spaces. | ||
* The indent string used for pretty-printing. The default indent string is | ||
* four spaces. | ||
*/ | ||
indent?: string; | ||
/** | ||
* The newline string used for pretty-printing. If left undefined, the | ||
* default newline string is "\n". | ||
* The newline string used for pretty-printing. The default newline string | ||
* is "\n". | ||
*/ | ||
newline?: string; | ||
/** | ||
* Whether pretty-printing is enabled. If left undefined, pretty-printing | ||
* is enabled. | ||
* Whether pretty-printing is enabled. By default, pretty-printing is | ||
* enabled. | ||
*/ | ||
@@ -29,4 +42,4 @@ pretty?: boolean; | ||
/** | ||
* Implementation of the IStringOptions interface used to provide default values | ||
* to fields. | ||
* Implementation of the IStringOptions interface used to provide default | ||
* values to fields. | ||
* | ||
@@ -40,37 +53,3 @@ * @private | ||
pretty: boolean; | ||
constructor(stringOptions?: IStringOptions); | ||
constructor(options: IStringOptions); | ||
} | ||
/** | ||
* The options associated with the XML declaration. This object is used to | ||
* create a new {@link XmlDecl} object. | ||
*/ | ||
export interface IDeclarationOptions { | ||
/** | ||
* The XML encoding to be included in the declaration. This value must be a | ||
* valid encoding. If left undefined, no encoding is included. | ||
*/ | ||
encoding?: string; | ||
/** | ||
* The XML standalone attribute to be included. This value must be "yes" or | ||
* "no". If left undefined, no standalone attribute is included. | ||
*/ | ||
standalone?: string; | ||
/** | ||
* The XML version to be included in the declaration. This value must be a | ||
* valid XML version number. If left undefined, the default version is | ||
* "1.0". | ||
*/ | ||
version?: string; | ||
} | ||
/** | ||
* Implementation of the IDeclarationOptions interface used to provide default | ||
* values to fields. | ||
* | ||
* @private | ||
*/ | ||
export declare class DeclarationOptions implements IDeclarationOptions { | ||
encoding?: string; | ||
standalone?: string; | ||
version: string; | ||
constructor(declarationOptions?: IDeclarationOptions); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -18,12 +17,12 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
var utils_1 = require("./utils"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var validate_1 = require("./validate"); | ||
/** | ||
* Implementation of the IStringOptions interface used to provide default values | ||
* to fields. | ||
* Implementation of the IStringOptions interface used to provide default | ||
* values to fields. | ||
* | ||
* @private | ||
*/ | ||
var StringOptions = (function () { | ||
function StringOptions(stringOptions) { | ||
if (stringOptions === void 0) { stringOptions = {}; } | ||
var StringOptions = /** @class */ (function () { | ||
function StringOptions(options) { | ||
this.doubleQuotes = false; | ||
@@ -33,41 +32,14 @@ this.indent = " "; | ||
this.pretty = true; | ||
if (!utils_1.isObject(stringOptions)) { | ||
throw new TypeError("options should be an Object or undefined"); | ||
if (!validate_1.isUndefined(options.doubleQuotes)) { | ||
this.doubleQuotes = options.doubleQuotes; | ||
} | ||
if (!utils_1.isBoolean(stringOptions.doubleQuotes)) { | ||
if (!utils_1.isUndefined(stringOptions.doubleQuotes)) { | ||
throw new TypeError("options.doubleQuotes should be a boolean" | ||
+ " or undefined"); | ||
} | ||
if (!validate_1.isUndefined(options.indent)) { | ||
this.indent = options.indent; | ||
} | ||
else { | ||
this.doubleQuotes = stringOptions.doubleQuotes; | ||
if (!validate_1.isUndefined(options.newline)) { | ||
this.newline = options.newline; | ||
} | ||
if (!utils_1.isString(stringOptions.indent)) { | ||
if (!utils_1.isUndefined(stringOptions.indent)) { | ||
throw new TypeError("options.indent should be a string" | ||
+ " or undefined"); | ||
} | ||
if (!validate_1.isUndefined(options.pretty)) { | ||
this.pretty = options.pretty; | ||
} | ||
else { | ||
this.indent = stringOptions.indent; | ||
} | ||
if (!utils_1.isString(stringOptions.newline)) { | ||
if (!utils_1.isUndefined(stringOptions.newline)) { | ||
throw new TypeError("options.newline should be a string" | ||
+ " or undefined"); | ||
} | ||
} | ||
else { | ||
this.newline = stringOptions.newline; | ||
} | ||
if (!utils_1.isBoolean(stringOptions.pretty)) { | ||
if (!utils_1.isUndefined(stringOptions.pretty)) { | ||
throw new TypeError("options.pretty should be a boolean" | ||
+ " or undefined"); | ||
} | ||
} | ||
else { | ||
this.pretty = stringOptions.pretty; | ||
} | ||
} | ||
@@ -77,45 +49,1 @@ return StringOptions; | ||
exports.StringOptions = StringOptions; | ||
/** | ||
* Implementation of the IDeclarationOptions interface used to provide default | ||
* values to fields. | ||
* | ||
* @private | ||
*/ | ||
var DeclarationOptions = (function () { | ||
function DeclarationOptions(declarationOptions) { | ||
if (declarationOptions === void 0) { declarationOptions = {}; } | ||
this.version = "1.0"; | ||
if (!utils_1.isObject(declarationOptions)) { | ||
throw new TypeError("options should be an Object or undefined"); | ||
} | ||
if (!utils_1.isString(declarationOptions.encoding)) { | ||
if (!utils_1.isUndefined(declarationOptions.encoding)) { | ||
throw new TypeError("options.encoding should be a string" | ||
+ " or undefined"); | ||
} | ||
} | ||
else { | ||
this.encoding = declarationOptions.encoding; | ||
} | ||
if (!utils_1.isString(declarationOptions.standalone)) { | ||
if (!utils_1.isUndefined(declarationOptions.standalone)) { | ||
throw new TypeError("options.standalone should be a string" | ||
+ " or undefined"); | ||
} | ||
} | ||
else { | ||
this.standalone = declarationOptions.standalone; | ||
} | ||
if (!utils_1.isString(declarationOptions.version)) { | ||
if (!utils_1.isUndefined(declarationOptions.version)) { | ||
throw new TypeError("options.version should be a string" | ||
+ " or undefined"); | ||
} | ||
} | ||
else { | ||
this.version = declarationOptions.version; | ||
} | ||
} | ||
return DeclarationOptions; | ||
}()); | ||
exports.DeclarationOptions = DeclarationOptions; |
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -17,10 +17,5 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
/** | ||
* Verifies that the specified string only contains characters permitted by the | ||
* XML specification. | ||
* Returns true if the specified string only contains characters permitted by | ||
* the XML specification. | ||
* | ||
* @param str The string to validate. | ||
* | ||
* @returns Whether the specified string only contains characters permitted by | ||
* the XML specification. | ||
* | ||
* @private | ||
@@ -30,10 +25,13 @@ */ | ||
/** | ||
* Verifies that the specified string only contains a single character, and | ||
* 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. | ||
* | ||
* @private | ||
*/ | ||
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. | ||
* | ||
* @param str The string to validate. | ||
* | ||
* @returns Whether the specified string only contains a single character, and | ||
* that this character is permitted by the XML specification. | ||
* | ||
* @private | ||
@@ -43,12 +41,21 @@ */ | ||
/** | ||
* Verifies that the specified string only contains characters permitted by the | ||
* XML specification for names. | ||
* Returns true if the specified string only contains characters permitted by | ||
* the XML specification for names. | ||
* | ||
* @param str The string to validate. | ||
* @private | ||
*/ | ||
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. | ||
* | ||
* @returns Whether the specified string only contains characters permitted by | ||
* the XML specification for names. | ||
* @private | ||
*/ | ||
export declare function fixName(str: string): string; | ||
/** | ||
* Returns true if the specified value is undefined. | ||
* | ||
* @private | ||
*/ | ||
export declare function validateName(str: string): boolean; | ||
export declare function isUndefined(val: any): val is undefined; |
@@ -0,3 +1,4 @@ | ||
"use strict"; | ||
/** | ||
* Copyright (C) 2016 Michael Kourlas | ||
* Copyright (C) 2016-2019 Michael Kourlas | ||
* | ||
@@ -16,56 +17,95 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Verifies that the specified string only contains characters permitted by the | ||
* XML specification. | ||
* Returns true if the specified string only contains characters permitted by | ||
* the XML specification. | ||
* | ||
* @param str The string to validate. | ||
* | ||
* @returns Whether the specified string only contains characters permitted by | ||
* the XML specification. | ||
* | ||
* @private | ||
*/ | ||
function validateChar(str) { | ||
var charRegex = "\\u0009|\\u000A|\\u000D|[\\u0020-\\uD7FF]|" | ||
+ "[\\uE000-\\uFFFD]"; | ||
var surrogateCharRegex = "[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]"; | ||
return new RegExp("^((" + charRegex + ")|(" + surrogateCharRegex + "))*$") | ||
.test(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; | ||
/** | ||
* Verifies that the specified string only contains a single character, and | ||
* 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. | ||
* | ||
* @private | ||
*/ | ||
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. | ||
* | ||
* @param str The string to validate. | ||
* | ||
* @returns Whether the specified string only contains a single character, and | ||
* that this character is permitted by the XML specification. | ||
* | ||
* @private | ||
*/ | ||
function validateSingleChar(str) { | ||
if (str.length === 0) { | ||
return false; | ||
} | ||
var firstChar = str.charCodeAt(0); | ||
if (str.length === 1) { | ||
return new RegExp("^\\u0009|\\u000A|\\u000D|[\\u0020-\\uD7FF]|" | ||
+ "[\\uE000-\\uFFFD]$").test(str); | ||
return (firstChar === 0x9 || firstChar === 0xA || firstChar === 0xD | ||
|| (firstChar >= 0x20 && firstChar <= 0xD7FF) | ||
|| (firstChar >= 0xE000 && firstChar <= 0xFFFD)); | ||
} | ||
else if (str.length === 2) { | ||
return new RegExp("^[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]$").test(str); | ||
} | ||
else { | ||
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; | ||
/** | ||
* Verifies that the specified string only contains characters permitted by the | ||
* XML specification for names. | ||
* Returns true if the specified string only contains characters permitted by | ||
* the XML specification for names. | ||
* | ||
* @param str The string to validate. | ||
* | ||
* @returns Whether the specified string only contains characters permitted by | ||
* the XML specification for names. | ||
* | ||
* @private | ||
@@ -77,32 +117,171 @@ */ | ||
} | ||
var nameStartChar = ":|[A-Z]|_|[a-z]|[\\u00C0-\\u00D6]|[\\u00D8-\\u00F6]" | ||
+ "|[\\u00F8-\\u02FF]|[\\u0370-\\u037D]" | ||
+ "|[\\u037F-\\u1FFF]|[\\u200C-\\u200D]" | ||
+ "|[\\u2070-\\u218F]|[\\u2C00-\\u2FEF]" | ||
+ "|[\\u3001-\\uD7FF]|[\\uF900-\\uFDCF]" | ||
+ "|[\\uFDF0-\\uFFFD]"; | ||
var nameStartCharWithSurrogatePair = "[\\uD800-\\uDB7F][\\uDC00-\\uDFFF]"; | ||
var nameChar = nameStartChar + "|-|\\.|[0-9]|\\u00B7|[\\u0300-\\u036F]" + | ||
"|[\\u203F-\\u2040]"; | ||
var nameCharWithSurrogatePair = nameChar + "|" + | ||
nameStartCharWithSurrogatePair; | ||
if (new RegExp("^" + nameStartChar + "$").test(str.charAt(0))) { | ||
if (str.length === 1) { | ||
return true; | ||
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; | ||
} | ||
return new RegExp("^(" + nameCharWithSurrogatePair + ")+$") | ||
.test(str.substr(1)); | ||
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; | ||
} | ||
else if (str.length >= 2) { | ||
if (new RegExp("^" + nameStartCharWithSurrogatePair + "$") | ||
.test(str.substr(0, 2))) { | ||
if (str.length === 2) { | ||
return true; | ||
} | ||
return new RegExp("^(" + nameCharWithSurrogatePair + ")+$") | ||
.test(str.substr(2)); | ||
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. | ||
* | ||
* @private | ||
*/ | ||
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; | ||
} | ||
return false; | ||
// 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.validateName = validateName; | ||
exports.fixName = fixName; | ||
/** | ||
* Returns true if the specified value is undefined. | ||
* | ||
* @private | ||
*/ | ||
function isUndefined(val) { | ||
return Object.prototype.toString.call(val) === "[object Undefined]"; | ||
} | ||
exports.isUndefined = isUndefined; |
xmlcreate | ||
Copyright (C) 2016-2017 Michael Kourlas | ||
Copyright (C) 2016-2019 Michael Kourlas |
{ | ||
"name": "xmlcreate", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Simple XML builder for Node.js", | ||
@@ -32,18 +32,18 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/chai": "^3.4.35", | ||
"@types/mocha": "^2.2.39", | ||
"chai": "^3.5.0", | ||
"del": "^2.2.2", | ||
"gulp": "^3.9.1", | ||
"gulp-mocha": "^4.0.1", | ||
"gulp-sourcemaps": "^2.4.1", | ||
"gulp-tslint": "^7.1.0", | ||
"gulp-typedoc": "^2.0.2", | ||
"gulp-typescript": "^3.1.5", | ||
"merge2": "^1.0.3", | ||
"mocha": "^3.2.0", | ||
"tslint": "^4.5.1", | ||
"typedoc": "^0.5.7", | ||
"typescript": "^2.2.1" | ||
"@types/chai": "^4.1.7", | ||
"@types/mocha": "^5.2.6", | ||
"chai": "^4.2.0", | ||
"del": "^3.0.0", | ||
"gulp": "^4.0.0", | ||
"gulp-mocha": "^6.0.0", | ||
"gulp-sourcemaps": "^2.6.5", | ||
"gulp-tslint": "^8.1.4", | ||
"gulp-typedoc": "^2.2.2", | ||
"gulp-typescript": "^5.0.0", | ||
"merge2": "^1.2.3", | ||
"mocha": "^6.0.1", | ||
"tslint": "^5.12.1", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.3.3333" | ||
} | ||
} |
112
README.md
@@ -8,34 +8,15 @@ # xmlcreate # | ||
xmlcreate is a Node.js module that can be used to easily build XML using a | ||
simple API. | ||
xmlcreate is a Node.js module that can be used to build XML using a simple API. | ||
## Features ## | ||
xmlcreate uses a DOM-style API to build XML. Each construct in XML, including | ||
elements, attributes, and even the document itself, is represented as a node | ||
in a tree with a document node at the root. | ||
xmlcreate allows you to use a series of chained function calls to build an XML | ||
tree. | ||
Each node has a set of properties corresponding to the node's properties in | ||
XML. For example, the element node has a name property, while the CDATA node | ||
has a character data property. | ||
Once the tree is built, it can be serialized to text. The formatting of the | ||
text is customizable. | ||
Each node also has a set of children corresponding to the node's value, | ||
children or attributes in XML, depending on the context. For example, the XML | ||
declaration and document type declaration are represented as child nodes of the | ||
root document node, while sub-elements and element attributes are represented | ||
as child nodes of element nodes. | ||
xmlcreate can perform some basic validation to check that the resulting XML | ||
is well-formed. | ||
xmlcreate performs some basic validation during tree building to ensure that | ||
the resulting XML is well-formed. Nodes can only have certain types or | ||
quantities of nodes as children, while node properties are checked to ensure | ||
that they do not contain characters disallowed in XML for that node or in | ||
general. | ||
However, xmlcreate does not perform comprehensive validation. For example, it | ||
does not match the tree structure against a schema or confirm that entity | ||
references are valid. You should use a XML parser for this purpose instead. | ||
Once the tree is built, the tree itself or any of its subtrees can be | ||
serialized to text. The formatting of the text is customizable. | ||
## Installation ## | ||
@@ -49,3 +30,3 @@ | ||
You can also build xmlcreate from source using npm, gulp, and typings: | ||
You can also build xmlcreate from source using npm and gulp: | ||
@@ -55,13 +36,5 @@ ``` | ||
npm install | ||
gulp | ||
./node_modules/.bin/gulp | ||
``` | ||
You'll need to install gulp first if you don't have it: | ||
``` | ||
npm install -g gulp | ||
``` | ||
You can then copy the folder into your node_modules directory. | ||
The `default` target will build the production variant of xmlcreate, run all | ||
@@ -71,9 +44,9 @@ tests, and build the documentation. | ||
You can build the production variant without running tests using the target | ||
`prod`. You can also build the development version using the target `dev`. At | ||
the moment, the only difference between the two is that the development version | ||
includes source maps. | ||
`prod`. You can also build the development version using the target `dev`. | ||
The only difference between the two is that the development version includes | ||
source maps. | ||
## Usage ## | ||
The documentation for the current version is available [here](http://www.kourlas.com/node-xmlcreate/docs/1.0.2/). | ||
The documentation for the current version is available [here](http://www.kourlas.com/node-xmlcreate/docs/2.0.0/). | ||
@@ -88,36 +61,51 @@ You can also build the documentation using gulp: | ||
The following example illustrates the basic usage of xmlcreate: | ||
The following TypeScript example illustrates the basic usage of xmlcreate: | ||
```javascript | ||
var xmlcreate = require("xmlcreate"); | ||
```typescript | ||
import {document} from "xmlcreate"; | ||
var document = xmlcreate.document("html"); | ||
document | ||
const tree = document(); | ||
tree | ||
.decl({encoding: "UTF-8"}) | ||
.up() | ||
.dtd("html", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", | ||
"-//W3C//DTD XHTML 1.0 Strict//EN") | ||
.dtd({ | ||
name: "html", | ||
pubId: "-//W3C//DTD XHTML 1.0 Strict//EN", | ||
sysId: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" | ||
}) | ||
.up() | ||
.root() | ||
.attribute("xmlns", "http://www.w3.org/1999/xhtml") | ||
.element({name: "html"}) | ||
.attribute({name: "xmlns"}) | ||
.text({charData: "http://www.w3.org/1999/xhtml"}) | ||
.up() | ||
.up() | ||
.attribute("xml:lang", "en") | ||
.attribute({name: "xml:lang"}) | ||
.text({charData: "en"}) | ||
.up() | ||
.up() | ||
.attribute("lang", "en") | ||
.up() | ||
.element("head") | ||
.element("title") | ||
.charData("My page title") | ||
.element({name: "head"}) | ||
.element({name: "title"}) | ||
.charData({charData: "My page title"}) | ||
.up() | ||
.up() | ||
.up() | ||
.element("body") | ||
.element("h1") | ||
.charData("Welcome!") | ||
.element({name: "body"}) | ||
.element({name: "h1"}) | ||
.charData({charData: "Welcome!"}) | ||
.up() | ||
.up() | ||
.element("p") | ||
.charData("This is some text on my website."); | ||
.element({name: "p"}) | ||
.charData({charData: "This is some text on my website."}) | ||
.up() | ||
.up() | ||
.element({name: "div"}) | ||
.element({name: "img"}) | ||
.attribute({name: "src"}) | ||
.text({charData: "picture.png"}) | ||
.up() | ||
.up() | ||
.attribute({name: "alt"}) | ||
.text({charData: "picture"}).up().up().up().up().up(); | ||
console.log(document.toString({doubleQuotes: true})); | ||
console.log(tree.toString({doubleQuotes: true})); | ||
``` | ||
@@ -141,3 +129,3 @@ | ||
Additional examples can be found in examples/example.js. | ||
A JavaScript version of this example can be found in the examples directory. | ||
@@ -144,0 +132,0 @@ ## Tests ## |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
159225
49
4145
143
1