Skip to content

Commit 9e4af4b

Browse files
author
James Hill
committed
Added filter to virtual DOM to remove non HTML attributes
1 parent b825335 commit 9e4af4b

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hypnode",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/attributes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Hypnode } from './index';
99
interface IAttrs {
1010
[index: string]: any;
1111
children?: string | Hypnode[];
12+
ref?: (el: HTMLElement) => void;
1213
id?: string;
1314
title?: string;
1415
class?: string;

src/virtualDom.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,28 @@ interface IVNode {
2222
function virtualDom(tag: Tag, attrs: IAttrs, children: any[]): any {
2323
return {
2424
nodeName: tag,
25-
attrs,
25+
attrs: filterValidAttributes(attrs),
2626
children,
2727
};
2828
}
2929

30+
/* -----------------------------------
31+
*
32+
* Filter
33+
*
34+
* -------------------------------- */
35+
36+
function filterValidAttributes(attrs: IAttrs) {
37+
const keys = Object.keys(attrs);
38+
const valid = keys.filter(key => key.slice(0, 2) !== 'on' && key !== 'ref');
39+
40+
return valid.reduce((result: IAttrs, key) => {
41+
result[key] = attrs[key];
42+
43+
return result;
44+
}, {});
45+
}
46+
3047
/* -----------------------------------
3148
*
3249
* Export

tests/src/virtualDom.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IAttrs } from '../../src/attributes';
2+
import { IVNode } from '../../src/virtualDom';
23

34
/* -----------------------------------
45
*
@@ -57,6 +58,22 @@ describe('Core:virtualDom', () => {
5758

5859
expect(result).toEqual(sample);
5960
});
61+
62+
it('filters non HTML attributes from virtual nodes', () => {
63+
const sample: IVNode = {
64+
nodeName: 'div',
65+
attrs: { title: testTitle },
66+
children: [],
67+
};
68+
69+
const result = h('div', {
70+
title: testTitle,
71+
onClick: ev => console.log(ev),
72+
ref: (el: HTMLElement) => console.log(el),
73+
});
74+
75+
expect(result).toEqual(sample);
76+
});
6077
});
6178

6279
/* -----------------------------------

0 commit comments

Comments
 (0)