attrs.js
1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* @flow */
import { escape } from '../util'
import {
isDef,
isUndef,
extend
} from 'shared/util'
import {
isBooleanAttr,
isEnumeratedAttr,
isFalsyAttrValue,
convertEnumeratedValue
} from 'web/util/attrs'
import { isSSRUnsafeAttr } from 'web/server/util'
export default function renderAttrs (node: VNodeWithData): string {
let attrs = node.data.attrs
let res = ''
const opts = node.parent && node.parent.componentOptions
if (isUndef(opts) || opts.Ctor.options.inheritAttrs !== false) {
let parent = node.parent
while (isDef(parent)) {
if (isDef(parent.data) && isDef(parent.data.attrs)) {
attrs = extend(extend({}, attrs), parent.data.attrs)
}
parent = parent.parent
}
}
if (isUndef(attrs)) {
return res
}
for (const key in attrs) {
if (isSSRUnsafeAttr(key)) {
continue
}
if (key === 'style') {
// leave it to the style module
continue
}
res += renderAttr(key, attrs[key])
}
return res
}
export function renderAttr (key: string, value: string): string {
if (isBooleanAttr(key)) {
if (!isFalsyAttrValue(value)) {
return ` ${key}="${key}"`
}
} else if (isEnumeratedAttr(key)) {
return ` ${key}="${escape(convertEnumeratedValue(key, value))}"`
} else if (!isFalsyAttrValue(value)) {
return ` ${key}="${escape(String(value))}"`
}
return ''
}