2
0

domstubs.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. getAttributeNS: function DOMElement_getAttributeNS(NS, name) {
  71. if (name in this.attributes) {
  72. return this.attributes[name];
  73. }
  74. if (NS) {
  75. var suffix = ':' + name;
  76. for (var fullName in this.attributes) {
  77. if (fullName.slice(-suffix.length) === suffix) {
  78. return this.attributes[fullName];
  79. }
  80. }
  81. }
  82. return null;
  83. },
  84. setAttributeNS: function DOMElement_setAttributeNS(NS, name, value) {
  85. value = value || '';
  86. value = xmlEncode(value);
  87. this.attributes[name] = value;
  88. },
  89. appendChild: function DOMElement_appendChild(element) {
  90. var childNodes = this.childNodes;
  91. if (childNodes.indexOf(element) === -1) {
  92. childNodes.push(element);
  93. }
  94. },
  95. cloneNode: function DOMElement_cloneNode() {
  96. var newNode = new DOMElement(this.nodeName);
  97. newNode.childNodes = this.childNodes;
  98. newNode.attributes = this.attributes;
  99. newNode.textContent = this.textContent;
  100. return newNode;
  101. },
  102. toString: function DOMElement_toString() {
  103. var buf = [];
  104. var serializer = this.getSerializer();
  105. var chunk;
  106. while ((chunk = serializer.getNext()) !== null) {
  107. buf.push(chunk);
  108. }
  109. return buf.join('');
  110. },
  111. getSerializer: function DOMElement_getSerializer() {
  112. return new DOMElementSerializer(this);
  113. }
  114. };
  115. function DOMElementSerializer(node) {
  116. this._node = node;
  117. this._state = 0;
  118. this._loopIndex = 0;
  119. this._attributeKeys = null;
  120. this._childSerializer = null;
  121. }
  122. DOMElementSerializer.prototype = {
  123. getNext: function DOMElementSerializer_getNext() {
  124. var node = this._node;
  125. switch (this._state) {
  126. case 0:
  127. ++this._state;
  128. return '<' + node.nodeName;
  129. case 1:
  130. ++this._state;
  131. if (node.nodeName === 'svg:svg') {
  132. return ' xmlns:xlink="http://www.w3.org/1999/xlink"' + ' xmlns:svg="http://www.w3.org/2000/svg"';
  133. }
  134. case 2:
  135. ++this._state;
  136. this._loopIndex = 0;
  137. this._attributeKeys = Object.keys(node.attributes);
  138. case 3:
  139. if (this._loopIndex < this._attributeKeys.length) {
  140. var name = this._attributeKeys[this._loopIndex++];
  141. return ' ' + name + '="' + xmlEncode(node.attributes[name]) + '"';
  142. }
  143. ++this._state;
  144. return '>';
  145. case 4:
  146. if (node.nodeName === 'svg:tspan' || node.nodeName === 'svg:style') {
  147. this._state = 6;
  148. return xmlEncode(node.textContent);
  149. }
  150. ++this._state;
  151. this._loopIndex = 0;
  152. case 5:
  153. var value;
  154. while (true) {
  155. value = this._childSerializer && this._childSerializer.getNext();
  156. if (value !== null) {
  157. return value;
  158. }
  159. var nextChild = node.childNodes[this._loopIndex++];
  160. if (nextChild) {
  161. this._childSerializer = new DOMElementSerializer(nextChild);
  162. } else {
  163. this._childSerializer = null;
  164. ++this._state;
  165. break;
  166. }
  167. }
  168. case 6:
  169. ++this._state;
  170. return '</' + node.nodeName + '>';
  171. case 7:
  172. return null;
  173. default:
  174. throw new Error('Unexpected serialization state: ' + this._state);
  175. }
  176. }
  177. };
  178. var document = {
  179. childNodes: [],
  180. get currentScript() {
  181. return { src: '' };
  182. },
  183. get documentElement() {
  184. return this;
  185. },
  186. createElementNS: function createElementNS(NS, element) {
  187. var elObject = new DOMElement(element);
  188. return elObject;
  189. },
  190. createElement: function createElement(element) {
  191. return this.createElementNS('', element);
  192. },
  193. getElementsByTagName: function getElementsByTagName(element) {
  194. if (element === 'head') {
  195. return [this.head || (this.head = new DOMElement('head'))];
  196. }
  197. return [];
  198. }
  199. };
  200. function Image() {
  201. this._src = null;
  202. this.onload = null;
  203. }
  204. Image.prototype = {
  205. get src() {
  206. return this._src;
  207. },
  208. set src(value) {
  209. this._src = value;
  210. if (this.onload) {
  211. this.onload();
  212. }
  213. }
  214. };
  215. exports.document = document;
  216. exports.Image = Image;
  217. var exported_symbols = Object.keys(exports);
  218. exports.setStubs = function (namespace) {
  219. exported_symbols.forEach(function (key) {
  220. console.assert(!(key in namespace), 'property should not be set: ' + key);
  221. namespace[key] = exports[key];
  222. });
  223. };
  224. exports.unsetStubs = function (namespace) {
  225. exported_symbols.forEach(function (key) {
  226. console.assert(key in namespace, 'property should be set: ' + key);
  227. delete namespace[key];
  228. });
  229. };