All files / src/core object-factory.js

0% Statements 0/61
0% Branches 0/1
0% Functions 0/1
0% Lines 0/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                                                                                                                         
/**
 * Object Factory for Coherent.js
 * @fileoverview Creates Coherent objects with HTML elements and text nodes
 */
 
import { HTML_ELEMENTS, CoherentTypes } from '../types/constants.js';
 
/**
 * Creates a Coherent object with the specified tag and properties
 * @param {string} tag - HTML tag name
 * @param {Object} [props={}] - Properties object
 * @returns {Object} Coherent object with element structure
 * @throws {Error} When invalid HTML element is provided
 */
export function createElement(tag, props = {}) {
    if (!HTML_ELEMENTS.has(tag)) {
        throw new Error(`Invalid HTML element: ${tag}`);
    }
 
    // Create the coherent object structure
    const coherentObj = {
        [tag]: {
            ...props,
            _type: CoherentTypes.ELEMENT
        }
    };
 
    return coherentObj;
}
 
/**
 * Creates a text node
 * @param {string} text - Text content
 * @returns {Object} Coherent text object
 */
export function createTextNode(text) {
    return {
        text: String(text),
        _type: CoherentTypes.OBJECT
    };
}
 
/**
 * Helper function to create common elements
 */
export const h = {
    div: (props) => createElement('div', props),
    span: (props) => createElement('span', props),
    p: (props) => createElement('p', props),
    h1: (props) => createElement('h1', props),
    h2: (props) => createElement('h2', props),
    h3: (props) => createElement('h3', props),
    h4: (props) => createElement('h4', props),
    h5: (props) => createElement('h5', props),
    h6: (props) => createElement('h6', props),
    a: (props) => createElement('a', props),
    img: (props) => createElement('img', props),
    button: (props) => createElement('button', props),
    input: (props) => createElement('input', props),
    form: (props) => createElement('form', props),
    ul: (props) => createElement('ul', props),
    ol: (props) => createElement('ol', props),
    li: (props) => createElement('li', props),
    table: (props) => createElement('table', props),
    tr: (props) => createElement('tr', props),
    td: (props) => createElement('td', props),
    th: (props) => createElement('th', props)
};