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: normalize attribute #163

Merged
merged 3 commits into from
Sep 15, 2021
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
21 changes: 21 additions & 0 deletions docs/docs/050-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,24 @@ Due to [the limitation of PostHTML](https://github.com/posthtml/htmlnano/issues/
- `body`
- `colgroup`
- `tbody`

### normalizeAttributeValues

Normalize casing of attribute values.

The module won't impact the plain-text size of the output. However it will improve the compression ratio of gzip/brotli used in HTTP compression.

#### Example

Source:

```html
<form method="GET"></form>
```

Minified:

```html
<form method="get"></form>
```

61 changes: 61 additions & 0 deletions lib/modules/normalizeAttributeValues.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const caseInsensitiveAttributes = {
autocomplete: ['form'],
charset: ['meta', 'script'],
contenteditable: null,
crossorigin: ['audio', 'img', 'link', 'script', 'video'],
dir: null,
draggable: null,
dropzone: null,
formmethod: ['button', 'input'],
inputmode: ['input', 'textarea'],
kind: ['track'],
method: ['form'],
preload: ['audio', 'video'],
referrerpolicy: ['a', 'area', 'iframe', 'img', 'link'],
sandbox: ['iframe'],
spellcheck: null,
scope: ['th'],
shape: ['area'],
sizes: ['link'],
step: ['input'],
translate: null,
type: [
'a',
'link',
'button',
'embed',
'object',
'script',
'source',
'style',
'input',
'menu',
'menuitem'
],
wrap: ['textarea']
};

export default function normalizeAttributeValues(tree) {
tree.walk(node => {
if (!node.attrs) {
return node;
}

Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
const attrNameLower = attrName.toLowerCase();
if (
Object.hasOwnProperty.call(caseInsensitiveAttributes, attrNameLower)
&& (
caseInsensitiveAttributes[attrNameLower] === null
|| caseInsensitiveAttributes[attrNameLower].includes(node.tag)
)
) {
node.attrs[attrName] = attrValue.toLowerCase();
}
});

return node;
});

return tree;
}
1 change: 1 addition & 0 deletions lib/presets/safe.es6
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default {
},
minifyConditionalComments: false,
removeRedundantAttributes: false,
normalizeAttributeValues: true,
removeEmptyAttributes: true,
removeComments: 'safe',
removeAttributeQuotes: false,
Expand Down
14 changes: 14 additions & 0 deletions test/modules/normalizeAttributeValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { init } from '../htmlnano';
import safePreset from '../../lib/presets/safe';

describe('normalizeAttributeValues', () => {
const options = safePreset;

it('default behavior', () => {
return init(
'<form id="FOo" method="GET"></form>',
'<form id="FOo" method="get"></form>',
options
);
});
});