XmlAttributeText.js 2.8 KB

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