@hyperframes/parsers 0.9.52 → 0.9.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assetResolution.js +3 -1
- package/dist/assets.js +68 -1
- package/dist/cssUrlScans.d.ts +64 -0
- package/dist/cssUrlScans.js +455 -0
- package/package.json +6 -2
package/dist/assetResolution.js
CHANGED
|
@@ -267,7 +267,9 @@ function gitBashFile(src) {
|
|
|
267
267
|
}
|
|
268
268
|
function resolveProjectRelativeSrc(src, baseDir, compiledDir) {
|
|
269
269
|
const cleanSrc = fileUrlAsPath(cleanAssetUrl(src));
|
|
270
|
-
if (isAbsolute2(cleanSrc) && existsSync(cleanSrc))
|
|
270
|
+
if (isAbsolute2(cleanSrc) && !namesAnotherNetworkRoot(baseDir, cleanSrc) && existsSync(cleanSrc)) {
|
|
271
|
+
return cleanSrc;
|
|
272
|
+
}
|
|
271
273
|
const candidates = /* @__PURE__ */ new Set();
|
|
272
274
|
for (const variant of decodeUrlPathVariants(cleanSrc)) {
|
|
273
275
|
for (const candidate of reanchoredCandidates(variant, baseDir, compiledDir)) {
|
package/dist/assets.js
CHANGED
|
@@ -57,6 +57,73 @@ function isPathInside(childPath, parentPath) {
|
|
|
57
57
|
// src/rewriteSubCompPaths.ts
|
|
58
58
|
import { posix } from "path";
|
|
59
59
|
|
|
60
|
+
// src/cssUrlScans.ts
|
|
61
|
+
var WS = /\s/;
|
|
62
|
+
var isWs = (char) => char !== void 0 && WS.test(char);
|
|
63
|
+
var isQuote = (char) => char === '"' || char === "'";
|
|
64
|
+
function memoForward(search) {
|
|
65
|
+
let from = 0;
|
|
66
|
+
let at = -1;
|
|
67
|
+
return (start) => {
|
|
68
|
+
if (start < from || start > at) {
|
|
69
|
+
from = start;
|
|
70
|
+
at = search(start);
|
|
71
|
+
}
|
|
72
|
+
return at;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function finder(source, pattern) {
|
|
76
|
+
return memoForward((start) => {
|
|
77
|
+
pattern.lastIndex = start;
|
|
78
|
+
return pattern.exec(source)?.index ?? source.length;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function urlFinders(source) {
|
|
82
|
+
return {
|
|
83
|
+
ws: finder(source, /\S/g),
|
|
84
|
+
stop: finder(source, /[)"']/g),
|
|
85
|
+
close: finder(source, /\S/g)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function quotedArgument(source, w, f, trimmed) {
|
|
89
|
+
const quote = source[w];
|
|
90
|
+
const e = f.stop(w + 1);
|
|
91
|
+
if (e === w + 1 || source[e] !== quote) return null;
|
|
92
|
+
if (trimmed && (isWs(source[w + 1]) || isWs(source[e - 1]))) return null;
|
|
93
|
+
const z = f.close(e + 1);
|
|
94
|
+
return source[z] === ")" ? { end: z + 1, quote, url: source.slice(w + 1, e) } : null;
|
|
95
|
+
}
|
|
96
|
+
function trimmedUrlArgument(source, start, f) {
|
|
97
|
+
const w = f.ws(start);
|
|
98
|
+
if (isQuote(source[w])) return quotedArgument(source, w, f, true);
|
|
99
|
+
const e = f.stop(w);
|
|
100
|
+
if (source[w] === ")" || source[e] !== ")") return null;
|
|
101
|
+
let j = e;
|
|
102
|
+
while (isWs(source[j - 1])) j--;
|
|
103
|
+
return { end: e + 1, quote: "", url: source.slice(w, j) };
|
|
104
|
+
}
|
|
105
|
+
function* urlMatches(source, argument) {
|
|
106
|
+
const opener = /\burl\(/g;
|
|
107
|
+
const f = urlFinders(source);
|
|
108
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
109
|
+
const arg = argument(source, open.index + 4, f);
|
|
110
|
+
if (!arg) continue;
|
|
111
|
+
yield { index: open.index, parts: [source.slice(open.index, arg.end), arg.quote, arg.url] };
|
|
112
|
+
opener.lastIndex = arg.end;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
var cssUrlMatches = (source) => urlMatches(source, trimmedUrlArgument);
|
|
116
|
+
function replaceMatches(source, matches, replace) {
|
|
117
|
+
const call = replace;
|
|
118
|
+
let out = "";
|
|
119
|
+
let kept = 0;
|
|
120
|
+
for (const { index, parts } of matches) {
|
|
121
|
+
out += source.slice(kept, index) + call(...parts);
|
|
122
|
+
kept = index + parts[0].length;
|
|
123
|
+
}
|
|
124
|
+
return out + source.slice(kept);
|
|
125
|
+
}
|
|
126
|
+
|
|
60
127
|
// src/srcset.ts
|
|
61
128
|
function skipSrcsetDescriptors(srcset, position) {
|
|
62
129
|
let inParens = false;
|
|
@@ -147,7 +214,7 @@ function rewriteInlineStyleAssetUrls(elements, compSrcPath, getStyle, setStyle,
|
|
|
147
214
|
}
|
|
148
215
|
function rewriteCssAssetUrls(cssText, compSrcPath, assetExists) {
|
|
149
216
|
if (!cssText) return cssText;
|
|
150
|
-
return cssText
|
|
217
|
+
return replaceMatches(cssText, cssUrlMatches(cssText), (full, quote, rawUrl) => {
|
|
151
218
|
const urlValue = (rawUrl || "").trim();
|
|
152
219
|
const rewritten = rewriteAssetPath(compSrcPath, urlValue, assetExists);
|
|
153
220
|
if (rewritten === urlValue) return full;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** One match as `matchAll` reports it: where it starts, then the full text and each group. */
|
|
2
|
+
interface ScanMatch<G extends Array<string | undefined>> {
|
|
3
|
+
index: number;
|
|
4
|
+
parts: [string, ...G];
|
|
5
|
+
}
|
|
6
|
+
type UrlParts = [quote: string, url: string];
|
|
7
|
+
type ImportParts = [
|
|
8
|
+
urlQuote: string | undefined,
|
|
9
|
+
url: string | undefined,
|
|
10
|
+
quote: string | undefined,
|
|
11
|
+
path: string | undefined,
|
|
12
|
+
media: string
|
|
13
|
+
];
|
|
14
|
+
type HashRefParts = [pre: string, quote: string, id: string, post: string];
|
|
15
|
+
type SceneUrlParts = [
|
|
16
|
+
urlQuote: string | undefined,
|
|
17
|
+
url: string | undefined,
|
|
18
|
+
quote: string | undefined,
|
|
19
|
+
path: string | undefined
|
|
20
|
+
];
|
|
21
|
+
/** Matches of `/\burl\(\s*(["']?)([^)"'\s](?:[^)"']*[^)"'\s])?)\1\s*\)/g`. */
|
|
22
|
+
declare const cssUrlMatches: (source: string) => Generator<ScanMatch<UrlParts>, any, any>;
|
|
23
|
+
/** Matches of `/\burl\(\s*(["']?)([^)"']+)\1\s*\)/g`. */
|
|
24
|
+
declare const rawCssUrlMatches: (source: string) => Generator<ScanMatch<UrlParts>, any, any>;
|
|
25
|
+
type Replacer<M> = M extends ScanMatch<infer G> ? (full: string, ...groups: G) => string : never;
|
|
26
|
+
/** `source.replace(pattern, replace)` for the matches a scan found in `source`. */
|
|
27
|
+
declare function replaceMatches<M extends ScanMatch<Array<string | undefined>>>(source: string, matches: Iterable<M>, replace: Replacer<M>): string;
|
|
28
|
+
/** Matches of `/@import\s+(?:url\(\s*(["']?)([^)"']+)\1\s*\)|(["'])([^"']+)\3)\s*([^;]*);\s*\/g`. */
|
|
29
|
+
declare function cssImportMatches(source: string): Generator<ScanMatch<ImportParts>>;
|
|
30
|
+
/** Matches of `/(url\(\s*)(["']?)#([^"')\s]+)\2(\s*\))/gi`. */
|
|
31
|
+
declare function urlHashRefMatches(source: string): Generator<ScanMatch<HashRefParts>>;
|
|
32
|
+
/** Matches of `/url\(\s*(['"]?)(.*?)\1\s*\)|@import\s+(['"])(.*?)\3/gi`. */
|
|
33
|
+
declare function sceneCssUrlMatches(source: string): Generator<ScanMatch<SceneUrlParts>>;
|
|
34
|
+
/** Matches of `/@import\s+url\([^)]*\)\s*;|@import\s+["'][^"']+["']\s*;/gi`. */
|
|
35
|
+
declare function importRuleMatches(source: string): Generator<ScanMatch<[]>>;
|
|
36
|
+
/** `/(?:^|;|\{)\s*background(?:-image)?\s*:[^;}]*(?:\bgradient\s*\(|url\s*\()/i.test(source)`. */
|
|
37
|
+
declare function hasBackgroundImageDeclaration(source: string): boolean;
|
|
38
|
+
/** Matches of `/background(?:-image)?\s*:\s*[^;}"']*?url\(\s*["']?(https?:\/\/[^"')]+)["']?\s*\)/gi`. */
|
|
39
|
+
declare function remoteBackgroundUrlMatches(source: string): Generator<ScanMatch<[url: string]>>;
|
|
40
|
+
/** Matches of `/url\(["']?(https?:\/\/[^"')]+)["']?\)/gi`. */
|
|
41
|
+
declare const remoteFontFaceUrlMatches: (source: string) => Generator<ScanMatch<[url: string]>, any, any>;
|
|
42
|
+
/** Matches of `/url\(["']?(?!data:|file:|https?:\/\/)([^"')]+)["']?\)/gi`. */
|
|
43
|
+
declare const localFontFaceUrlMatches: (source: string) => Generator<ScanMatch<[url: string]>, any, any>;
|
|
44
|
+
type BareOrQuotedParts = [
|
|
45
|
+
double: string | undefined,
|
|
46
|
+
single: string | undefined,
|
|
47
|
+
bare: string | undefined
|
|
48
|
+
];
|
|
49
|
+
/** Matches of `/\burl\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi`. */
|
|
50
|
+
declare function bareOrQuotedCssUrlMatches(source: string): Generator<ScanMatch<BareOrQuotedParts>>;
|
|
51
|
+
/** Matches of `/\b(?:-webkit-)?mask-image\s*:\s*[^;{}]*url\(\s*(?:"([^"]+)"|'([^']+)'|([^"')\s]+))\s*\)/gi`. */
|
|
52
|
+
declare function maskImageUrlMatches(source: string): Generator<ScanMatch<BareOrQuotedParts>>;
|
|
53
|
+
/** Matches of `/<style\b[^>]*>([\s\S]*?)<\/style>/gi`. */
|
|
54
|
+
declare function styleBlockMatches(source: string): Generator<ScanMatch<[css: string]>>;
|
|
55
|
+
/** Matches of `/@font-face\s*\{([^}]*)\}/gi`. */
|
|
56
|
+
declare function fontFaceBlockMatches(source: string): Generator<ScanMatch<[body: string]>>;
|
|
57
|
+
/** Matches of `/\/\*[\s\S]*?\*\//g`. */
|
|
58
|
+
declare function blockCommentMatches(source: string): Generator<ScanMatch<[]>>;
|
|
59
|
+
/** Matches of `/<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi`. */
|
|
60
|
+
declare function stylesheetLinkMatches(source: string): Generator<ScanMatch<[]>>;
|
|
61
|
+
/** Matches of `/([^{}]+)\{([^}]*)\}/g`. */
|
|
62
|
+
declare function cssRuleMatches(source: string): Generator<ScanMatch<[selector: string, declarations: string]>>;
|
|
63
|
+
|
|
64
|
+
export { type ScanMatch, bareOrQuotedCssUrlMatches, blockCommentMatches, cssImportMatches, cssRuleMatches, cssUrlMatches, fontFaceBlockMatches, hasBackgroundImageDeclaration, importRuleMatches, localFontFaceUrlMatches, maskImageUrlMatches, rawCssUrlMatches, remoteBackgroundUrlMatches, remoteFontFaceUrlMatches, replaceMatches, sceneCssUrlMatches, styleBlockMatches, stylesheetLinkMatches, urlHashRefMatches };
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
// src/cssUrlScans.ts
|
|
2
|
+
var WS = /\s/;
|
|
3
|
+
var WORD = /\w/;
|
|
4
|
+
var BACKGROUND = /background/iy;
|
|
5
|
+
var IMAGE_SUFFIX = /-image/iy;
|
|
6
|
+
var URL_OPEN = /url\(/iy;
|
|
7
|
+
var REMOTE_SCHEME = /https?:\/\//iy;
|
|
8
|
+
var NON_LOCAL_SCHEME = /data:|file:|https?:\/\//iy;
|
|
9
|
+
var isWs = (char) => char !== void 0 && WS.test(char);
|
|
10
|
+
var isQuote = (char) => char === '"' || char === "'";
|
|
11
|
+
function endOfMatchAt(pattern, source, index) {
|
|
12
|
+
pattern.lastIndex = index;
|
|
13
|
+
return pattern.test(source) ? pattern.lastIndex : -1;
|
|
14
|
+
}
|
|
15
|
+
function memoForward(search) {
|
|
16
|
+
let from = 0;
|
|
17
|
+
let at = -1;
|
|
18
|
+
return (start) => {
|
|
19
|
+
if (start < from || start > at) {
|
|
20
|
+
from = start;
|
|
21
|
+
at = search(start);
|
|
22
|
+
}
|
|
23
|
+
return at;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function finder(source, pattern) {
|
|
27
|
+
return memoForward((start) => {
|
|
28
|
+
pattern.lastIndex = start;
|
|
29
|
+
return pattern.exec(source)?.index ?? source.length;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function firstAccepted(source, candidates, accept) {
|
|
33
|
+
return memoForward((start) => {
|
|
34
|
+
let i = candidates(start);
|
|
35
|
+
while (i < source.length && !accept(i)) i = candidates(i + 1);
|
|
36
|
+
return i;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function urlFinders(source) {
|
|
40
|
+
return {
|
|
41
|
+
ws: finder(source, /\S/g),
|
|
42
|
+
stop: finder(source, /[)"']/g),
|
|
43
|
+
close: finder(source, /\S/g)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function quotedArgument(source, w, f, trimmed) {
|
|
47
|
+
const quote = source[w];
|
|
48
|
+
const e = f.stop(w + 1);
|
|
49
|
+
if (e === w + 1 || source[e] !== quote) return null;
|
|
50
|
+
if (trimmed && (isWs(source[w + 1]) || isWs(source[e - 1]))) return null;
|
|
51
|
+
const z = f.close(e + 1);
|
|
52
|
+
return source[z] === ")" ? { end: z + 1, quote, url: source.slice(w + 1, e) } : null;
|
|
53
|
+
}
|
|
54
|
+
function rawUrlArgument(source, start, f) {
|
|
55
|
+
const w = f.ws(start);
|
|
56
|
+
if (isQuote(source[w])) return quotedArgument(source, w, f, false);
|
|
57
|
+
if (source[w] === ")") return w > start ? { end: w + 1, quote: "", url: source[w - 1] } : null;
|
|
58
|
+
const e = f.stop(w);
|
|
59
|
+
return source[e] === ")" ? { end: e + 1, quote: "", url: source.slice(w, e) } : null;
|
|
60
|
+
}
|
|
61
|
+
function trimmedUrlArgument(source, start, f) {
|
|
62
|
+
const w = f.ws(start);
|
|
63
|
+
if (isQuote(source[w])) return quotedArgument(source, w, f, true);
|
|
64
|
+
const e = f.stop(w);
|
|
65
|
+
if (source[w] === ")" || source[e] !== ")") return null;
|
|
66
|
+
let j = e;
|
|
67
|
+
while (isWs(source[j - 1])) j--;
|
|
68
|
+
return { end: e + 1, quote: "", url: source.slice(w, j) };
|
|
69
|
+
}
|
|
70
|
+
function* urlMatches(source, argument) {
|
|
71
|
+
const opener = /\burl\(/g;
|
|
72
|
+
const f = urlFinders(source);
|
|
73
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
74
|
+
const arg = argument(source, open.index + 4, f);
|
|
75
|
+
if (!arg) continue;
|
|
76
|
+
yield { index: open.index, parts: [source.slice(open.index, arg.end), arg.quote, arg.url] };
|
|
77
|
+
opener.lastIndex = arg.end;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
var cssUrlMatches = (source) => urlMatches(source, trimmedUrlArgument);
|
|
81
|
+
var rawCssUrlMatches = (source) => urlMatches(source, rawUrlArgument);
|
|
82
|
+
function replaceMatches(source, matches, replace) {
|
|
83
|
+
const call = replace;
|
|
84
|
+
let out = "";
|
|
85
|
+
let kept = 0;
|
|
86
|
+
for (const { index, parts } of matches) {
|
|
87
|
+
out += source.slice(kept, index) + call(...parts);
|
|
88
|
+
kept = index + parts[0].length;
|
|
89
|
+
}
|
|
90
|
+
return out + source.slice(kept);
|
|
91
|
+
}
|
|
92
|
+
function importTarget(source, w, f, quoteStop) {
|
|
93
|
+
if (source.startsWith("url(", w)) {
|
|
94
|
+
const arg = rawUrlArgument(source, w + 4, f);
|
|
95
|
+
return arg && { end: arg.end, groups: [arg.quote, arg.url, void 0, void 0] };
|
|
96
|
+
}
|
|
97
|
+
const quote = source[w];
|
|
98
|
+
if (!isQuote(quote)) return null;
|
|
99
|
+
const e = quoteStop(w + 1);
|
|
100
|
+
if (e === w + 1 || source[e] !== quote) return null;
|
|
101
|
+
return { end: e + 1, groups: [void 0, void 0, quote, source.slice(w + 1, e)] };
|
|
102
|
+
}
|
|
103
|
+
function* cssImportMatches(source) {
|
|
104
|
+
const opener = /@import/g;
|
|
105
|
+
const ws = finder(source, /\S/g);
|
|
106
|
+
const quoteStop = finder(source, /["']/g);
|
|
107
|
+
const semicolon = finder(source, /;/g);
|
|
108
|
+
const tail = finder(source, /\S/g);
|
|
109
|
+
const f = urlFinders(source);
|
|
110
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
111
|
+
const w = ws(open.index + 7);
|
|
112
|
+
const target = w > open.index + 7 ? importTarget(source, w, f, quoteStop) : null;
|
|
113
|
+
if (!target) continue;
|
|
114
|
+
const media = tail(target.end);
|
|
115
|
+
const semi = semicolon(media);
|
|
116
|
+
if (semi === source.length) continue;
|
|
117
|
+
const end = tail(semi + 1);
|
|
118
|
+
const full = source.slice(open.index, end);
|
|
119
|
+
yield { index: open.index, parts: [full, ...target.groups, source.slice(media, semi)] };
|
|
120
|
+
opener.lastIndex = end;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function* urlHashRefMatches(source) {
|
|
124
|
+
const opener = /url\(/gi;
|
|
125
|
+
const ws = finder(source, /\S/g);
|
|
126
|
+
const idEnd = finder(source, /[\s"')]/g);
|
|
127
|
+
const close = finder(source, /\S/g);
|
|
128
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
129
|
+
const w = ws(open.index + 4);
|
|
130
|
+
const quote = isQuote(source[w]) ? source[w] : "";
|
|
131
|
+
const hash = w + quote.length;
|
|
132
|
+
const e = source[hash] === "#" ? idEnd(hash + 1) : hash + 1;
|
|
133
|
+
if (e === hash + 1 || quote && source[e] !== quote) continue;
|
|
134
|
+
const post = e + quote.length;
|
|
135
|
+
const z = close(post);
|
|
136
|
+
if (source[z] !== ")") continue;
|
|
137
|
+
const full = source.slice(open.index, z + 1);
|
|
138
|
+
const id = source.slice(hash + 1, e);
|
|
139
|
+
yield {
|
|
140
|
+
index: open.index,
|
|
141
|
+
parts: [full, source.slice(open.index, w), quote, id, source.slice(post, z + 1)]
|
|
142
|
+
};
|
|
143
|
+
opener.lastIndex = z + 1;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function sceneFinders(source) {
|
|
147
|
+
const closeWs = finder(source, /\S/g);
|
|
148
|
+
const closer = (quote) => firstAccepted(
|
|
149
|
+
source,
|
|
150
|
+
finder(source, new RegExp(quote, "g")),
|
|
151
|
+
(j) => source[closeWs(j + 1)] === ")"
|
|
152
|
+
);
|
|
153
|
+
let runEnd = -1;
|
|
154
|
+
let runStart = -1;
|
|
155
|
+
const wsRunStart = (end) => {
|
|
156
|
+
if (end !== runEnd) {
|
|
157
|
+
for (runEnd = runStart = end; isWs(source[runStart - 1]); ) runStart--;
|
|
158
|
+
}
|
|
159
|
+
return runStart;
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
ws: finder(source, /\S/g),
|
|
163
|
+
lineEnd: finder(source, /[\n\r\u2028\u2029]/g),
|
|
164
|
+
paren: finder(source, /\)/g),
|
|
165
|
+
close: finder(source, /\S/g),
|
|
166
|
+
closer: { '"': closer('"'), "'": closer("'") },
|
|
167
|
+
quote: { '"': finder(source, /"/g), "'": finder(source, /'/g) },
|
|
168
|
+
wsRunStart
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function sceneUrlAt(source, open, f) {
|
|
172
|
+
const w = f.ws(open + 4);
|
|
173
|
+
const quote = source[w];
|
|
174
|
+
const line = f.lineEnd(w);
|
|
175
|
+
const closing = isQuote(quote) ? f.closer[quote](w + 1) : line;
|
|
176
|
+
if (closing < line) {
|
|
177
|
+
const groups = [quote, source.slice(w + 1, closing), void 0, void 0];
|
|
178
|
+
return { end: f.close(closing + 1) + 1, groups };
|
|
179
|
+
}
|
|
180
|
+
const paren = f.paren(w);
|
|
181
|
+
if (paren === source.length) return null;
|
|
182
|
+
const bodyEnd = Math.max(w, f.wsRunStart(paren));
|
|
183
|
+
if (bodyEnd > line) return null;
|
|
184
|
+
return { end: paren + 1, groups: ["", source.slice(w, bodyEnd), void 0, void 0] };
|
|
185
|
+
}
|
|
186
|
+
function sceneImportAt(source, open, f) {
|
|
187
|
+
const w = f.ws(open + 7);
|
|
188
|
+
const quote = source[w];
|
|
189
|
+
if (w === open + 7 || !isQuote(quote)) return null;
|
|
190
|
+
const closing = f.quote[quote](w + 1);
|
|
191
|
+
if (closing >= f.lineEnd(w + 1)) return null;
|
|
192
|
+
return { end: closing + 1, groups: [void 0, void 0, quote, source.slice(w + 1, closing)] };
|
|
193
|
+
}
|
|
194
|
+
function* sceneCssUrlMatches(source) {
|
|
195
|
+
const opener = /url\(|@import/gi;
|
|
196
|
+
const f = sceneFinders(source);
|
|
197
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
198
|
+
const at = open[0].length === 4 ? sceneUrlAt : sceneImportAt;
|
|
199
|
+
const match = at(source, open.index, f);
|
|
200
|
+
if (!match) continue;
|
|
201
|
+
yield { index: open.index, parts: [source.slice(open.index, match.end), ...match.groups] };
|
|
202
|
+
opener.lastIndex = match.end;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function importRuleClose(source, w, paren, quote) {
|
|
206
|
+
const url = endOfMatchAt(URL_OPEN, source, w);
|
|
207
|
+
if (url >= 0) return paren(url) < source.length ? paren(url) : -1;
|
|
208
|
+
if (!isQuote(source[w])) return -1;
|
|
209
|
+
const close = quote(w + 1);
|
|
210
|
+
return close > w + 1 && close < source.length ? close : -1;
|
|
211
|
+
}
|
|
212
|
+
function* importRuleMatches(source) {
|
|
213
|
+
const opener = /@import/gi;
|
|
214
|
+
const ws = finder(source, /\S/g);
|
|
215
|
+
const paren = finder(source, /\)/g);
|
|
216
|
+
const quote = finder(source, /["']/g);
|
|
217
|
+
const tail = finder(source, /\S/g);
|
|
218
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
219
|
+
const w = ws(open.index + 7);
|
|
220
|
+
const close = w > open.index + 7 ? importRuleClose(source, w, paren, quote) : -1;
|
|
221
|
+
const z = close < 0 ? -1 : tail(close + 1);
|
|
222
|
+
if (source[z] !== ";") continue;
|
|
223
|
+
yield { index: open.index, parts: [source.slice(open.index, z + 1)] };
|
|
224
|
+
opener.lastIndex = z + 1;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function backgroundValueStart(source, at, ws) {
|
|
228
|
+
let k = endOfMatchAt(BACKGROUND, source, at);
|
|
229
|
+
if (k < 0) return -1;
|
|
230
|
+
k = Math.max(k, endOfMatchAt(IMAGE_SUFFIX, source, k));
|
|
231
|
+
const colon = ws(k);
|
|
232
|
+
return source[colon] === ":" ? colon + 1 : -1;
|
|
233
|
+
}
|
|
234
|
+
function hasBackgroundImageDeclaration(source) {
|
|
235
|
+
const ws = finder(source, /\S/g);
|
|
236
|
+
const colonWs = finder(source, /\S/g);
|
|
237
|
+
const valueEnd = finder(source, /[;}]/g);
|
|
238
|
+
const tokenWs = finder(source, /\S/g);
|
|
239
|
+
const imageValue = firstAccepted(source, finder(source, /gradient|url/gi), (i) => {
|
|
240
|
+
const gradient = source[i] === "g" || source[i] === "G";
|
|
241
|
+
if (gradient && WORD.test(source[i - 1] ?? "")) return false;
|
|
242
|
+
return source[tokenWs(i + (gradient ? 8 : 3))] === "(";
|
|
243
|
+
});
|
|
244
|
+
const opener = /[;{]/g;
|
|
245
|
+
for (let start = 0; start !== null; ) {
|
|
246
|
+
const value = backgroundValueStart(source, ws(start), colonWs);
|
|
247
|
+
if (value >= 0 && imageValue(value) < valueEnd(value)) return true;
|
|
248
|
+
const next = opener.exec(source);
|
|
249
|
+
start = next ? next.index + 1 : null;
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
function bareUrlArgument(source, start, f, head) {
|
|
254
|
+
const w = f.ws ? f.ws(start) : start;
|
|
255
|
+
const from = isQuote(source[w]) ? w + 1 : w;
|
|
256
|
+
const body = head(from);
|
|
257
|
+
const e = body < 0 ? body : f.stop(body);
|
|
258
|
+
if (e <= body) return null;
|
|
259
|
+
const close = isQuote(source[e]) ? f.close ? f.close(e + 1) : e + 1 : e;
|
|
260
|
+
return source[close] === ")" ? { end: close + 1, quote: "", url: source.slice(from, e) } : null;
|
|
261
|
+
}
|
|
262
|
+
var remoteHead = (source) => (at) => endOfMatchAt(REMOTE_SCHEME, source, at);
|
|
263
|
+
var localHead = (source) => (at) => endOfMatchAt(NON_LOCAL_SCHEME, source, at) < 0 ? at : -1;
|
|
264
|
+
function* remoteBackgroundUrlMatches(source) {
|
|
265
|
+
const opener = /background/gi;
|
|
266
|
+
const ws = finder(source, /\S/g);
|
|
267
|
+
const valueEnd = finder(source, /[;}"']/g);
|
|
268
|
+
const f = urlFinders(source);
|
|
269
|
+
const head = remoteHead(source);
|
|
270
|
+
const urlAt = (open) => bareUrlArgument(source, open + 4, f, head);
|
|
271
|
+
const firstUrl = firstAccepted(source, finder(source, /url\(/gi), (i) => urlAt(i) !== null);
|
|
272
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
273
|
+
const value = backgroundValueStart(source, open.index, ws);
|
|
274
|
+
if (value < 0) continue;
|
|
275
|
+
const start = ws(value);
|
|
276
|
+
const url = firstUrl(start);
|
|
277
|
+
const arg = url < valueEnd(start) ? urlAt(url) : null;
|
|
278
|
+
if (!arg) continue;
|
|
279
|
+
yield { index: open.index, parts: [source.slice(open.index, arg.end), arg.url] };
|
|
280
|
+
opener.lastIndex = arg.end;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function* fontFaceUrlMatches(source, head) {
|
|
284
|
+
const opener = /url\(/gi;
|
|
285
|
+
const f = { stop: finder(source, /[)"']/g) };
|
|
286
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
287
|
+
const arg = bareUrlArgument(source, open.index + 4, f, head);
|
|
288
|
+
if (!arg) continue;
|
|
289
|
+
yield { index: open.index, parts: [source.slice(open.index, arg.end), arg.url] };
|
|
290
|
+
opener.lastIndex = arg.end;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
var remoteFontFaceUrlMatches = (source) => fontFaceUrlMatches(source, remoteHead(source));
|
|
294
|
+
var localFontFaceUrlMatches = (source) => fontFaceUrlMatches(source, localHead(source));
|
|
295
|
+
function bareOrQuotedFinders(source) {
|
|
296
|
+
return {
|
|
297
|
+
ws: finder(source, /\S/g),
|
|
298
|
+
quote: { '"': finder(source, /"/g), "'": finder(source, /'/g) },
|
|
299
|
+
bareEnd: finder(source, /[\s"')]/g),
|
|
300
|
+
quotedClose: finder(source, /\S/g),
|
|
301
|
+
bareClose: finder(source, /\S/g)
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function bareOrQuotedArgument(source, start, f) {
|
|
305
|
+
const w = f.ws(start);
|
|
306
|
+
const quote = source[w];
|
|
307
|
+
if (isQuote(quote)) {
|
|
308
|
+
const e2 = f.quote[quote](w + 1);
|
|
309
|
+
if (e2 === w + 1 || e2 === source.length) return null;
|
|
310
|
+
const body = source.slice(w + 1, e2);
|
|
311
|
+
const z2 = f.quotedClose(e2 + 1);
|
|
312
|
+
const groups = quote === '"' ? [body, void 0, void 0] : [void 0, body, void 0];
|
|
313
|
+
return source[z2] === ")" ? { end: z2 + 1, groups } : null;
|
|
314
|
+
}
|
|
315
|
+
const e = f.bareEnd(w);
|
|
316
|
+
const z = e === w ? -1 : f.bareClose(e);
|
|
317
|
+
return source[z] === ")" ? { end: z + 1, groups: [void 0, void 0, source.slice(w, e)] } : null;
|
|
318
|
+
}
|
|
319
|
+
function* bareOrQuotedCssUrlMatches(source) {
|
|
320
|
+
const opener = /\burl\(/gi;
|
|
321
|
+
const f = bareOrQuotedFinders(source);
|
|
322
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
323
|
+
const arg = bareOrQuotedArgument(source, open.index + 4, f);
|
|
324
|
+
if (!arg) continue;
|
|
325
|
+
yield { index: open.index, parts: [source.slice(open.index, arg.end), ...arg.groups] };
|
|
326
|
+
opener.lastIndex = arg.end;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
function lastUrlBefore(source) {
|
|
330
|
+
const f = bareOrQuotedFinders(source);
|
|
331
|
+
let acceptedArg = null;
|
|
332
|
+
const nextUrl = firstAccepted(source, finder(source, /url\(/gi), (i) => {
|
|
333
|
+
acceptedArg = bareOrQuotedArgument(source, i + 4, f);
|
|
334
|
+
return acceptedArg !== null;
|
|
335
|
+
});
|
|
336
|
+
let last = null;
|
|
337
|
+
let next = nextUrl(0);
|
|
338
|
+
let nextArg = acceptedArg;
|
|
339
|
+
return (limit) => {
|
|
340
|
+
while (next < limit && nextArg) {
|
|
341
|
+
last = { at: next, arg: nextArg };
|
|
342
|
+
next = nextUrl(next + 1);
|
|
343
|
+
nextArg = acceptedArg;
|
|
344
|
+
}
|
|
345
|
+
return last;
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function* maskImageUrlMatches(source) {
|
|
349
|
+
const opener = /\b(?:-webkit-)?mask-image/gi;
|
|
350
|
+
const ws = finder(source, /\S/g);
|
|
351
|
+
const valueEnd = finder(source, /[;{}]/g);
|
|
352
|
+
const urlBefore = lastUrlBefore(source);
|
|
353
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
354
|
+
const colon = ws(opener.lastIndex);
|
|
355
|
+
const value = source[colon] === ":" ? ws(colon + 1) : -1;
|
|
356
|
+
const url = value < 0 ? null : urlBefore(valueEnd(value));
|
|
357
|
+
if (!url || url.at < value) continue;
|
|
358
|
+
const { end, groups } = url.arg;
|
|
359
|
+
yield { index: open.index, parts: [source.slice(open.index, end), ...groups] };
|
|
360
|
+
opener.lastIndex = end;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function* styleBlockMatches(source) {
|
|
364
|
+
const opener = /<style\b/gi;
|
|
365
|
+
const tagEnd = finder(source, />/g);
|
|
366
|
+
const closer = finder(source, /<\/style>/gi);
|
|
367
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
368
|
+
const body = tagEnd(open.index + 6) + 1;
|
|
369
|
+
const close = body > source.length ? source.length : closer(body);
|
|
370
|
+
if (close === source.length) return;
|
|
371
|
+
yield {
|
|
372
|
+
index: open.index,
|
|
373
|
+
parts: [source.slice(open.index, close + 8), source.slice(body, close)]
|
|
374
|
+
};
|
|
375
|
+
opener.lastIndex = close + 8;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
function* fontFaceBlockMatches(source) {
|
|
379
|
+
const opener = /@font-face/gi;
|
|
380
|
+
const ws = finder(source, /\S/g);
|
|
381
|
+
const brace = finder(source, /\}/g);
|
|
382
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
383
|
+
const w = ws(open.index + 10);
|
|
384
|
+
const close = source[w] === "{" ? brace(w + 1) : source.length;
|
|
385
|
+
if (close === source.length) continue;
|
|
386
|
+
yield {
|
|
387
|
+
index: open.index,
|
|
388
|
+
parts: [source.slice(open.index, close + 1), source.slice(w + 1, close)]
|
|
389
|
+
};
|
|
390
|
+
opener.lastIndex = close + 1;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
function* blockCommentMatches(source) {
|
|
394
|
+
const opener = /\/\*/g;
|
|
395
|
+
const closer = finder(source, /\*\//g);
|
|
396
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
397
|
+
const close = closer(open.index + 2);
|
|
398
|
+
if (close === source.length) return;
|
|
399
|
+
yield { index: open.index, parts: [source.slice(open.index, close + 2)] };
|
|
400
|
+
opener.lastIndex = close + 2;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function* stylesheetLinkMatches(source) {
|
|
404
|
+
const opener = /<link\b/gi;
|
|
405
|
+
const tagEnd = finder(source, />/g);
|
|
406
|
+
const rel = finder(source, /\brel=["']stylesheet["']/gi);
|
|
407
|
+
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
|
408
|
+
const end = tagEnd(open.index + 5);
|
|
409
|
+
if (end === source.length) return;
|
|
410
|
+
if (rel(open.index + 5) > end) continue;
|
|
411
|
+
yield { index: open.index, parts: [source.slice(open.index, end + 1)] };
|
|
412
|
+
opener.lastIndex = end + 1;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
function* cssRuleMatches(source) {
|
|
416
|
+
const ruleStart = finder(source, /[^{}]/g);
|
|
417
|
+
const brace = finder(source, /[{}]/g);
|
|
418
|
+
const close = finder(source, /\}/g);
|
|
419
|
+
for (let at = 0; at < source.length; ) {
|
|
420
|
+
const start = ruleStart(at);
|
|
421
|
+
const open = brace(start);
|
|
422
|
+
if (source[open] !== "{") {
|
|
423
|
+
at = open + 1;
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
const end = close(open + 1);
|
|
427
|
+
if (end === source.length) return;
|
|
428
|
+
const selector = source.slice(start, open);
|
|
429
|
+
yield {
|
|
430
|
+
index: start,
|
|
431
|
+
parts: [source.slice(start, end + 1), selector, source.slice(open + 1, end)]
|
|
432
|
+
};
|
|
433
|
+
at = end + 1;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
export {
|
|
437
|
+
bareOrQuotedCssUrlMatches,
|
|
438
|
+
blockCommentMatches,
|
|
439
|
+
cssImportMatches,
|
|
440
|
+
cssRuleMatches,
|
|
441
|
+
cssUrlMatches,
|
|
442
|
+
fontFaceBlockMatches,
|
|
443
|
+
hasBackgroundImageDeclaration,
|
|
444
|
+
importRuleMatches,
|
|
445
|
+
localFontFaceUrlMatches,
|
|
446
|
+
maskImageUrlMatches,
|
|
447
|
+
rawCssUrlMatches,
|
|
448
|
+
remoteBackgroundUrlMatches,
|
|
449
|
+
remoteFontFaceUrlMatches,
|
|
450
|
+
replaceMatches,
|
|
451
|
+
sceneCssUrlMatches,
|
|
452
|
+
styleBlockMatches,
|
|
453
|
+
stylesheetLinkMatches,
|
|
454
|
+
urlHashRefMatches
|
|
455
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperframes/parsers",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.53",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/heygen-com/hyperframes",
|
|
@@ -56,6 +56,10 @@
|
|
|
56
56
|
"import": "./dist/assets.js",
|
|
57
57
|
"types": "./dist/assets.d.ts"
|
|
58
58
|
},
|
|
59
|
+
"./css-url-scans": {
|
|
60
|
+
"import": "./dist/cssUrlScans.js",
|
|
61
|
+
"types": "./dist/cssUrlScans.d.ts"
|
|
62
|
+
},
|
|
59
63
|
"./composition": {
|
|
60
64
|
"import": "./dist/composition.js",
|
|
61
65
|
"types": "./dist/composition.d.ts"
|
|
@@ -139,7 +143,7 @@
|
|
|
139
143
|
"tsx": "^4.21.0",
|
|
140
144
|
"typescript": "^5.0.0",
|
|
141
145
|
"vitest": "^4.1.12",
|
|
142
|
-
"@hyperframes/core": "0.9.
|
|
146
|
+
"@hyperframes/core": "0.9.53"
|
|
143
147
|
},
|
|
144
148
|
"scripts": {
|
|
145
149
|
"build": "tsup",
|