-
Notifications
You must be signed in to change notification settings - Fork 13
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
Build: regex-based <g> insertion matching svg tag, to be more reliable #259
Conversation
Have we looked into converting the other transforms to use See example in |
I agree! ...and that's tracked in #123. I didn't have time yet to do it — This was meant as a quick fix for the problem as currently it's blocking a PR to get in, and I couldn't see any easy code in the If you think it's good enough — even if I agree not ideal — let's merge this first. If not, I can try to find some time to look into #123. :) |
Oh nice that we have that tracked. I'll try and take this for a spin later. Also summoning @dmsnell since we used the word regex here. |
fileContent.slice( fileContent.indexOf('viewBox="0 0 24 24">') + 20, -6 ) + // child elements of SVG | ||
'</g>' + | ||
fileContent.slice( -6 ); // closing SVG tag | ||
fileContent = fileContent.replace( /<svg[^>]*>/i, '$&<g>' ); // add <g> after <svg> |
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.
if we run this twice, won't get we <g><g>
?
although what @gwwar brought up is 2000% relevant here, that non-RegEx-based parsers are quite important here, for a quick implementation we can find only those <svg
things that aren't followed by <g
things
const svgWithoutG = [
// (?:<svg) - find the opening prefix (non-capture)
// ([^>]*) - capture any tag-internal content
// (?:>(?!<g>)) - followed by a closing `>` which is not also followed by a `<g>`
/(?:<svg)([^>]*)(?:>(?!<g>))/,
'<svg\1><g>'
];
const _svgWithoutG = [
// grab even those with a closing `</g>`
// then normalize the endings so they all have it
/(?:(<\/g>)?<\/svg>)/,
'</g></svg>'
];
Note that what this won't do is handle nesting - if we have nested <g>
groups we'll likely mess them up.
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.
if we run this twice, won't get we
<g><g>
?
Not a problem: this is always run only on a re-generated file for safety.
I'd rather keep the simpler form, but nice use of lookahead there, and I didn't know there was a way in JS' runtime to do non-capture. TIL!
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.
I meant that when we regenerate the files again the current RegExps will double the group element.
I'd rather keep the simpler form
sure, but neither one is really complicated… the given ones are just a little misleading because of how they leave out the <g>
part
We discussed in private with Dennis as there was some misunderstanding on how this was generated / regenerated. To explain this more extensively: the files are generated every time from the source, so there's no instance where the |
The previous
svg-transform-add-g
grunt task used a blind match on the string, relying onviewBox="0 0 24 24">
to be at the end of the<svg>
tag.This PR updates the script to use a RegEx matching the
<svg>
tag in a more generic way:<svg[^>]*>
(matches anything inside the svg tag that is not a>
, then gets the>
as the ending of the tag)</svg>
(escaping the/
)This bug was found during testing of #256.
To test
/svg-min
folder