|
1 |
| -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 |
| -var xtend = require('xtend'); |
4 |
| -var spaces = require('space-separated-tokens').stringify; |
5 |
| -var commas = require('comma-separated-tokens').stringify; |
6 |
| -var information = require('property-information'); |
7 |
| -var entities = require('stringify-entities'); |
8 |
| -var kebab = require('kebab-case'); |
9 |
| -var ccount = require('ccount'); |
10 |
| -var all = require('./all'); |
| 3 | +var xtend = require('xtend') |
| 4 | +var spaces = require('space-separated-tokens').stringify |
| 5 | +var commas = require('comma-separated-tokens').stringify |
| 6 | +var information = require('property-information') |
| 7 | +var entities = require('stringify-entities') |
| 8 | +var kebab = require('kebab-case') |
| 9 | +var ccount = require('ccount') |
| 10 | +var all = require('./all') |
11 | 11 |
|
12 |
| -module.exports = element; |
| 12 | +module.exports = element |
13 | 13 |
|
14 | 14 | /* Constants. */
|
15 |
| -var DATA = 'data'; |
16 |
| -var EMPTY = ''; |
| 15 | +var DATA = 'data' |
| 16 | +var EMPTY = '' |
17 | 17 |
|
18 | 18 | /* Characters. */
|
19 |
| -var SPACE = ' '; |
20 |
| -var DQ = '"'; |
21 |
| -var SQ = '\''; |
22 |
| -var EQ = '='; |
23 |
| -var LT = '<'; |
24 |
| -var GT = '>'; |
25 |
| -var SO = '/'; |
| 19 | +var SPACE = ' ' |
| 20 | +var DQ = '"' |
| 21 | +var SQ = "'" |
| 22 | +var EQ = '=' |
| 23 | +var LT = '<' |
| 24 | +var GT = '>' |
| 25 | +var SO = '/' |
26 | 26 |
|
27 | 27 | /* Stringify an element `node`. */
|
28 | 28 | function element(ctx, node, index, parent) {
|
29 |
| - var name = node.tagName; |
30 |
| - var content = all(ctx, name === 'template' ? node.content : node); |
31 |
| - var selfClosing = ctx.voids.indexOf(name.toLowerCase()) !== -1; |
32 |
| - var attrs = attributes(ctx, node.properties); |
33 |
| - var omit = ctx.omit; |
34 |
| - var value = ''; |
| 29 | + var name = node.tagName |
| 30 | + var content = all(ctx, name === 'template' ? node.content : node) |
| 31 | + var selfClosing = ctx.voids.indexOf(name.toLowerCase()) !== -1 |
| 32 | + var attrs = attributes(ctx, node.properties) |
| 33 | + var omit = ctx.omit |
| 34 | + var value = '' |
35 | 35 |
|
36 | 36 | /* If the node is categorised as void, but it has
|
37 | 37 | * children, remove the categorisation. This
|
38 | 38 | * enables for example `menuitem`s, which are
|
39 | 39 | * void in W3C HTML but not void in WHATWG HTML, to
|
40 | 40 | * be stringified properly. */
|
41 |
| - selfClosing = content ? false : selfClosing; |
| 41 | + selfClosing = content ? false : selfClosing |
42 | 42 |
|
43 | 43 | if (attrs || !omit || !omit.opening(node, index, parent)) {
|
44 |
| - value = LT + name + (attrs ? SPACE + attrs : EMPTY); |
| 44 | + value = LT + name + (attrs ? SPACE + attrs : EMPTY) |
45 | 45 |
|
46 | 46 | if (selfClosing && ctx.close) {
|
47 | 47 | if (!ctx.tightClose || attrs.charAt(attrs.length - 1) === SO) {
|
48 |
| - value += SPACE; |
| 48 | + value += SPACE |
49 | 49 | }
|
50 | 50 |
|
51 |
| - value += SO; |
| 51 | + value += SO |
52 | 52 | }
|
53 | 53 |
|
54 |
| - value += GT; |
| 54 | + value += GT |
55 | 55 | }
|
56 | 56 |
|
57 |
| - value += content; |
| 57 | + value += content |
58 | 58 |
|
59 | 59 | if (!selfClosing && (!omit || !omit.closing(node, index, parent))) {
|
60 |
| - value += LT + SO + name + GT; |
| 60 | + value += LT + SO + name + GT |
61 | 61 | }
|
62 | 62 |
|
63 |
| - return value; |
| 63 | + return value |
64 | 64 | }
|
65 | 65 |
|
66 | 66 | /* Stringify all attributes. */
|
67 | 67 | function attributes(ctx, props) {
|
68 |
| - var values = []; |
69 |
| - var key; |
70 |
| - var value; |
71 |
| - var result; |
72 |
| - var length; |
73 |
| - var index; |
74 |
| - var last; |
| 68 | + var values = [] |
| 69 | + var key |
| 70 | + var value |
| 71 | + var result |
| 72 | + var length |
| 73 | + var index |
| 74 | + var last |
75 | 75 |
|
76 | 76 | for (key in props) {
|
77 |
| - value = props[key]; |
| 77 | + value = props[key] |
78 | 78 |
|
79 | 79 | if (value == null) {
|
80 |
| - continue; |
| 80 | + continue |
81 | 81 | }
|
82 | 82 |
|
83 |
| - result = attribute(ctx, key, value); |
| 83 | + result = attribute(ctx, key, value) |
84 | 84 |
|
85 | 85 | if (result) {
|
86 |
| - values.push(result); |
| 86 | + values.push(result) |
87 | 87 | }
|
88 | 88 | }
|
89 | 89 |
|
90 |
| - length = values.length; |
91 |
| - index = -1; |
| 90 | + length = values.length |
| 91 | + index = -1 |
92 | 92 |
|
93 | 93 | while (++index < length) {
|
94 |
| - result = values[index]; |
95 |
| - last = ctx.tight && result.charAt(result.length - 1); |
| 94 | + result = values[index] |
| 95 | + last = ctx.tight && result.charAt(result.length - 1) |
96 | 96 |
|
97 | 97 | /* In tight mode, don’t add a space after quoted attributes. */
|
98 | 98 | if (index !== length - 1 && last !== DQ && last !== SQ) {
|
99 |
| - values[index] = result + SPACE; |
| 99 | + values[index] = result + SPACE |
100 | 100 | }
|
101 | 101 | }
|
102 | 102 |
|
103 |
| - return values.join(EMPTY); |
| 103 | + return values.join(EMPTY) |
104 | 104 | }
|
105 | 105 |
|
106 | 106 | /* Stringify one attribute. */
|
107 | 107 | function attribute(ctx, key, value) {
|
108 |
| - var info = information(key) || {}; |
109 |
| - var name; |
| 108 | + var info = information(key) || {} |
| 109 | + var name |
110 | 110 |
|
111 | 111 | if (
|
112 | 112 | value == null ||
|
113 | 113 | (typeof value === 'number' && isNaN(value)) ||
|
114 | 114 | (!value && info.boolean) ||
|
115 | 115 | (value === false && info.overloadedBoolean)
|
116 | 116 | ) {
|
117 |
| - return EMPTY; |
| 117 | + return EMPTY |
118 | 118 | }
|
119 | 119 |
|
120 |
| - name = attributeName(ctx, key); |
| 120 | + name = attributeName(ctx, key) |
121 | 121 |
|
122 | 122 | if ((value && info.boolean) || (value === true && info.overloadedBoolean)) {
|
123 |
| - return name; |
| 123 | + return name |
124 | 124 | }
|
125 | 125 |
|
126 |
| - return name + attributeValue(ctx, key, value); |
| 126 | + return name + attributeValue(ctx, key, value) |
127 | 127 | }
|
128 | 128 |
|
129 | 129 | /* Stringify the attribute name. */
|
130 | 130 | function attributeName(ctx, key) {
|
131 |
| - var info = information(key) || {}; |
132 |
| - var name = info.name || kebab(key); |
| 131 | + var info = information(key) || {} |
| 132 | + var name = info.name || kebab(key) |
133 | 133 |
|
134 | 134 | if (
|
135 | 135 | name.slice(0, DATA.length) === DATA &&
|
136 | 136 | /\d/.test(name.charAt(DATA.length))
|
137 | 137 | ) {
|
138 |
| - name = DATA + '-' + name.slice(4); |
| 138 | + name = DATA + '-' + name.slice(4) |
139 | 139 | }
|
140 | 140 |
|
141 |
| - return entities(name, xtend(ctx.entities, { |
142 |
| - subset: ctx.NAME |
143 |
| - })); |
| 141 | + return entities(name, xtend(ctx.entities, {subset: ctx.NAME})) |
144 | 142 | }
|
145 | 143 |
|
146 | 144 | /* Stringify the attribute value. */
|
147 | 145 | function attributeValue(ctx, key, value) {
|
148 |
| - var info = information(key) || {}; |
149 |
| - var options = ctx.entities; |
150 |
| - var quote = ctx.quote; |
151 |
| - var alternative = ctx.alternative; |
152 |
| - var unquoted; |
| 146 | + var info = information(key) || {} |
| 147 | + var options = ctx.entities |
| 148 | + var quote = ctx.quote |
| 149 | + var alternative = ctx.alternative |
| 150 | + var unquoted |
153 | 151 |
|
154 | 152 | if (typeof value === 'object' && 'length' in value) {
|
155 | 153 | /* `spaces` doesn’t accept a second argument, but it’s
|
156 | 154 | * given here just to keep the code cleaner. */
|
157 | 155 | value = (info.commaSeparated ? commas : spaces)(value, {
|
158 | 156 | padLeft: !ctx.tightLists
|
159 |
| - }); |
| 157 | + }) |
160 | 158 | }
|
161 | 159 |
|
162 |
| - value = String(value); |
| 160 | + value = String(value) |
163 | 161 |
|
164 | 162 | if (value || !ctx.collapseEmpty) {
|
165 |
| - unquoted = value; |
| 163 | + unquoted = value |
166 | 164 |
|
167 | 165 | /* Check unquoted value. */
|
168 | 166 | if (ctx.unquoted) {
|
169 |
| - unquoted = entities(value, xtend(options, { |
170 |
| - subset: ctx.UNQUOTED, |
171 |
| - attribute: true |
172 |
| - })); |
| 167 | + unquoted = entities( |
| 168 | + value, |
| 169 | + xtend(options, {subset: ctx.UNQUOTED, attribute: true}) |
| 170 | + ) |
173 | 171 | }
|
174 | 172 |
|
175 | 173 | /* If `value` contains entities when unquoted... */
|
176 | 174 | if (!ctx.unquoted || unquoted !== value) {
|
177 | 175 | /* If the alternative is less common than `quote`, switch. */
|
178 |
| - if ( |
179 |
| - alternative && |
180 |
| - ccount(value, quote) > ccount(value, alternative) |
181 |
| - ) { |
182 |
| - quote = alternative; |
| 176 | + if (alternative && ccount(value, quote) > ccount(value, alternative)) { |
| 177 | + quote = alternative |
183 | 178 | }
|
184 | 179 |
|
185 |
| - value = entities(value, xtend(options, { |
186 |
| - subset: quote === SQ ? ctx.SINGLE_QUOTED : ctx.DOUBLE_QUOTED, |
187 |
| - attribute: true |
188 |
| - })); |
| 180 | + value = entities( |
| 181 | + value, |
| 182 | + xtend(options, { |
| 183 | + subset: quote === SQ ? ctx.SINGLE_QUOTED : ctx.DOUBLE_QUOTED, |
| 184 | + attribute: true |
| 185 | + }) |
| 186 | + ) |
189 | 187 |
|
190 |
| - value = quote + value + quote; |
| 188 | + value = quote + value + quote |
191 | 189 | }
|
192 | 190 |
|
193 | 191 | /* Don’t add a `=` for unquoted empties. */
|
194 |
| - value = value ? EQ + value : value; |
| 192 | + value = value ? EQ + value : value |
195 | 193 | }
|
196 | 194 |
|
197 |
| - return value; |
| 195 | + return value |
198 | 196 | }
|
0 commit comments