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

Feat: removeAttributeQuotes #104

Merged
merged 1 commit into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,37 @@ Minified:
<img src="foo.jpg" alt="">
```

### removeAttributeQuotes
Remove quotes around attributes when possible, see [HTML Standard - 12.1.2.3 Attributes - Unquoted attribute value syntax](https://html.spec.whatwg.org/multipage/syntax.html#attributes-2).

##### Example
Source:
```html
<div class="foo" title="hello world"></div>
```

Minified:
```html
<div class=foo title="hello world"></div>
```

##### Notice
The feature is implemented by [posthtml-render's `quoteAllAttributes`](https://github.com/posthtml/posthtml-render#options), which is one of the PostHTML's option. So `removeAttributeQuotes` could be overriden by other PostHTML's plugins and PostHTML's configuration.

For example:

```js
posthtml([
htmlnano({
removeAttributeQuotes: true
})
]).process(html, {
quoteAllAttributes: true
})
```

`removeAttributeQuotes` will not work because PostHTML's `quoteAllAttributes` takes the priority.

### removeUnusedCss

Removes unused CSS inside `<style>` tags with either [uncss](https://github.com/uncss/uncss)
Expand Down
12 changes: 12 additions & 0 deletions lib/modules/removeAttributeQuotes.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Specification: https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
// See also: https://github.com/posthtml/posthtml-render/pull/30
// See also: https://github.com/posthtml/htmlnano/issues/6#issuecomment-707105334

/** Disable quoteAllAttributes while not overriding the configuration */
export default function removeAttributeQuotes(tree) {
if (tree.options && typeof tree.options.quoteAllAttributes === 'undefined') {
tree.options.quoteAllAttributes = false;
}

return tree;
}
1 change: 1 addition & 0 deletions lib/presets/max.es6
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import safePreset from './safe';
export default { ...safePreset,
collapseWhitespace: 'all',
removeComments: 'all',
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeUnusedCss: {},
minifyCss: {
Expand Down
1 change: 1 addition & 0 deletions lib/presets/safe.es6
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default {
removeEmptyAttributes: true,
removeRedundantAttributes: false,
removeComments: 'safe',
removeAttributeQuotes: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although the feature is safe to enable (won't break anything), but it requires a completely rewritten for every existing unit test cases.

Since html-minifier doesn't enable the feature by default, I make it disabled in safe presets as well.

sortAttributesWithLists: true,
minifyUrls: false,
};
30 changes: 30 additions & 0 deletions test/modules/removeAttributeQuotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { init } from '../htmlnano';
import safePreset from '../../lib/presets/safe';

import posthtml from 'posthtml';
import htmlnano from '../..';
import expect from 'expect';

describe('removeAttributeQuotes', () => {
const options = { ...safePreset, removeAttributeQuotes: true };
const html = '<div class="foo" title="hello world"></div>';

it('default behavior', () => {
return init(
html,
'<div class=foo title="hello world"></div>',
options
);
});

it('shouldn\'t override exists options', () => {
return posthtml([
htmlnano(options, {})
]).process(
html,
{ quoteAllAttributes: true }
).then((result) => {
expect(result.html).toBe(html);
});
});
});