From 8ffbdc1f490f02bc024c6494dad50bd4e0b3edeb Mon Sep 17 00:00:00 2001 From: Elson Correia Date: Thu, 9 Jan 2025 19:12:39 -0500 Subject: [PATCH] fix overriding the event in the props --- package.json | 2 +- src/html.spec.ts | 23 +++++++++++++++++++++++ src/html.ts | 12 +++++++----- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 90b9961..ec0e92a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@beforesemicolon/markup", - "version": "1.13.3", + "version": "1.13.4", "description": "Reactive HTML Templating System", "engines": { "node": ">=18.16.0" diff --git a/src/html.spec.ts b/src/html.spec.ts index 53412af..c92d0ce 100644 --- a/src/html.spec.ts +++ b/src/html.spec.ts @@ -2,6 +2,8 @@ import '../test.common.ts'; import { html, HtmlTemplate } from './html.ts' import { effect, state } from './state.ts' import {when, repeat, oneOf, is, element, suspense} from './helpers/index.ts' +import { text } from 'node:stream/consumers' +import exp = require('node:constants') describe('html', () => { @@ -842,6 +844,27 @@ describe('html', () => { expect(countUpSpy).toHaveBeenCalledWith(expect.any(Event)); }) + it('should override attribute in the object', () => { + const clickMock1 = jest.fn(); + const clickMock2 = jest.fn(); + + const props = { + onclick: clickMock1, + type: 'submit' + } + + const temp = html``.render(document.body) + + const [btn] = temp.refs['btn'] as HTMLButtonElement[]; + + expect(document.body.innerHTML).toBe(''); + + btn.click(); + + expect(clickMock1).not.toHaveBeenCalled() + expect(clickMock2).toHaveBeenCalled() + }) + it('should handle input value', () => { const [value, setValue] = state(""); diff --git a/src/html.ts b/src/html.ts index 412d325..815948a 100644 --- a/src/html.ts +++ b/src/html.ts @@ -187,22 +187,24 @@ function createTemplate( const isRef = name === 'ref' + !isRef && slots.remove(attrSlots[name]) + if (isRef || /\$val([0-9]+)/.test(value)) { __self__.setAttribute('data-slot-id', id) const v = value.trim() - return slots.push({ + + attrSlots[name] = { type: 'attribute', name, value: v, nodeSelector: `[data-slot-id="${id}"]`, valueParts: isRef ? [v] : parseDynamicRawValue(v), - }) + } + + return slots.push(attrSlots[name]) } setElementAttribute(__self__, name, value) - - // ensure prop attributes do not override inline attributes - slots.remove(attrSlots[name]) }, } as unknown as ElementLike },