Skip to content
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

handle whitespace within and around inline tags #519

Merged
merged 5 commits into from
Mar 1, 2016
Merged

handle whitespace within and around inline tags #519

merged 5 commits into from
Mar 1, 2016

Conversation

alexlamsl
Copy link
Collaborator

inspired by the tests in PHPTAL:

<div>a <a href=#> b </a> c</div>

should minify to:

<div>a <a href=#>b </a>c</div>

before this PR:

<div>a <a href=#>b</a> c</div>

note the whitespace placement before c.

One case we cannot handle optimally is:

<div> <span> a </span> </div>

with this PR this turns to:

<div><span>a </span></div>

even though we should be able to remove that final whitespace safely.

@alexlamsl
Copy link
Collaborator Author

Did I say we have a sub-optimal case? 😏

(As it turns out, we don't need to flush buffer to results at all, so that helps.)

text = prevTag || nextTag ? collapseWhitespaceSmart(text, prevTag, nextTag, options) : trimWhitespace(text);
if (!text && / $/.test(currentChars) && prevTag && prevTag.charAt(0) === '/') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure we don't want \s$ instead of $?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of collapsed whitespaces always getting turned into - but I guess no harm making them\s

@kangax
Copy link
Owner

kangax commented Mar 1, 2016

Do we know how this affects minifier performance?

@alexlamsl
Copy link
Collaborator Author

I've run benchmark.js and no noticeable performance difference is observed.

In terms of minified sizes, they stayed almost the same - eloquentjavascript got <1KB bigger due to lots of:

<a>P </a>

es6 got <1KB smaller due to consecutive trailing spaces being optimised.

@alexlamsl
Copy link
Collaborator Author

@kangax comments addressed 😉

@kangax
Copy link
Owner

kangax commented Mar 1, 2016

Does it mean that "eloquentjavascript" was rendering incorrectly before? With some spaces removed?

@@ -658,7 +672,7 @@
equal(minify(input, { collapseWhitespace: true }), output);

input = '<p> foo <span> blah <i> 22</i> </span> bar <img src=""></p>';
output = '<p>foo <span>blah <i>22</i></span> bar <img src=""></p>';
output = '<p>foo <span>blah <i>22</i> </span>bar <img src=""></p>';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why is this space necessary here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's removing the space after </span> rather than the one before.

If there isn't any space before </span> then this wouldn't have happened.

kangax added a commit that referenced this pull request Mar 1, 2016
handle whitespace within and around inline tags
@kangax kangax merged commit 2b530b3 into kangax:gh-pages Mar 1, 2016
@alexlamsl
Copy link
Collaborator Author

Yes - those spaces were significant, so removing them packed those links too close to normal text after them.

@alexlamsl alexlamsl mentioned this pull request Mar 8, 2016
@XhmikosR
Copy link
Collaborator

@alexlamsl: are you sure this change doesn't break things? I notice differences in a project of mine, namely https://github.com/mpc-hc/mpc-hc.org (run it with grunt server). I had to turn on conservativeCollapse to get the same output as with the older version. Look for the spaces around the font awesome icons.

@XhmikosR
Copy link
Collaborator

I also notice spaces before the end closing bracket, like

<a href=/acknowledgments/ >

I don't remember this one happening with the old version.

EDIT: happens with 1.2.0 too.

@alexlamsl
Copy link
Collaborator Author

<a href="/acknowledgements/"> should transform to <a href=/acknowledgements/ >, as <a href=/acknowledgements/> would cause the final slash to be confused as a closing slash for XML.

And I don't think this PR is the one that changes this IIRC...

@XhmikosR
Copy link
Collaborator

But it's not XML.

@alexlamsl
Copy link
Collaborator Author

Found it: f8db419

The old behaviour is to retain the quotes as it'd cause some browsers to consume that as />. The new behaviour saves one byte by removing the quotes but add a trailing whitespace to avoid parsing errors.

@alexlamsl
Copy link
Collaborator Author

As in, <a href=/acknowledgements/> will get confused as <a href="/acknowledgements"/> in some browsers, hence <a href=/acknowledgements/ > is the shortest representation that is also safe.

I'll have a go at your project now - is there a specific link you'd want me to visit after grunt serve?

@XhmikosR
Copy link
Collaborator

It's non standard behavior if "some" browsers behave like that. And HTML is not XML so the specs are clear. Anyway, that issue is a separate one. I'm more concerned about the whitespace issue I mention #519 (comment).

@XhmikosR
Copy link
Collaborator

Just any page, but it's clearer in the homepage in the news post icons.

Note that I have already reverted to the old version in master so in order to see what I mean you will need to set grunt-contrib-htmlmin to ^1.0.0 or 1.1.0.

@alexlamsl
Copy link
Collaborator Author

Thanks, I'll investigate now - I guess I can roll back and forth between the versions to see the difference as well...

@alexlamsl
Copy link
Collaborator Author

Feels like an idiot, but... 😓

> grunt serve

Warning: Task "serve" not found. Use --force to continue.

Aborted due to warnings.


Execution Time (2016-03-21 12:01:01 UTC)
loading tasks  3ms  ████████████75%
Total 4ms

Edit: I guess it's grunt server...

@alexlamsl
Copy link
Collaborator Author

@XhmikosR I know I'm probably being annoying now, but is there a way I can test without installing ruby and jekyll and possibly more which I don't have on my PC? 😰

@alexlamsl
Copy link
Collaborator Author

Okay, so I visit https://mpc-hc.org/ on Chrome 49:

  • grab document.body.innerHTML
  • run it on http://kangax.github.io/html-minifier/ with most (safer) options and without conservativeCollapse
  • paste the results back into document.body.innerHTML

And I compare it with another tab of the original site. The only thing I've noticed so far are the disappearing whitespace between fa icons and the following text.

Are there other discrepancies I should be paying attention to as well?

@XhmikosR
Copy link
Collaborator

It's not possible to build without Ruby, sorry.

But what you see is the issue I described, yes. There might be more, but the font awesome ones were the obvious.

@alexlamsl
Copy link
Collaborator Author

The whitespace here is being removed </span> Downloads - I'll track it down and try it and report back 😉

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.

3 participants