2
0

domstubs.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. function xmlEncode(s) {
  24. let i = 0,
  25. ch;
  26. s = String(s);
  27. while (i < s.length && (ch = s[i]) !== "&" && ch !== "<" && ch !== '"' && ch !== "\n" && ch !== "\r" && ch !== "\t") {
  28. i++;
  29. }
  30. if (i >= s.length) {
  31. return s;
  32. }
  33. let buf = s.substring(0, i);
  34. while (i < s.length) {
  35. ch = s[i++];
  36. switch (ch) {
  37. case "&":
  38. buf += "&amp;";
  39. break;
  40. case "<":
  41. buf += "&lt;";
  42. break;
  43. case '"':
  44. buf += "&quot;";
  45. break;
  46. case "\n":
  47. buf += "&#xA;";
  48. break;
  49. case "\r":
  50. buf += "&#xD;";
  51. break;
  52. case "\t":
  53. buf += "&#x9;";
  54. break;
  55. default:
  56. buf += ch;
  57. break;
  58. }
  59. }
  60. return buf;
  61. }
  62. function DOMElement(name) {
  63. this.nodeName = name;
  64. this.childNodes = [];
  65. this.attributes = {};
  66. this.textContent = "";
  67. if (name === "style") {
  68. this.sheet = {
  69. cssRules: [],
  70. insertRule(rule) {
  71. this.cssRules.push(rule);
  72. }
  73. };
  74. }
  75. }
  76. DOMElement.prototype = {
  77. getAttribute: function DOMElement_getAttribute(name) {
  78. if (name in this.attributes) {
  79. return this.attributes[name];
  80. }
  81. return null;
  82. },
  83. getAttributeNS: function DOMElement_getAttributeNS(NS, name) {
  84. if (name in this.attributes) {
  85. return this.attributes[name];
  86. }
  87. if (NS) {
  88. const suffix = ":" + name;
  89. for (const fullName in this.attributes) {
  90. if (fullName.slice(-suffix.length) === suffix) {
  91. return this.attributes[fullName];
  92. }
  93. }
  94. }
  95. return null;
  96. },
  97. setAttribute: function DOMElement_setAttribute(name, value) {
  98. this.attributes[name] = value || "";
  99. },
  100. setAttributeNS: function DOMElement_setAttributeNS(NS, name, value) {
  101. this.setAttribute(name, value);
  102. },
  103. append: function DOMElement_append(...elements) {
  104. const childNodes = this.childNodes;
  105. for (const element of elements) {
  106. if (!childNodes.includes(element)) {
  107. childNodes.push(element);
  108. }
  109. }
  110. },
  111. appendChild: function DOMElement_appendChild(element) {
  112. const childNodes = this.childNodes;
  113. if (!childNodes.includes(element)) {
  114. childNodes.push(element);
  115. }
  116. },
  117. hasChildNodes: function DOMElement_hasChildNodes() {
  118. return this.childNodes.length !== 0;
  119. },
  120. cloneNode: function DOMElement_cloneNode() {
  121. const newNode = new DOMElement(this.nodeName);
  122. newNode.childNodes = this.childNodes;
  123. newNode.attributes = this.attributes;
  124. newNode.textContent = this.textContent;
  125. return newNode;
  126. },
  127. toString: function DOMElement_toString() {
  128. const buf = [];
  129. const serializer = this.getSerializer();
  130. let chunk;
  131. while ((chunk = serializer.getNext()) !== null) {
  132. buf.push(chunk);
  133. }
  134. return buf.join("");
  135. },
  136. getSerializer: function DOMElement_getSerializer() {
  137. return new DOMElementSerializer(this);
  138. }
  139. };
  140. function DOMElementSerializer(node) {
  141. this._node = node;
  142. this._state = 0;
  143. this._loopIndex = 0;
  144. this._attributeKeys = null;
  145. this._childSerializer = null;
  146. }
  147. DOMElementSerializer.prototype = {
  148. getNext: function DOMElementSerializer_getNext() {
  149. const node = this._node;
  150. switch (this._state) {
  151. case 0:
  152. ++this._state;
  153. return "<" + node.nodeName;
  154. case 1:
  155. ++this._state;
  156. if (node.nodeName === "svg:svg") {
  157. return ' xmlns:xlink="http://www.w3.org/1999/xlink"' + ' xmlns:svg="http://www.w3.org/2000/svg"';
  158. }
  159. case 2:
  160. ++this._state;
  161. this._loopIndex = 0;
  162. this._attributeKeys = Object.keys(node.attributes);
  163. case 3:
  164. if (this._loopIndex < this._attributeKeys.length) {
  165. const name = this._attributeKeys[this._loopIndex++];
  166. return " " + name + '="' + xmlEncode(node.attributes[name]) + '"';
  167. }
  168. ++this._state;
  169. return ">";
  170. case 4:
  171. if (node.nodeName === "svg:tspan" || node.nodeName === "svg:style") {
  172. this._state = 6;
  173. return xmlEncode(node.textContent);
  174. }
  175. ++this._state;
  176. this._loopIndex = 0;
  177. case 5:
  178. while (true) {
  179. const value = this._childSerializer && this._childSerializer.getNext();
  180. if (value !== null) {
  181. return value;
  182. }
  183. const nextChild = node.childNodes[this._loopIndex++];
  184. if (nextChild) {
  185. this._childSerializer = new DOMElementSerializer(nextChild);
  186. } else {
  187. this._childSerializer = null;
  188. ++this._state;
  189. break;
  190. }
  191. }
  192. case 6:
  193. ++this._state;
  194. return "</" + node.nodeName + ">";
  195. case 7:
  196. return null;
  197. default:
  198. throw new Error("Unexpected serialization state: " + this._state);
  199. }
  200. }
  201. };
  202. const document = {
  203. childNodes: [],
  204. get currentScript() {
  205. return {
  206. src: ""
  207. };
  208. },
  209. get documentElement() {
  210. return this;
  211. },
  212. createElementNS(NS, element) {
  213. const elObject = new DOMElement(element);
  214. return elObject;
  215. },
  216. createElement(element) {
  217. return this.createElementNS("", element);
  218. },
  219. getElementsByTagName(element) {
  220. if (element === "head") {
  221. return [this.head || (this.head = new DOMElement("head"))];
  222. }
  223. return [];
  224. }
  225. };
  226. function Image() {
  227. this._src = null;
  228. this.onload = null;
  229. }
  230. Image.prototype = {
  231. get src() {
  232. return this._src;
  233. },
  234. set src(value) {
  235. this._src = value;
  236. if (this.onload) {
  237. this.onload();
  238. }
  239. }
  240. };
  241. exports.document = document;
  242. exports.Image = Image;
  243. const exported_symbols = Object.keys(exports);
  244. exports.setStubs = function (namespace) {
  245. exported_symbols.forEach(function (key) {
  246. console.assert(!(key in namespace), "property should not be set: " + key);
  247. namespace[key] = exports[key];
  248. });
  249. };
  250. exports.unsetStubs = function (namespace) {
  251. exported_symbols.forEach(function (key) {
  252. console.assert(key in namespace, "property should be set: " + key);
  253. delete namespace[key];
  254. });
  255. };