domstubs.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. return name in this.attributes ? this.attributes[name] : null;
  72. },
  73. setAttributeNS: function DOMElement_setAttributeNS(NS, name, value) {
  74. value = value || '';
  75. value = xmlEncode(value);
  76. this.attributes[name] = value;
  77. },
  78. appendChild: function DOMElement_appendChild(element) {
  79. var childNodes = this.childNodes;
  80. if (childNodes.indexOf(element) === -1) {
  81. childNodes.push(element);
  82. }
  83. },
  84. toString: function DOMElement_toString() {
  85. var buf = [];
  86. buf.push('<' + this.nodeName);
  87. if (this.nodeName === 'svg:svg') {
  88. buf.push(' xmlns:xlink="http://www.w3.org/1999/xlink"' + ' xmlns:svg="http://www.w3.org/2000/svg"');
  89. }
  90. for (var i in this.attributes) {
  91. buf.push(' ' + i + '="' + xmlEncode(this.attributes[i]) + '"');
  92. }
  93. buf.push('>');
  94. if (this.nodeName === 'svg:tspan' || this.nodeName === 'svg:style') {
  95. buf.push(xmlEncode(this.textContent));
  96. } else {
  97. this.childNodes.forEach(function (childNode) {
  98. buf.push(childNode.toString());
  99. });
  100. }
  101. buf.push('</' + this.nodeName + '>');
  102. return buf.join('');
  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. };
  112. var document = {
  113. childNodes: [],
  114. get currentScript() {
  115. return { src: '' };
  116. },
  117. get documentElement() {
  118. return this;
  119. },
  120. createElementNS: function createElementNS(NS, element) {
  121. var elObject = new DOMElement(element);
  122. return elObject;
  123. },
  124. createElement: function createElement(element) {
  125. return this.createElementNS('', element);
  126. },
  127. getElementsByTagName: function getElementsByTagName(element) {
  128. if (element === 'head') {
  129. return [this.head || (this.head = new DOMElement('head'))];
  130. }
  131. return [];
  132. }
  133. };
  134. function Image() {
  135. this._src = null;
  136. this.onload = null;
  137. }
  138. Image.prototype = {
  139. get src() {
  140. return this._src;
  141. },
  142. set src(value) {
  143. this._src = value;
  144. if (this.onload) {
  145. this.onload();
  146. }
  147. }
  148. };
  149. exports.document = document;
  150. exports.Image = Image;
  151. var exported_symbols = Object.keys(exports);
  152. exports.setStubs = function (namespace) {
  153. exported_symbols.forEach(function (key) {
  154. console.assert(!(key in namespace), 'property should not be set: ' + key);
  155. namespace[key] = exports[key];
  156. });
  157. };
  158. exports.unsetStubs = function (namespace) {
  159. exported_symbols.forEach(function (key) {
  160. console.assert(key in namespace, 'property should be set: ' + key);
  161. delete namespace[key];
  162. });
  163. };