XmlCharData.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 escape_1 = require("../escape");
  20. var validate_1 = require("../validate");
  21. /**
  22. * Represents character data.
  23. *
  24. * Restricted characters, such as the ampersand (`&`), the opening angle
  25. * bracket (`<`), and the closing angle bracket (`>`) when it appears in the
  26. * string `]]>`, are all automatically escaped.
  27. */
  28. var XmlCharData = /** @class */ (function () {
  29. function XmlCharData(parent, validation, options) {
  30. this._validation = validation;
  31. if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInCharData)) {
  32. this._replaceInvalidCharsInCharData = (options.replaceInvalidCharsInCharData);
  33. }
  34. else {
  35. this._replaceInvalidCharsInCharData = false;
  36. }
  37. this._parent = parent;
  38. this.charData = options.charData;
  39. }
  40. Object.defineProperty(XmlCharData.prototype, "charData", {
  41. /**
  42. * Gets the text of this character data.
  43. */
  44. get: function () {
  45. return this._charData;
  46. },
  47. /**
  48. * Sets the text of this character data.
  49. */
  50. set: function (charData) {
  51. if (this._replaceInvalidCharsInCharData) {
  52. charData = (0, validate_1.fixChar)(charData);
  53. }
  54. else if (this._validation && !(0, validate_1.validateChar)(charData)) {
  55. throw new Error((0, error_1.getContext)(this.up()) + ": character data"
  56. + ("\"" + charData + "\" should not contain characters not")
  57. + " allowed in XML");
  58. }
  59. this._charData = charData;
  60. },
  61. enumerable: false,
  62. configurable: true
  63. });
  64. /**
  65. * Returns an XML string representation of this character data.
  66. */
  67. XmlCharData.prototype.toString = function () {
  68. var str = this._charData;
  69. str = (0, escape_1.escapeAmpersands)(str);
  70. str = (0, escape_1.escapeLeftAngleBrackets)(str);
  71. str = (0, escape_1.escapeRightAngleBracketsInCdataTerminator)(str);
  72. return str;
  73. };
  74. /**
  75. * Returns the parent of this character data.
  76. */
  77. XmlCharData.prototype.up = function () {
  78. return this._parent;
  79. };
  80. return XmlCharData;
  81. }());
  82. exports.default = XmlCharData;