diff options
| author | Shipwreckt <me@shipwreckt.co.uk> | 2025-10-31 20:02:14 +0000 |
|---|---|---|
| committer | Shipwreckt <me@shipwreckt.co.uk> | 2025-10-31 20:02:14 +0000 |
| commit | 7a52ddeba2a68388b544f529d2d92104420f77b0 (patch) | |
| tree | 15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/posthtml-match-helper/lib | |
| parent | 53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff) | |
Changed from static to 11ty!
Diffstat (limited to 'node_modules/posthtml-match-helper/lib')
| -rw-r--r-- | node_modules/posthtml-match-helper/lib/index.d.ts | 10 | ||||
| -rw-r--r-- | node_modules/posthtml-match-helper/lib/index.js | 149 |
2 files changed, 159 insertions, 0 deletions
diff --git a/node_modules/posthtml-match-helper/lib/index.d.ts b/node_modules/posthtml-match-helper/lib/index.d.ts new file mode 100644 index 0000000..9e9ef25 --- /dev/null +++ b/node_modules/posthtml-match-helper/lib/index.d.ts @@ -0,0 +1,10 @@ +import type { AttrMatcher, StringMatcher } from "posthtml"; + +interface Matcher { + tag?: StringMatcher; + attrs: AttrMatcher; +} + +declare function createMatcher(matcher: string | string[]): Matcher | Matcher[]; + +export default createMatcher; diff --git a/node_modules/posthtml-match-helper/lib/index.js b/node_modules/posthtml-match-helper/lib/index.js new file mode 100644 index 0000000..6e7ca64 --- /dev/null +++ b/node_modules/posthtml-match-helper/lib/index.js @@ -0,0 +1,149 @@ +const selectorReg = /^([^#\.\[]+)?(?:#([^\.\[]+))?(?:\.((?:[^\[\]\\]|\\.)+))?((?:\[[^\]]*\])+)?$/; // eslint-disable-line +const attributeReg = /^([a-zA-Z0-9_-]*[^~|^$*!=])(?:([~|^$*!]?)=['"]?([^'"]*)['"]?)?$/; +const splitReg = /\s*,\s*/; + +function expandMatcher(matcher) { + if (typeof matcher === "string") { + const match = matcher.match(selectorReg); + + if (match) { + matcher = {}; + const tag = match[1]; + const id = match[2]; + const className = match[3]; + const attrs = match[4]; + let attributes; + + if (tag) { + matcher.tag = tag; + } + + if (attrs) { + attributes = expandAttributes(attrs); + } else if (id || className) { + attributes = {}; + } + + if (id) { + attributes.id = id; + } + + if (className) { + attributes.class = new RegExp(getCombinations(className.split(".")).map((order) => { + return "(?:^|\\s)" + order.join("\\s(?:.*?\\s)?") + "(?:\\s|$)"; + }).join("|")); + } + + if (attributes) { + matcher.attrs = attributes; + } + } else { + matcher = {tag: matcher}; + } + } + + return matcher; +} + +function cssAttrToRegExp(value, operator) { + let reg; + + switch (operator) { + case "~": + reg = "(?:^|\\s)" + value + "(?:\\s|$)"; + break; + + case "|": + reg = "^" + value + "(?:-|$)"; + break; + + case "^": + reg = "^" + value; + break; + + case "$": + reg = value + "$"; + break; + + case "*": + reg = value; + break; + + case "!": + reg = "^((?!" + value + ")[\\s\\S])*$"; + break; + + default: + reg = "^" + value + "$"; + break; + } + + return new RegExp(reg); +} + +function expandAttributes(attrs) { + attrs = attrs.slice(1, -1); + + if (attrs.length > 0) { + attrs = attrs.split("]["); + const attrObject = {}; + let l = attrs.length; + let attrMatch; + let name; + let operator; + let value; + + while (l--) { + attrMatch = attrs[l].match(attributeReg); + + if (attrMatch) { + name = attrMatch[1]; + operator = attrMatch[2]; + value = attrMatch[3]; + + if (!value) { + value = true; + } + + attrObject[name] = (operator) ? cssAttrToRegExp(value, operator) : value; + } + } + + return attrObject; + } +} + +function getCombinations(values, subresult) { + subresult = subresult || []; + let result = []; + + for (const value of values) { + if (subresult.indexOf(value) < 0) { + const _subresult = subresult.concat([value]); + + if (_subresult.length < values.length) { + result = result.concat(getCombinations(values, _subresult)); + } else { + result.push(_subresult); + } + } + } + + return result; +} + +export default function (matcher) { + if (typeof matcher === "string") { + if (matcher.match(splitReg)) { + matcher = matcher.split(splitReg); + } else { + return expandMatcher(matcher); + } + } + + if (Array.isArray(matcher)) { + return matcher.map(expandMatcher); + } + + return matcher; +} |
