Skip to content

Commit f659f35

Browse files
[improvement] Now can add attributes via --data.
Also now always puts attributes in double quotes (except for the --attrs variable).
1 parent f7504dd commit f659f35

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,22 @@ Attributes can be set via a selector (_it can be useful for styling_), or you ca
119119
a[title='Title!'] {
120120
/* Specific attribute */
121121
--attr-href: './index.html';
122+
--data-attribute: 'Value';
122123

123124
/* And massively! */
124125
--attrs: 'target="_self" rel="noopener"';
125126
}
126127
```
127128

128129
```html
129-
<a title="Title!" href="./index.html" target="_self" rel="noopener"> </a>
130+
<a
131+
title="Title!"
132+
data-attribute="Value"
133+
href="./index.html"
134+
target="_self"
135+
rel="noopener"
136+
>
137+
</a>
130138
```
131139

132140
You can also add **text** to the tag via `--text` property:
@@ -148,15 +156,15 @@ div {
148156
--text: 'The text inside the div';
149157
}
150158
div span {
151-
--text: 'The text inside span';
159+
--text: ' The text inside span';
152160
--text-before: '| before';
153-
--text-after: '| after';
161+
--text-after: ' after';
154162
}
155163
```
156164

157165
```html
158166
<div>
159-
The text inside the div | before<span> The text inside span </span>| after
167+
The text inside the div | before<span> The text inside span</span> after
160168
</div>
161169
```
162170

@@ -242,7 +250,7 @@ new CssToHtml({
242250

243251
#### Formatting
244252

245-
Before giving you html, it is formatted by the [prettier](https://github.com/prettier/prettier-synchronized) library.
253+
Before giving you html, it is formatted by the [html-format](https://www.npmjs.com/package/html-format) library.
246254
You can either enable or disable formatting:
247255

248256
```js

elementOfHtml.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,17 @@ export class ElementOfHtml {
184184
}
185185
}
186186
#parseAttrVariable(declaration) {
187-
let declName = declaration.property.split('-')[2]
188-
let name, values
187+
let [type, name, value] = this.#getDeclarationData(declaration)
189188

190-
switch (declName) {
189+
switch (type) {
191190
case 'attr':
192191
case 'data':
193-
// Deleting the "--attr-" in a variable
194-
name = declaration.property.replace('--', '').replace('attr-', '')
195-
values = declaration.value ? '=' + declaration.value : ''
196-
return name + values
192+
if (!value) { return name }
197193

198-
case 'attrs':
199-
values = declaration.value.substring(1, declaration.value.length - 1)
194+
return name + `="${value}"`
200195

201-
return values
196+
case 'attrs':
197+
return value
202198
}
203199
}
204200

@@ -208,4 +204,28 @@ export class ElementOfHtml {
208204
#isInnerElement(element) {
209205
return element.parentSelector.startsWith(this.fullSelector)
210206
}
207+
#getDeclarationData(declaration) {
208+
let type = declaration.property.split('-')[2],
209+
name, value
210+
211+
if (declaration.property.includes('--attr')) {
212+
name = declaration.property.replace('--attr-', '')
213+
}
214+
else if (declaration.property.includes('--data')) {
215+
name = declaration.property.replace('--', '')
216+
}
217+
218+
// Removing nested quotes
219+
if (
220+
declaration.value[0] == '"' ||
221+
declaration.value[0] == "'" ||
222+
declaration.value[0] == '`'
223+
) {
224+
value = declaration.value.substring(1, declaration.value.length - 1)
225+
} else {
226+
value = declaration.value
227+
}
228+
229+
return [type, name, value]
230+
}
211231
}

0 commit comments

Comments
 (0)