Skip to content

Commit

Permalink
fix(cleanupIds): handle when 2 ids referenced in one attr (#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco authored Sep 25, 2023
1 parent f07e9de commit 42f7752
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^14.1.0",
"@types/css-tree": "^2.0.0",
"@types/csso": "^5.0.0",
"@types/csso": "^5.0.1",
"@types/jest": "^29.5.5",
"del": "^6.0.0",
"eslint": "^8.24.0",
Expand Down
25 changes: 13 additions & 12 deletions plugins/cleanupIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { referencesProps } = require('./_collections.js');
exports.name = 'cleanupIds';
exports.description = 'removes unused IDs and minifies used';

const regReferencesUrl = /\burl\((["'])?#(.+?)\1\)/;
const regReferencesUrl = /\burl\((["'])?#(.+?)\1\)/g;
const regReferencesHref = /^#(.+?)$/;
const regReferencesBegin = /(\D+)\./;
const generateIdChars = [
Expand Down Expand Up @@ -146,7 +146,7 @@ exports.fn = (_root, params) => {
*/
const nodeById = new Map();
/**
* @type {Map<string, Array<{element: XastElement, name: string, value: string }>>}
* @type {Map<string, Array<{element: XastElement, name: string }>>}
*/
const referencesById = new Map();
let deoptimized = false;
Expand Down Expand Up @@ -191,34 +191,34 @@ exports.fn = (_root, params) => {
} else {
// collect all references
/**
* @type {null | string}
* @type {string[]}
*/
let id = null;
let ids = [];
if (referencesProps.includes(name)) {
const match = value.match(regReferencesUrl);
if (match != null) {
id = match[2]; // url() reference
const matches = value.matchAll(regReferencesUrl);
for (const match of matches) {
ids.push(match[2]); // url() reference
}
}
if (name === 'href' || name.endsWith(':href')) {
const match = value.match(regReferencesHref);
if (match != null) {
id = match[1]; // href reference
ids.push(match[1]); // href reference
}
}
if (name === 'begin') {
const match = value.match(regReferencesBegin);
if (match != null) {
id = match[1]; // href reference
ids.push(match[1]); // href reference
}
}
if (id != null) {
for (const id of ids) {
let refs = referencesById.get(id);
if (refs == null) {
refs = [];
referencesById.set(id, refs);
}
refs.push({ element: node, name, value });
refs.push({ element: node, name });
}
}
}
Expand Down Expand Up @@ -253,7 +253,8 @@ exports.fn = (_root, params) => {
currentIdString = getIdString(currentId);
} while (isIdPreserved(currentIdString));
node.attributes.id = currentIdString;
for (const { element, name, value } of refs) {
for (const { element, name } of refs) {
const value = element.attributes[name];
if (value.includes('#')) {
// replace id in href and url()
element.attributes[name] = value.replace(
Expand Down
31 changes: 31 additions & 0 deletions test/plugins/cleanupIds.22.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1009,12 +1009,12 @@ __metadata:
languageName: node
linkType: hard

"@types/csso@npm:^5.0.0":
version: 5.0.0
resolution: "@types/csso@npm:5.0.0"
"@types/csso@npm:^5.0.1":
version: 5.0.1
resolution: "@types/csso@npm:5.0.1"
dependencies:
"@types/css-tree": "*"
checksum: 4aba6f76c4b402cb045525773b18f86fba7f61dc8be451b6c94b926f99c1f83c5c4b3ec62c1423da6f75437b954c8b4eaf9c4008e7a85bb10dd892a8fc42a0ae
checksum: 62c7e534dfde79c1bf3b3761baec06ac2aa2b568da9be4a548b95e709b65b3944b8ad4cd4d8926de2b4bb301b4f102fb0f8c1354cb203a92ebccf59e58c957a6
languageName: node
linkType: hard

Expand Down Expand Up @@ -4725,7 +4725,7 @@ __metadata:
"@rollup/plugin-node-resolve": ^14.1.0
"@trysound/sax": 0.2.0
"@types/css-tree": ^2.0.0
"@types/csso": ^5.0.0
"@types/csso": ^5.0.1
"@types/jest": ^29.5.5
commander: ^7.2.0
css-select: ^5.1.0
Expand Down

0 comments on commit 42f7752

Please sign in to comment.