ion-icon.entry.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { r as registerInstance, h, H as Host, c as getElement } from './index-b72adede.js';
  2. import { i as isStr, b as inheritAttributes, g as getUrl, c as getName, d as isRTL } from './utils-2c56d1c8.js';
  3. const validateContent = (svgContent) => {
  4. const div = document.createElement('div');
  5. div.innerHTML = svgContent;
  6. // setup this way to ensure it works on our buddy IE
  7. for (let i = div.childNodes.length - 1; i >= 0; i--) {
  8. if (div.childNodes[i].nodeName.toLowerCase() !== 'svg') {
  9. div.removeChild(div.childNodes[i]);
  10. }
  11. }
  12. // must only have 1 root element
  13. const svgElm = div.firstElementChild;
  14. if (svgElm && svgElm.nodeName.toLowerCase() === 'svg') {
  15. const svgClass = svgElm.getAttribute('class') || '';
  16. svgElm.setAttribute('class', (svgClass + ' s-ion-icon').trim());
  17. // root element must be an svg
  18. // lets double check we've got valid elements
  19. // do not allow scripts
  20. if (isValid(svgElm)) {
  21. return div.innerHTML;
  22. }
  23. }
  24. return '';
  25. };
  26. const isValid = (elm) => {
  27. if (elm.nodeType === 1) {
  28. if (elm.nodeName.toLowerCase() === 'script') {
  29. return false;
  30. }
  31. for (let i = 0; i < elm.attributes.length; i++) {
  32. const name = elm.attributes[i].name;
  33. if (isStr(name) && name.toLowerCase().indexOf('on') === 0) {
  34. return false;
  35. }
  36. }
  37. for (let i = 0; i < elm.childNodes.length; i++) {
  38. if (!isValid(elm.childNodes[i])) {
  39. return false;
  40. }
  41. }
  42. }
  43. return true;
  44. };
  45. const isSvgDataUrl = (url) => url.startsWith('data:image/svg+xml');
  46. const isEncodedDataUrl = (url) => url.indexOf(';utf8,') !== -1;
  47. const ioniconContent = new Map();
  48. const requests = new Map();
  49. let parser;
  50. const getSvgContent = (url, sanitize) => {
  51. // see if we already have a request for this url
  52. let req = requests.get(url);
  53. if (!req) {
  54. if (typeof fetch !== 'undefined' && typeof document !== 'undefined') {
  55. /**
  56. * If the url is a data url of an svg, then try to parse it
  57. * with the DOMParser. This works with content security policies enabled.
  58. */
  59. if (isSvgDataUrl(url) && isEncodedDataUrl(url)) {
  60. if (!parser) {
  61. /**
  62. * Create an instance of the DOM parser. This creates a single
  63. * parser instance for the entire app, which is more efficient.
  64. */
  65. parser = new DOMParser();
  66. }
  67. const doc = parser.parseFromString(url, 'text/html');
  68. const svg = doc.querySelector('svg');
  69. if (svg) {
  70. ioniconContent.set(url, svg.outerHTML);
  71. }
  72. return Promise.resolve();
  73. }
  74. else {
  75. // we don't already have a request
  76. req = fetch(url).then((rsp) => {
  77. if (rsp.ok) {
  78. return rsp.text().then((svgContent) => {
  79. if (svgContent && sanitize !== false) {
  80. svgContent = validateContent(svgContent);
  81. }
  82. ioniconContent.set(url, svgContent || '');
  83. });
  84. }
  85. ioniconContent.set(url, '');
  86. });
  87. // cache for the same requests
  88. requests.set(url, req);
  89. }
  90. }
  91. else {
  92. // set to empty for ssr scenarios and resolve promise
  93. ioniconContent.set(url, '');
  94. return Promise.resolve();
  95. }
  96. }
  97. return req;
  98. };
  99. const iconCss = ":host{display:inline-block;width:1em;height:1em;contain:strict;fill:currentColor;-webkit-box-sizing:content-box !important;box-sizing:content-box !important}:host .ionicon{stroke:currentColor}.ionicon-fill-none{fill:none}.ionicon-stroke-width{stroke-width:32px;stroke-width:var(--ionicon-stroke-width, 32px)}.icon-inner,.ionicon,svg{display:block;height:100%;width:100%}@supports (background: -webkit-named-image(i)){:host(.icon-rtl) .icon-inner{-webkit-transform:scaleX(-1);transform:scaleX(-1)}}@supports not selector(:dir(rtl)) and selector(:host-context([dir='rtl'])){:host(.icon-rtl) .icon-inner{-webkit-transform:scaleX(-1);transform:scaleX(-1)}}:host(.flip-rtl):host-context([dir='rtl']) .icon-inner{-webkit-transform:scaleX(-1);transform:scaleX(-1)}@supports selector(:dir(rtl)){:host(.flip-rtl:dir(rtl)) .icon-inner{-webkit-transform:scaleX(-1);transform:scaleX(-1)}:host(.flip-rtl:dir(ltr)) .icon-inner{-webkit-transform:scaleX(1);transform:scaleX(1)}}:host(.icon-small){font-size:1.125rem !important}:host(.icon-large){font-size:2rem !important}:host(.ion-color){color:var(--ion-color-base) !important}:host(.ion-color-primary){--ion-color-base:var(--ion-color-primary, #3880ff)}:host(.ion-color-secondary){--ion-color-base:var(--ion-color-secondary, #0cd1e8)}:host(.ion-color-tertiary){--ion-color-base:var(--ion-color-tertiary, #f4a942)}:host(.ion-color-success){--ion-color-base:var(--ion-color-success, #10dc60)}:host(.ion-color-warning){--ion-color-base:var(--ion-color-warning, #ffce00)}:host(.ion-color-danger){--ion-color-base:var(--ion-color-danger, #f14141)}:host(.ion-color-light){--ion-color-base:var(--ion-color-light, #f4f5f8)}:host(.ion-color-medium){--ion-color-base:var(--ion-color-medium, #989aa2)}:host(.ion-color-dark){--ion-color-base:var(--ion-color-dark, #222428)}";
  100. const Icon = class {
  101. constructor(hostRef) {
  102. registerInstance(this, hostRef);
  103. this.iconName = null;
  104. this.inheritedAttributes = {};
  105. this.didLoadIcon = false;
  106. this.svgContent = undefined;
  107. this.isVisible = false;
  108. this.mode = getIonMode();
  109. this.color = undefined;
  110. this.ios = undefined;
  111. this.md = undefined;
  112. this.flipRtl = undefined;
  113. this.name = undefined;
  114. this.src = undefined;
  115. this.icon = undefined;
  116. this.size = undefined;
  117. this.lazy = false;
  118. this.sanitize = true;
  119. }
  120. componentWillLoad() {
  121. this.inheritedAttributes = inheritAttributes(this.el, ['aria-label']);
  122. }
  123. connectedCallback() {
  124. // purposely do not return the promise here because loading
  125. // the svg file should not hold up loading the app
  126. // only load the svg if it's visible
  127. this.waitUntilVisible(this.el, '50px', () => {
  128. this.isVisible = true;
  129. this.loadIcon();
  130. });
  131. }
  132. componentDidLoad() {
  133. /**
  134. * Addresses an Angular issue where property values are assigned after the 'connectedCallback' but prior to the registration of watchers.
  135. * This enhancement ensures the loading of an icon when the component has finished rendering and the icon has yet to apply the SVG data.
  136. * This modification pertains to the usage of Angular's binding syntax:
  137. * `<ion-icon [name]="myIconName"></ion-icon>`
  138. */
  139. if (!this.didLoadIcon) {
  140. this.loadIcon();
  141. }
  142. }
  143. disconnectedCallback() {
  144. if (this.io) {
  145. this.io.disconnect();
  146. this.io = undefined;
  147. }
  148. }
  149. waitUntilVisible(el, rootMargin, cb) {
  150. if (this.lazy && typeof window !== 'undefined' && window.IntersectionObserver) {
  151. const io = (this.io = new window.IntersectionObserver((data) => {
  152. if (data[0].isIntersecting) {
  153. io.disconnect();
  154. this.io = undefined;
  155. cb();
  156. }
  157. }, { rootMargin }));
  158. io.observe(el);
  159. }
  160. else {
  161. // browser doesn't support IntersectionObserver
  162. // so just fallback to always show it
  163. cb();
  164. }
  165. }
  166. loadIcon() {
  167. if (this.isVisible) {
  168. const url = getUrl(this);
  169. if (url) {
  170. if (ioniconContent.has(url)) {
  171. // sync if it's already loaded
  172. this.svgContent = ioniconContent.get(url);
  173. }
  174. else {
  175. // async if it hasn't been loaded
  176. getSvgContent(url, this.sanitize).then(() => (this.svgContent = ioniconContent.get(url)));
  177. }
  178. this.didLoadIcon = true;
  179. }
  180. }
  181. this.iconName = getName(this.name, this.icon, this.mode, this.ios, this.md);
  182. }
  183. render() {
  184. const { flipRtl, iconName, inheritedAttributes, el } = this;
  185. const mode = this.mode || 'md';
  186. // we have designated that arrows & chevrons should automatically flip (unless flip-rtl is set to false) because "back" is left in ltr and right in rtl, and "forward" is the opposite
  187. const shouldAutoFlip = iconName
  188. ? (iconName.includes('arrow') || iconName.includes('chevron')) && flipRtl !== false
  189. : false;
  190. // if shouldBeFlippable is true, the icon should change direction when `dir` changes
  191. const shouldBeFlippable = flipRtl || shouldAutoFlip;
  192. return (h(Host, Object.assign({ role: "img", class: Object.assign(Object.assign({ [mode]: true }, createColorClasses(this.color)), { [`icon-${this.size}`]: !!this.size, 'flip-rtl': shouldBeFlippable, 'icon-rtl': shouldBeFlippable && isRTL(el) }) }, inheritedAttributes), this.svgContent ? (h("div", { class: "icon-inner", innerHTML: this.svgContent })) : (h("div", { class: "icon-inner" }))));
  193. }
  194. static get assetsDirs() { return ["svg"]; }
  195. get el() { return getElement(this); }
  196. static get watchers() { return {
  197. "name": ["loadIcon"],
  198. "src": ["loadIcon"],
  199. "icon": ["loadIcon"],
  200. "ios": ["loadIcon"],
  201. "md": ["loadIcon"]
  202. }; }
  203. };
  204. const getIonMode = () => (typeof document !== 'undefined' && document.documentElement.getAttribute('mode')) || 'md';
  205. const createColorClasses = (color) => {
  206. return color
  207. ? {
  208. 'ion-color': true,
  209. [`ion-color-${color}`]: true,
  210. }
  211. : null;
  212. };
  213. Icon.style = iconCss;
  214. export { Icon as ion_icon };