Code Monkey home page Code Monkey logo

uhandlers's Introduction

webreflection

WebReflection Ltd

uhandlers's People

Contributors

dependabot[bot] avatar webreflection avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

pwfoo

uhandlers's Issues

Add support for style as special attribute

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);
};

Incorrect rendering with html`<textarea>${undefined}</textarea>`

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>&lt;!--isµ0--&gt;</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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.