Skip to content

Commit acc2bcd

Browse files
The work with a text has been changed.
Now the text can be added only through one comment. Now the text is set through comments, which have <@> and </@>
1 parent 01bdf4a commit acc2bcd

File tree

3 files changed

+51
-63
lines changed

3 files changed

+51
-63
lines changed

elementOfHtml.js

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class ElementOfHtml {
5656

5757
this.#setAttributes(entryRule)
5858
this.#setText(entryRule)
59-
this.#setTextFromComments(entryRule)
59+
this.#setTextFromComment(entryRule)
6060
this.#setTag()
6161
this.#setId()
6262
this.#setClasses()
@@ -152,37 +152,22 @@ export class ElementOfHtml {
152152
this.textBefore = this?.textBefore?.slice(1, -1)
153153
this.textAfter = this?.textAfter?.slice(1, -1)
154154
}
155-
#setTextFromComments(entryRule) {
156-
for (let decl of entryRule.declarations) {
157-
let textDeclarations = decl?.comment?.match(/@(inside|before|after)/gi)
158-
159-
if (!textDeclarations) continue
155+
#setTextFromComment(entryRule) {
156+
let commentWithText = entryRule.declarations.find(decl =>
157+
decl?.comment?.match(/<@>.*<\/@>/s)[0]
158+
)
159+
if (!commentWithText) return
160160

161-
for (let declType of textDeclarations) {
162-
let position = declType.toLowerCase().replace('@', '')
163-
let text = decl.comment
164-
// Pulling text only for a specific position
165-
.match(new RegExp(`(?<=${position}).*?(?=@|$)`, 's'))[0]
161+
// Removing spaces necessary for readability of a comment
162+
commentWithText.comment = commentWithText.comment.slice(1, -1)
166163

167-
// Removing spaces necessary for readability of a comment
168-
if (text.at(0) == ' ')
169-
text = text.replace(' ', '')
170-
if (text.at(-1) == ' ')
171-
text = text.replace(/.$/, '')
164+
let textBefore = commentWithText.comment.match(/^.*(?=<@>)/s)?.at(0)
165+
let text = commentWithText.comment.match(/(?<=<@>).*?(?=<\/@>|$)/s)?.at(0)
166+
let textAfter = commentWithText.comment.match(/(?<=<\/@>).*$/s)?.at(0)
172167

173-
switch (position) {
174-
case 'inside':
175-
this.text = text
176-
break
177-
case 'before':
178-
this.textBefore = text
179-
break
180-
case 'after':
181-
this.textAfter = text
182-
break
183-
}
184-
}
185-
}
168+
this.textBefore = textBefore
169+
this.text = text
170+
this.textAfter = textAfter
186171
}
187172
#setParentAndSelfSelector(fullSelector) {
188173
let partsOfSelector = fullSelector.split(' ')

tests/IO.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ test('Empty CSS should not be processed.', () => {
7171
})
7272

7373
test('The code from the CSS file must be processed.', () => {
74-
fs.writeFileSync(cssFilePath, 'div {} div span { /* @inside text */ }')
74+
fs.writeFileSync(cssFilePath, 'div {} div span { /* <@>text</@> */ }')
7575

7676
assert.equal(
7777
new CssToHtml({

tests/text.test.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,76 @@ import { CssToHtml } from '../cssToHtml.js'
22
import assert from 'assert'
33
import test from 'node:test'
44

5-
test('The text without additional spaces should be saved without spaces.', () => {
5+
test('The text inside the tag must be processed', () => {
66
assert.equal(
77
new CssToHtml({
8-
css:
9-
`div { /*
10-
@before before text @inside inner text @after after text
11-
*/ }`,
8+
css: `div { /* <@>inner text</@> */ }`,
129
})
1310
.outputHTML,
1411

15-
`before text<div>inner text</div>after text
16-
`
12+
`<div>inner text</div>\n`
1713
)
1814
})
19-
test('The text with additional spaces should keep them.', () => {
20-
assert.match(
15+
test('The text before the tag must be processed', () => {
16+
assert.equal(
2117
new CssToHtml({
22-
css:
23-
`div { /*
24-
@before before text @inside inner text @after after text
25-
*/ }`,
18+
css: `div { /* before text<@></@> */ }`,
2619
})
2720
.outputHTML,
2821

29-
/ +before text +<div> +inner text +<\/div> +after text[\n]/,
22+
`before text<div></div>\n`
3023
)
3124
})
32-
test('The text should be saved from different comments.', () => {
25+
test('The text after the tag must be processed', () => {
3326
assert.equal(
27+
new CssToHtml({
28+
css: `div { /* <@></@>after text */ }`,
29+
})
30+
.outputHTML,
31+
32+
`<div></div>after text\n`
33+
)
34+
})
35+
test('The text without additional spaces should be saved without spaces.', () => {
36+
assert.equal(
37+
new CssToHtml({
38+
css: `div { /* before text<@>inner text</@>after text */ }`,
39+
})
40+
.outputHTML,
41+
42+
`before text<div>inner text</div>after text\n`
43+
)
44+
})
45+
test('The text with additional spaces should keep them.', () => {
46+
assert.match(
3447
new CssToHtml({
3548
css:
36-
`div { /*
37-
@before before text @inside inner text @after after text
38-
*/
39-
/*
40-
@inside inner text
41-
*/
42-
/*
43-
@after after text
44-
*/
45-
}`,
49+
`div { /* before text <@> inner text </@> after text */ }`,
4650
})
4751
.outputHTML,
4852

49-
`before text<div>inner text
50-
</div>after text
51-
`,
53+
/ +before text +<div> +inner text +<\/div> +after text[\n]/,
5254
)
5355
})
5456
test('The text should be processed along with the new lines.', () => {
5557
assert.equal(
5658
new CssToHtml({
5759
css: `
5860
div { /*
59-
@before before 1
61+
before 1
6062
before 2
61-
@inside
63+
<@>
6264
inner 1
6365
inner 2
64-
inner 3
65-
@after after 1
66+
inner 3
67+
</@>after 1
6668
after 2
6769
*/ }`,
6870
})
6971
.outputHTML,
7072

71-
`before 1
73+
`
74+
before 1
7275
before 2
7376
<div>
7477
inner 1

0 commit comments

Comments
 (0)