elem.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { hasClass } from './class';
  2. let idCounter = 0;
  3. export function uniqueId() {
  4. idCounter += 1;
  5. return `v${idCounter}`;
  6. }
  7. export function ensureId(elem) {
  8. if (elem.id == null || elem.id === '') {
  9. elem.id = uniqueId();
  10. }
  11. return elem.id;
  12. }
  13. /**
  14. * Returns true if object is an instance of SVGGraphicsElement.
  15. * @see https://developer.mozilla.org/en-US/docs/Web/API/SVGGraphicsElement
  16. */
  17. export function isSVGGraphicsElement(elem) {
  18. if (elem == null) {
  19. return false;
  20. }
  21. return typeof elem.getScreenCTM === 'function' && elem instanceof SVGElement;
  22. }
  23. export const ns = {
  24. svg: 'http://www.w3.org/2000/svg',
  25. xmlns: 'http://www.w3.org/2000/xmlns/',
  26. xml: 'http://www.w3.org/XML/1998/namespace',
  27. xlink: 'http://www.w3.org/1999/xlink',
  28. xhtml: 'http://www.w3.org/1999/xhtml',
  29. };
  30. export const svgVersion = '1.1';
  31. export function createElement(tagName, doc = document) {
  32. return doc.createElement(tagName);
  33. }
  34. export function createElementNS(tagName, namespaceURI = ns.xhtml, doc = document) {
  35. return doc.createElementNS(namespaceURI, tagName);
  36. }
  37. export function createSvgElement(tagName, doc = document) {
  38. return createElementNS(tagName, ns.svg, doc);
  39. }
  40. export function createSvgDocument(content) {
  41. if (content) {
  42. const xml = `<svg xmlns="${ns.svg}" xmlns:xlink="${ns.xlink}" version="${svgVersion}">${content}</svg>`; // lgtm[js/html-constructed-from-input]
  43. const { documentElement } = parseXML(xml, { async: false });
  44. return documentElement;
  45. }
  46. const svg = document.createElementNS(ns.svg, 'svg');
  47. svg.setAttributeNS(ns.xmlns, 'xmlns:xlink', ns.xlink);
  48. svg.setAttribute('version', svgVersion);
  49. return svg;
  50. }
  51. export function parseXML(data, options = {}) {
  52. let xml;
  53. try {
  54. const parser = new DOMParser();
  55. if (options.async != null) {
  56. const instance = parser;
  57. instance.async = options.async;
  58. }
  59. xml = parser.parseFromString(data, options.mimeType || 'text/xml');
  60. }
  61. catch (error) {
  62. xml = undefined;
  63. }
  64. if (!xml || xml.getElementsByTagName('parsererror').length) {
  65. throw new Error(`Invalid XML: ${data}`);
  66. }
  67. return xml;
  68. }
  69. export function tagName(node, lowercase = true) {
  70. const nodeName = node.nodeName;
  71. return lowercase ? nodeName.toLowerCase() : nodeName.toUpperCase();
  72. }
  73. export function index(elem) {
  74. let index = 0;
  75. let node = elem.previousSibling;
  76. while (node) {
  77. if (node.nodeType === 1) {
  78. index += 1;
  79. }
  80. node = node.previousSibling;
  81. }
  82. return index;
  83. }
  84. export function find(elem, selector) {
  85. return elem.querySelectorAll(selector);
  86. }
  87. export function findOne(elem, selector) {
  88. return elem.querySelector(selector);
  89. }
  90. export function findParentByClass(elem, className, terminator) {
  91. const ownerSVGElement = elem.ownerSVGElement;
  92. let node = elem.parentNode;
  93. while (node && node !== terminator && node !== ownerSVGElement) {
  94. if (hasClass(node, className)) {
  95. return node;
  96. }
  97. node = node.parentNode;
  98. }
  99. return null;
  100. }
  101. export function contains(parent, child) {
  102. const bup = child && child.parentNode;
  103. return (parent === bup ||
  104. !!(bup && bup.nodeType === 1 && parent.compareDocumentPosition(bup) & 16) // eslint-disable-line no-bitwise
  105. );
  106. }
  107. export function remove(elem) {
  108. if (elem) {
  109. const elems = Array.isArray(elem) ? elem : [elem];
  110. elems.forEach((item) => {
  111. if (item.parentNode) {
  112. item.parentNode.removeChild(item);
  113. }
  114. });
  115. }
  116. }
  117. export function empty(elem) {
  118. while (elem.firstChild) {
  119. elem.removeChild(elem.firstChild);
  120. }
  121. }
  122. export function append(elem, elems) {
  123. const arr = Array.isArray(elems) ? elems : [elems];
  124. arr.forEach((child) => {
  125. if (child != null) {
  126. elem.appendChild(child);
  127. }
  128. });
  129. }
  130. export function prepend(elem, elems) {
  131. const child = elem.firstChild;
  132. return child ? before(child, elems) : append(elem, elems);
  133. }
  134. export function before(elem, elems) {
  135. const parent = elem.parentNode;
  136. if (parent) {
  137. const arr = Array.isArray(elems) ? elems : [elems];
  138. arr.forEach((child) => {
  139. if (child != null) {
  140. parent.insertBefore(child, elem);
  141. }
  142. });
  143. }
  144. }
  145. export function after(elem, elems) {
  146. const parent = elem.parentNode;
  147. if (parent) {
  148. const arr = Array.isArray(elems) ? elems : [elems];
  149. arr.forEach((child) => {
  150. if (child != null) {
  151. parent.insertBefore(child, elem.nextSibling);
  152. }
  153. });
  154. }
  155. }
  156. export function appendTo(elem, target) {
  157. if (target != null) {
  158. target.appendChild(elem);
  159. }
  160. }
  161. export function isElement(x) {
  162. return !!x && x.nodeType === 1;
  163. }
  164. // Determines whether a node is an HTML node
  165. export function isHTMLElement(elem) {
  166. try {
  167. // Using W3 DOM2 (works for FF, Opera and Chrome)
  168. return elem instanceof HTMLElement;
  169. }
  170. catch (e) {
  171. // Browsers not supporting W3 DOM2 don't have HTMLElement and
  172. // an exception is thrown and we end up here. Testing some
  173. // properties that all elements have (works on IE7)
  174. return (typeof elem === 'object' &&
  175. elem.nodeType === 1 &&
  176. typeof elem.style === 'object' &&
  177. typeof elem.ownerDocument === 'object');
  178. }
  179. }
  180. export function children(parent, className) {
  181. const matched = [];
  182. let elem = parent.firstChild;
  183. for (; elem; elem = elem.nextSibling) {
  184. if (elem.nodeType === 1) {
  185. if (!className || hasClass(elem, className)) {
  186. matched.push(elem);
  187. }
  188. }
  189. }
  190. return matched;
  191. }
  192. //# sourceMappingURL=elem.js.map