XmlProcInst.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 validate_1 = require("../validate");
  20. /**
  21. * Represents a processing instruction.
  22. *
  23. * A processing instruction is structured as follows, where `{target}` and
  24. * `{content}` are the target and content of the processing instruction
  25. * respectively:
  26. *
  27. * ```xml
  28. * <?{target} {content}?>
  29. * ```
  30. */
  31. var XmlProcInst = /** @class */ (function () {
  32. function XmlProcInst(parent, validation, options) {
  33. this._validation = validation;
  34. this._parent = parent;
  35. this.content = options.content;
  36. this.target = options.target;
  37. }
  38. Object.defineProperty(XmlProcInst.prototype, "content", {
  39. /**
  40. * Gets the content of this processing instruction.
  41. */
  42. get: function () {
  43. return this._content;
  44. },
  45. /**
  46. * Sets the content of this processing instruction.
  47. */
  48. set: function (content) {
  49. if (!(0, validate_1.isUndefined)(content)) {
  50. if (this._validation && !(0, validate_1.validateChar)(content)) {
  51. throw new Error((0, error_1.getContext)(this.up()) + ": processing"
  52. + (" instruction content \"" + content + "\" should")
  53. + " not contain characters not allowed in XML");
  54. }
  55. else if (this._validation && content.indexOf("?>") !== -1) {
  56. throw new Error((0, error_1.getContext)(this.up()) + ": processing"
  57. + (" instruction content \"" + content + "\" should")
  58. + " not contain the string '?>'");
  59. }
  60. }
  61. this._content = content;
  62. },
  63. enumerable: false,
  64. configurable: true
  65. });
  66. Object.defineProperty(XmlProcInst.prototype, "target", {
  67. /**
  68. * Gets the target of this processing instruction.
  69. */
  70. get: function () {
  71. return this._target;
  72. },
  73. /**
  74. * Sets the content of this processing instruction.
  75. */
  76. set: function (target) {
  77. if (this._validation && !(0, validate_1.validateName)(target)) {
  78. throw new Error((0, error_1.getContext)(this.up()) + ": processing"
  79. + (" instruction target \"" + target + "\" should")
  80. + " not contain characters not allowed in XML"
  81. + " names");
  82. }
  83. if (this._validation && target === "xml") {
  84. throw new Error((0, error_1.getContext)(this.up()) + ": processing"
  85. + (" instruction target \"" + target + "\" should")
  86. + " not be the string 'xml'");
  87. }
  88. this._target = target;
  89. },
  90. enumerable: false,
  91. configurable: true
  92. });
  93. /**
  94. * Returns an XML string representation of this processing instruction.
  95. */
  96. XmlProcInst.prototype.toString = function () {
  97. if ((0, validate_1.isUndefined)(this._content)) {
  98. return "<?" + this._target + "?>";
  99. }
  100. else {
  101. return "<?" + this._target + " " + this._content + "?>";
  102. }
  103. };
  104. /**
  105. * Returns the parent of this processing instruction.
  106. */
  107. XmlProcInst.prototype.up = function () {
  108. return this._parent;
  109. };
  110. return XmlProcInst;
  111. }());
  112. exports.default = XmlProcInst;