XMLSerializer.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const impl = utils.implSymbol;
  5. class XMLSerializer {
  6. constructor() {
  7. return iface.setup(Object.create(new.target.prototype));
  8. }
  9. serializeToString(root) {
  10. if (!this || !module.exports.is(this)) {
  11. throw new TypeError("Illegal invocation");
  12. }
  13. if (arguments.length < 1) {
  14. throw new TypeError(
  15. "Failed to execute 'serializeToString' on 'XMLSerializer': 1 argument required, but only " +
  16. arguments.length +
  17. " present."
  18. );
  19. }
  20. const args = [];
  21. {
  22. let curArg = arguments[0];
  23. curArg = utils.tryImplForWrapper(curArg);
  24. args.push(curArg);
  25. }
  26. return this[impl].serializeToString(...args);
  27. }
  28. }
  29. Object.defineProperties(XMLSerializer.prototype, {
  30. serializeToString: { enumerable: true },
  31. [Symbol.toStringTag]: { value: "XMLSerializer", configurable: true }
  32. });
  33. const iface = {
  34. // When an interface-module that implements this interface as a mixin is loaded, it will append its own `.is()`
  35. // method into this array. It allows objects that directly implements *those* interfaces to be recognized as
  36. // implementing this mixin interface.
  37. _mixedIntoPredicates: [],
  38. is(obj) {
  39. if (obj) {
  40. if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation) {
  41. return true;
  42. }
  43. for (const isMixedInto of module.exports._mixedIntoPredicates) {
  44. if (isMixedInto(obj)) {
  45. return true;
  46. }
  47. }
  48. }
  49. return false;
  50. },
  51. isImpl(obj) {
  52. if (obj) {
  53. if (obj instanceof Impl.implementation) {
  54. return true;
  55. }
  56. const wrapper = utils.wrapperForImpl(obj);
  57. for (const isMixedInto of module.exports._mixedIntoPredicates) {
  58. if (isMixedInto(wrapper)) {
  59. return true;
  60. }
  61. }
  62. }
  63. return false;
  64. },
  65. convert(obj, { context = "The provided value" } = {}) {
  66. if (module.exports.is(obj)) {
  67. return utils.implForWrapper(obj);
  68. }
  69. throw new TypeError(`${context} is not of type 'XMLSerializer'.`);
  70. },
  71. create(constructorArgs, privateData) {
  72. let obj = Object.create(XMLSerializer.prototype);
  73. obj = this.setup(obj, constructorArgs, privateData);
  74. return obj;
  75. },
  76. createImpl(constructorArgs, privateData) {
  77. let obj = Object.create(XMLSerializer.prototype);
  78. obj = this.setup(obj, constructorArgs, privateData);
  79. return utils.implForWrapper(obj);
  80. },
  81. _internalSetup(obj) {},
  82. setup(obj, constructorArgs, privateData) {
  83. if (!privateData) privateData = {};
  84. privateData.wrapper = obj;
  85. this._internalSetup(obj);
  86. Object.defineProperty(obj, impl, {
  87. value: new Impl.implementation(constructorArgs, privateData),
  88. configurable: true
  89. });
  90. obj[impl][utils.wrapperSymbol] = obj;
  91. if (Impl.init) {
  92. Impl.init(obj[impl], privateData);
  93. }
  94. return obj;
  95. },
  96. interface: XMLSerializer,
  97. expose: {
  98. Window: { XMLSerializer }
  99. }
  100. }; // iface
  101. module.exports = iface;
  102. const Impl = require("./XMLSerializer-impl.js");