Skip to content

fix(removeUnknownsAndDefaults): keep href on the a element#2234

Open
sjh9714 wants to merge 1 commit into
svg:mainfrom
sjh9714:codex/20260725-2230-a-href
Open

fix(removeUnknownsAndDefaults): keep href on the a element#2234
sjh9714 wants to merge 1 commit into
svg:mainfrom
sjh9714:codex/20260725-2230-a-href

Conversation

@sjh9714

@sjh9714 sjh9714 commented Jul 25, 2026

Copy link
Copy Markdown

Summary

Fixes #2230.

removeUnknownsAndDefaults deleted the href attribute from <a> elements, so
links in an optimized SVG stopped working:

optimize(`<svg viewBox="0 0 160 40" xmlns="http://www.w3.org/2000/svg">
  <a href="https://developer.mozilla.org/"><text x="10" y="25">MDN Web Docs</text></a>
</svg>`, { plugins: ['removeUnknownsAndDefaults'] });
// -> <a><text x="10" y="25">MDN Web Docs</text></a>

In _collections.js, elems.a pulls in the xlink attribute group — which is
why xlink:href survives — but its own attrs set never listed the SVG 2
href, so the plugin treated it as unknown. Twelve other entries in the same
file already list 'href' next to 'xlink:href' (image, use,
linearGradient, feImage, filter, mpath, script, …), so a looks like
an oversight.

Changes

  • Add 'href' to elems.a.attrs in plugins/_collections.js (one line,
    alphabetically placed to match the surrounding entries)
  • Add test/plugins/removeUnknownsAndDefaults.18.svg.txt covering both <a href> and <a xlink:href>; the xlink:href case is the control and passed
    before this change too

Testing

  • yarn jest test/plugins/_index.test.js -t 'removeUnknownsAndDefaults'
  • yarn test
  • yarn lint
  • yarn test:types

The new fixture fails on main (- <a href="https://developer.mozilla.org/"> /
+ <a>) and passes with the change. I did not run yarn test:regression or
yarn test:bundles locally.

Note on #2062

#2061/#2062 is the same class of bug for animation elements. That PR takes a
different route (a new animationTargetElement group) and does not touch
elems.a, so this is not a duplicate — but it also adds a
removeUnknownsAndDefaults.18.svg.txt. .18 is the next free index on main;
happy to renumber if #2062 lands first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

removeUnknownsAndDefaults removes href attribute from <a> elements

2 participants