diff --git a/package-lock.json b/package-lock.json index 10d114e..ccae80f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rehtm", - "version": "0.0.1", + "version": "0.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "rehtm", - "version": "0.0.1", + "version": "0.4.0", "license": "MIT", "dependencies": { "htm": "^3.1.1" @@ -23,7 +23,7 @@ "eslint": "^8.30.0", "jest": "^29.3.1", "jest-environment-jsdom": "^29.3.1", - "minicomp": "^0.2.0", + "minicomp": "^0.4.0", "ts-inference-check": "^0.2.1", "ts-jest": "^29.0.3", "ts-node": "^10.9.1", @@ -5034,6 +5034,12 @@ "integrity": "sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==", "dev": true }, + "node_modules/haken": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/haken/-/haken-0.0.2.tgz", + "integrity": "sha512-JmfP1zsP8S9IX+NEA3vyKGY/2btw/jGEuaq5U1GKU6p7qbVVFJmxfG1NX1M8qkMPBByfjoFF02MRgzZH2QxlXw==", + "dev": true + }, "node_modules/has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -7278,10 +7284,13 @@ } }, "node_modules/minicomp": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/minicomp/-/minicomp-0.2.0.tgz", - "integrity": "sha512-YQ+vUt4v01S+qK+UhAKBe2gAH6fxViz6Y83uAh4dpUP1PesYEgl2x1BiHryH7UJrkPl5lXyNYSh7E1SnQfr+DQ==", - "dev": true + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/minicomp/-/minicomp-0.4.0.tgz", + "integrity": "sha512-B1W4vRuCPM3EfymGFb34sP+QHV8oziSzdqunDKNB4cLvlGrcKnY5ymn3VA/pQNlaist99VQqo2CA+Rx1VRZP6Q==", + "dev": true, + "dependencies": { + "haken": "^0.0.2" + } }, "node_modules/minimatch": { "version": "3.1.2", @@ -12390,6 +12399,12 @@ "integrity": "sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==", "dev": true }, + "haken": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/haken/-/haken-0.0.2.tgz", + "integrity": "sha512-JmfP1zsP8S9IX+NEA3vyKGY/2btw/jGEuaq5U1GKU6p7qbVVFJmxfG1NX1M8qkMPBByfjoFF02MRgzZH2QxlXw==", + "dev": true + }, "has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -14062,10 +14077,13 @@ "dev": true }, "minicomp": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/minicomp/-/minicomp-0.2.0.tgz", - "integrity": "sha512-YQ+vUt4v01S+qK+UhAKBe2gAH6fxViz6Y83uAh4dpUP1PesYEgl2x1BiHryH7UJrkPl5lXyNYSh7E1SnQfr+DQ==", - "dev": true + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/minicomp/-/minicomp-0.4.0.tgz", + "integrity": "sha512-B1W4vRuCPM3EfymGFb34sP+QHV8oziSzdqunDKNB4cLvlGrcKnY5ymn3VA/pQNlaist99VQqo2CA+Rx1VRZP6Q==", + "dev": true, + "requires": { + "haken": "^0.0.2" + } }, "minimatch": { "version": "3.1.2", diff --git a/package.json b/package.json index 84df9ac..1113d11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rehtm", - "version": "0.4.0", + "version": "0.5.0", "description": "create HTML using HTM", "main": "dist/commonjs/index.js", "module": "dist/es/index.js", @@ -48,7 +48,7 @@ "eslint": "^8.30.0", "jest": "^29.3.1", "jest-environment-jsdom": "^29.3.1", - "minicomp": "^0.2.0", + "minicomp": "^0.4.0", "ts-inference-check": "^0.2.1", "ts-jest": "^29.0.3", "ts-node": "^10.9.1", diff --git a/src/extensions/test/object-props.ext.test.ts b/src/extensions/test/object-props.ext.test.ts index 19f19e7..8be2d9e 100644 --- a/src/extensions/test/object-props.ext.test.ts +++ b/src/extensions/test/object-props.ext.test.ts @@ -9,7 +9,10 @@ describe('object properties', () => { test('adds object properties to custom elements.', () => { const cb = jest.fn() - define('op-ext-test', () => { + const cb2 = jest.fn() + + define('op-ext-test', ({test: t}) => { + cb2(t) onProperty('test', cb) return '
' @@ -20,6 +23,8 @@ describe('object properties', () => { const el = fact.create('op-ext-test', { test: obj, id: 'foo' }, [], fact) as HTMLElement document.body.append(el) + expect(cb2).toBeCalledTimes(1) + expect(cb2).toBeCalledWith(obj) expect(cb).toBeCalledTimes(1) expect(cb).toBeCalledWith(obj) expect(el.id).toBe('foo') diff --git a/src/factory/dom.ts b/src/factory/dom.ts index 27e5532..b37c176 100644 --- a/src/factory/dom.ts +++ b/src/factory/dom.ts @@ -46,11 +46,16 @@ export const domFactoryExt: (document: Document) => DOMFactoryExt = (document) = } }, - fill: (node, value, fallback) => { + fill: (node, value, fallback, self) => { if (value instanceof document.defaultView!.Node) { node.childNodes.forEach((child) => node.removeChild(child)) node.appendChild(value) - } else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { + } else if (Array.isArray(value)) { + node.childNodes.forEach((child) => node.removeChild(child)) + for (const child of value) { + self.append(node, child, self) + } + }else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { node.textContent = value.toString() } else { fallback() diff --git a/src/factory/test/factory.test.ts b/src/factory/test/factory.test.ts index ffbdd49..f61edb7 100644 --- a/src/factory/test/factory.test.ts +++ b/src/factory/test/factory.test.ts @@ -23,6 +23,14 @@ describe('factory', () => { expect(b.textContent).toBe('Hi Hi!') }) + test('can fill DOM elements with arrays of stuff.', () => { + const factory = domFactory() + const b = document.createElement('b') + factory.fill(b, 'Hellow Hellow', factory) + factory.fill(b, ['hellow', 'world'], factory) + expect(b.textContent).toBe('hellowworld') + }) + test('throws error for unsupported tags.', () => { const factory = domFactory() expect(() => factory.create(42, {}, [], factory)).toThrowError() @@ -41,7 +49,7 @@ describe('factory', () => { test('throws error for unsupported content to fill.', () => { const factory = domFactory() const b = document.createElement('b') - expect(() => factory.fill(b, [42], factory)).toThrowError() + expect(() => factory.fill(b, () => {}, factory)).toThrowError() }) test('can be extended to support new tag types.', () => { @@ -129,7 +137,7 @@ describe('factory', () => { const fact = extend(domFactory(), { fill(el, child, fallback) { - if (Array.isArray(child)) { + if (typeof child === 'function') { cb() return fallback(el, '42') @@ -140,7 +148,7 @@ describe('factory', () => { }) const b = document.createElement('b') - fact.fill(b, [41], fact) + fact.fill(b, () => {}, fact) expect(cb).toBeCalledTimes(1) expect(b.textContent).toBe('42') }) diff --git a/src/test/build.test.ts b/src/test/build.test.ts index 6154cf4..9b2b4a3 100644 --- a/src/test/build.test.ts +++ b/src/test/build.test.ts @@ -127,4 +127,17 @@ describe(build, () => { expect(div$).not.toBeNull() expect(div$!.textContent).toBe('halo World!') }) + + test('can embed arrays in elements.', () => { + const { html } = build(domFactory()) + + const c1 = html`World` + const c2 = html`
halo ${[c1, '!']}
` + + document.body.appendChild(c2) + + const div$ = document.querySelector('div') + expect(div$).not.toBeNull() + expect(div$!.textContent).toBe('halo World!') + }) })