domstubs.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. toString: function DOMElement_toString() {
  96. var buf = [];
  97. buf.push('<' + this.nodeName);
  98. if (this.nodeName === 'svg:svg') {
  99. buf.push(' xmlns:xlink="http://www.w3.org/1999/xlink"' + ' xmlns:svg="http://www.w3.org/2000/svg"');
  100. }
  101. for (var i in this.attributes) {
  102. buf.push(' ' + i + '="' + xmlEncode(this.attributes[i]) + '"');
  103. }
  104. buf.push('>');
  105. if (this.nodeName === 'svg:tspan' || this.nodeName === 'svg:style') {
  106. buf.push(xmlEncode(this.textContent));
  107. } else {
  108. this.childNodes.forEach(function (childNode) {
  109. buf.push(childNode.toString());
  110. });
  111. }
  112. buf.push('</' + this.nodeName + '>');
  113. return buf.join('');
  114. },
  115. cloneNode: function DOMElement_cloneNode() {
  116. var newNode = new DOMElement(this.nodeName);
  117. newNode.childNodes = this.childNodes;
  118. newNode.attributes = this.attributes;
  119. newNode.textContent = this.textContent;
  120. return newNode;
  121. }
  122. };
  123. var document = {
  124. childNodes: [],
  125. get currentScript() {
  126. return { src: '' };
  127. },
  128. get documentElement() {
  129. return this;
  130. },
  131. createElementNS: function createElementNS(NS, element) {
  132. var elObject = new DOMElement(element);
  133. return elObject;
  134. },
  135. createElement: function createElement(element) {
  136. return this.createElementNS('', element);
  137. },
  138. getElementsByTagName: function getElementsByTagName(element) {
  139. if (element === 'head') {
  140. return [this.head || (this.head = new DOMElement('head'))];
  141. }
  142. return [];
  143. }
  144. };
  145. function Image() {
  146. this._src = null;
  147. this.onload = null;
  148. }
  149. Image.prototype = {
  150. get src() {
  151. return this._src;
  152. },
  153. set src(value) {
  154. this._src = value;
  155. if (this.onload) {
  156. this.onload();
  157. }
  158. }
  159. };
  160. exports.document = document;
  161. exports.Image = Image;
  162. var exported_symbols = Object.keys(exports);
  163. exports.setStubs = function (namespace) {
  164. exported_symbols.forEach(function (key) {
  165. console.assert(!(key in namespace), 'property should not be set: ' + key);
  166. namespace[key] = exports[key];
  167. });
  168. };
  169. exports.unsetStubs = function (namespace) {
  170. exported_symbols.forEach(function (key) {
  171. console.assert(key in namespace, 'property should be set: ' + key);
  172. delete namespace[key];
  173. });
  174. };