2
0

domstubs.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. appendChild: function DOMElement_appendChild(element) {
  104. const childNodes = this.childNodes;
  105. if (!childNodes.includes(element)) {
  106. childNodes.push(element);
  107. }
  108. },
  109. hasChildNodes: function DOMElement_hasChildNodes() {
  110. return this.childNodes.length !== 0;
  111. },
  112. cloneNode: function DOMElement_cloneNode() {
  113. const newNode = new DOMElement(this.nodeName);
  114. newNode.childNodes = this.childNodes;
  115. newNode.attributes = this.attributes;
  116. newNode.textContent = this.textContent;
  117. return newNode;
  118. },
  119. toString: function DOMElement_toString() {
  120. const buf = [];
  121. const serializer = this.getSerializer();
  122. let chunk;
  123. while ((chunk = serializer.getNext()) !== null) {
  124. buf.push(chunk);
  125. }
  126. return buf.join("");
  127. },
  128. getSerializer: function DOMElement_getSerializer() {
  129. return new DOMElementSerializer(this);
  130. }
  131. };
  132. function DOMElementSerializer(node) {
  133. this._node = node;
  134. this._state = 0;
  135. this._loopIndex = 0;
  136. this._attributeKeys = null;
  137. this._childSerializer = null;
  138. }
  139. DOMElementSerializer.prototype = {
  140. getNext: function DOMElementSerializer_getNext() {
  141. const node = this._node;
  142. switch (this._state) {
  143. case 0:
  144. ++this._state;
  145. return "<" + node.nodeName;
  146. case 1:
  147. ++this._state;
  148. if (node.nodeName === "svg:svg") {
  149. return ' xmlns:xlink="http://www.w3.org/1999/xlink"' + ' xmlns:svg="http://www.w3.org/2000/svg"';
  150. }
  151. case 2:
  152. ++this._state;
  153. this._loopIndex = 0;
  154. this._attributeKeys = Object.keys(node.attributes);
  155. case 3:
  156. if (this._loopIndex < this._attributeKeys.length) {
  157. const name = this._attributeKeys[this._loopIndex++];
  158. return " " + name + '="' + xmlEncode(node.attributes[name]) + '"';
  159. }
  160. ++this._state;
  161. return ">";
  162. case 4:
  163. if (node.nodeName === "svg:tspan" || node.nodeName === "svg:style") {
  164. this._state = 6;
  165. return xmlEncode(node.textContent);
  166. }
  167. ++this._state;
  168. this._loopIndex = 0;
  169. case 5:
  170. while (true) {
  171. const value = this._childSerializer && this._childSerializer.getNext();
  172. if (value !== null) {
  173. return value;
  174. }
  175. const nextChild = node.childNodes[this._loopIndex++];
  176. if (nextChild) {
  177. this._childSerializer = new DOMElementSerializer(nextChild);
  178. } else {
  179. this._childSerializer = null;
  180. ++this._state;
  181. break;
  182. }
  183. }
  184. case 6:
  185. ++this._state;
  186. return "</" + node.nodeName + ">";
  187. case 7:
  188. return null;
  189. default:
  190. throw new Error("Unexpected serialization state: " + this._state);
  191. }
  192. }
  193. };
  194. const document = {
  195. childNodes: [],
  196. get currentScript() {
  197. return {
  198. src: ""
  199. };
  200. },
  201. get documentElement() {
  202. return this;
  203. },
  204. createElementNS(NS, element) {
  205. const elObject = new DOMElement(element);
  206. return elObject;
  207. },
  208. createElement(element) {
  209. return this.createElementNS("", element);
  210. },
  211. getElementsByTagName(element) {
  212. if (element === "head") {
  213. return [this.head || (this.head = new DOMElement("head"))];
  214. }
  215. return [];
  216. }
  217. };
  218. function Image() {
  219. this._src = null;
  220. this.onload = null;
  221. }
  222. Image.prototype = {
  223. get src() {
  224. return this._src;
  225. },
  226. set src(value) {
  227. this._src = value;
  228. if (this.onload) {
  229. this.onload();
  230. }
  231. }
  232. };
  233. exports.document = document;
  234. exports.Image = Image;
  235. const exported_symbols = Object.keys(exports);
  236. exports.setStubs = function (namespace) {
  237. exported_symbols.forEach(function (key) {
  238. console.assert(!(key in namespace), "property should not be set: " + key);
  239. namespace[key] = exports[key];
  240. });
  241. };
  242. exports.unsetStubs = function (namespace) {
  243. exported_symbols.forEach(function (key) {
  244. console.assert(key in namespace, "property should be set: " + key);
  245. delete namespace[key];
  246. });
  247. };