ionicons
Version:
Premium icons for Ionic.
86 lines (83 loc) • 2.45 kB
JavaScript
let CACHED_MAP;
const getIconMap = () => {
if (!CACHED_MAP) {
const win = window;
win.Ionicons = win.Ionicons || {};
CACHED_MAP = win.Ionicons.map = win.Ionicons.map || new Map();
}
return CACHED_MAP;
};
const addIcons = (icons) => {
const map = getIconMap();
Object.keys(icons).forEach(name => {
map.set(name, icons[name]);
});
};
const getName = (name, mode, ios, md) => {
// default to "md" if somehow the mode wasn't set
mode = (mode || 'md').toLowerCase();
mode = mode === 'ios' ? 'ios' : 'md';
// if an icon was passed in using the ios or md attributes
// set the iconName to whatever was passed in
if (ios && mode === 'ios') {
name = ios.toLowerCase();
}
else if (md && mode === 'md') {
name = md.toLowerCase();
}
else if (name) {
name = name.toLowerCase();
if (!/^md-|^ios-|^logo-/.test(name)) {
// this does not have one of the defaults
// so lets auto add in the mode prefix for them
name = `${mode}-${name}`;
}
}
if (typeof name !== 'string' || name.trim() === '') {
return null;
}
// only allow alpha characters and dash
const invalidChars = name.replace(/[a-z]|-|\d/gi, '');
if (invalidChars !== '') {
return null;
}
return name;
};
const getSrc = (src) => {
if (typeof src === 'string') {
src = src.trim();
if (isSrc(src)) {
return src;
}
}
return null;
};
const isSrc = (str) => {
return str.length > 0 && /(\/|\.)/.test(str);
};
const isValid = (elm) => {
if (elm.nodeType === 1) {
if (elm.nodeName.toLowerCase() === 'script') {
return false;
}
for (let i = 0; i < elm.attributes.length; i++) {
const val = elm.attributes[i].value;
if (typeof val === 'string' && val.toLowerCase().indexOf('on') === 0) {
return false;
}
}
for (let i = 0; i < elm.childNodes.length; i++) {
if (!isValid(elm.childNodes[i])) {
return false;
}
}
}
return true;
};
exports.addIcons = addIcons;
exports.getIconMap = getIconMap;
exports.getName = getName;
exports.getSrc = getSrc;
exports.isSrc = isSrc;
exports.isValid = isValid;
;