Skip to content

Commit deea5cf

Browse files
committed
feat: render boolean attributes correctly (previously #317)
1 parent b824a27 commit deea5cf

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/server/generators/attribute.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { booleanHtmlAttributes } from '../../shared/constants'
12
import { isUndefined } from '../../shared/typeof'
23

34
/**
@@ -17,7 +18,7 @@ export default function attributeGenerator({ attribute } = {}, type, data) {
1718
if (data.hasOwnProperty(attr)) {
1819
watchedAttrs.push(attr)
1920

20-
attributeStr += isUndefined(data[attr])
21+
attributeStr += isUndefined(data[attr]) || booleanHtmlAttributes.includes(attr)
2122
? attr
2223
: `${attr}="${data[attr]}"`
2324

src/shared/constants.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,48 @@ export const tagsWithInnerContent = ['noscript', 'script', 'style']
5151

5252
// Attributes which are inserted as childNodes instead of HTMLAttribute
5353
export const tagAttributeAsInnerContent = ['innerHTML', 'cssText']
54+
55+
// from: https://github.com/kangax/html-minifier/blob/gh-pages/src/htmlminifier.js#L202
56+
export const booleanHtmlAttributes = [
57+
'allowfullscreen',
58+
'async',
59+
'autofocus',
60+
'autoplay',
61+
'checked',
62+
'compact',
63+
'controls',
64+
'declare',
65+
'default',
66+
'defaultchecked',
67+
'defaultmuted',
68+
'defaultselected',
69+
'defer',
70+
'disabled',
71+
'enabled',
72+
'formnovalidate',
73+
'hidden',
74+
'indeterminate',
75+
'inert',
76+
'ismap',
77+
'itemscope',
78+
'loop',
79+
'multiple',
80+
'muted',
81+
'nohref',
82+
'noresize',
83+
'noshade',
84+
'novalidate',
85+
'nowrap',
86+
'open',
87+
'pauseonexit',
88+
'readonly',
89+
'required',
90+
'reversed',
91+
'scoped',
92+
'seamless',
93+
'selected',
94+
'sortable',
95+
'truespeed',
96+
'typemustmatch',
97+
'visible'
98+
]

test/utils/meta-info-data.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ const metaInfoData = {
141141
},
142142
change: {
143143
data: [{ src: 'src', async: true, defer: true, [defaultOptions.tagIDKeyName]: 'content2' }],
144-
expect: ['<script data-vue-meta="true" src="src" async="true" defer="true" data-vmid="content2"></script>']
144+
expect: ['<script data-vue-meta="true" src="src" async="true" defer data-vmid="content2"></script>'],
145+
test(side, defaultTest) {
146+
if (side === 'client') {
147+
// jsdom doesnt generate valid boolean attributes
148+
this.expect[0] = this.expect[0].replace('defer', 'defer="true"')
149+
}
150+
}
145151
},
146152
remove: {
147153
data: [],

0 commit comments

Comments
 (0)