domstubs.js 6.6 KB

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