Text.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const implSymbol = utils.implSymbol;
  5. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  6. const CharacterData = require("./CharacterData.js");
  7. const interfaceName = "Text";
  8. exports.is = value => {
  9. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  10. };
  11. exports.isImpl = value => {
  12. return utils.isObject(value) && value instanceof Impl.implementation;
  13. };
  14. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  15. if (exports.is(value)) {
  16. return utils.implForWrapper(value);
  17. }
  18. throw new globalObject.TypeError(`${context} is not of type 'Text'.`);
  19. };
  20. function makeWrapper(globalObject, newTarget) {
  21. let proto;
  22. if (newTarget !== undefined) {
  23. proto = newTarget.prototype;
  24. }
  25. if (!utils.isObject(proto)) {
  26. proto = globalObject[ctorRegistrySymbol]["Text"].prototype;
  27. }
  28. return Object.create(proto);
  29. }
  30. exports.create = (globalObject, constructorArgs, privateData) => {
  31. const wrapper = makeWrapper(globalObject);
  32. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  33. };
  34. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  35. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  36. return utils.implForWrapper(wrapper);
  37. };
  38. exports._internalSetup = (wrapper, globalObject) => {
  39. CharacterData._internalSetup(wrapper, globalObject);
  40. };
  41. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  42. privateData.wrapper = wrapper;
  43. exports._internalSetup(wrapper, globalObject);
  44. Object.defineProperty(wrapper, implSymbol, {
  45. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  46. configurable: true
  47. });
  48. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  49. if (Impl.init) {
  50. Impl.init(wrapper[implSymbol]);
  51. }
  52. return wrapper;
  53. };
  54. exports.new = (globalObject, newTarget) => {
  55. const wrapper = makeWrapper(globalObject, newTarget);
  56. exports._internalSetup(wrapper, globalObject);
  57. Object.defineProperty(wrapper, implSymbol, {
  58. value: Object.create(Impl.implementation.prototype),
  59. configurable: true
  60. });
  61. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  62. if (Impl.init) {
  63. Impl.init(wrapper[implSymbol]);
  64. }
  65. return wrapper[implSymbol];
  66. };
  67. const exposed = new Set(["Window"]);
  68. exports.install = (globalObject, globalNames) => {
  69. if (!globalNames.some(globalName => exposed.has(globalName))) {
  70. return;
  71. }
  72. const ctorRegistry = utils.initCtorRegistry(globalObject);
  73. class Text extends globalObject.CharacterData {
  74. constructor() {
  75. const args = [];
  76. {
  77. let curArg = arguments[0];
  78. if (curArg !== undefined) {
  79. curArg = conversions["DOMString"](curArg, {
  80. context: "Failed to construct 'Text': parameter 1",
  81. globals: globalObject
  82. });
  83. } else {
  84. curArg = "";
  85. }
  86. args.push(curArg);
  87. }
  88. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  89. }
  90. splitText(offset) {
  91. const esValue = this !== null && this !== undefined ? this : globalObject;
  92. if (!exports.is(esValue)) {
  93. throw new globalObject.TypeError("'splitText' called on an object that is not a valid instance of Text.");
  94. }
  95. if (arguments.length < 1) {
  96. throw new globalObject.TypeError(
  97. `Failed to execute 'splitText' on 'Text': 1 argument required, but only ${arguments.length} present.`
  98. );
  99. }
  100. const args = [];
  101. {
  102. let curArg = arguments[0];
  103. curArg = conversions["unsigned long"](curArg, {
  104. context: "Failed to execute 'splitText' on 'Text': parameter 1",
  105. globals: globalObject
  106. });
  107. args.push(curArg);
  108. }
  109. return utils.tryWrapperForImpl(esValue[implSymbol].splitText(...args));
  110. }
  111. get wholeText() {
  112. const esValue = this !== null && this !== undefined ? this : globalObject;
  113. if (!exports.is(esValue)) {
  114. throw new globalObject.TypeError("'get wholeText' called on an object that is not a valid instance of Text.");
  115. }
  116. return esValue[implSymbol]["wholeText"];
  117. }
  118. get assignedSlot() {
  119. const esValue = this !== null && this !== undefined ? this : globalObject;
  120. if (!exports.is(esValue)) {
  121. throw new globalObject.TypeError(
  122. "'get assignedSlot' called on an object that is not a valid instance of Text."
  123. );
  124. }
  125. return utils.tryWrapperForImpl(esValue[implSymbol]["assignedSlot"]);
  126. }
  127. }
  128. Object.defineProperties(Text.prototype, {
  129. splitText: { enumerable: true },
  130. wholeText: { enumerable: true },
  131. assignedSlot: { enumerable: true },
  132. [Symbol.toStringTag]: { value: "Text", configurable: true }
  133. });
  134. ctorRegistry[interfaceName] = Text;
  135. Object.defineProperty(globalObject, interfaceName, {
  136. configurable: true,
  137. writable: true,
  138. value: Text
  139. });
  140. };
  141. const Impl = require("../../jsdom/living/nodes/Text-impl.js");