-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dereferenceUses
plugin
#1279
Open
strarsis
wants to merge
37
commits into
svg:main
Choose a base branch
from
strarsis:dereferenceUses-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
782a87b
Add plugin file and tests.
strarsis 8594b3a
Add code to replace the `<symbol>` element with configurable wrapper …
strarsis 3bd002b
Fix test.
strarsis cb915dc
Add to plugins.js.
strarsis 48d7c21
Apply linter fix to dereferenceUses plugin.
strarsis 2de6eaa
Resolve package lock conflict with upstream.
strarsis c4397c7
Add new test for plugin (1279#issuecomment-803706532).
strarsis fa93ae0
Apply styles of <use/> element on top of styles of referenced element.
strarsis 137341e
Fix expected result in test.
strarsis 4d658eb
Merge branch 'master' of https://github.com/svg/svgo into dereference…
strarsis 8fd5222
Apply recommended code style.
strarsis 7d9135a
Improve constant and variable names.
strarsis 0e426f6
Improve comment.
strarsis a37eb7b
Fix attribute override logic.
strarsis 45244fc
Merge branch 'master' into dereferenceUses-plugin
strarsis 60088b0
Simplify plugin test description.
strarsis 8a497ef
Simplify plugin tests descriptions.
strarsis 5dc8c65
Use href attribute names array for finding href attribute.
strarsis 246d861
Remove large, rather redundant plugin test.
strarsis 454e338
Rename options argument/variable to params.
strarsis 96d0f7e
Use destructuring for plugin params.
strarsis 470ab35
Merge branch 'main' into dereferenceUses-plugin
strarsis bb15cbe
Add param typings for dereferenceUses plugin.
strarsis e7226cb
Rewrite dereferenceUses plugin for new `svgo` architecture.
strarsis 8db961d
Improve test comments.
strarsis cd30067
Increase test coverage.
strarsis 92dcf8e
Fix `yarn` lockfile.
strarsis 5712fee
Appy prettier.
strarsis 314dd8e
Add `structuredClone` polyfill for older `node`s.
strarsis 869c113
Add TS definitions for `@ungap/structured-clone`.
strarsis e797bb0
Merge remote-tracking branch 'origin' into dereferenceUses-plugin
strarsis 99a01d4
Update imports and plugin declaration for current `svgo`.
strarsis 886ade6
Add plugin documentation in new `svgo` documentation format.
strarsis bdde2ef
Update test fixture files to new `svgo` conventions.
strarsis bb4adde
Add `dereferenceUses` plugin as built-in `svgo` plugin.
strarsis 58697fd
Remove npm lockfile.
strarsis eed23f1
Fix code lint errors in plugin code.
strarsis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
'use strict'; | ||
|
||
const { querySelectorAll, querySelector } = require('../lib/xast.js'); | ||
|
||
exports.type = 'full'; | ||
|
||
exports.active = true; | ||
|
||
exports.description = 'dereferences <use/> elements'; | ||
|
||
exports.params = { | ||
keepHref: false, // keep (xlink:)href attributes | ||
|
||
symbolContainer: 'svg', // browsers use <svg/> as container of <symbol/> content (`g` could also be used) | ||
}; | ||
|
||
const OverridingUseAttributeNames = [ | ||
'x', | ||
'y', | ||
'width', | ||
'height', | ||
'href', | ||
'xlink:href', | ||
]; | ||
const HrefAttributeNames = ['href', 'xlink:href']; | ||
|
||
/** | ||
* Dereferences <use> elements | ||
* | ||
* | ||
* @param {Object} document document element | ||
* @param {Object} options plugin params | ||
* | ||
* @author strarsis <[email protected]> | ||
*/ | ||
exports.fn = function (document, options) { | ||
options = options || {}; | ||
strarsis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// collect <use/>s | ||
const useElements = querySelectorAll(document, 'use'); | ||
|
||
// no <use/>s, nothing to do | ||
if (useElements === null) { | ||
return document; | ||
} | ||
|
||
// replace each <use/> with its referenced node | ||
for (const useElement of useElements) { | ||
// `href`/`xlink:href` value | ||
const hrefAttribute = useElement.attr('href') | ||
? useElement.attr('href') | ||
: useElement.attr('xlink:href'); | ||
strarsis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!hrefAttribute || hrefAttribute.value.length === 0) continue; | ||
const href = hrefAttribute.value; | ||
|
||
// look up referenced element | ||
const targetElement = querySelector(document, href); | ||
if (!targetElement) continue; | ||
|
||
// clone referenced element for insertion | ||
const insertElement = targetElement.clone(); | ||
|
||
// Attribute inheritance of the referenced element | ||
// @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use | ||
// "Only the attributes x, y, width, height and href on the use element will override those set on the referenced element. | ||
// However, any other attributes not set on the referenced element will be applied to the use element." | ||
const insertElementAttributeNames = Object.keys(insertElement.attributes); | ||
for (const attributeName in useElement.attributes) { | ||
// don't remove attributes from the referenced element that, by specs, override the one of the <use> element | ||
if ( | ||
insertElementAttributeNames.includes(attributeName) && | ||
!OverridingUseAttributeNames.includes(attributeName) | ||
) | ||
continue; | ||
|
||
// don't remove href attribute with keepHref option turned on | ||
if (!options.keepHref && HrefAttributeNames.includes(attributeName)) | ||
continue; | ||
|
||
// set overriding attributes from referenced node | ||
insertElement.attributes[attributeName] = | ||
useElement.attributes[attributeName]; | ||
} | ||
|
||
// only the original node is allowed to have this ID (IDs must be unique) | ||
delete insertElement.attributes['id']; | ||
|
||
// <symbol/> elements are template elements (hence not visible), | ||
// browsers would place a <symbol/> element as a different element | ||
if (insertElement.isElem('symbol')) { | ||
insertElement.name = options.symbolContainer; | ||
} | ||
|
||
// apply styles of <use/> element on top of the referenced Element | ||
const useElementProperties = useElement.style.getProperties(); | ||
for (const propertyName in useElementProperties) { | ||
const property = useElementProperties[propertyName]; | ||
|
||
insertElement.style.setProperty( | ||
propertyName, | ||
property.value, | ||
property.priority | ||
); | ||
} | ||
|
||
// replace the <use/> element with the referenced element | ||
const useParentElement = useElement.parentNode; | ||
|
||
// position of <use/> in parent | ||
const useElementPosition = useParentElement.children.indexOf(useElement); | ||
|
||
useParentElement.children.splice(useElementPosition, 1, insertElement); | ||
} | ||
|
||
return document; | ||
}; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor with visitor please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I am eager to try it with a plugin.