XmlAttribute.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. var __importDefault = (this && this.__importDefault) || function (mod) {
  18. return (mod && mod.__esModule) ? mod : { "default": mod };
  19. };
  20. Object.defineProperty(exports, "__esModule", { value: true });
  21. var error_1 = require("../error");
  22. var escape_1 = require("../escape");
  23. var options_1 = require("../options");
  24. var validate_1 = require("../validate");
  25. var XmlAttributeText_1 = __importDefault(require("./XmlAttributeText"));
  26. var XmlCharRef_1 = __importDefault(require("./XmlCharRef"));
  27. var XmlEntityRef_1 = __importDefault(require("./XmlEntityRef"));
  28. /**
  29. * Represents an attribute.
  30. *
  31. * An attribute is part of the start tag of an element and is
  32. * structured as follows, where `{name}` is the name of the attribute and
  33. * `{value}` is the value of the attribute:
  34. *
  35. * ```xml
  36. * <element {name}="{value}">
  37. * ```
  38. *
  39. * The `{name}` value is a property of this node, while the `{value}` property
  40. * consists of the children of this node.
  41. *
  42. * Attributes can have an unlimited number of attribute text, character
  43. * references, and entity references.
  44. */
  45. var XmlAttribute = /** @class */ (function () {
  46. function XmlAttribute(parent, validation, options) {
  47. this._validation = validation;
  48. if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInName)) {
  49. this._replaceInvalidCharsInName = options.replaceInvalidCharsInName;
  50. }
  51. else {
  52. this._replaceInvalidCharsInName = false;
  53. }
  54. this._children = [];
  55. this._parent = parent;
  56. this.name = options.name;
  57. }
  58. Object.defineProperty(XmlAttribute.prototype, "name", {
  59. /**
  60. * Gets the name of this attribute.
  61. */
  62. get: function () {
  63. return this._name;
  64. },
  65. /**
  66. * Sets the name of this attribute.
  67. */
  68. set: function (name) {
  69. if (this._replaceInvalidCharsInName) {
  70. name = (0, validate_1.fixName)(name);
  71. if (name.length === 0) {
  72. throw new Error((0, error_1.getContext)(this.up()) + ": attribute name"
  73. + " should not be empty");
  74. }
  75. }
  76. else if (this._validation && !(0, validate_1.validateName)(name)) {
  77. if (name.length === 0) {
  78. throw new Error((0, error_1.getContext)(this.up()) + ": attribute name"
  79. + " should not be empty");
  80. }
  81. else {
  82. throw new Error((0, error_1.getContext)(this.up()) + ": attribute name"
  83. + (" \"" + name + "\" should not contain characters not")
  84. + " allowed in XML names");
  85. }
  86. }
  87. this._name = name;
  88. },
  89. enumerable: false,
  90. configurable: true
  91. });
  92. /**
  93. * Adds a character reference to this attribute and returns the new
  94. * character reference.
  95. */
  96. XmlAttribute.prototype.charRef = function (options) {
  97. var charRef = new XmlCharRef_1.default(this, this._validation, options);
  98. this._children.push(charRef);
  99. return charRef;
  100. };
  101. /**
  102. * Adds an entity reference to this attribute and returns the new entity
  103. * reference.
  104. */
  105. XmlAttribute.prototype.entityRef = function (options) {
  106. var charRef = new XmlEntityRef_1.default(this, this._validation, options);
  107. this._children.push(charRef);
  108. return charRef;
  109. };
  110. /**
  111. * Adds attribute text to this attribute and returns the new text.
  112. */
  113. XmlAttribute.prototype.text = function (options) {
  114. var textNode = new XmlAttributeText_1.default(this, this._validation, options);
  115. this._children.push(textNode);
  116. return textNode;
  117. };
  118. /**
  119. * Returns an XML string representation of this attribute.
  120. */
  121. XmlAttribute.prototype.toString = function (options) {
  122. if (options === void 0) { options = {}; }
  123. var optionsObj = new options_1.StringOptions(options);
  124. var quote = optionsObj.doubleQuotes ? "\"" : "'";
  125. var str = this._name + "=" + quote;
  126. for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
  127. var child = _a[_i];
  128. if (optionsObj.doubleQuotes) {
  129. str += (0, escape_1.escapeDoubleQuotes)(child.toString());
  130. }
  131. else {
  132. str += (0, escape_1.escapeSingleQuotes)(child.toString());
  133. }
  134. }
  135. str += quote;
  136. return str;
  137. };
  138. /**
  139. * Returns the parent of this attribute.
  140. */
  141. XmlAttribute.prototype.up = function () {
  142. return this._parent;
  143. };
  144. return XmlAttribute;
  145. }());
  146. exports.default = XmlAttribute;