Skip to content

Commit 19ff3a7

Browse files
The processing of new lines has been improved.
Now new lines are not placed where they are not expected.
1 parent eb0fd2e commit 19ff3a7

File tree

6 files changed

+62
-40
lines changed

6 files changed

+62
-40
lines changed

cssToHtml.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,21 @@ export class CssToHtml {
104104
newContent += '\n'
105105

106106
for (let element of this.#elements) {
107-
newContent += element.string
107+
newContent += element.string + '\n'
108108
}
109109

110+
if (contentEndIndex == 0)
111+
// Deleting an extra new line at the end
112+
newContent = newContent.slice(0, -1)
113+
110114
newContent += this.#html.substring(contentEndIndex)
111115
}
112116
else {
113117
for (let element of this.#elements) {
114-
newContent += element.string
118+
newContent += element.string + '\n'
115119
}
120+
// Deleting an extra new line at the end
121+
newContent = newContent.slice(0, -1)
116122
}
117123

118124
if (this.format) {

elementOfHtml.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,24 @@ export class ElementOfHtml {
7777
this.#string += this.textBefore ? `${this.textBefore}` : ''
7878
}
7979
#addTextAfter() {
80-
this.#string += this.textAfter ? `${this.textAfter}\n` : '\n'
80+
this.#string += this.textAfter ? `${this.textAfter}` : ''
8181
}
8282
#addText() {
8383
this.#string += this.text ? `${this.text}` : ''
8484
}
8585
#addInnerElements() {
86-
if (this.innerElements.length > 0) {
86+
if (this.innerElements.length <= 0) return
87+
88+
if (!this.innerElements[0].textBefore) {
8789
this.#string += '\n'
8890
}
89-
for (let innerElement of this.innerElements) {
91+
92+
for (let innerElement of this.innerElements ?? []) {
9093
this.#string += innerElement.string
94+
95+
if (!innerElement.textAfter) {
96+
this.#string += '\n'
97+
}
9198
}
9299
}
93100
#createEndString() {
@@ -155,15 +162,16 @@ export class ElementOfHtml {
155162
#setTextFromComment(entryRule) {
156163
let commentWithText = entryRule.declarations.find(decl =>
157164
decl?.comment?.match(/{{.*}}/s)[0]
158-
)
165+
)?.comment
166+
159167
if (!commentWithText) return
160168

161169
// Removing spaces necessary for readability of a comment
162-
commentWithText.comment = commentWithText.comment.slice(1, -1)
170+
commentWithText = commentWithText.slice(1, -1)
163171

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)
172+
let textBefore = commentWithText.match(/^.*(?={{)/s)?.at(0)
173+
let text = commentWithText.match(/(?<={{).*?(?=}}|$)/s)?.at(0)
174+
let textAfter = commentWithText.match(/(?<=}}).*$/s)?.at(0)
167175

168176
this.textBefore = textBefore
169177
this.text = text

tests/IO.test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ const htmlFilePath = path.resolve(import.meta.dirname + '/helpers/test.html')
99
const cssFilePath = path.resolve(import.meta.dirname + '/helpers/test.css')
1010

1111
test('Writing code to a file should work.', () => {
12+
fs.writeFileSync(htmlFilePath, '')
13+
1214
new CssToHtml({
1315
css: `div {}`,
1416
write: { in: htmlFilePath }
1517
})
1618

17-
assert.equal(fs.readFileSync(htmlFilePath, 'utf8'), '<div></div>\n')
19+
assert.equal(fs.readFileSync(htmlFilePath, 'utf8'), '<div></div>')
1820
})
1921

2022
test('The code must be written after certain content.', () => {
2123
fs.writeFileSync(htmlFilePath,
2224
`<some-html-content>
23-
</some-html-content>
24-
`)
25+
</some-html-content>`)
26+
2527
new CssToHtml({
2628
css: `div {}`,
2729
write: {
@@ -40,8 +42,7 @@ test('The code must be written after certain content.', () => {
4042
test('The code must be written before and after certain content.', () => {
4143
fs.writeFileSync(htmlFilePath,
4244
`<some-html-content>
43-
</some-html-content>
44-
`)
45+
</some-html-content>`)
4546
new CssToHtml({
4647
css: `div {}`,
4748
write: {
@@ -54,8 +55,7 @@ test('The code must be written before and after certain content.', () => {
5455
assert.equal(fs.readFileSync(htmlFilePath, 'utf8'),
5556
`<some-html-content>
5657
<div></div>
57-
</some-html-content>
58-
`)
58+
</some-html-content>`)
5959
})
6060

6161
test('Empty CSS should not be processed.', () => {
@@ -80,8 +80,7 @@ test('The code from the CSS file must be processed.', () => {
8080

8181
`<div>
8282
<span>text</span>
83-
</div>
84-
`)
83+
</div>`)
8584
})
8685

8786

tests/attrs.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test('Attributes in the selector must be processed.', () => {
99
})
1010
.outputHTML,
1111

12-
'<div attr-1 data-attr role="button"></div>\n',
12+
'<div attr-1 data-attr role="button"></div>',
1313
)
1414
})
1515
test('Attributes in the rule must be processed.', () => {
@@ -25,6 +25,6 @@ div {
2525
})
2626
.outputHTML,
2727

28-
'<div href="#" role="button" data-attr="15" tabindex="0" data-v></div>\n',
28+
'<div href="#" role="button" data-attr="15" tabindex="0" data-v></div>',
2929
)
3030
})

tests/general.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ test('The code must be converted.', () => {
1414
<span></span>
1515
</div>
1616
<custom-tag></custom-tag>
17-
<input />
18-
`,
17+
<input />`,
1918
)
2019
})
2120
test('With formatting turned off, the code should look ugly.', () => {
@@ -48,7 +47,7 @@ test('The selector must be fully processed.', () => {
4847
})
4948
.outputHTML,
5049

51-
'<div id="some-id" class="some-class class2" data-attr></div>\n',
50+
'<div id="some-id" class="some-class class2" data-attr></div>',
5251
)
5352
})
5453

tests/text.test.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test('The text inside the tag must be processed', () => {
99
})
1010
.outputHTML,
1111

12-
`<div>inner text</div>\n`
12+
`<div>inner text</div>`
1313
)
1414
})
1515
test('The text before the tag must be processed', () => {
@@ -19,7 +19,7 @@ test('The text before the tag must be processed', () => {
1919
})
2020
.outputHTML,
2121

22-
`before text<div></div>\n`
22+
`before text<div></div>`
2323
)
2424
})
2525
test('The text after the tag must be processed', () => {
@@ -29,7 +29,7 @@ test('The text after the tag must be processed', () => {
2929
})
3030
.outputHTML,
3131

32-
`<div></div>after text\n`
32+
`<div></div>after text`
3333
)
3434
})
3535
test('The text without additional spaces should be saved without spaces.', () => {
@@ -39,46 +39,56 @@ test('The text without additional spaces should be saved without spaces.', () =>
3939
})
4040
.outputHTML,
4141

42-
`before text<div>inner text</div>after text\n`
42+
`before text<div>inner text</div>after text`
4343
)
4444
})
4545
test('The text with additional spaces should keep them.', () => {
46-
assert.match(
46+
assert.equal(
4747
new CssToHtml({
4848
css:
49-
`div { /* before text {{ inner text }} after text */ }`,
49+
`div {
50+
/* before text {{ inner text }} after text */
51+
}
52+
div span {
53+
/*
54+
before span {{ inner
55+
span }} after span
56+
*/
57+
}`,
5058
})
5159
.outputHTML,
5260

53-
/ +before text +<div> +inner text +<\/div> +after text[\n]/,
61+
` before text <div> inner text
62+
before span <span> inner
63+
span </span> after span
64+
</div> after text`,
5465
)
5566
})
67+
5668
test('The text should be processed along with the new lines.', () => {
5769
assert.equal(
5870
new CssToHtml({
5971
css: `
60-
div { /*
61-
before 1
62-
before 2
72+
div { /*
73+
before 1
74+
before 2
6375
{{
6476
inner 1
6577
inner 2
6678
inner 3
6779
}}after 1
68-
after 2
80+
after 2
6981
*/ }`,
7082
})
7183
.outputHTML,
7284

73-
`
74-
before 1
85+
` before 1
7586
before 2
7687
<div>
7788
inner 1
7889
inner 2
7990
inner 3
8091
</div>after 1
81-
after 2
82-
`,
92+
after 2`,
8393
)
84-
})
94+
})

0 commit comments

Comments
 (0)