-
-
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
Preserve whitespace in elements containing text #1220
Conversation
src: local("Some Font"), | ||
local("SomeFont"), | ||
url(SomeFont.ttf); | ||
src: local("Some Font"), local("SomeFont"), url(SomeFont.ttf); |
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.
csso
preserves the newlines in this src
value, which were previously being turned into spaces by the sax
normalization.
959ac4c
to
d318337
Compare
@GreLI It would be really great to see this merged 🙇 |
Please rebase |
d318337
to
5e21313
Compare
5e21313
to
a6ae757
Compare
@TrySound rebased 👍 |
lib/svgo/svg2js.js
Outdated
// Save info about <text> tag to prevent trimming of meaningful whitespace | ||
if (data.name == 'text' && !data.prefix) { | ||
// Save info about tags containing text to prevent trimming of meaningful whitespace | ||
if (textElems.indexOf(data.name) !== -1 && !data.prefix) { |
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.
Use includes array method please
lib/svgo/svg2js.js
Outdated
@@ -4,12 +4,13 @@ var SAX = require('sax'), | |||
JSAPI = require('./jsAPI.js'), | |||
CSSClassList = require('./css-class-list'), | |||
CSSStyleDeclaration = require('./css-style-declaration'), | |||
entityDeclaration = /<!ENTITY\s+(\S+)\s+(?:'([^']+)'|"([^"]+)")\s*>/g; | |||
entityDeclaration = /<!ENTITY\s+(\S+)\s+(?:'([^']+)'|"([^"]+)")\s*>/g, | |||
textElems = require('../../plugins/_collections.js').textElems; |
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.
Put require before regexp
Thanks! |
Problem
SVGO configures its SAX parser to normalize whitespace, collapsing multiple space characters into one.
This can change the semantic meaning of SVG content:
<text>
and<tspan>
nodes when whitespace is being preserved (e.g.xml:space="preserve"
orstyle="white-space: pre"
).<text>
and<tspan>
nodes where glyphs are spaced explicitly using multiplex
values. Changing the number of characters in the node changes the indexes of characters (see SVGO improperly alters text spacing #1044).Solution
This patch disables space normalization in
sax
. It removes trimming for node names classified as "textContent" in_collections.js
. In addition, it changes the pretty printing logic to not add indents or whitespace when outputting such nodes.I needed to update a few text fixtures which captured the previous space normalizing behavior. Existing plugin tests have a mixture of text nodes containing and not containing extra whitespace, so they cover the change in functionality. I've also added a test to
svg2js
to verify that the whitespace is preserved when parsing SVGs.Related issues