XmlDocument.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. "use strict";
  2. /**
  3. * Copyright (C) 2016-2019 Michael Kourlas
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. var __importDefault = (this && this.__importDefault) || function (mod) {
  18. return (mod && mod.__esModule) ? mod : { "default": mod };
  19. };
  20. Object.defineProperty(exports, "__esModule", { value: true });
  21. var options_1 = require("../options");
  22. var validate_1 = require("../validate");
  23. var XmlComment_1 = __importDefault(require("./XmlComment"));
  24. var XmlDecl_1 = __importDefault(require("./XmlDecl"));
  25. var XmlDtd_1 = __importDefault(require("./XmlDtd"));
  26. var XmlElement_1 = __importDefault(require("./XmlElement"));
  27. var XmlProcInst_1 = __importDefault(require("./XmlProcInst"));
  28. /**
  29. * Represents a document.
  30. *
  31. * A sample document is structured as follows:
  32. *
  33. * ```xml
  34. * <?xml version="1.0" encoding="UTF-8"?>
  35. * <DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  36. * "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  37. * <html>
  38. * <head>
  39. * <title>My page title</title>
  40. * </head>
  41. * <body>
  42. * <h1>Welcome!</h1>
  43. * <p>I hope you enjoy visiting my website.</p>
  44. * <img src="picture.png"/>
  45. * </body>
  46. * </html>
  47. * ```
  48. *
  49. * Each component of the document, such as the declaration, document type
  50. * definition, and root element, are children of this node.
  51. *
  52. * Documents must have exactly one element, which is the document's root
  53. * element.
  54. *
  55. * Documents can have exactly one declaration and one document type definition
  56. * in that order, so long as they precede the element.
  57. *
  58. * Documents can have an unlimited number of comments or processing
  59. * instructions, so long as they follow the declaration, if one exists.
  60. */
  61. var XmlDocument = /** @class */ (function () {
  62. function XmlDocument(options) {
  63. this._children = [];
  64. this._validation = !(0, validate_1.isUndefined)(options.validation)
  65. ? options.validation
  66. : true;
  67. }
  68. /**
  69. * Adds a comment to this document and returns the new comment.
  70. */
  71. XmlDocument.prototype.comment = function (options) {
  72. var comment = new XmlComment_1.default(this, this._validation, options);
  73. this._children.push(comment);
  74. return comment;
  75. };
  76. /**
  77. * Adds a declaration to this document and returns the new declaration.
  78. */
  79. XmlDocument.prototype.decl = function (options) {
  80. if (options === void 0) { options = {}; }
  81. if (this._validation && this._children.length !== 0) {
  82. throw new Error("in XML document: declaration must be the first"
  83. + " child");
  84. }
  85. var declaration = new XmlDecl_1.default(this, this._validation, options);
  86. this._children.push(declaration);
  87. return declaration;
  88. };
  89. /**
  90. * Adds a document type definition to this document and returns the new
  91. * document type definition.
  92. */
  93. XmlDocument.prototype.dtd = function (options) {
  94. var filteredChildren = this._children.filter(function (value) {
  95. return value instanceof XmlElement_1.default;
  96. });
  97. if (this._validation && filteredChildren.length !== 0) {
  98. throw new Error("in XML document: DTD must precede the root"
  99. + " element");
  100. }
  101. var dtd = new XmlDtd_1.default(this, this._validation, options);
  102. this._children.push(dtd);
  103. return dtd;
  104. };
  105. /**
  106. * Adds the root element to this document and returns the element.
  107. */
  108. XmlDocument.prototype.element = function (options) {
  109. var filteredChildren = this._children.filter(function (value) {
  110. return value instanceof XmlElement_1.default;
  111. });
  112. if (this._validation && filteredChildren.length !== 0) {
  113. throw new Error("in XML document: only one root element is"
  114. + " permitted");
  115. }
  116. var element = new XmlElement_1.default(this, this._validation, options);
  117. this._children.push(element);
  118. return element;
  119. };
  120. /**
  121. * Adds a processing instruction to this document and returns the new
  122. * processing instruction.
  123. */
  124. XmlDocument.prototype.procInst = function (options) {
  125. var procInst = new XmlProcInst_1.default(this, this._validation, options);
  126. this._children.push(procInst);
  127. return procInst;
  128. };
  129. /**
  130. * Returns an XML string representation of this document using the
  131. * specified options.
  132. */
  133. XmlDocument.prototype.toString = function (options) {
  134. if (options === void 0) { options = {}; }
  135. var filteredChildren = this._children.filter(function (value) {
  136. return value instanceof XmlElement_1.default;
  137. });
  138. if (this._validation && filteredChildren.length !== 1) {
  139. throw new Error("in XML document: no more than one root element"
  140. + " is permitted");
  141. }
  142. var optionsObj = new options_1.StringOptions(options);
  143. var str = "";
  144. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  145. var node = _a[_i];
  146. if (node instanceof XmlDecl_1.default
  147. || node instanceof XmlDtd_1.default
  148. || node instanceof XmlElement_1.default) {
  149. str += node.toString(options);
  150. }
  151. else {
  152. str += node.toString();
  153. }
  154. if (optionsObj.pretty) {
  155. str += optionsObj.newline;
  156. }
  157. }
  158. var len = str.length - optionsObj.newline.length;
  159. if (str.substr(len) === optionsObj.newline) {
  160. str = str.substr(0, len);
  161. }
  162. return str;
  163. };
  164. return XmlDocument;
  165. }());
  166. exports.default = XmlDocument;