|
| 1 | +let suf; |
| 2 | +let rulesObj = { |
| 3 | + include: [/^ui5-/], |
| 4 | + exclude: [], |
| 5 | +}; |
| 6 | +const tagsCache = new Map(); // true/false means the tag should/should not be cached, undefined means not known yet. |
| 7 | + |
| 8 | +/** |
| 9 | + * Sets the suffix to be used for custom elements scoping, f.e. pass "demo" to get tags such as "ui5-button-demo". |
| 10 | + * Note: by default all tags starting with "ui5-" will be scoped, unless you change this by calling "setCustomElementsScopingRules" |
| 11 | + * |
| 12 | + * @public |
| 13 | + * @param suffix The scoping suffix |
| 14 | + */ |
| 15 | +const setCustomElementsScopingSuffix = suffix => { |
| 16 | + if (!suffix.match(/^[a-zA-Z0-9_-]+$/)) { |
| 17 | + throw new Error("Only alphanumeric characters and dashes allowed for the scoping suffix"); |
| 18 | + } |
| 19 | + |
| 20 | + suf = suffix; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Returns the currently set scoping suffix, or undefined if not set. |
| 25 | + * |
| 26 | + * @public |
| 27 | + * @returns {String|undefined} |
| 28 | + */ |
| 29 | +const getCustomElementsScopingSuffix = () => { |
| 30 | + return suf; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Sets the rules, governing which custom element tags to scope and which not, f.e. |
| 35 | + * setCustomElementsScopingRules({include: [/^ui5-/]}, exclude: [/^ui5-mylib-/, /^ui5-carousel$/]); |
| 36 | + * will scope all elements starting with "ui5-" but not the ones starting with "ui5-mylib-" and not "ui5-carousel". |
| 37 | + * |
| 38 | + * @public |
| 39 | + * @param rules Object with "include" and "exclude" properties, both arrays of regular expressions. Note that "include" |
| 40 | + * rules are applied first and "exclude" rules second. |
| 41 | + */ |
| 42 | +const setCustomElementsScopingRules = rules => { |
| 43 | + if (!rules || !rules.include) { |
| 44 | + throw new Error(`"rules" must be an object with at least an "include" property`); |
| 45 | + } |
| 46 | + |
| 47 | + if (!Array.isArray(rules.include) || rules.include.some(rule => !(rule instanceof RegExp))) { |
| 48 | + throw new Error(`"rules.include" must be an array of regular expressions`); |
| 49 | + } |
| 50 | + |
| 51 | + if (rules.exclude && (!Array.isArray(rules.exclude) || rules.exclude.some(rule => !(rule instanceof RegExp)))) { |
| 52 | + throw new Error(`"rules.exclude" must be an array of regular expressions`); |
| 53 | + } |
| 54 | + |
| 55 | + rules.exclude = rules.exclude || []; |
| 56 | + rulesObj = rules; |
| 57 | + tagsCache.clear(); // reset the cache upon setting new rules |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Returns the rules, governing which custom element tags to scope and which not. By default, all elements |
| 62 | + * starting with "ui5-" are scoped. The default rules are: {include: [/^ui5-/]}. |
| 63 | + * |
| 64 | + * @public |
| 65 | + * @returns {Object} |
| 66 | + */ |
| 67 | +const getCustomElementsScopingRules = () => { |
| 68 | + return rulesObj; |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Determines whether custom elements with the given tag should be scoped or not. |
| 73 | + * The tag is first matched against the "include" rules and then against the "exclude" rules and the |
| 74 | + * result is cached until new rules are set. |
| 75 | + * |
| 76 | + * @public |
| 77 | + * @param tag |
| 78 | + */ |
| 79 | +const shouldScopeCustomElement = tag => { |
| 80 | + if (!tagsCache.has(tag)) { |
| 81 | + const result = rulesObj.include.some(rule => tag.match(rule)) && !rulesObj.exclude.some(rule => tag.match(rule)); |
| 82 | + tagsCache.set(tag, result); |
| 83 | + } |
| 84 | + |
| 85 | + return tagsCache.get(tag); |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * Returns the currently set scoping suffix, if any and if the tag should be scoped, or undefined otherwise. |
| 90 | + * |
| 91 | + * @public |
| 92 | + * @param tag |
| 93 | + * @returns {String} |
| 94 | + */ |
| 95 | +const getEffectiveScopingSuffixForTag = tag => { |
| 96 | + if (shouldScopeCustomElement(tag)) { |
| 97 | + return getCustomElementsScopingSuffix(); |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +export { |
| 102 | + setCustomElementsScopingSuffix, |
| 103 | + getCustomElementsScopingSuffix, |
| 104 | + setCustomElementsScopingRules, |
| 105 | + getCustomElementsScopingRules, |
| 106 | + shouldScopeCustomElement, |
| 107 | + getEffectiveScopingSuffixForTag, |
| 108 | +}; |
0 commit comments