Skip to content

Commit fb5d3f6

Browse files
Refactor valid attributes to use HTML5 valid attributes
1 parent db5fcf0 commit fb5d3f6

14 files changed

+5633
-223
lines changed

docs-src/md/changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.0.0-RC.1 (2023-03-08)
2+
3+
First Release Candidate
4+
5+
* Prevent attributes used for passing props from being added to first node
6+
* Updated docs
7+
18
## 1.0.0-beta.16 (2022-11-06)
29

310
* Fix aware props propagation

docs-src/md/introduction.md

+102-50
Large diffs are not rendered by default.

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.16</strong>
17+
Currently <strong>v1.0.0-RC.1</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

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ <h1 class="display-1 fw-bold mb-4">Demo</h1>
5151
</div>
5252
</div>
5353
</main>
54-
<footer>
54+
<footer class="footer mt-auto py-3">
5555
<div class="container-lg">
5656
<div class="text-muted text-center">
5757
Handcrafted with
@@ -77,6 +77,15 @@ <h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
7777
</div>
7878
<!-- /.modal-header -->
7979
<div class="modal-body">
80+
<h2 id="1.0.0-rc.1-(2023-03-08)" tabindex="-1">
81+
<a class="header-anchor" href="#1.0.0-rc.1-(2023-03-08)">#</a>
82+
1.0.0-RC.1 (2023-03-08)
83+
</h2>
84+
<p>First Release Candidate</p>
85+
<ul>
86+
<li>Prevent attributes used for passing props from being added to first node</li>
87+
<li>Updated docs</li>
88+
</ul>
8089
<h2 id="1.0.0-beta.16-(2022-11-06)" tabindex="-1">
8190
<a class="header-anchor" href="#1.0.0-beta.16-(2022-11-06)">#</a>
8291
1.0.0-beta.16 (2022-11-06)

docs/docs.html

+130-34
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@
162162
<li class="nav-item">
163163
<a class="nav-link" href="#attributes">Attributes</a>
164164
</li>
165+
<li class="nav-item">
166+
<a class="nav-link" href="#advanced-attributes-configurations">Advanced attributes configurations</a>
167+
</li>
165168
</ul>
166169
</li>
167170
<li class="nav-item">
@@ -183,11 +186,6 @@
183186
</li>
184187
</ul>
185188
</nav>
186-
<blockquote>
187-
<p>
188-
This plugin is still in early stage of development and the current API may change.
189-
</p>
190-
</blockquote>
191189
<h2 id="installation" tabindex="-1">
192190
<a class="header-anchor" href="#installation">#</a>
193191
Installation
@@ -199,7 +197,7 @@ <h2 id="introduction" tabindex="-1">
199197
</h2>
200198
<p>
201199
This PostHTML plugin provides an HTML-friendly syntax for write components in your templates.
202-
If you are familiar with Blade, you will find similar syntax as this plugin is inspired by it.
200+
If you are familiar with Blade, React, Vue or similar, you will find familiar syntax as this plugin is inspired by them.
203201
See below a basic example, as code is worth a thousand words.
204202
</p>
205203
<p>
@@ -245,15 +243,19 @@ <h2 id="basic-example" tabindex="-1">
245243
&lt;/body&gt;
246244
&lt;/html&gt;</code></pre>
247245
<p>
248-
You may notice that our
246+
You may notice that the
249247
<code>src/button.html</code>
250248
component has a
251249
<code>type</code>
252250
and
253251
<code>class</code>
254252
attribute, and when we use the component in
255253
<code>src/index.html</code>
256-
we add type and class attribute.
254+
we pass
255+
<code>type</code>
256+
and
257+
<code>class</code>
258+
attribute.
257259
The result is that
258260
<code>type</code>
259261
is override, and
@@ -266,7 +268,11 @@ <h2 id="basic-example" tabindex="-1">
266268
and
267269
<code>style</code>
268270
attributes are merged, while all others attribute are override.
269-
You can also override class and style attribute by prepending
271+
You can also override
272+
<code>class</code>
273+
and
274+
<code>style</code>
275+
attributes by prepending
270276
<code>override:</code>
271277
to the class attribute. Example:
272278
</p>
@@ -282,20 +288,25 @@ <h2 id="basic-example" tabindex="-1">
282288
<code>props</code>
283289
via
284290
<code>&lt;script props&gt;</code>
285-
. More details on this in
291+
or if they are not in the following file
292+
<a href="https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js">valid-attributes.js</a>
293+
.
294+
You can also manage valid attributes via options.
295+
More details on this in
286296
<a href="#attributes">Attributes</a>
287297
section.
288298
</p>
289299
<p>
290300
The
291301
<code>&lt;yield&gt;</code>
292302
tag is where your content will be injected.
293-
In next section you can find all available options and then examples for each features.
303+
In next section you can find all available options and then examples for each feature.
294304
</p>
295305
<p>
296306
See also the
297307
<code>docs-src</code>
298-
folder where you can find more examples. You can run
308+
folder where you can find more examples.
309+
You can run
299310
<code>npm run build</code>
300311
to compile them.
301312
</p>
@@ -612,6 +623,33 @@ <h2 id="options" tabindex="-1">
612623
.
613624
</td>
614625
</tr>
626+
<tr>
627+
<td style="text-align:center">
628+
<strong>elementAttributes</strong>
629+
</td>
630+
<td style="text-align:center">
631+
<code>{}</code>
632+
</td>
633+
<td style="text-align:left">An object with tag name and a function modifier of valid-attributes.js.</td>
634+
</tr>
635+
<tr>
636+
<td style="text-align:center">
637+
<strong>safelistAttributes</strong>
638+
</td>
639+
<td style="text-align:center">
640+
<code>['data-*']</code>
641+
</td>
642+
<td style="text-align:left">An array of attributes name to be added to default valid attributes.</td>
643+
</tr>
644+
<tr>
645+
<td style="text-align:center">
646+
<strong>blacklistAttributes</strong>
647+
</td>
648+
<td style="text-align:center">
649+
<code>[]</code>
650+
</td>
651+
<td style="text-align:left">An array of attributes name to be removed from default valid attributes.</td>
652+
</tr>
615653
</tbody>
616654
</table>
617655
<h2 id="features" tabindex="-1">
@@ -718,7 +756,7 @@ <h3 id="multiple-folders" tabindex="-1">
718756
and
719757
<code>./src/layouts</code>
720758
.
721-
You can setup the plugin like below:
759+
You can set up the plugin like below:
722760
</p>
723761
<pre><code class="language-js">// index.js
724762
const options = {
@@ -734,7 +772,7 @@ <h3 id="namespaces" tabindex="-1">
734772
Namespaces
735773
</h3>
736774
<p>
737-
With namespaces you can define a top level root path to your components like shown in below example.
775+
With namespaces, you can define a top level root path to your components like shown in below example.
738776
It can be useful for handle custom theme, where you define a specific top level root, with fallback root when component it's not found,
739777
and a custom root for override, something like a child theme.
740778
</p>
@@ -896,7 +934,7 @@ <h3 id="slots" tabindex="-1">
896934
&lt;/div&gt;
897935
&lt;/div&gt;</code></pre>
898936
<p>
899-
By default the content is replaced, but you can also prepend or append the content, or keep the default content by not filling the slot.
937+
By default, the content is replaced, but you can also prepend or append the content, or keep the default content by not filling the slot.
900938
</p>
901939
<p>Add some default content in the component:</p>
902940
<pre><code class="language-html">&lt;!-- src/modal.html --&gt;
@@ -1022,7 +1060,7 @@ <h3 id="stacks" tabindex="-1">
10221060
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
10231061
&lt;/push&gt;</code></pre>
10241062
<p>
1025-
By default the content is pushed in the stack in the given order.
1063+
By default, the content is pushed in the stack in the given order.
10261064
If you would like to prepend content onto the beginning of a stack, you should use the
10271065
<code>prepend</code>
10281066
attribute:
@@ -1084,15 +1122,7 @@ <h3 id="props" tabindex="-1">
10841122
Default prop value
10851123
&lt;/div&gt;</code></pre>
10861124
<p>
1087-
If you don't add the props in
1088-
<code>&lt;script props&gt;</code>
1089-
inside your component, all props will be added as attributes to the first node of your component or to the node with attribute
1090-
<code>attributes</code>
1091-
.
1092-
More details on this in the next section.
1093-
</p>
1094-
<p>
1095-
So in the
1125+
In the
10961126
<code>&lt;script props&gt;</code>
10971127
you have access to passed props via object
10981128
<code>props</code>
@@ -1134,7 +1164,7 @@ <h3 id="props" tabindex="-1">
11341164
<code>class</code>
11351165
attribute is merged with
11361166
<code>class</code>
1137-
attribute of the first node. Let's see in next section more about this.
1167+
attribute of the first node. In the next section you will know more about this.
11381168
</p>
11391169
<p>
11401170
You can change how attributes are merged with global props defined via options by passing a callback function used by lodash method
@@ -1199,10 +1229,17 @@ <h3 id="attributes" tabindex="-1">
11991229
Attributes
12001230
</h3>
12011231
<p>
1202-
Your can pass any attributes to your components and this will be added to the first node of your component, or to the node with an attribute named
1232+
You can pass any attributes to your components and this will be added to the first node of your component,
1233+
or to the node with an attribute named
12031234
<code>attributes</code>
1235+
. If you are familiar with VueJS this is the same as so called
1236+
<a href="https://vuejs.org/guide/components/attrs.html">fallthrough attribute</a>
1237+
, or with Laravel Blade is
1238+
<a href="https://laravel.com/docs/10.x/blade#component-attributes">component-attributes</a>
12041239
.
1205-
By default
1240+
</p>
1241+
<p>
1242+
By default
12061243
<code>class</code>
12071244
and
12081245
<code>style</code>
@@ -1212,9 +1249,13 @@ <h3 id="attributes" tabindex="-1">
12121249
<code>style</code>
12131250
attribute.
12141251
All others attributes are override by default.
1215-
Only attribute not defined as
1252+
Only attributes defined in
1253+
<a href="https://github.com/thewebartisan7/posthtml-components/blob/main/src/valid-attributes.js">valid-attributes.js</a>
1254+
or not defined as
12161255
<code>props</code>
1217-
will be used.
1256+
in the
1257+
<code>&lt;script props&gt;</code>
1258+
.
12181259
</p>
12191260
<p>As already seen in basic example:</p>
12201261
<pre><code class="language-html">&lt;!-- src/button.html --&gt;
@@ -1239,7 +1280,6 @@ <h3 id="attributes" tabindex="-1">
12391280
<code>props</code>
12401281
.
12411282
</p>
1242-
<p>If you are familiar with Laravel Blade, this is also how Blade handle this.</p>
12431283
<p>
12441284
As said early,
12451285
<code>class</code>
@@ -1255,7 +1295,7 @@ <h3 id="attributes" tabindex="-1">
12551295
<pre><code class="language-html">&lt;!-- dist/index.html --&gt;
12561296
&lt;button type=&quot;submit&quot; class=&quot;btn-custom&quot;&gt;My button&lt;/button&gt;</code></pre>
12571297
<p>
1258-
If you want to use another node for add such attributes, then you can add the attribute
1298+
If you want to use another node and not the first one, then you can add the attribute
12591299
<code>attributes</code>
12601300
like shown below.
12611301
</p>
@@ -1280,6 +1320,53 @@ <h3 id="attributes" tabindex="-1">
12801320
<a href="https://github.com/posthtml/posthtml-attrs-parser">posthtml-attrs-parser</a>
12811321
plugin.
12821322
</p>
1323+
<h3 id="advanced-attributes-configurations" tabindex="-1">
1324+
<a class="header-anchor" href="#advanced-attributes-configurations">#</a>
1325+
Advanced attributes configurations
1326+
</h3>
1327+
<p>
1328+
If default configurations for valid attributes are not right for you, then you can configure them as explained below.
1329+
</p>
1330+
<pre><code class="language-js">// index.js
1331+
const { readFileSync, writeFileSync } = require('fs')
1332+
1333+
const posthtml = require('posthtml')
1334+
const components = require('posthtml-components')
1335+
1336+
const options = {
1337+
root: './src',
1338+
1339+
// Add attributes to specific tag or override defaults
1340+
elementAttributes: {
1341+
DIV: (defaultAttributes) =&gt; {
1342+
/* Add new one */
1343+
defaultAttributes.push('custom-attribute-name');
1344+
1345+
return defaultAttributes;
1346+
},
1347+
DIV: (defaultAttributes) =&gt; {
1348+
/* Override all */
1349+
defaultAttributes = ['custom-attribute-name', 'another-one'];
1350+
1351+
return defaultAttributes;
1352+
},
1353+
},
1354+
1355+
// Add attributes to all tags, use '*' as wildcard for attribute name that starts with
1356+
safelistAttributes: [
1357+
'custom-attribute-name',
1358+
'attribute-name-start-with-*'
1359+
],
1360+
1361+
// Remove attributes from all tags that support it
1362+
blacklistAttributes: [
1363+
'role'
1364+
]
1365+
}
1366+
1367+
posthtml(components(options))
1368+
.process(readFileSync('src/index.html', 'utf8'))
1369+
.then((result) =&gt; writeFileSync('dist/index.html', result.html, 'utf8'))</code></pre>
12831370
<h2 id="examples" tabindex="-1">
12841371
<a class="header-anchor" href="#examples">#</a>
12851372
Examples
@@ -1289,7 +1376,7 @@ <h2 id="examples" tabindex="-1">
12891376
<code>&lt;slot&gt;</code>
12901377
and
12911378
<code>&lt;fill&gt;</code>
1292-
or you can create component for each &quot;block&quot; of your component, and you can also support both of them.
1379+
or you can create component for each block of your component, and you can also support both of them.
12931380
You can find an example of this inside
12941381
<code>docs-src/components/modal</code>
12951382
. Below is a short explanation about the both approach.
@@ -1470,7 +1557,7 @@ <h2 id="migration" tabindex="-1">
14701557
</div>
14711558
</div>
14721559
</main>
1473-
<footer>
1560+
<footer class="footer mt-auto py-3">
14741561
<div class="container-lg">
14751562
<div class="text-muted text-center">
14761563
Handcrafted with
@@ -1496,6 +1583,15 @@ <h5 class="modal-title" id="modalWithComponentsLabel">Changelog</h5>
14961583
</div>
14971584
<!-- /.modal-header -->
14981585
<div class="modal-body">
1586+
<h2 id="1.0.0-rc.1-(2023-03-08)" tabindex="-1">
1587+
<a class="header-anchor" href="#1.0.0-rc.1-(2023-03-08)">#</a>
1588+
1.0.0-RC.1 (2023-03-08)
1589+
</h2>
1590+
<p>First Release Candidate</p>
1591+
<ul>
1592+
<li>Prevent attributes used for passing props from being added to first node</li>
1593+
<li>Updated docs</li>
1594+
</ul>
14991595
<h2 id="1.0.0-beta.16-(2022-11-06)" tabindex="-1">
15001596
<a class="header-anchor" href="#1.0.0-beta.16-(2022-11-06)">#</a>
15011597
1.0.0-beta.16 (2022-11-06)

0 commit comments

Comments
 (0)