-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
bind.js
206 lines (164 loc) · 6.28 KB
/
bind.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { dontAutoEvaluateFunctions, evaluate } from '../evaluator'
import { reactive } from '../reactivity'
import { setClasses } from './classes'
import { setStyles } from './styles'
export default function bind(el, name, value, modifiers = []) {
// Register bound data as pure observable data for other APIs to use.
if (! el._x_bindings) el._x_bindings = reactive({})
el._x_bindings[name] = value
name = modifiers.includes('camel') ? camelCase(name) : name
switch (name) {
case 'value':
bindInputValue(el, value)
break;
case 'style':
bindStyles(el, value)
break;
case 'class':
bindClasses(el, value)
break;
// 'selected' and 'checked' are special attributes that aren't necessarily
// synced with their corresponding properties when updated, so both the
// attribute and property need to be updated when bound.
case 'selected':
case 'checked':
bindAttributeAndProperty(el, name, value)
break;
default:
bindAttribute(el, name, value)
break;
}
}
function bindInputValue(el, value) {
if (el.type === 'radio') {
// Set radio value from x-bind:value, if no "value" attribute exists.
// If there are any initial state values, radio will have a correct
// "checked" value since x-bind:value is processed before x-model.
if (el.attributes.value === undefined) {
el.value = value
}
// @todo: yuck
if (window.fromModel) {
if (typeof value === 'boolean') {
el.checked = safeParseBoolean(el.value) === value
} else {
el.checked = checkedAttrLooseCompare(el.value, value)
}
}
} else if (el.type === 'checkbox') {
// If we are explicitly binding a string to the :value, set the string,
// If the value is a boolean/array/number/null/undefined, leave it alone, it will be set to "on"
// automatically.
if (Number.isInteger(value)) {
el.value = value
} else if (! Array.isArray(value) && typeof value !== 'boolean' && ! [null, undefined].includes(value)) {
el.value = String(value)
} else {
if (Array.isArray(value)) {
el.checked = value.some(val => checkedAttrLooseCompare(val, el.value))
} else {
el.checked = !!value
}
}
} else if (el.tagName === 'SELECT') {
updateSelect(el, value)
} else {
if (el.value === value) return
el.value = value === undefined ? '' : value
}
}
function bindClasses(el, value) {
if (el._x_undoAddedClasses) el._x_undoAddedClasses()
el._x_undoAddedClasses = setClasses(el, value)
}
function bindStyles(el, value) {
if (el._x_undoAddedStyles) el._x_undoAddedStyles()
el._x_undoAddedStyles = setStyles(el, value)
}
function bindAttributeAndProperty(el, name, value) {
bindAttribute(el, name, value)
setPropertyIfChanged(el, name, value)
}
function bindAttribute(el, name, value) {
if ([null, undefined, false].includes(value) && attributeShouldntBePreservedIfFalsy(name)) {
el.removeAttribute(name)
} else {
if (isBooleanAttr(name)) value = name
setIfChanged(el, name, value)
}
}
function setIfChanged(el, attrName, value) {
if (el.getAttribute(attrName) != value) {
el.setAttribute(attrName, value)
}
}
function setPropertyIfChanged(el, propName, value) {
if (el[propName] !== value) {
el[propName] = value
}
}
function updateSelect(el, value) {
const arrayWrappedValue = [].concat(value).map(value => { return value + '' })
Array.from(el.options).forEach(option => {
option.selected = arrayWrappedValue.includes(option.value)
})
}
function camelCase(subject) {
return subject.toLowerCase().replace(/-(\w)/g, (match, char) => char.toUpperCase())
}
function checkedAttrLooseCompare(valueA, valueB) {
return valueA == valueB
}
export function safeParseBoolean(rawValue) {
if ([1, '1', 'true', 'on', 'yes', true].includes(rawValue)) {
return true
}
if ([0, '0', 'false', 'off', 'no', false].includes(rawValue)) {
return false
}
return rawValue ? Boolean(rawValue) : null
}
function isBooleanAttr(attrName) {
// As per HTML spec table https://html.spec.whatwg.org/multipage/indices.html#attributes-3:boolean-attribute
// Array roughly ordered by estimated usage
const booleanAttributes = [
'disabled','checked','required','readonly','open', 'selected',
'autofocus', 'itemscope', 'multiple', 'novalidate','allowfullscreen',
'allowpaymentrequest', 'formnovalidate', 'autoplay', 'controls', 'loop',
'muted', 'playsinline', 'default', 'ismap', 'reversed', 'async', 'defer',
'nomodule'
]
return booleanAttributes.includes(attrName)
}
function attributeShouldntBePreservedIfFalsy(name) {
return ! ['aria-pressed', 'aria-checked', 'aria-expanded', 'aria-selected'].includes(name)
}
export function getBinding(el, name, fallback) {
// First let's get it out of Alpine bound data.
if (el._x_bindings && el._x_bindings[name] !== undefined) return el._x_bindings[name]
return getAttributeBinding(el, name, fallback)
}
export function extractProp(el, name, fallback, extract = true) {
// First let's get it out of Alpine bound data.
if (el._x_bindings && el._x_bindings[name] !== undefined) return el._x_bindings[name]
if (el._x_inlineBindings && el._x_inlineBindings[name] !== undefined) {
let binding = el._x_inlineBindings[name]
binding.extract = extract
return dontAutoEvaluateFunctions(() => {
return evaluate(el, binding.expression)
})
}
return getAttributeBinding(el, name, fallback)
}
function getAttributeBinding(el, name, fallback) {
// If not, we'll return the literal attribute.
let attr = el.getAttribute(name)
// Nothing bound:
if (attr === null) return typeof fallback === 'function' ? fallback() : fallback
// The case of a custom attribute with no value. Ex: <div manual>
if (attr === '') return true
if (isBooleanAttr(name)) {
return !! [name, 'true'].includes(attr)
}
return attr
}