domstubs.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/ */
  3. function xmlEncode(s) {
  4. let i = 0,
  5. ch;
  6. s = String(s);
  7. while (
  8. i < s.length &&
  9. (ch = s[i]) !== "&" &&
  10. ch !== "<" &&
  11. ch !== '"' &&
  12. ch !== "\n" &&
  13. ch !== "\r" &&
  14. ch !== "\t"
  15. ) {
  16. i++;
  17. }
  18. if (i >= s.length) {
  19. return s;
  20. }
  21. let buf = s.substring(0, i);
  22. while (i < s.length) {
  23. ch = s[i++];
  24. switch (ch) {
  25. case "&":
  26. buf += "&amp;";
  27. break;
  28. case "<":
  29. buf += "&lt;";
  30. break;
  31. case '"':
  32. buf += "&quot;";
  33. break;
  34. case "\n":
  35. buf += "&#xA;";
  36. break;
  37. case "\r":
  38. buf += "&#xD;";
  39. break;
  40. case "\t":
  41. buf += "&#x9;";
  42. break;
  43. default:
  44. buf += ch;
  45. break;
  46. }
  47. }
  48. return buf;
  49. }
  50. function DOMElement(name) {
  51. this.nodeName = name;
  52. this.childNodes = [];
  53. this.attributes = {};
  54. this.textContent = "";
  55. if (name === "style") {
  56. this.sheet = {
  57. cssRules: [],
  58. insertRule(rule) {
  59. this.cssRules.push(rule);
  60. },
  61. };
  62. }
  63. }
  64. DOMElement.prototype = {
  65. getAttribute: function DOMElement_getAttribute(name) {
  66. if (name in this.attributes) {
  67. return this.attributes[name];
  68. }
  69. return null;
  70. },
  71. getAttributeNS: function DOMElement_getAttributeNS(NS, name) {
  72. // Fast path
  73. if (name in this.attributes) {
  74. return this.attributes[name];
  75. }
  76. // Slow path - used by test/unit/display_svg_spec.js
  77. // Assuming that there is only one matching attribute for a given name,
  78. // across all namespaces.
  79. if (NS) {
  80. const suffix = ":" + name;
  81. for (const fullName in this.attributes) {
  82. if (fullName.slice(-suffix.length) === suffix) {
  83. return this.attributes[fullName];
  84. }
  85. }
  86. }
  87. return null;
  88. },
  89. setAttribute: function DOMElement_setAttribute(name, value) {
  90. this.attributes[name] = value || "";
  91. },
  92. setAttributeNS: function DOMElement_setAttributeNS(NS, name, value) {
  93. this.setAttribute(name, value);
  94. },
  95. append: function DOMElement_append(...elements) {
  96. const childNodes = this.childNodes;
  97. for (const element of elements) {
  98. if (!childNodes.includes(element)) {
  99. childNodes.push(element);
  100. }
  101. }
  102. },
  103. appendChild: function DOMElement_appendChild(element) {
  104. const childNodes = this.childNodes;
  105. if (!childNodes.includes(element)) {
  106. childNodes.push(element);
  107. }
  108. },
  109. hasChildNodes: function DOMElement_hasChildNodes() {
  110. return this.childNodes.length !== 0;
  111. },
  112. cloneNode: function DOMElement_cloneNode() {
  113. const newNode = new DOMElement(this.nodeName);
  114. newNode.childNodes = this.childNodes;
  115. newNode.attributes = this.attributes;
  116. newNode.textContent = this.textContent;
  117. return newNode;
  118. },
  119. // This method is offered for convenience. It is recommended to directly use
  120. // getSerializer because that allows you to process the chunks as they come
  121. // instead of requiring the whole image to fit in memory.
  122. toString: function DOMElement_toString() {
  123. const buf = [];
  124. const serializer = this.getSerializer();
  125. let chunk;
  126. while ((chunk = serializer.getNext()) !== null) {
  127. buf.push(chunk);
  128. }
  129. return buf.join("");
  130. },
  131. getSerializer: function DOMElement_getSerializer() {
  132. return new DOMElementSerializer(this);
  133. },
  134. };
  135. function DOMElementSerializer(node) {
  136. this._node = node;
  137. this._state = 0;
  138. this._loopIndex = 0;
  139. this._attributeKeys = null;
  140. this._childSerializer = null;
  141. }
  142. DOMElementSerializer.prototype = {
  143. /**
  144. * Yields the next chunk in the serialization of the element.
  145. *
  146. * @returns {string|null} null if the element has fully been serialized.
  147. */
  148. getNext: function DOMElementSerializer_getNext() {
  149. const node = this._node;
  150. switch (this._state) {
  151. case 0: // Start opening tag.
  152. ++this._state;
  153. return "<" + node.nodeName;
  154. case 1: // Add SVG namespace if this is the root element.
  155. ++this._state;
  156. if (node.nodeName === "svg:svg") {
  157. return (
  158. ' xmlns:xlink="http://www.w3.org/1999/xlink"' +
  159. ' xmlns:svg="http://www.w3.org/2000/svg"'
  160. );
  161. }
  162. /* falls through */
  163. case 2: // Initialize variables for looping over attributes.
  164. ++this._state;
  165. this._loopIndex = 0;
  166. this._attributeKeys = Object.keys(node.attributes);
  167. /* falls through */
  168. case 3: // Serialize any attributes and end opening tag.
  169. if (this._loopIndex < this._attributeKeys.length) {
  170. const name = this._attributeKeys[this._loopIndex++];
  171. return " " + name + '="' + xmlEncode(node.attributes[name]) + '"';
  172. }
  173. ++this._state;
  174. return ">";
  175. case 4: // Serialize textContent for tspan/style elements.
  176. if (node.nodeName === "svg:tspan" || node.nodeName === "svg:style") {
  177. this._state = 6;
  178. return xmlEncode(node.textContent);
  179. }
  180. ++this._state;
  181. this._loopIndex = 0;
  182. /* falls through */
  183. case 5: // Serialize child nodes (only for non-tspan/style elements).
  184. while (true) {
  185. const value =
  186. this._childSerializer && this._childSerializer.getNext();
  187. if (value !== null) {
  188. return value;
  189. }
  190. const nextChild = node.childNodes[this._loopIndex++];
  191. if (nextChild) {
  192. this._childSerializer = new DOMElementSerializer(nextChild);
  193. } else {
  194. this._childSerializer = null;
  195. ++this._state;
  196. break;
  197. }
  198. }
  199. /* falls through */
  200. case 6: // Ending tag.
  201. ++this._state;
  202. return "</" + node.nodeName + ">";
  203. case 7: // Done.
  204. return null;
  205. default:
  206. throw new Error("Unexpected serialization state: " + this._state);
  207. }
  208. },
  209. };
  210. const document = {
  211. childNodes: [],
  212. get currentScript() {
  213. return { src: "" };
  214. },
  215. get documentElement() {
  216. return this;
  217. },
  218. createElementNS(NS, element) {
  219. const elObject = new DOMElement(element);
  220. return elObject;
  221. },
  222. createElement(element) {
  223. return this.createElementNS("", element);
  224. },
  225. getElementsByTagName(element) {
  226. if (element === "head") {
  227. return [this.head || (this.head = new DOMElement("head"))];
  228. }
  229. return [];
  230. },
  231. };
  232. function Image() {
  233. this._src = null;
  234. this.onload = null;
  235. }
  236. Image.prototype = {
  237. get src() {
  238. return this._src;
  239. },
  240. set src(value) {
  241. this._src = value;
  242. if (this.onload) {
  243. this.onload();
  244. }
  245. },
  246. };
  247. exports.document = document;
  248. exports.Image = Image;
  249. const exported_symbols = Object.keys(exports);
  250. exports.setStubs = function (namespace) {
  251. exported_symbols.forEach(function (key) {
  252. console.assert(!(key in namespace), "property should not be set: " + key);
  253. namespace[key] = exports[key];
  254. });
  255. };
  256. exports.unsetStubs = function (namespace) {
  257. exported_symbols.forEach(function (key) {
  258. console.assert(key in namespace, "property should be set: " + key);
  259. delete namespace[key];
  260. });
  261. };