Removing unsued input styles in function exports #176
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.
This is a first direction of fixing an old bug introduced with custom inputs.
This was first noted when working with custom inputs that used some of Mechanic's ui-components. Some SVG based design functions included in the project that included the custom inputs, would fail at export (either PNG or SVG) with the following message:
Uncaught (in promise) Error: Pseudo-elements are not supported by css-select.
Through some digging, it was found that this is thrown by SVGO (that uses css-select) when trying to compute optimizations over the resulting SVG. It threw that because it turns out CSS meant for input elements (like text field, number field, etc...) got bundled into the SVG being exported, and those included pseudo-elements.
This happens because a section was added when adding CSS support for SVG, that clones all style in the design functions iframe into the SVG, so that exported SVG actually included styles defined in the design function. But, it wasn't considered at the time that this would carry over any other styles that may not even be for the design function, like the ones from custom inputs. Custom inputs style is there in the first place because Mechanic adds dependencies to the script run in the iframe that imports the inputs code, so it can run validations and other value things, but therefore also adds any style associated with inputs.
This problem was first posted as #129. I go back and forth on whether if it's an actual problem that the input's CSS gets to the iframe. What is a problem for sure is that it makes it to the export.
This draft includes a potential fix focusing on the export step. Search through style rules that are included in the iframe, and skipping any that has rules that don't match to anything in the export or raise error when searching. Those error might be because of badly written CSS or unsupported selector (like pseudo-elements).
Adds two dependencies that are already included by SVGO: css-select (to query rule selectors over the SVG) and css-tree (to parse and write CSS strings).
Let me know what you think! I will open a new fresh PR with cleaner commits if we decide to take it in.