Skip to content

Commit 71aa6c2

Browse files
committed
feat: language attribute support
1 parent 67037f6 commit 71aa6c2

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Format your html and inline css markup according to the [HTML5 syntax Style Guid
1616
- [x] Removes spaces at the equal sign
1717
- [x] Add blank lines to separate large or logical code blocks
1818
- [x] Add 2 spaces of indentation. *Do not use TAB*.
19-
- [ ] Add language attribute
19+
- [x] Add language attribute
2020
- [ ] Add character encoding
2121
- [x] Attribute order
2222
- [x] Boolean attributes
@@ -90,6 +90,11 @@ Default:
9090
Default: false
9191
Description: *Sort the order of attributes in elements*
9292

93+
- **lang**
94+
Type: `String | Boolean(only false)`
95+
Default: false
96+
Description: *Add a `lang` attribute in elements, eg: `{ lang: 'fr' }`*
97+
9398
### `mini`
9499
Type: `Object`
95100
Default:

src/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@ const jsPrettier = (tree, {rules: {indent, eol}, jsBeautifyOptions}) => {
272272
return prettier(tree);
273273
};
274274

275+
const addLang = (tree, {rules: {lang}}) => {
276+
if (!lang) {
277+
return tree;
278+
}
279+
280+
tree.map(node => {
281+
if (node.tag) {
282+
node.attrs = Object.assign({}, {lang}, node.attrs);
283+
}
284+
285+
return node;
286+
});
287+
288+
return tree;
289+
};
290+
275291
const beautify = (tree, options) => [
276292
clean,
277293
parseConditional,
@@ -282,6 +298,7 @@ const beautify = (tree, options) => [
282298
lowerAttributeName,
283299
attributesBoolean,
284300
sortAttr,
301+
addLang,
285302
eof,
286303
mini
287304
].reduce((previousValue, module) => typeof module === 'function' ? module(previousValue, options) : previousValue, tree);

src/rules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export default {
44
blankLines: '\n',
55
eof: '\n',
66
eol: '\n',
7-
sortAttr: false
7+
sortAttr: false,
8+
lang: false
89
};

test/test-plugin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,20 @@ test('processing with plugin beautify should return format js', async t => {
162162
);
163163
});
164164

165+
test('processing with plugin beautify to add language attribute', async t => {
166+
const fixture = '<p >a</p>';
167+
const expected = '<p lang="en">a</p>\n';
168+
t.deepEqual(expected, (await processing(fixture, [beautify({rules: {lang: 'en'}})])).html);
169+
});
170+
171+
test('processing with plugin beautify to add language attribute with existing attr', async t => {
172+
const fixture = '<p data="e">a</p>';
173+
const expected = '<p lang="en" data="e">a</p>\n';
174+
t.deepEqual(expected, (await processing(fixture, [beautify({rules: {lang: 'en'}})])).html);
175+
});
176+
177+
test('processing with plugin beautify to add language attribute with existing lang attribute', async t => {
178+
const fixture = '<p lang="fr">a</p>';
179+
const expected = '<p lang="fr">a</p>\n';
180+
t.deepEqual(expected, (await processing(fixture, [beautify({rules: {lang: 'en'}})])).html);
181+
});

0 commit comments

Comments
 (0)