XmlCdata.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 CDATA section.
  22. *
  23. * A CDATA section is structured as follows, where `{data}` is the
  24. * character data of the section:
  25. *
  26. * ```xml
  27. * <![CDATA[{data}]]>
  28. * ```
  29. */
  30. var XmlCdata = /** @class */ (function () {
  31. function XmlCdata(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(XmlCdata.prototype, "charData", {
  43. /**
  44. * Gets the character data of this CDATA section.
  45. */
  46. get: function () {
  47. return this._charData;
  48. },
  49. /**
  50. * Sets the character data of this CDATA section.
  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()) + ": CDATA section"
  58. + (" \"" + charData + "\" should not contain characters")
  59. + " not allowed in XML");
  60. }
  61. if (this._replaceInvalidCharsInCharData) {
  62. charData = charData.replace("]]>", "\uFFFD\uFFFD\uFFFD");
  63. }
  64. else if (this._validation && charData.indexOf("]]>") !== -1) {
  65. throw new Error((0, error_1.getContext)(this.up()) + ": CDATA section"
  66. + (" \"" + charData + "\" should not contain the string")
  67. + " ']]>'");
  68. }
  69. this._charData = charData;
  70. },
  71. enumerable: false,
  72. configurable: true
  73. });
  74. /**
  75. * Returns an XML string representation of this CDATA section.
  76. */
  77. XmlCdata.prototype.toString = function () {
  78. return "<![CDATA[" + this._charData + "]]>";
  79. };
  80. /**
  81. * Returns the parent of this CDATA section.
  82. */
  83. XmlCdata.prototype.up = function () {
  84. return this._parent;
  85. };
  86. return XmlCdata;
  87. }());
  88. exports.default = XmlCdata;