@iconfu_svg-inject.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import {
  2. __commonJS
  3. } from "./chunk-Y2F7D3TJ.js";
  4. // ../../../../Work2/Orienteering/mobile_h5/actMgt/node_modules/@iconfu/svg-inject/dist/svg-inject.js
  5. var require_svg_inject = __commonJS({
  6. "../../../../Work2/Orienteering/mobile_h5/actMgt/node_modules/@iconfu/svg-inject/dist/svg-inject.js"(exports, module) {
  7. (function(window2, document2) {
  8. var _CREATE_ELEMENT_ = "createElement";
  9. var _GET_ELEMENTS_BY_TAG_NAME_ = "getElementsByTagName";
  10. var _LENGTH_ = "length";
  11. var _STYLE_ = "style";
  12. var _TITLE_ = "title";
  13. var _UNDEFINED_ = "undefined";
  14. var _SET_ATTRIBUTE_ = "setAttribute";
  15. var _GET_ATTRIBUTE_ = "getAttribute";
  16. var NULL = null;
  17. var __SVGINJECT = "__svgInject";
  18. var ID_SUFFIX = "--inject-";
  19. var ID_SUFFIX_REGEX = new RegExp(ID_SUFFIX + "\\d+", "g");
  20. var LOAD_FAIL = "LOAD_FAIL";
  21. var SVG_NOT_SUPPORTED = "SVG_NOT_SUPPORTED";
  22. var SVG_INVALID = "SVG_INVALID";
  23. var ATTRIBUTE_EXCLUSION_NAMES = ["src", "alt", "onload", "onerror"];
  24. var A_ELEMENT = document2[_CREATE_ELEMENT_]("a");
  25. var IS_SVG_SUPPORTED = typeof SVGRect != _UNDEFINED_;
  26. var DEFAULT_OPTIONS = {
  27. useCache: true,
  28. copyAttributes: true,
  29. makeIdsUnique: true
  30. };
  31. var IRI_TAG_PROPERTIES_MAP = {
  32. clipPath: ["clip-path"],
  33. "color-profile": NULL,
  34. cursor: NULL,
  35. filter: NULL,
  36. linearGradient: ["fill", "stroke"],
  37. marker: ["marker", "marker-end", "marker-mid", "marker-start"],
  38. mask: NULL,
  39. pattern: ["fill", "stroke"],
  40. radialGradient: ["fill", "stroke"]
  41. };
  42. var INJECTED = 1;
  43. var FAIL = 2;
  44. var uniqueIdCounter = 1;
  45. var xmlSerializer;
  46. var domParser;
  47. function svgStringToSvgDoc(svgStr) {
  48. domParser = domParser || new DOMParser();
  49. return domParser.parseFromString(svgStr, "text/xml");
  50. }
  51. function svgElemToSvgString(svgElement) {
  52. xmlSerializer = xmlSerializer || new XMLSerializer();
  53. return xmlSerializer.serializeToString(svgElement);
  54. }
  55. function getAbsoluteUrl(url) {
  56. A_ELEMENT.href = url;
  57. return A_ELEMENT.href;
  58. }
  59. function loadSvg(url, callback, errorCallback) {
  60. if (url) {
  61. var req = new XMLHttpRequest();
  62. req.onreadystatechange = function() {
  63. if (req.readyState == 4) {
  64. var status = req.status;
  65. if (status == 200) {
  66. callback(req.responseXML, req.responseText.trim());
  67. } else if (status >= 400) {
  68. errorCallback();
  69. } else if (status == 0) {
  70. errorCallback();
  71. }
  72. }
  73. };
  74. req.open("GET", url, true);
  75. req.send();
  76. }
  77. }
  78. function copyAttributes(imgElem, svgElem) {
  79. var attribute;
  80. var attributeName;
  81. var attributeValue;
  82. var attributes = imgElem.attributes;
  83. for (var i = 0; i < attributes[_LENGTH_]; i++) {
  84. attribute = attributes[i];
  85. attributeName = attribute.name;
  86. if (ATTRIBUTE_EXCLUSION_NAMES.indexOf(attributeName) == -1) {
  87. attributeValue = attribute.value;
  88. if (attributeName == _TITLE_) {
  89. var titleElem;
  90. var firstElementChild = svgElem.firstElementChild;
  91. if (firstElementChild && firstElementChild.localName.toLowerCase() == _TITLE_) {
  92. titleElem = firstElementChild;
  93. } else {
  94. titleElem = document2[_CREATE_ELEMENT_ + "NS"]("http://www.w3.org/2000/svg", _TITLE_);
  95. svgElem.insertBefore(titleElem, firstElementChild);
  96. }
  97. titleElem.textContent = attributeValue;
  98. } else {
  99. svgElem[_SET_ATTRIBUTE_](attributeName, attributeValue);
  100. }
  101. }
  102. }
  103. }
  104. function makeIdsUnique(svgElem, onlyReferenced) {
  105. var idSuffix = ID_SUFFIX + uniqueIdCounter++;
  106. var funcIriRegex = /url\("?#([a-zA-Z][\w:.-]*)"?\)/g;
  107. var idElements = svgElem.querySelectorAll("[id]");
  108. var idElem;
  109. var referencedIds = onlyReferenced ? [] : NULL;
  110. var tagName;
  111. var iriTagNames = {};
  112. var iriProperties = [];
  113. var changed = false;
  114. var i, j;
  115. if (idElements[_LENGTH_]) {
  116. for (i = 0; i < idElements[_LENGTH_]; i++) {
  117. tagName = idElements[i].localName;
  118. if (tagName in IRI_TAG_PROPERTIES_MAP) {
  119. iriTagNames[tagName] = 1;
  120. }
  121. }
  122. for (tagName in iriTagNames) {
  123. (IRI_TAG_PROPERTIES_MAP[tagName] || [tagName]).forEach(function(mappedProperty) {
  124. if (iriProperties.indexOf(mappedProperty) < 0) {
  125. iriProperties.push(mappedProperty);
  126. }
  127. });
  128. }
  129. if (iriProperties[_LENGTH_]) {
  130. iriProperties.push(_STYLE_);
  131. }
  132. var descElements = svgElem[_GET_ELEMENTS_BY_TAG_NAME_]("*");
  133. var element = svgElem;
  134. var propertyName;
  135. var value;
  136. var newValue;
  137. for (i = -1; element != NULL; ) {
  138. if (element.localName == _STYLE_) {
  139. value = element.textContent;
  140. newValue = value && value.replace(funcIriRegex, function(match, id) {
  141. if (referencedIds) {
  142. referencedIds[id] = 1;
  143. }
  144. return "url(#" + id + idSuffix + ")";
  145. });
  146. if (newValue !== value) {
  147. element.textContent = newValue;
  148. }
  149. } else if (element.hasAttributes()) {
  150. for (j = 0; j < iriProperties[_LENGTH_]; j++) {
  151. propertyName = iriProperties[j];
  152. value = element[_GET_ATTRIBUTE_](propertyName);
  153. newValue = value && value.replace(funcIriRegex, function(match, id) {
  154. if (referencedIds) {
  155. referencedIds[id] = 1;
  156. }
  157. return "url(#" + id + idSuffix + ")";
  158. });
  159. if (newValue !== value) {
  160. element[_SET_ATTRIBUTE_](propertyName, newValue);
  161. }
  162. }
  163. ["xlink:href", "href"].forEach(function(refAttrName) {
  164. var iri = element[_GET_ATTRIBUTE_](refAttrName);
  165. if (/^\s*#/.test(iri)) {
  166. iri = iri.trim();
  167. element[_SET_ATTRIBUTE_](refAttrName, iri + idSuffix);
  168. if (referencedIds) {
  169. referencedIds[iri.substring(1)] = 1;
  170. }
  171. }
  172. });
  173. }
  174. element = descElements[++i];
  175. }
  176. for (i = 0; i < idElements[_LENGTH_]; i++) {
  177. idElem = idElements[i];
  178. if (!referencedIds || referencedIds[idElem.id]) {
  179. idElem.id += idSuffix;
  180. changed = true;
  181. }
  182. }
  183. }
  184. return changed;
  185. }
  186. function makeIdsUniqueCached(svgString) {
  187. return svgString.replace(ID_SUFFIX_REGEX, ID_SUFFIX + uniqueIdCounter++);
  188. }
  189. function inject(imgElem, svgElem, absUrl, options) {
  190. if (svgElem) {
  191. svgElem[_SET_ATTRIBUTE_]("data-inject-url", absUrl);
  192. var parentNode = imgElem.parentNode;
  193. if (parentNode) {
  194. if (options.copyAttributes) {
  195. copyAttributes(imgElem, svgElem);
  196. }
  197. var beforeInject = options.beforeInject;
  198. var injectElem = beforeInject && beforeInject(imgElem, svgElem) || svgElem;
  199. parentNode.replaceChild(injectElem, imgElem);
  200. imgElem[__SVGINJECT] = INJECTED;
  201. removeOnLoadAttribute(imgElem);
  202. var afterInject = options.afterInject;
  203. if (afterInject) {
  204. afterInject(imgElem, injectElem);
  205. }
  206. }
  207. } else {
  208. svgInvalid(imgElem, options);
  209. }
  210. }
  211. function mergeOptions() {
  212. var mergedOptions = {};
  213. var args = arguments;
  214. for (var i = 0; i < args[_LENGTH_]; i++) {
  215. var argument = args[i];
  216. for (var key in argument) {
  217. if (argument.hasOwnProperty(key)) {
  218. mergedOptions[key] = argument[key];
  219. }
  220. }
  221. }
  222. return mergedOptions;
  223. }
  224. function addStyleToHead(css) {
  225. var head = document2[_GET_ELEMENTS_BY_TAG_NAME_]("head")[0];
  226. if (head) {
  227. var style = document2[_CREATE_ELEMENT_](_STYLE_);
  228. style.type = "text/css";
  229. style.appendChild(document2.createTextNode(css));
  230. head.appendChild(style);
  231. }
  232. }
  233. function buildSvgElement(svgStr, verify) {
  234. if (verify) {
  235. var svgDoc;
  236. try {
  237. svgDoc = svgStringToSvgDoc(svgStr);
  238. } catch (e) {
  239. return NULL;
  240. }
  241. if (svgDoc[_GET_ELEMENTS_BY_TAG_NAME_]("parsererror")[_LENGTH_]) {
  242. return NULL;
  243. }
  244. return svgDoc.documentElement;
  245. } else {
  246. var div = document2.createElement("div");
  247. div.innerHTML = svgStr;
  248. return div.firstElementChild;
  249. }
  250. }
  251. function removeOnLoadAttribute(imgElem) {
  252. imgElem.removeAttribute("onload");
  253. }
  254. function errorMessage(msg) {
  255. console.error("SVGInject: " + msg);
  256. }
  257. function fail(imgElem, status, options) {
  258. imgElem[__SVGINJECT] = FAIL;
  259. if (options.onFail) {
  260. options.onFail(imgElem, status);
  261. } else {
  262. errorMessage(status);
  263. }
  264. }
  265. function svgInvalid(imgElem, options) {
  266. removeOnLoadAttribute(imgElem);
  267. fail(imgElem, SVG_INVALID, options);
  268. }
  269. function svgNotSupported(imgElem, options) {
  270. removeOnLoadAttribute(imgElem);
  271. fail(imgElem, SVG_NOT_SUPPORTED, options);
  272. }
  273. function loadFail(imgElem, options) {
  274. fail(imgElem, LOAD_FAIL, options);
  275. }
  276. function removeEventListeners(imgElem) {
  277. imgElem.onload = NULL;
  278. imgElem.onerror = NULL;
  279. }
  280. function imgNotSet(msg) {
  281. errorMessage("no img element");
  282. }
  283. function createSVGInject(globalName, options) {
  284. var defaultOptions = mergeOptions(DEFAULT_OPTIONS, options);
  285. var svgLoadCache = {};
  286. if (IS_SVG_SUPPORTED) {
  287. addStyleToHead('img[onload^="' + globalName + '("]{visibility:hidden;}');
  288. }
  289. function SVGInject(img, options2) {
  290. options2 = mergeOptions(defaultOptions, options2);
  291. var run = function(resolve) {
  292. var allFinish = function() {
  293. var onAllFinish = options2.onAllFinish;
  294. if (onAllFinish) {
  295. onAllFinish();
  296. }
  297. resolve && resolve();
  298. };
  299. if (img && typeof img[_LENGTH_] != _UNDEFINED_) {
  300. var injectIndex = 0;
  301. var injectCount = img[_LENGTH_];
  302. if (injectCount == 0) {
  303. allFinish();
  304. } else {
  305. var finish = function() {
  306. if (++injectIndex == injectCount) {
  307. allFinish();
  308. }
  309. };
  310. for (var i = 0; i < injectCount; i++) {
  311. SVGInjectElement(img[i], options2, finish);
  312. }
  313. }
  314. } else {
  315. SVGInjectElement(img, options2, allFinish);
  316. }
  317. };
  318. return typeof Promise == _UNDEFINED_ ? run() : new Promise(run);
  319. }
  320. function SVGInjectElement(imgElem, options2, callback) {
  321. if (imgElem) {
  322. var svgInjectAttributeValue = imgElem[__SVGINJECT];
  323. if (!svgInjectAttributeValue) {
  324. removeEventListeners(imgElem);
  325. if (!IS_SVG_SUPPORTED) {
  326. svgNotSupported(imgElem, options2);
  327. callback();
  328. return;
  329. }
  330. var beforeLoad = options2.beforeLoad;
  331. var src = beforeLoad && beforeLoad(imgElem) || imgElem[_GET_ATTRIBUTE_]("src");
  332. if (!src) {
  333. if (src === "") {
  334. loadFail(imgElem, options2);
  335. }
  336. callback();
  337. return;
  338. }
  339. var onFinishCallbacks = [];
  340. imgElem[__SVGINJECT] = onFinishCallbacks;
  341. var onFinish = function() {
  342. callback();
  343. onFinishCallbacks.forEach(function(onFinishCallback) {
  344. onFinishCallback();
  345. });
  346. };
  347. var absUrl = getAbsoluteUrl(src);
  348. var useCacheOption = options2.useCache;
  349. var makeIdsUniqueOption = options2.makeIdsUnique;
  350. var setSvgLoadCacheValue = function(val) {
  351. if (useCacheOption) {
  352. svgLoadCache[absUrl].forEach(function(svgLoad2) {
  353. svgLoad2(val);
  354. });
  355. svgLoadCache[absUrl] = val;
  356. }
  357. };
  358. if (useCacheOption) {
  359. var svgLoad = svgLoadCache[absUrl];
  360. var handleLoadValue = function(loadValue) {
  361. if (loadValue === LOAD_FAIL) {
  362. loadFail(imgElem, options2);
  363. } else if (loadValue === SVG_INVALID) {
  364. svgInvalid(imgElem, options2);
  365. } else {
  366. var hasUniqueIds = loadValue[0];
  367. var svgString = loadValue[1];
  368. var uniqueIdsSvgString = loadValue[2];
  369. var svgElem;
  370. if (makeIdsUniqueOption) {
  371. if (hasUniqueIds === NULL) {
  372. svgElem = buildSvgElement(svgString, false);
  373. hasUniqueIds = makeIdsUnique(svgElem, false);
  374. loadValue[0] = hasUniqueIds;
  375. loadValue[2] = hasUniqueIds && svgElemToSvgString(svgElem);
  376. } else if (hasUniqueIds) {
  377. svgString = makeIdsUniqueCached(uniqueIdsSvgString);
  378. }
  379. }
  380. svgElem = svgElem || buildSvgElement(svgString, false);
  381. inject(imgElem, svgElem, absUrl, options2);
  382. }
  383. onFinish();
  384. };
  385. if (typeof svgLoad != _UNDEFINED_) {
  386. if (svgLoad.isCallbackQueue) {
  387. svgLoad.push(handleLoadValue);
  388. } else {
  389. handleLoadValue(svgLoad);
  390. }
  391. return;
  392. } else {
  393. var svgLoad = [];
  394. svgLoad.isCallbackQueue = true;
  395. svgLoadCache[absUrl] = svgLoad;
  396. }
  397. }
  398. loadSvg(absUrl, function(svgXml, svgString) {
  399. var svgElem = svgXml instanceof Document ? svgXml.documentElement : buildSvgElement(svgString, true);
  400. var afterLoad = options2.afterLoad;
  401. if (afterLoad) {
  402. var svgElemOrSvgString = afterLoad(svgElem, svgString) || svgElem;
  403. if (svgElemOrSvgString) {
  404. var isString = typeof svgElemOrSvgString == "string";
  405. svgString = isString ? svgElemOrSvgString : svgElemToSvgString(svgElem);
  406. svgElem = isString ? buildSvgElement(svgElemOrSvgString, true) : svgElemOrSvgString;
  407. }
  408. }
  409. if (svgElem instanceof SVGElement) {
  410. var hasUniqueIds = NULL;
  411. if (makeIdsUniqueOption) {
  412. hasUniqueIds = makeIdsUnique(svgElem, false);
  413. }
  414. if (useCacheOption) {
  415. var uniqueIdsSvgString = hasUniqueIds && svgElemToSvgString(svgElem);
  416. setSvgLoadCacheValue([hasUniqueIds, svgString, uniqueIdsSvgString]);
  417. }
  418. inject(imgElem, svgElem, absUrl, options2);
  419. } else {
  420. svgInvalid(imgElem, options2);
  421. setSvgLoadCacheValue(SVG_INVALID);
  422. }
  423. onFinish();
  424. }, function() {
  425. loadFail(imgElem, options2);
  426. setSvgLoadCacheValue(LOAD_FAIL);
  427. onFinish();
  428. });
  429. } else {
  430. if (Array.isArray(svgInjectAttributeValue)) {
  431. svgInjectAttributeValue.push(callback);
  432. } else {
  433. callback();
  434. }
  435. }
  436. } else {
  437. imgNotSet();
  438. }
  439. }
  440. SVGInject.setOptions = function(options2) {
  441. defaultOptions = mergeOptions(defaultOptions, options2);
  442. };
  443. SVGInject.create = createSVGInject;
  444. SVGInject.err = function(img, fallbackSrc) {
  445. if (img) {
  446. if (img[__SVGINJECT] != FAIL) {
  447. removeEventListeners(img);
  448. if (!IS_SVG_SUPPORTED) {
  449. svgNotSupported(img, defaultOptions);
  450. } else {
  451. removeOnLoadAttribute(img);
  452. loadFail(img, defaultOptions);
  453. }
  454. if (fallbackSrc) {
  455. removeOnLoadAttribute(img);
  456. img.src = fallbackSrc;
  457. }
  458. }
  459. } else {
  460. imgNotSet();
  461. }
  462. };
  463. window2[globalName] = SVGInject;
  464. return SVGInject;
  465. }
  466. var SVGInjectInstance = createSVGInject("SVGInject");
  467. if (typeof module == "object" && typeof module.exports == "object") {
  468. module.exports = SVGInjectInstance;
  469. }
  470. })(window, document);
  471. }
  472. });
  473. export default require_svg_inject();
  474. /*! Bundled license information:
  475. @iconfu/svg-inject/dist/svg-inject.js:
  476. (**
  477. * SVGInject - Version 1.2.3
  478. * A tiny, intuitive, robust, caching solution for injecting SVG files inline into the DOM.
  479. *
  480. * https://github.com/iconfu/svg-inject
  481. *
  482. * Copyright (c) 2018 INCORS, the creators of iconfu.com
  483. * @license MIT License - https://github.com/iconfu/svg-inject/blob/master/LICENSE
  484. *)
  485. */
  486. //# sourceMappingURL=@iconfu_svg-inject.js.map