choc

The Chocolate Factory

API documentation

choc.__version__ - currently-loaded library version

choc.__version__ is a string consisting of three parts, major/minor/revision, such as "1.6.4". See What’s New for details on what changed in each version.

choc - Construct a DOM element

element = choc("TAG", attributes, contents);

In additional to the above syntax, this notation is also supported:

element = choc.TAG(attributes, contents);

A destructuring import makes this more convenient:

const {TAG} = choc;
element = TAG(attributes, contents);

This is the recommended idiom, and can be managed by use of the chocimport script.

DOM - Locate a DOM element

element = DOM(selector);

Note that this is very similar - but not identical - to both document.querySelector and jQuery’s selection. Their behaviours differ when zero or multiple elements match:

set_content - Populate a DOM element

element = set_content(elem_or_sel, contents);

on - Respond to DOM events

idx = on(event, selector, callback, options);

The event will be attached to the document. Elements rendered in the document are capable of triggering these events.

Inside the callback, the event object is exactly the one given by the browser, with one additional attribute:

For example, on("click", ".thing", e => set_content(e.match, "Hi!")); will change the contents of the element clicked on, regardless of the presence of other elements with the same selector.

This is similar to e.currentTarget but due to technical limitations, currentTarget will always be document during these event handlers.

NOTE: Asynchronous event handlers are fully supported; however, as the event object is not reconstructed for each handler, the match attribute is only available during the initial, synchronous, part of the function. For example:

on("click", "button", async e => {
    console.log(e.match); //Works
    const self = e.match;
    await something();
    console.log(e.match); //Is now null
    console.log(self); //This will work though
});

//the same is true for other forms of asynchronicity:
on("click", "button", e => {
    console.log(e.match); //Works
    setTimeout(() => {
        console.log(e.match); //Is now null
    }, 1000);
});

apply_fixes - Provide extra functionality and compatibility for dialogs

apply_fixes({
    close_selector: selector,
    click_outside: false | true | "formless",
});

Improves support for the <dialog> element in browsers that lack it, such as older versions of Firefox. Regardless of the options provided, this will add the showModal and close methods to all dialog elements, with a basic level of functionality. Additional features can be selected, all are optional:

Note that this function can be imported and called under the name fix_dialogs for compatibility with older versions of the Chocolate Factory.

Additional DOM methods added

apply_fixes({methods: 1}) will add the following method to all DOM elements:

lindt - Construct an element template

template = lindt("TAG", attributes, contents);

Analogous to choc() but for templated usage.

The lindt function is useful only in conjunction with replace_content.

replace_content - Apply an element template

element = replace_content(elem_or_sel, template);

Analogous to set_content() but for templated usage.

When using templates, replace_content will attempt to reuse DOM elements as much as possible. Repeatedly calling replace_content on the same element with similar templates will apply only those changes, keeping other elements unchanged.

Recommended best practice:

xmlns_xlat - Translate namespace aliases into URIs

(New in 1.7.0)

xmlns_xlat.math = "http://www.w3.org/1998/Math/MathML";

This mapping of aliases to URIs may be externally mutated to create new aliases as needed. It comes prepopulated with the alias svg for http://www.w3.org/2000/svg.

Compatibility features

The five primary functions choc, set_content, on, DOM, and fix_dialogs are made available on the window object as pseudo-globals, for the convenience of non-module code (where an import statement is not available). These may therefore also be used from the browser console.

Special cases

Though Chocolate Factory tries its best to be generic and handle everything the same way, there are occasionally quirks that are best handled in their own way.

  1. Normally an element is constructed, then given its attributes, then given its children. (This parallels the order of parameters.) However, constructing a SELECT element with a value attribute will reapply the value after adding the children, since valid values for a drop-down list are defined by its children.
  2. TODO: Look for any others that need to be documented. There aren’t many.