domstubs.js 6.6 KB

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