XmlDecl.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. Object.defineProperty(exports, "__esModule", { value: true });
  18. var error_1 = require("../error");
  19. var options_1 = require("../options");
  20. var validate_1 = require("../validate");
  21. /**
  22. * Represents a declaration.
  23. *
  24. * A declaration is structured as follows, where `{version}` is the XML
  25. * version, `{encoding}` is the encoding of the document, and `{standalone}`
  26. * is either "yes" or "no", depending on whether the document may contain
  27. * external markup declarations:
  28. *
  29. * ```xml
  30. * <?xml version="{version}" encoding="{encoding}" standalone="{standalone}"?>
  31. * ```
  32. */
  33. var XmlDecl = /** @class */ (function () {
  34. function XmlDecl(parent, validation, options) {
  35. this._version = "1.0";
  36. this._validation = validation;
  37. this._parent = parent;
  38. this.encoding = options.encoding;
  39. this.standalone = options.standalone;
  40. if (!(0, validate_1.isUndefined)(options.version)) {
  41. this.version = options.version;
  42. }
  43. }
  44. Object.defineProperty(XmlDecl.prototype, "encoding", {
  45. /**
  46. * Gets the encoding associated with this declaration.
  47. */
  48. get: function () {
  49. return this._encoding;
  50. },
  51. /**
  52. * Sets the encoding associated with this declaration.
  53. */
  54. set: function (encoding) {
  55. if (this._validation && !(0, validate_1.isUndefined)(encoding)) {
  56. if (!validateEncoding(encoding)) {
  57. throw new Error((0, error_1.getContext)(this.up()) + ": declaration"
  58. + (" encoding attribute " + encoding + " should be a")
  59. + " valid encoding");
  60. }
  61. }
  62. this._encoding = encoding;
  63. },
  64. enumerable: false,
  65. configurable: true
  66. });
  67. Object.defineProperty(XmlDecl.prototype, "standalone", {
  68. /**
  69. * Gets the value of the standalone attribute associated with this
  70. * declaration.
  71. */
  72. get: function () {
  73. return this._standalone;
  74. },
  75. /**
  76. * Sets the value of the standalone attribute associated with this
  77. * declaration.
  78. */
  79. set: function (standalone) {
  80. if (this._validation && !(0, validate_1.isUndefined)(standalone)) {
  81. if (standalone !== "yes" && standalone !== "no") {
  82. throw new Error((0, error_1.getContext)(this.up()) + ": declaration"
  83. + (" standalone attribute " + standalone + " should")
  84. + " be the string 'yes' or the string 'no'");
  85. }
  86. }
  87. this._standalone = standalone;
  88. },
  89. enumerable: false,
  90. configurable: true
  91. });
  92. Object.defineProperty(XmlDecl.prototype, "version", {
  93. /**
  94. * Gets the XML version associated with this declaration.
  95. */
  96. get: function () {
  97. return this._version;
  98. },
  99. /**
  100. * Sets the XML version associated with this declaration.
  101. */
  102. set: function (version) {
  103. if (this._validation && !validateVersion(version)) {
  104. throw new Error((0, error_1.getContext)(this.up()) + ": declaration version"
  105. + (" attribute " + version + " should be a valid XML")
  106. + " version");
  107. }
  108. this._version = version;
  109. },
  110. enumerable: false,
  111. configurable: true
  112. });
  113. /**
  114. * Returns an XML string representation of this declaration.
  115. */
  116. XmlDecl.prototype.toString = function (options) {
  117. if (options === void 0) { options = {}; }
  118. var optionsObj = new options_1.StringOptions(options);
  119. var quote = optionsObj.doubleQuotes ? '"' : "'";
  120. var str = "<?xml version=" + quote + this._version + quote;
  121. if (!(0, validate_1.isUndefined)(this._encoding)) {
  122. str += " encoding=" + quote + this._encoding + quote;
  123. }
  124. if (!(0, validate_1.isUndefined)(this._standalone)) {
  125. str += " standalone=" + quote + this._standalone + quote;
  126. }
  127. str += "?>";
  128. return str;
  129. };
  130. /**
  131. * Returns the parent of this declaration.
  132. */
  133. XmlDecl.prototype.up = function () {
  134. return this._parent;
  135. };
  136. return XmlDecl;
  137. }());
  138. exports.default = XmlDecl;
  139. /**
  140. * Returns true if the specified encoding only contains characters permitted by
  141. * the XML specification.
  142. */
  143. function validateEncoding(str) {
  144. if (str.length === 0) {
  145. return false;
  146. }
  147. var initialChar = str.charCodeAt(0);
  148. if (!((initialChar >= 0x41 && initialChar <= 0x5A)
  149. || (initialChar >= 0x61 && initialChar <= 0x7A))) {
  150. return false;
  151. }
  152. for (var i = 1; i < str.length; i++) {
  153. var char = str.charCodeAt(i);
  154. if (char === 0x5F
  155. || char === 0x2D
  156. || char === 0x2E
  157. || (char >= 0x30 && char <= 0x39)
  158. || (char >= 0x41 && char <= 0x5A)
  159. || (char >= 0x61 && char <= 0x7A)) {
  160. continue;
  161. }
  162. if (i + 1 === str.length) {
  163. return false;
  164. }
  165. return false;
  166. }
  167. return true;
  168. }
  169. /**
  170. * Returns true if the specified version only contains characters permitted by
  171. * the XML specification.
  172. */
  173. function validateVersion(str) {
  174. for (var i = 0; i <= 9; i++) {
  175. if (str === "1." + i) {
  176. return true;
  177. }
  178. }
  179. return false;
  180. }