Comments (5)
Thanks for the well articulated thoughts and comprehensive code example. Overall its a good direction, a few thoughts on what this implementation should look like (in my opinion):
- We need a size efficient way to determine if we need to set the IDL or not. A tagname map of inputs would be too large for a 2kb library. I think we can do this by checking against 3 interfaces:
HTMLInputElement
,HTMLSelectElement
,HTMLTextareaElement
. All of those have avalue
IDL that can be set. I dont think we should set the actual value attribute of any of these. This would fall in line with how Vue does it, but would be in contrast to React. false
removing an attribute is a personal preference of mine that I think leads to much more concise and readable code, despite being a slight deviation from . Vue 2 used to do attributes this way and it switching to false being a valid value, its been a minor pain (in my opinion) and the number of use cases where false is a desired attribute is rare, and in those cases providing the stringfalse
is acceptable. So I don’t currently plan to change this part of the implementation — opinionated as it may be.
And with that said — this is now published/fixed in alpha.5
from arrow-js.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>arrow-js</title>
</head>
<body>
<div id="app"></div>
<script type='module'>
import { reactive, html } from 'https://unpkg.com/@arrow-js/[email protected]/dist/index.min.mjs';
const data = reactive({
text: 'hello',
});
html`
<div>${() => data.text}</div>
<input value="${() => data.text}" @input="${e => (data.text = e.target.value)}">
<input value="${() => data.text}" @input="${e => (data.text = e.target.value)}">
`(document.getElementById('app'))
</script>
</body>
</html>
from arrow-js.
yeah interesting. Attribute binding in arrow uses setAttribute
— inputs are weird in that they have their own state with a value content attribute and a value IDL property. AFAIK the IDL property is the one that is actually displayed — I wonder if we should have a few special cases where we detect if the attribute being set has an matching own property (hasOwn
) and set it accordingly.
from arrow-js.
I think in case of input and maybe textarea you need to patch that element directly by setting their internal property.
At the same time, ignore the setting through the setAttribute property, because in this case there is a small security problem (we can see the password in the attribute through the element inspector for inputs with the password type)
from arrow-js.
I would suggest such a solution, which, in addition to setting value directly to the element, also includes the correct processing of boolean attributes when they have a false value, but they are still put down, which affects, for example, html5 validation through the required attribute.
type AttributeValue = string | number | boolean;
function patchProperties(element: Element, attrName: string, attrValue: AttributeValue): boolean {
const tagName = element.tagName.toLowerCase();
const fn = specialCasesMap[tagName];
let stop = fn ? fn(element, attrName, attrValue) : false;
// check IDL props
if (Object.getPrototypeOf(element).hasOwnProperty(attrName)) {
element[attrName] = attrValue;
}
// blocking the setting of all boolean attributes, except for data-attributes or other custom attributes
if (!stop && typeof attrValue === 'boolean' && !attrName.includes('-')) {
stop = true;
}
// we give out the blocking flag for setting this attribute through setAttribute
return stop;
}
const specialCasesMap: Record<string, typeof patchProperties> = {
input: (element: HTMLInputElement, attrName: string, attrValue: AttributeValue) => {
if (attrName === 'value' && typeof attrValue === 'boolean') {
// checkbox case
element.checked = attrValue;
} else if (attrName === 'autoFocus') {
// autofocus case
element.autofocus = Boolean(attrValue);
}
return false;
},
textarea: (element: HTMLTextAreaElement, attrName: string, attrValue: AttributeValue) => {
if (attrName === 'value') {
// redirect value to innerHTML
element.innerHTML = String(attrValue);
return true;
}
return false;
},
};
from arrow-js.
Related Issues (20)
- Incorrect representation of a table HOT 4
- XSS issue for user generated content HOT 2
- w: not printing all outputs? HOT 4
- Can arrow js be used along with a react component / function? HOT 1
- Т HOT 1
- Keyed lists causing re-render HOT 3
- Click handlers not working anymore HOT 4
- Wrong number of elements in benchmark arrow vs vue HOT 1
- Types don't work in Deno HOT 1
- Allow arbitrary access to HTML element IDL attributes HOT 8
- Mutating reactive data inside a watcher does not re-render
- Empty tagged template strings seem to break reactivity HOT 4
- HTML boolean attributes break dynamic expressions HOT 3
- Syntax Highlighting for HTML template literals HOT 4
- Include HTMLElement in conditionally rendered template. HOT 1
- Incorrect rendering for Reactive on array item HOT 1
- Static string cleared from attribute HOT 1
- SSR
- Hydration HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arrow-js.