XmlCharRef.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 character reference.
  22. *
  23. * A character reference is structured as follows, where `{dec}` is the
  24. * decimal representation code point corresponding to a particular Unicode
  25. * character:
  26. *
  27. * ```xml
  28. * &#{dec};
  29. * ```
  30. *
  31. * The corresponding hexadecimal version is structured as follows, where
  32. * `{hex}` is the hexadecimal representation code point corresponding to a
  33. * particular Unicode character:
  34. *
  35. * ```xml
  36. * &#x{hex};
  37. * ```
  38. *
  39. * Unicode characters outside of the Basic Multilingual Plane are represented
  40. * using a surrogate pair consisting of two character references.
  41. *
  42. * The `{dec}` and `{hex}` values are defined by the `char` and `hex`
  43. * properties of this node; the former is the character to be represented while
  44. * the latter indicates whether the decimal or hexadecimal representation
  45. * should be used.
  46. */
  47. var XmlCharRef = /** @class */ (function () {
  48. function XmlCharRef(parent, validation, options) {
  49. this._hex = false;
  50. this._validation = validation;
  51. this._parent = parent;
  52. this.char = options.char;
  53. if (!(0, validate_1.isUndefined)(options.hex)) {
  54. this.hex = options.hex;
  55. }
  56. }
  57. Object.defineProperty(XmlCharRef.prototype, "char", {
  58. /**
  59. * Gets the character of this character reference.
  60. */
  61. get: function () {
  62. return this._char;
  63. },
  64. /**
  65. * Sets the character of this character reference.
  66. */
  67. set: function (char) {
  68. if (this._validation && !(0, validate_1.validateSingleChar)(char)) {
  69. throw new Error((0, error_1.getContext)(this.up()) + ": character reference"
  70. + (" \"" + char + "\" should reference a single character,")
  71. + " and this character should be allowed in XML");
  72. }
  73. this._char = char;
  74. },
  75. enumerable: false,
  76. configurable: true
  77. });
  78. Object.defineProperty(XmlCharRef.prototype, "hex", {
  79. /**
  80. * Gets whether the decimal or hexadecimal representation should be used
  81. * for this character reference.
  82. */
  83. get: function () {
  84. return this._hex;
  85. },
  86. /**
  87. * Sets whether the decimal or hexadecimal representation should be used
  88. * for this character reference.
  89. */
  90. set: function (hex) {
  91. this._hex = hex;
  92. },
  93. enumerable: false,
  94. configurable: true
  95. });
  96. /**
  97. * Returns an XML string representation of this character reference.
  98. */
  99. XmlCharRef.prototype.toString = function () {
  100. var char;
  101. if (this._char.length === 1) {
  102. char = this._char.charCodeAt(0);
  103. }
  104. else {
  105. var first = this._char.charCodeAt(0);
  106. if (first >= 0xD800 && first <= 0xDBFF && this._char.length > 1) {
  107. var second = this._char.charCodeAt(1);
  108. if (second >= 0xDC00 && second <= 0xDFFF) {
  109. char = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  110. }
  111. else {
  112. throw new Error((0, error_1.getContext)(this.up()) + ": character"
  113. + (" reference \"" + this.char + "\" should")
  114. + " reference a valid Unicode character");
  115. }
  116. }
  117. else {
  118. char = first;
  119. }
  120. }
  121. if (this._hex) {
  122. return "&#x" + char.toString(16) + ";";
  123. }
  124. else {
  125. return "&#" + char + ";";
  126. }
  127. };
  128. /**
  129. * Returns the parent of this character reference.
  130. */
  131. XmlCharRef.prototype.up = function () {
  132. return this._parent;
  133. };
  134. return XmlCharRef;
  135. }());
  136. exports.default = XmlCharRef;