Skip to content

Commit 15d2d1a

Browse files
Now can add text via comments.
1 parent 229f339 commit 15d2d1a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

elementOfHtml.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class ElementOfHtml {
5656

5757
this.#setAttributes(entryRule)
5858
this.#setText(entryRule)
59+
this.#setTextFromComments(entryRule)
5960
this.#setTag()
6061
this.#setId()
6162
this.#setClasses()
@@ -73,7 +74,7 @@ export class ElementOfHtml {
7374
endOfString
7475
}
7576
#addTextBefore() {
76-
this.#string += this.textBefore ? ` ${this.textBefore}` : ''
77+
this.#string += this.textBefore ? `${this.textBefore}` : ''
7778
}
7879
#addTextAfter() {
7980
this.#string += this.textAfter ? `${this.textAfter}\n` : '\n'
@@ -136,6 +137,8 @@ export class ElementOfHtml {
136137
decl => decl.property == '--text'
137138
)?.value
138139

140+
if (!this.text) return
141+
139142
// Removing extra quotes
140143
let splittedText = this.text
141144
.split(',')
@@ -163,6 +166,32 @@ export class ElementOfHtml {
163166
)
164167
?.value.slice(1, -1)
165168
}
169+
#setTextFromComments(entryRule) {
170+
const replaceRegexp = / {1,}text(|-(before|after)): ?/
171+
172+
for (let decl of entryRule.declarations) {
173+
let textDeclaration = decl?.comment?.match(/text-(before|after):|text:/i)?.at(0)
174+
175+
if (!textDeclaration) continue
176+
177+
let position = textDeclaration.toLowerCase().replace(':', '')
178+
let text = decl.comment.replace(replaceRegexp, '')
179+
// Removing one mandatory space at the end
180+
.slice(0, -1)
181+
182+
switch (position) {
183+
case 'text':
184+
this.text = text
185+
break
186+
case 'text-before':
187+
this.textBefore = text
188+
break
189+
case 'text-after':
190+
this.textAfter = text
191+
break
192+
}
193+
}
194+
}
166195
#setParentAndSelfSelector(fullSelector) {
167196
let partsOfSelector = fullSelector.split(' ')
168197

0 commit comments

Comments
 (0)