domstubs.js 6.4 KB

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