UNPKG

7.13 kBJavaScriptView Raw
1import { r as registerInstance, h, H as Host, a as getElement } from './index-468d974f.js';
2import { i as isStr, g as getUrl, b as getName } from './utils-4f847845.js';
3
4const validateContent = (svgContent) => {
5 const div = document.createElement('div');
6 div.innerHTML = svgContent;
7 // setup this way to ensure it works on our buddy IE
8 for (let i = div.childNodes.length - 1; i >= 0; i--) {
9 if (div.childNodes[i].nodeName.toLowerCase() !== 'svg') {
10 div.removeChild(div.childNodes[i]);
11 }
12 }
13 // must only have 1 root element
14 const svgElm = div.firstElementChild;
15 if (svgElm && svgElm.nodeName.toLowerCase() === 'svg') {
16 const svgClass = svgElm.getAttribute('class') || '';
17 svgElm.setAttribute('class', (svgClass + ' s-ion-icon').trim());
18 // root element must be an svg
19 // lets double check we've got valid elements
20 // do not allow scripts
21 if (isValid(svgElm)) {
22 return div.innerHTML;
23 }
24 }
25 return '';
26};
27const isValid = (elm) => {
28 if (elm.nodeType === 1) {
29 if (elm.nodeName.toLowerCase() === 'script') {
30 return false;
31 }
32 for (let i = 0; i < elm.attributes.length; i++) {
33 const val = elm.attributes[i].value;
34 if (isStr(val) && val.toLowerCase().indexOf('on') === 0) {
35 return false;
36 }
37 }
38 for (let i = 0; i < elm.childNodes.length; i++) {
39 if (!isValid(elm.childNodes[i])) {
40 return false;
41 }
42 }
43 }
44 return true;
45};
46
47const ioniconContent = new Map();
48const requests = new Map();
49const getSvgContent = (url, sanitize) => {
50 // see if we already have a request for this url
51 let req = requests.get(url);
52 if (!req) {
53 if (typeof fetch !== 'undefined' && typeof document !== 'undefined') {
54 // we don't already have a request
55 req = fetch(url).then((rsp) => {
56 if (rsp.ok) {
57 return rsp.text().then((svgContent) => {
58 if (svgContent && sanitize !== false) {
59 svgContent = validateContent(svgContent);
60 }
61 ioniconContent.set(url, svgContent || '');
62 });
63 }
64 ioniconContent.set(url, '');
65 });
66 // cache for the same requests
67 requests.set(url, req);
68 }
69 else {
70 // set to empty for ssr scenarios and resolve promise
71 ioniconContent.set(url, '');
72 return Promise.resolve();
73 }
74 }
75 return req;
76};
77
78const 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%}:host(.flip-rtl) .icon-inner{-webkit-transform:scaleX(-1);transform:scaleX(-1)}:host(.icon-small){font-size:18px !important}:host(.icon-large){font-size:32px !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)}";
79
80const Icon = class {
81 constructor(hostRef) {
82 registerInstance(this, hostRef);
83 this.iconName = null;
84 this.isVisible = false;
85 /**
86 * The mode determines which platform styles to use.
87 */
88 this.mode = getIonMode();
89 /**
90 * If enabled, ion-icon will be loaded lazily when it's visible in the viewport.
91 * Default, `false`.
92 */
93 this.lazy = false;
94 /**
95 * When set to `false`, SVG content that is HTTP fetched will not be checked
96 * if the response SVG content has any `<script>` elements, or any attributes
97 * that start with `on`, such as `onclick`.
98 * @default true
99 */
100 this.sanitize = true;
101 }
102 connectedCallback() {
103 // purposely do not return the promise here because loading
104 // the svg file should not hold up loading the app
105 // only load the svg if it's visible
106 this.waitUntilVisible(this.el, '50px', () => {
107 this.isVisible = true;
108 this.loadIcon();
109 });
110 }
111 disconnectedCallback() {
112 if (this.io) {
113 this.io.disconnect();
114 this.io = undefined;
115 }
116 }
117 waitUntilVisible(el, rootMargin, cb) {
118 if (this.lazy && typeof window !== 'undefined' && window.IntersectionObserver) {
119 const io = (this.io = new window.IntersectionObserver((data) => {
120 if (data[0].isIntersecting) {
121 io.disconnect();
122 this.io = undefined;
123 cb();
124 }
125 }, { rootMargin }));
126 io.observe(el);
127 }
128 else {
129 // browser doesn't support IntersectionObserver
130 // so just fallback to always show it
131 cb();
132 }
133 }
134 loadIcon() {
135 if (this.isVisible) {
136 const url = getUrl(this);
137 if (url) {
138 if (ioniconContent.has(url)) {
139 // sync if it's already loaded
140 this.svgContent = ioniconContent.get(url);
141 }
142 else {
143 // async if it hasn't been loaded
144 getSvgContent(url, this.sanitize).then(() => (this.svgContent = ioniconContent.get(url)));
145 }
146 }
147 }
148 const label = this.iconName = getName(this.name, this.icon, this.mode, this.ios, this.md);
149 if (!this.ariaLabel && this.ariaHidden !== 'true') {
150 // user did not provide a label
151 // come up with the label based on the icon name
152 if (label) {
153 this.ariaLabel = label.replace(/\-/g, ' ');
154 }
155 }
156 }
157 render() {
158 const { iconName } = this;
159 const mode = this.mode || 'md';
160 const flipRtl = this.flipRtl ||
161 (iconName &&
162 (iconName.indexOf('arrow') > -1 || iconName.indexOf('chevron') > -1) &&
163 this.flipRtl !== false);
164 return (h(Host, { role: "img", class: Object.assign(Object.assign({ [mode]: true }, createColorClasses(this.color)), { [`icon-${this.size}`]: !!this.size, 'flip-rtl': !!flipRtl && this.el.ownerDocument.dir === 'rtl' }) }, this.svgContent ? (h("div", { class: "icon-inner", innerHTML: this.svgContent })) : (h("div", { class: "icon-inner" }))));
165 }
166 static get assetsDirs() { return ["svg"]; }
167 get el() { return getElement(this); }
168 static get watchers() { return {
169 "name": ["loadIcon"],
170 "src": ["loadIcon"],
171 "icon": ["loadIcon"]
172 }; }
173};
174const getIonMode = () => (typeof document !== 'undefined' && document.documentElement.getAttribute('mode')) || 'md';
175const createColorClasses = (color) => {
176 return color
177 ? {
178 'ion-color': true,
179 [`ion-color-${color}`]: true,
180 }
181 : null;
182};
183Icon.style = iconCss;
184
185export { Icon as ion_icon };