Skip to content

Commit 69120ea

Browse files
authoredOct 19, 2021
feat: update attribute formatting (#45)
1 parent 73b0aa3 commit 69120ea

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed
 

Diff for: ‎__tests__/index.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ test('should align attributes vertically', () => {
2626

2727
expect(format(html)).toEqual(
2828
`
29-
<input name="test"
30-
value="true"
31-
class="form-control"
29+
<input
30+
name="test"
31+
value="true"
32+
class="form-control"
3233
>
3334
`
3435
);
@@ -106,8 +107,9 @@ test('should support xml', () => {
106107
const xml = `<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://www.example.com</loc></url><url><loc>https://www.example.com/test</loc></url></urlset>`;
107108

108109
expect(format(xml)).toEqual(`
109-
<?xml version="1.0"
110-
encoding="utf-8"
110+
<?xml
111+
version="1.0"
112+
encoding="utf-8"
111113
?>
112114
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
113115
<url>
@@ -176,13 +178,15 @@ test('should support html directives', () => {
176178

177179
expect(format(html)).toEqual(`
178180
<!doctype html>
179-
<html class="no-js"
180-
lang
181+
<html
182+
class="no-js"
183+
lang
181184
>
182185
<head>
183186
<meta charset="utf-8">
184-
<meta http-equiv="x-ua-compatible"
185-
content="ie=edge"
187+
<meta
188+
http-equiv="x-ua-compatible"
189+
content="ie=edge"
186190
>
187191
<title>
188192
test
@@ -195,9 +199,10 @@ test('should support html directives', () => {
195199
<p>
196200
Hello world! This is HTML5 Boilerplate.
197201
</p>
198-
<script src="https://code.jquery.com/jquery-{{JQUERY_VERSION}}.min.js"
199-
integrity="{{JQUERY_SRI_HASH}}"
200-
crossorigin="anonymous"
202+
<script
203+
src="https://code.jquery.com/jquery-{{JQUERY_VERSION}}.min.js"
204+
integrity="{{JQUERY_SRI_HASH}}"
205+
crossorigin="anonymous"
201206
>
202207
</script>
203208
<script src="js/plugins.js">
@@ -208,9 +213,10 @@ test('should support html directives', () => {
208213
window.ga=function(){ga.q.push(arguments)};ga.q=[];ga.l=+new Date;
209214
ga('create','UA-XXXXX-Y','auto');ga('send','pageview')
210215
</script>
211-
<script src="https://www.google-analytics.com/analytics.js"
212-
async
213-
defer
216+
<script
217+
src="https://www.google-analytics.com/analytics.js"
218+
async
219+
defer
214220
>
215221
</script>
216222
</body>
@@ -269,9 +275,10 @@ test('should sort attributes with `sortAttributes` function', () => {
269275
const sortAttributes = (names) => names.sort();
270276
expect(format(html, { sortAttributes })).toEqual(
271277
`
272-
<input class="form-control"
273-
name="test"
274-
value="true"
278+
<input
279+
class="form-control"
280+
name="test"
281+
value="true"
275282
>
276283
`
277284
);

Diff for: ‎index.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ const format = function(html, { sortAttributes = names => names } = {}) {
4747
};
4848

4949
const getAttributeIndentation = tagName => {
50-
return getIndentation(indentSize * currentDepth + tagName.length - 1);
51-
};
52-
53-
const getAttributeIndentationForCurrentTag = () => {
54-
return getAttributeIndentation(currentTag);
50+
return getIndentation(indentSize * currentDepth - 1);
5551
};
5652

5753
const append = content => {
@@ -119,14 +115,8 @@ const format = function(html, { sortAttributes = names => names } = {}) {
119115
return;
120116
}
121117

122-
let firstAttribute = true;
123118
for (let name of names) {
124-
if (firstAttribute === true) {
125-
firstAttribute = false;
126-
appendAttribute(name, attributes[name]);
127-
} else {
128-
appendAttributeOnNewLine(name, attributes[name], tagName);
129-
}
119+
appendAttributeOnNewLine(name, attributes[name], tagName);
130120
}
131121
};
132122

0 commit comments

Comments
 (0)
Please sign in to comment.