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

Add a more aggressive whitespace removal option to the collapseWhitespace plugin #90

Merged

Conversation

dgkimpton
Copy link
Contributor

@dgkimpton dgkimpton commented Aug 11, 2020

I would like to propose an addition/change to the 'collapseWhitespace' plugin to more aggressively remove semantically redundant whitespace.

I saw, looking at the output of my minified code, lots of spaces being introduced where none existed in my input. These turned out to be a translation of the indentation/newlines into a single space. Since almost all of the markup was structural none of those spaces were adding anything of value.

Therefore I suggest that a new 'aggressive' mode be introduced that removes structural/incidental whitespace such as this. Actual space characters can be collapsed but should not be removed as they are essential between inline tags (e.g. title</b> <i>part).

This pull request is a first attempt at implementing such behaviour.

Is this something you would consider for inclusion in the main htmlnano project?

NOTE: a small behavioural change was introduced to match the documentation - a single space now replaces individual newlines/tabs. I don't think this should have any impact on downstream consumers since html generally allows whitespace to be collapsed/replaced.

…hat removes incidental spacing such as tabs and newlines. Useful when there is a lot of structured markup and not much actual text.

NOTE: a small behavioural change was introduced in order to match the
documentation - single whitespace (newlines/tabs) are now replaced by a
single space.
if (collapseType !== 'all') {
collapseType = 'conservative';
}
collapseType = (collapseType && validOptions.some(o=>o === collapseType)) ? collapseType : 'conservative';
Copy link
Member

Choose a reason for hiding this comment

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

This could be simplified to

Suggested change
collapseType = (collapseType && validOptions.some(o=>o === collapseType)) ? collapseType : 'conservative';
collapseType = validOptions.includes(collapseType) ? collapseType : 'conservative';

Copy link
Contributor Author

@dgkimpton dgkimpton Aug 16, 2020

Choose a reason for hiding this comment

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

Oh boy, I didn't know about the .includes method, I learnt something awesome today, thanks!

Although, I see you already merged the PR... so do you want a new PR for this simplification or do you want to just let it go?

Copy link
Member

Choose a reason for hiding this comment

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

so do you want a new PR for this simplification or do you want to just let it go?

It's up to you :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Always better to keep things tidy imho : #91

@maltsev
Copy link
Member

maltsev commented Aug 16, 2020

Thanks for your PR and sorry for the delay with getting back to you!

Is this something you would consider for inclusion in the main htmlnano project?
Sure!

@maltsev maltsev merged commit 024cf50 into posthtml:master Aug 16, 2020
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.

None yet

2 participants