WebReflection Ltd
webreflection / uhandlers Goto Github PK
View Code? Open in Web Editor NEWµhtml attributes handlers.
License: ISC License
µhtml attributes handlers.
License: ISC License
Add support of style attribute to use style property instead of attribute as both object (a CSSStyleDeclaration) and as a string (like regular attribute). Similar to hyperhtml-style for hyperhtml
Example with an object:
const node = document.createElement('div');
const styleHandler = style(node);
styleHandler({
"--theme-color": "red",
backgroundColor: "var(--theme-color)",
"border-color": "blue",
"z-index": [200, "important"],
});
node.outerHTML;
// "<div style=\"--theme-color:red; background-color:var(--theme-color); border-color: blue; z-index: 200 !important;\"></div>"
Example with a string:
const node = document.createElement('div');
const styleHandler = style(node);
styleHandler(`
--theme-color:red;
background-color:var(--theme-color);
border-color: blue;
z-index: 200 !important;
`);
node.outerHTML;
// "<div style=\"--theme-color:red; background-color:var(--theme-color); border-color: blue; z-index: 200 !important;\"></div>"
A possible implementation:
const {isArray} = Array;
// Compute hyphen case (WebkitBorderBottomLeftRadius -> -webkit-border-bottom-left-radius, msTextCombineHorizontal -> -ms-text-combine-horizontal)
const nameCaseMap = new Map();
{
const regExp = /^ms|[A-Z]/g;
for(let name in document.documentElement.style){
const hyphenCaseName = name.replace(regExp, "-$&").toLowerCase();
if(name !== hyphenCaseName){
nameCaseMap.set(name, hyphenCaseName);
}
}
}
let serializer;
function updateStyle(style, value){
if(typeof value !== "object"){
style.cssText = value;
return;
}
style.cssText = "";// erase previous values
const setProperty = style.setProperty.bind(style);
for (let name in value){
const rawValue = value[name];
name = nameCaseMap.get(name) || name;// as hyphen case
if(isArray(rawValue)){
setProperty(name, rawValue[0], rawValue[1]);
}else{
setProperty(name, rawValue);
}
}
}
export const style = node => value => {
const {style} = node;
// SVGElement don't have a style property until it's attached to a document.
// Use the native serializer and set the style attribute instead
if(!style){
if(!serializer){
const {namespaceURI, tagName} = document.documentElement;
serializer = document.implementation.createDocument(namespaceURI, tagName).documentElement.style;
}
updateStyle(serializer, value);
node.setAttribute("style", serializer.cssText);
return;
}
updateStyle(style, value);
};
import {render, html} from 'uhtml';
render(document.body, html`<textarea>${undefined}</textarea>`);
console.log(document.body.innerHTML);
This creates a textarea element with <!--isµ0-->
displayed as the current value - the console.log displays <textarea><!--isµ0--></textarea>
. Note the <!--isµ0-->
also ends up inside the <style>
element but this is less harmful as the style element just ignores the invalid content.
I think this could be fixed if the text
function initialized let oldValue = node.textContent;
- this way it would start with <!--isµ0-->
instead of undefined so it wouldn't match newValue
on the first call.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.