-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'svg:main' into patch-4
- Loading branch information
Showing
10 changed files
with
236 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ exports.description = 'prefix IDs'; | |
*/ | ||
const getBasename = (path) => { | ||
// extract everything after latest slash or backslash | ||
const matched = path.match(/[/\\]?([^/\\]+)$/); | ||
const matched = /[/\\]?([^/\\]+)$/.exec(path); | ||
if (matched) { | ||
return matched[1]; | ||
} | ||
|
@@ -118,7 +118,6 @@ const generatePrefix = (body, node, info, prefixGenerator, delim, history) => { | |
* Prefixes identifiers | ||
* | ||
* @author strarsis <[email protected]> | ||
* | ||
* @type {import('./plugins-types').Plugin<'prefixIds'>} | ||
*/ | ||
exports.fn = (_root, params, info) => { | ||
|
@@ -149,60 +148,48 @@ exports.fn = (_root, params, info) => { | |
return; | ||
} | ||
|
||
// parse styles | ||
let cssText = ''; | ||
if ( | ||
node.children[0].type === 'text' || | ||
node.children[0].type === 'cdata' | ||
) { | ||
cssText = node.children[0].value; | ||
} | ||
/** | ||
* @type {?csstree.CssNode} | ||
*/ | ||
let cssAst = null; | ||
try { | ||
cssAst = csstree.parse(cssText, { | ||
parseValue: true, | ||
parseCustomProperty: false, | ||
}); | ||
} catch { | ||
return; | ||
} | ||
for (const child of node.children) { | ||
if (child.type !== 'text' && child.type !== 'cdata') { | ||
continue; | ||
} | ||
|
||
csstree.walk(cssAst, (node) => { | ||
// #ID, .class selectors | ||
if ( | ||
(prefixIds && node.type === 'IdSelector') || | ||
(prefixClassNames && node.type === 'ClassSelector') | ||
) { | ||
node.name = prefixId(prefixGenerator, node.name); | ||
const cssText = child.value; | ||
/** @type {?csstree.CssNode} */ | ||
let cssAst = null; | ||
try { | ||
cssAst = csstree.parse(cssText, { | ||
parseValue: true, | ||
parseCustomProperty: false, | ||
}); | ||
} catch { | ||
return; | ||
} | ||
// url(...) references | ||
// csstree v2 changed this type | ||
// @ts-ignore | ||
if (node.type === 'Url' && node.value.length > 0) { | ||
const prefixed = prefixReference( | ||
prefixGenerator, | ||
// @ts-ignore | ||
unquote(node.value) | ||
); | ||
if (prefixed != null) { | ||
// @ts-ignore | ||
node.value = prefixed; | ||
|
||
csstree.walk(cssAst, (node) => { | ||
if ( | ||
(prefixIds && node.type === 'IdSelector') || | ||
(prefixClassNames && node.type === 'ClassSelector') | ||
) { | ||
node.name = prefixId(prefixGenerator, node.name); | ||
return; | ||
} | ||
} | ||
}); | ||
// @ts-ignore csstree v2 changed this type | ||
if (node.type === 'Url' && node.value.length > 0) { | ||
const prefixed = prefixReference( | ||
prefixGenerator, | ||
// @ts-ignore | ||
unquote(node.value) | ||
); | ||
if (prefixed != null) { | ||
// @ts-ignore | ||
node.value = prefixed; | ||
} | ||
} | ||
}); | ||
|
||
// update styles | ||
if ( | ||
node.children[0].type === 'text' || | ||
node.children[0].type === 'cdata' | ||
) { | ||
node.children[0].value = csstree.generate(cssAst); | ||
child.value = csstree.generate(cssAst); | ||
return; | ||
} | ||
return; | ||
} | ||
|
||
// prefix an ID attribute value | ||
|
@@ -243,15 +230,15 @@ exports.fn = (_root, params, info) => { | |
} | ||
} | ||
|
||
// prefix an URL attribute value | ||
// prefix a URL attribute value | ||
for (const name of referencesProps) { | ||
if ( | ||
node.attributes[name] != null && | ||
node.attributes[name].length !== 0 | ||
) { | ||
node.attributes[name] = node.attributes[name].replace( | ||
/url\((.*?)\)/gi, | ||
(match, url) => { | ||
/\burl\((["'])?(#.+?)\1\)/gi, | ||
(match, _, url) => { | ||
const prefixed = prefixReference(prefixGenerator, url); | ||
if (prefixed == null) { | ||
return match; | ||
|
Oops, something went wrong.