UNPKG

1.95 kBJavaScriptView Raw
1/* @flow */
2
3import { makeMap } from 'shared/util'
4
5// these are reserved for web because they are directly compiled away
6// during template compilation
7export const isReservedAttr = makeMap('style,class')
8
9// attributes that should be using props for binding
10const acceptValue = makeMap('input,textarea,option,select,progress')
11export const mustUseProp = (tag: string, type: ?string, attr: string): boolean => {
12 return (
13 (attr === 'value' && acceptValue(tag)) && type !== 'button' ||
14 (attr === 'selected' && tag === 'option') ||
15 (attr === 'checked' && tag === 'input') ||
16 (attr === 'muted' && tag === 'video')
17 )
18}
19
20export const isEnumeratedAttr = makeMap('contenteditable,draggable,spellcheck')
21
22const isValidContentEditableValue = makeMap('events,caret,typing,plaintext-only')
23
24export const convertEnumeratedValue = (key: string, value: any) => {
25 return isFalsyAttrValue(value) || value === 'false'
26 ? 'false'
27 // allow arbitrary string value for contenteditable
28 : key === 'contenteditable' && isValidContentEditableValue(value)
29 ? value
30 : 'true'
31}
32
33export const isBooleanAttr = makeMap(
34 'allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,' +
35 'default,defaultchecked,defaultmuted,defaultselected,defer,disabled,' +
36 'enabled,formnovalidate,hidden,indeterminate,inert,ismap,itemscope,loop,multiple,' +
37 'muted,nohref,noresize,noshade,novalidate,nowrap,open,pauseonexit,readonly,' +
38 'required,reversed,scoped,seamless,selected,sortable,translate,' +
39 'truespeed,typemustmatch,visible'
40)
41
42export const xlinkNS = 'http://www.w3.org/1999/xlink'
43
44export const isXlink = (name: string): boolean => {
45 return name.charAt(5) === ':' && name.slice(0, 5) === 'xlink'
46}
47
48export const getXlinkProp = (name: string): string => {
49 return isXlink(name) ? name.slice(6, name.length) : ''
50}
51
52export const isFalsyAttrValue = (val: any): boolean => {
53 return val == null || val === false
54}