Skip to content

Commit 1d9072a

Browse files
committed
fix: only show boolean attrs with truthy value
1 parent 39d9eb6 commit 1d9072a

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ package-lock.json
4242
lib
4343
es
4444
.vue-meta
45+
.nuxt
4546

4647
# examples yarn lock
4748
examples/yarn.lock

src/client/updaters/tag.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ export default function updateTag(appId, { attribute, tagIDKeyName } = {}, type,
5252
const _attr = includes(dataAttributes, attr)
5353
? `data-${attr}`
5454
: attr
55-
const value = isUndefined(tag[attr]) || includes(booleanHtmlAttributes, attr) ? '' : tag[attr]
55+
56+
const isBooleanAttribute = includes(booleanHtmlAttributes, attr)
57+
if (isBooleanAttribute && !tag[attr]) {
58+
continue
59+
}
60+
61+
const value = isBooleanAttribute ? '' : tag[attr]
5662
newElement.setAttribute(_attr, value)
5763
}
5864
}

src/server/generators/tag.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ export default function tagGenerator(appId, { attribute, tagIDKeyName } = {}, ty
3636
prefix = 'data-'
3737
}
3838

39-
return isUndefined(tag[attr]) || booleanHtmlAttributes.includes(attr)
39+
const isBooleanAttr = booleanHtmlAttributes.includes(attr)
40+
if (isBooleanAttr && !tag[attr]) {
41+
return attrsStr
42+
}
43+
44+
return isBooleanAttr
4045
? `${attrsStr} ${prefix}${attr}`
4146
: `${attrsStr} ${prefix}${attr}="${tag[attr]}"`
4247
}, '')

test/unit/escaping.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,35 @@ describe('escaping', () => {
7171
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
7272
})
7373
})
74+
75+
test.skip('special chars are escaped unless disabled by vmid', () => {
76+
const component = new Vue({
77+
metaInfo: {
78+
title: 'Hello',
79+
script: [
80+
{ vmid: 'yescape', innerHTML: ['12', 'asd'] }
81+
]
82+
}
83+
})
84+
85+
expect(getMetaInfo(component, [[/&/g, '&']])).toEqual({
86+
title: 'Hello',
87+
titleChunk: 'Hello',
88+
titleTemplate: '%s',
89+
htmlAttrs: {},
90+
headAttrs: {},
91+
bodyAttrs: {},
92+
meta: [],
93+
base: [],
94+
link: [],
95+
style: [],
96+
script: [
97+
{ innerHTML: 'Hello & Goodbye', vmid: 'yescape' },
98+
{ innerHTML: 'Hello & Goodbye', vmid: 'noscape' }
99+
],
100+
noscript: [],
101+
__dangerouslyDisableSanitizers: [],
102+
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
103+
})
104+
})
74105
})

test/utils/meta-info-data.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ const metaInfoData = {
113113
script: {
114114
add: {
115115
data: [
116-
{ src: 'src', async: true, defer: true, [defaultOptions.tagIDKeyName]: 'content' },
117-
{ src: 'src', async: true, defer: true, body: true }
116+
{ src: 'src', async: false, defer: true, [defaultOptions.tagIDKeyName]: 'content' },
117+
{ src: 'src', async: false, defer: true, body: true }
118118
],
119119
expect: [
120-
'<script data-vue-meta="test" src="src" async defer data-vmid="content"></script>',
121-
'<script data-vue-meta="test" src="src" async defer data-body="true"></script>'
120+
'<script data-vue-meta="test" src="src" defer data-vmid="content"></script>',
121+
'<script data-vue-meta="test" src="src" defer data-body="true"></script>'
122122
],
123123
test(side, defaultTest) {
124124
return () => {

0 commit comments

Comments
 (0)