Skip to content

Commit 0749871

Browse files
Remove attributes with undefined or null value
1 parent 68722eb commit 0749871

11 files changed

+63
-3
lines changed

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.0-beta.13 (2022-11-03)
2+
3+
* Remove attributes that have "undefined" or "null" value
4+
15
## 1.0.0-beta.12 (2022-10-31)
26

37
* Added more utilities

docs-src/md/changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.0-beta.13 (2022-11-03)
2+
3+
* Remove attributes that have "undefined" or "null" value
4+
15
## 1.0.0-beta.12 (2022-10-31)
26

37
* Added more utilities

docs-src/pages/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1 class="display-1 fw-bold mb-4">Build the web with PostHTML</h1>
1414
</div>
1515

1616
<p class="text-muted">
17-
Currently <strong>v1.0.0-beta.12</strong>
17+
Currently <strong>v1.0.0-beta.13</strong>
1818
<span class="px-1">·</span>
1919
<a href="#" class="link-secondary" data-bs-toggle="modal" data-bs-target="#modalWithComponents">Changelog</a>
2020
</p>

docs/demo.html

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ <h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
7777
</div>
7878
<!-- /.modal-header -->
7979
<div class="modal-body">
80+
<h2 id="1.0.0-beta.13-(2022-11-03)" tabindex="-1">
81+
<a class="header-anchor" href="#1.0.0-beta.13-(2022-11-03)">#</a>
82+
1.0.0-beta.13 (2022-11-03)
83+
</h2>
84+
<ul>
85+
<li>Remove attributes that have &quot;undefined&quot; or &quot;null&quot; value</li>
86+
</ul>
8087
<h2 id="1.0.0-beta.12-(2022-10-31)" tabindex="-1">
8188
<a class="header-anchor" href="#1.0.0-beta.12-(2022-10-31)">#</a>
8289
1.0.0-beta.12 (2022-10-31)

docs/docs.html

+7
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,13 @@ <h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
14881488
</div>
14891489
<!-- /.modal-header -->
14901490
<div class="modal-body">
1491+
<h2 id="1.0.0-beta.13-(2022-11-03)" tabindex="-1">
1492+
<a class="header-anchor" href="#1.0.0-beta.13-(2022-11-03)">#</a>
1493+
1.0.0-beta.13 (2022-11-03)
1494+
</h2>
1495+
<ul>
1496+
<li>Remove attributes that have &quot;undefined&quot; or &quot;null&quot; value</li>
1497+
</ul>
14911498
<h2 id="1.0.0-beta.12-(2022-10-31)" tabindex="-1">
14921499
<a class="header-anchor" href="#1.0.0-beta.12-(2022-10-31)">#</a>
14931500
1.0.0-beta.12 (2022-10-31)

docs/index.html

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ <h1 class="display-1 fw-bold mb-4">Build the web with PostHTML</h1>
5959
</div>
6060
<p class="text-muted">
6161
Currently
62-
<strong>v1.0.0-beta.12</strong>
62+
<strong>v1.0.0-beta.13</strong>
6363
<span class="px-1">·</span>
6464
<a href="#" class="link-secondary" data-bs-toggle="modal" data-bs-target="#modalWithComponents">Changelog</a>
6565
</p>
@@ -93,6 +93,13 @@ <h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
9393
</div>
9494
<!-- /.modal-header -->
9595
<div class="modal-body">
96+
<h2 id="1.0.0-beta.13-(2022-11-03)" tabindex="-1">
97+
<a class="header-anchor" href="#1.0.0-beta.13-(2022-11-03)">#</a>
98+
1.0.0-beta.13 (2022-11-03)
99+
</h2>
100+
<ul>
101+
<li>Remove attributes that have &quot;undefined&quot; or &quot;null&quot; value</li>
102+
</ul>
96103
<h2 id="1.0.0-beta.12-(2022-10-31)" tabindex="-1">
97104
<a class="header-anchor" href="#1.0.0-beta.12-(2022-10-31)">#</a>
98105
1.0.0-beta.12 (2022-10-31)

docs/test.html

+7
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ <h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
221221
</div>
222222
<!-- /.modal-header -->
223223
<div class="modal-body">
224+
<h2 id="1.0.0-beta.13-(2022-11-03)" tabindex="-1">
225+
<a class="header-anchor" href="#1.0.0-beta.13-(2022-11-03)">#</a>
226+
1.0.0-beta.13 (2022-11-03)
227+
</h2>
228+
<ul>
229+
<li>Remove attributes that have &quot;undefined&quot; or &quot;null&quot; value</li>
230+
</ul>
224231
<h2 id="1.0.0-beta.12-(2022-10-31)" tabindex="-1">
225232
<a class="header-anchor" href="#1.0.0-beta.12-(2022-10-31)">#</a>
226233
1.0.0-beta.12 (2022-10-31)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthtml-component",
3-
"version": "1.0.0-beta.12",
3+
"version": "1.0.0-beta.13",
44
"description": "PostHTML Components Blade-like with slots, attributes as props and custom tag",
55
"license": "MIT",
66
"repository": "thewebartisan7/posthtml-components",

src/process-attributes.js

+8
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,13 @@ module.exports = (currentNode, attributes, props, options) => {
6060
delete attributes[key];
6161
});
6262

63+
// Remove an attribute if value is 'null' or 'undefined'
64+
// so we can conditionally add an attribute
65+
each(nodeAttrs, (value, key) => {
66+
if (['undefined', 'null'].includes(value)) {
67+
delete nodeAttrs[key];
68+
}
69+
});
70+
6371
mainNode.attrs = nodeAttrs.compose();
6472
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script props>
2+
module.exports = {
3+
toggle: null,
4+
target: undefined
5+
};
6+
</script>
7+
<button class="btn btn-primary" data-bs-dismiss="true" data-bs-backdrop="false" data-bs-target="{{ target }}" data-bs-toggle="{{ toggle }}"><yield></yield></button>

test/test-attributes.js

+9
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,12 @@ test('Must override class and style attributes', async t => {
4949

5050
t.is(html, expected);
5151
});
52+
53+
test('Must remove an attributes that has "undefined" or "null" value', async t => {
54+
const actual = `<component src="components/remove-attributes.html">My button</component>`;
55+
const expected = `<button class="btn btn-primary" data-bs-dismiss="true" data-bs-backdrop="false">My button</button>`;
56+
57+
const html = await posthtml([plugin({root: './test/templates', tag: 'component'})]).process(actual).then(result => clean(result.html));
58+
59+
t.is(html, expected);
60+
});

0 commit comments

Comments
 (0)