XmlComment.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 comment.
  22. *
  23. * A comment is structured as follows, where `{content}` is the text of the
  24. * comment:
  25. *
  26. * ```xml
  27. * <!--{content}-->
  28. * ```
  29. */
  30. var XmlComment = /** @class */ (function () {
  31. function XmlComment(parent, validation, options) {
  32. this._validation = validation;
  33. if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInCharData)) {
  34. this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData);
  35. }
  36. else {
  37. this._replaceInvalidCharsInCharData = false;
  38. }
  39. this._parent = parent;
  40. this.charData = options.charData;
  41. }
  42. Object.defineProperty(XmlComment.prototype, "charData", {
  43. /**
  44. * Gets the text of this comment.
  45. */
  46. get: function () {
  47. return this._charData;
  48. },
  49. /**
  50. * Sets the text of this comment.
  51. */
  52. set: function (charData) {
  53. if (this._replaceInvalidCharsInCharData) {
  54. charData = (0, validate_1.fixChar)(charData);
  55. }
  56. else if (this._validation && !(0, validate_1.validateChar)(charData)) {
  57. throw new Error((0, error_1.getContext)(this.up()) + ": comment content"
  58. + (" \"" + charData + "\" should not contain characters")
  59. + " not allowed in XML");
  60. }
  61. if (this._replaceInvalidCharsInCharData) {
  62. charData = charData.replace("--", "\uFFFD\uFFFD");
  63. }
  64. else if (this._validation && charData.indexOf("--") !== -1) {
  65. throw new Error((0, error_1.getContext)(this.up()) + ": comment content"
  66. + (" \"" + charData + "\" should not contain the string")
  67. + " '--'");
  68. }
  69. if (this._replaceInvalidCharsInCharData) {
  70. if (charData.lastIndexOf("-") === charData.length - 1) {
  71. charData = charData.substr(0, charData.length - 1) + "\uFFFD";
  72. }
  73. }
  74. else if (this._validation
  75. && charData.lastIndexOf("-") === charData.length - 1) {
  76. throw new Error((0, error_1.getContext)(this.up()) + ": comment content"
  77. + (" \"" + charData + "\" should not end with the string")
  78. + " '-'");
  79. }
  80. this._charData = charData;
  81. },
  82. enumerable: false,
  83. configurable: true
  84. });
  85. /**
  86. * Returns an XML string representation of this comment.
  87. */
  88. XmlComment.prototype.toString = function () {
  89. return "<!--" + this._charData + "-->";
  90. };
  91. /**
  92. * Returns the parent of this comment.
  93. */
  94. XmlComment.prototype.up = function () {
  95. return this._parent;
  96. };
  97. return XmlComment;
  98. }());
  99. exports.default = XmlComment;