MCPcopy Create free account
hub / github.com/bigskysoftware/_hyperscript / _createElementSide

Function _createElementSide

src/parsetree/features/bind.js:145–194  ·  view source on GitHub ↗

* Create a read/write side for a DOM element, auto-detecting the * appropriate property based on element type.

(element, runtime)

Source from the content-addressed store, hash-verified

143 * appropriate property based on element type.
144 */
145function _createElementSide(element, runtime) {
146 var tag = element.tagName;
147 var type = tag === "INPUT" ? (element.getAttribute("type") || "text") : null;
148
149 // Radio buttons have unique semantics: the variable holds the group's
150 // selected value, not a per-element property.
151 if (tag === "INPUT" && type === "radio") {
152 var radioValue = element.value;
153 return {
154 element: element,
155 read: function () {
156 var checked = runtime.resolveProperty(element, "checked");
157 return checked ? radioValue : undefined;
158 },
159 write: function (value) {
160 element.checked = (value === radioValue);
161 }
162 };
163 }
164
165 // Look up property by INPUT:type, then by TAG
166 var prop = _bindProperty[tag + ":" + type] || _bindProperty[tag];
167
168 // Contenteditable elements
169 if (!prop && element.hasAttribute("contenteditable") && element.getAttribute("contenteditable") !== "false") {
170 prop = "textContent";
171 }
172
173 // Custom elements with a value property
174 if (!prop && tag.includes("-") && "value" in element) {
175 prop = "value";
176 }
177
178 if (!prop) {
179 throw new Error(
180 "bind cannot auto-detect a property for <" + tag.toLowerCase() + ">. " +
181 "Use an explicit property (e.g. 'bind $var to #el's value')."
182 );
183 }
184
185 var isNumeric = prop === "valueAsNumber";
186 return {
187 element: element,
188 read: function () {
189 var val = runtime.resolveProperty(element, prop);
190 return (isNumeric && val !== val) ? null : val;
191 },
192 write: function (value) { element[prop] = value; }
193 };
194}
195
196/**
197 * Create a read/write side for a parsed expression (variable, attribute, class, etc).

Callers 1

_resolveSideFunction · 0.70

Calls 1

resolvePropertyMethod · 0.45

Tested by

no test coverage detected