mudgen / webscript

Webscript is a Javascript library for creating DOM elements. Use it to create web applications. It is like HTML but it is Javascript. It is designed to work with existing libraries.
https://mudgen.github.io/webscript/docs/
MIT License
86 stars 9 forks source link

[WIP] Test for addChild #9

Closed clsource closed 4 years ago

clsource commented 4 years ago

Upon testing the createObjectElement I realized that the addChild function should be different. Since the values in child does not need the setAttributes needed in DOM.

improved the function and the test example.

mudgen commented 4 years ago

Okay. So then is the props argument to the createObjectElement function completely ignored then? It seems like it shouldn't hurt if people want to optionally add properties to their objects. What do you think?

clsource commented 4 years ago

The current addChild function works well for createDOMElement and createSVGElement because they need the attributes be a string to be DOM valid. But in an object the attribute can be a number or a boolean without the need to typecast to a string.

Example code with example output


(() => {
  const { body, img, button } = elementBuilders(createObject);
  const app = body.class`flex`(
    img.class`w-full`.src`img/card-top.jpg`.alt`Sunset in the mountains`,
    button.class`btn btn-lg`("Click Me"),
    button.class`btn btn-lg`(200)
  );

  console.log(app);
  console.log(JSON.stringify(app));
})();
{
    "tagName": "body",
    "children": [{
        "tagName": "img",
        "children": [],
        "value": "",
        "__isElement": true,
        "class": "w-full",
        "src": "img/card-top.jpg",
        "alt": "Sunset in the mountains"
    }, {
        "tagName": "button",
        "children": [],
        "value": "Click Me",
        "__isElement": true,
        "class": "btn btn-lg"
    }, {
        "tagName": "button",
        "children": [],
        "value": 200,
        "__isElement": true,
        "class": "btn btn-lg"
    }],
    "value": "",
    "__isElement": true,
    "class": "flex"
}
mudgen commented 4 years ago

Looks good.