版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CharacterData.js 6.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const Node_1 = __importDefault(require("../node/Node"));
  7. const CharacterDataUtility_1 = __importDefault(require("./CharacterDataUtility"));
  8. const NonDocumentChildNodeUtility_1 = __importDefault(require("../child-node/NonDocumentChildNodeUtility"));
  9. const ChildNodeUtility_1 = __importDefault(require("../child-node/ChildNodeUtility"));
  10. const MutationRecord_1 = __importDefault(require("../../mutation-observer/MutationRecord"));
  11. const MutationTypeEnum_1 = __importDefault(require("../../mutation-observer/MutationTypeEnum"));
  12. /**
  13. * Character data base class.
  14. *
  15. * Reference:
  16. * https://developer.mozilla.org/en-US/docs/Web/API/CharacterData.
  17. */
  18. class CharacterData extends Node_1.default {
  19. /**
  20. * Constructor.
  21. *
  22. * @param [data] Data.
  23. */
  24. constructor(data) {
  25. super();
  26. this._data = '';
  27. if (data) {
  28. this._data = data;
  29. }
  30. }
  31. /**
  32. * Returns text content.
  33. *
  34. * @returns Text content.
  35. */
  36. get length() {
  37. return this._data.length;
  38. }
  39. /**
  40. * Returns text content.
  41. *
  42. * @returns Text content.
  43. */
  44. get data() {
  45. return this._data;
  46. }
  47. /**
  48. * Sets text content.
  49. *
  50. * @param textContent Text content.
  51. */
  52. set data(data) {
  53. const oldValue = this._data;
  54. this._data = data;
  55. if (this.isConnected) {
  56. this.ownerDocument['_cacheID']++;
  57. }
  58. // MutationObserver
  59. if (this._observers.length > 0) {
  60. for (const observer of this._observers) {
  61. if (observer.options.characterData) {
  62. const record = new MutationRecord_1.default();
  63. record.target = this;
  64. record.type = MutationTypeEnum_1.default.characterData;
  65. record.oldValue = observer.options.characterDataOldValue ? oldValue : null;
  66. observer.callback([record]);
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * Returns text content.
  73. *
  74. * @returns Text content.
  75. */
  76. get textContent() {
  77. return this._data;
  78. }
  79. /**
  80. * Sets text content.
  81. *
  82. * @param textContent Text content.
  83. */
  84. set textContent(textContent) {
  85. this.data = textContent;
  86. }
  87. /**
  88. * Returns node value.
  89. *
  90. * @returns Node value.
  91. */
  92. get nodeValue() {
  93. return this._data;
  94. }
  95. /**
  96. * Sets node value.
  97. *
  98. * @param nodeValue Node value.
  99. */
  100. set nodeValue(nodeValue) {
  101. this.textContent = nodeValue;
  102. }
  103. /**
  104. * Previous element sibling.
  105. *
  106. * @returns Element.
  107. */
  108. get previousElementSibling() {
  109. return NonDocumentChildNodeUtility_1.default.previousElementSibling(this);
  110. }
  111. /**
  112. * Next element sibling.
  113. *
  114. * @returns Element.
  115. */
  116. get nextElementSibling() {
  117. return NonDocumentChildNodeUtility_1.default.nextElementSibling(this);
  118. }
  119. /**
  120. * Appends the given DOMString to the CharacterData.data string; when this method returns, data contains the concatenated DOMString.
  121. *
  122. * @param data Data.
  123. */
  124. appendData(data) {
  125. CharacterDataUtility_1.default.appendData(this, data);
  126. }
  127. /**
  128. * Removes the specified amount of characters, starting at the specified offset, from the CharacterData.data string; when this method returns, data contains the shortened DOMString.
  129. *
  130. * @param offset Offset.
  131. * @param count Count.
  132. */
  133. deleteData(offset, count) {
  134. CharacterDataUtility_1.default.deleteData(this, offset, count);
  135. }
  136. /**
  137. * Inserts the specified characters, at the specified offset, in the CharacterData.data string; when this method returns, data contains the modified DOMString.
  138. *
  139. * @param offset Offset.
  140. * @param data Data.
  141. */
  142. insertData(offset, data) {
  143. CharacterDataUtility_1.default.insertData(this, offset, data);
  144. }
  145. /**
  146. * Replaces the specified amount of characters, starting at the specified offset, with the specified DOMString; when this method returns, data contains the modified DOMString.
  147. *
  148. * @param offset Offset.
  149. * @param count Count.
  150. * @param data Data.
  151. */
  152. replaceData(offset, count, data) {
  153. CharacterDataUtility_1.default.replaceData(this, offset, count, data);
  154. }
  155. /**
  156. * Returns a DOMString containing the part of CharacterData.data of the specified length and starting at the specified offset.
  157. *
  158. * @param offset Offset.
  159. * @param count Count.
  160. */
  161. substringData(offset, count) {
  162. return CharacterDataUtility_1.default.substringData(this, offset, count);
  163. }
  164. /**
  165. * Removes the object from its parent children list.
  166. */
  167. remove() {
  168. ChildNodeUtility_1.default.remove(this);
  169. }
  170. /**
  171. * The Node.replaceWith() method replaces this Node in the children list of its parent with a set of Node or DOMString objects.
  172. *
  173. * @param nodes List of Node or DOMString.
  174. */
  175. replaceWith(...nodes) {
  176. ChildNodeUtility_1.default.replaceWith(this, ...nodes);
  177. }
  178. /**
  179. * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just before this ChildNode. DOMString objects are inserted as equivalent Text nodes.
  180. *
  181. * @param nodes List of Node or DOMString.
  182. */
  183. before(...nodes) {
  184. ChildNodeUtility_1.default.before(this, ...nodes);
  185. }
  186. /**
  187. * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just after this ChildNode. DOMString objects are inserted as equivalent Text nodes.
  188. *
  189. * @param nodes List of Node or DOMString.
  190. */
  191. after(...nodes) {
  192. ChildNodeUtility_1.default.after(this, ...nodes);
  193. }
  194. /**
  195. * Clones a node.
  196. *
  197. * @override
  198. * @param [deep=false] "true" to clone deep.
  199. * @returns Cloned node.
  200. */
  201. cloneNode(deep = false) {
  202. const clone = super.cloneNode(deep);
  203. clone._data = this._data;
  204. return clone;
  205. }
  206. }
  207. exports.default = CharacterData;
  208. //# sourceMappingURL=CharacterData.js.map