Skip to content

Commit b5c083f

Browse files
committed
v1.4.0
1 parent e29ab62 commit b5c083f

File tree

9 files changed

+103
-33
lines changed

9 files changed

+103
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.DS_Store

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"valuenow",
3535
"valuetext",
3636
"mydiv",
37+
"onref",
3738
"quickpage"
3839
]
3940
}

CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Change log html-tag-js
2+
3+
## 1.4.0
4+
5+
- Filter out null and undefined node from children
6+
e.g.
7+
8+
```jsx
9+
<div>
10+
{null}
11+
<span>hello</span>
12+
{undefined}
13+
</div>
14+
```
15+
16+
will render
17+
18+
```html
19+
<div>
20+
<span>hello</span>
21+
</div>
22+
```
23+
24+
- Support for empty JSXExpression
25+
e.g.
26+
27+
```jsx
28+
<div>{}</div>
29+
```
30+
31+
will render
32+
33+
```html
34+
<div></div>
35+
```

dist/tag.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ <h1>HTML-TAG-JS</h1>
4646
['\nAnother text'],
4747
],
4848
}),
49+
tag('span', {
50+
children: [null, undefined, tag.text('This is a span')],
51+
}),
4952
);
5053

5154
p.style.backgroundColor = 'blue';

jsx/jsx-to-tag.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ module.exports = (babel) => {
3535
}
3636

3737
const children = [];
38-
39-
childrenNode.forEach((node) => {
40-
children.push(parseNode(t, node));
41-
});
42-
38+
populateChildren(childrenNode, children, t);
4339
const arrayExpression = t.arrayExpression(children);
4440
path.replaceWith(arrayExpression, node);
4541
},
@@ -58,12 +54,9 @@ module.exports = (babel) => {
5854
const children = [];
5955
const options = [];
6056
const events = {};
61-
6257
const isComponent = /^[A-Z]((?!-).)*$/.test(tagName);
6358

64-
childrenNode.forEach((node) => {
65-
children.push(parseNode(t, node));
66-
});
59+
populateChildren(childrenNode, children, t);
6760

6861
attributes.forEach((attr) => {
6962
if (attr.type === 'JSXSpreadAttribute') {
@@ -142,18 +135,15 @@ module.exports = (babel) => {
142135
);
143136
}
144137

145-
if (attrs.length > 0 || children.length > 0) {
138+
if (attrs.length > 0) {
146139
args.push(t.objectExpression(attrs));
147140
}
148141

149142
if (children.length > 0) {
150143
args.push(t.arrayExpression(children));
151144
}
152145
} else {
153-
args.push(
154-
t.stringLiteral(tagName),
155-
t.objectExpression(options),
156-
);
146+
args.push(t.stringLiteral(tagName));
157147

158148
if (on.length > 0) {
159149
options.push(
@@ -173,14 +163,18 @@ module.exports = (babel) => {
173163
);
174164
}
175165

176-
if (children.length > 0) {
166+
if (children.length) {
177167
options.push(
178168
t.objectProperty(
179169
t.identifier('children'),
180170
t.arrayExpression(children),
181171
)
182172
);
183173
}
174+
175+
if (options.length) {
176+
args.push(t.objectExpression(options));
177+
}
184178
}
185179

186180

@@ -192,6 +186,12 @@ module.exports = (babel) => {
192186
};
193187
};
194188

189+
/**
190+
* Parse node to expression
191+
* @param {any} types babel.types
192+
* @param {any} node
193+
* @returns expression
194+
*/
195195
function parseNode(types, node) {
196196
const { type } = node;
197197

@@ -204,5 +204,25 @@ function parseNode(types, node) {
204204
}
205205

206206
const { expression } = node;
207+
const invalidExpressions = ['JSXEmptyExpression'];
208+
209+
if (invalidExpressions.includes(expression.type)) {
210+
return null;
211+
}
212+
207213
return expression;
208214
}
215+
216+
/**
217+
* Populate children
218+
* @param {Array<any>} childrenNode
219+
* @param {Array<any>} children
220+
* @param {any} t
221+
*/
222+
function populateChildren(childrenNode, children, t) {
223+
childrenNode.forEach((node) => {
224+
node = parseNode(t, node);
225+
if (!node) return;
226+
children.push(node);
227+
});
228+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@
105105
},
106106
"dependencies": {},
107107
"browserslist": "cover 100%,not android < 5"
108-
}
108+
}

src/tag.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export default function tag(tagName, options = {}, children = []) {
1010
function create(tagName, options = {}, children = []) {
1111
let $el;
1212

13+
if (Array.isArray(options)) {
14+
children = options;
15+
options = {};
16+
}
17+
1318
if (typeof tagName === 'function') {
1419
return tagName(options, children);
1520
} else if (tagName instanceof Node) {
@@ -87,16 +92,19 @@ function create(tagName, options = {}, children = []) {
8792
*/
8893
function addChildren($el, children) {
8994
for (let child of children) {
90-
if (child instanceof Text) {
95+
if (!(child instanceof Node)) {
96+
if (
97+
child === null
98+
|| child === undefined
99+
|| child === ''
100+
) continue;
101+
child = tag.text(`${child}`);
102+
} else if (child instanceof Text) {
91103
if ('clone' in child) {
92104
child = child.clone();
93105
}
94106
}
95107

96-
if (!(child instanceof Node)) {
97-
child = tag.text(`${child}`);
98-
}
99-
100108
$el.appendChild(child);
101109
}
102110
}
@@ -117,16 +125,6 @@ Object.defineProperties(tag, {
117125
return [...$els];
118126
},
119127
},
120-
parse: {
121-
value(html) {
122-
const $div = document.createElement('div');
123-
$div.innerHTML = html;
124-
if ($div.childElementCount === 1) {
125-
return $div.firstElementChild;
126-
}
127-
return [...$div.children];
128-
},
129-
},
130128
text: {
131129
value(text) {
132130
return document.createTextNode(text);

test/jsxToTag.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ test(`<Test>...</Test>`, () => {
9191
<div className='test2'></div>
9292
</Test>`;
9393
const transformed = babel.transformSync(code, config);
94-
expect(transformed.code).toBe(`tag(Test, {}, ["\\n ", tag("div", {
94+
expect(transformed.code).toBe(`tag(Test, ["\\n ", tag("div", {
9595
className: 'test1'
9696
}), "\\n ", tag("div", {
9797
className: 'test2'
@@ -193,3 +193,15 @@ test(`<button off:click={clickHandler}>Click me</button>`, () => {
193193
children: ["Click me"]
194194
});`);
195195
});
196+
197+
test(`<div>{}</div>`, () => {
198+
const code = `<div>{}</div>`;
199+
const transformed = babel.transformSync(code, config);
200+
expect(transformed.code).toBe(`tag("div");`);
201+
});
202+
203+
test(`<div>{/*Comment*/}</div>`, () => {
204+
const code = `<div>{/*Comment*/}</div>`;
205+
const transformed = babel.transformSync(code, config);
206+
expect(transformed.code).toBe(`tag("div");`);
207+
});

0 commit comments

Comments
 (0)