-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcssToHtml.js
164 lines (136 loc) · 3.99 KB
/
cssToHtml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import fs from 'fs-extra'
import path from 'path'
import format from "html-format"
import { parse } from '@adobe/css-tools'
import { createParser } from 'css-selector-parser'
import { ElementOfHtml } from './elementOfHtml.js'
export class CssToHtml {
static ENCODING = 'utf8'
static SELECTOR_PARSER = createParser({ syntax: 'progressive' })
static UNACCEPTABLE_SELECTORS = [
'WildcardTag',
'PseudoElement',
'PseudoClass',
':',
'*',
'+',
'>',
'||',
'~',
'|',
]
#pathToHTML
#html
#css
#astRules
#elements = []
#writeBefore
#writeAfter
#writeInFile = false
outputHTML
constructor({ css, write, format = true, }) {
this.#css = css
this.format = format
if (write?.in) {
this.#pathToHTML = path.normalize(write.in)
if (!fs.existsSync(this.#pathToHTML)) {
console.error(`The ${this.#pathToHTML} file was not found, so it will be created.`)
fs.createFileSync(this.#pathToHTML)
}
this.#writeInFile = true
this.#html = fs.readFileSync(this.#pathToHTML, CssToHtml.ENCODING)
this.#writeAfter = write?.after
this.#writeBefore = write?.before
}
let astRules = parse(this.#css).stylesheet.rules
if (!astRules.length)
return
this.#filterAstRules(astRules)
this.#initHTMLElements()
this.#createHTMLStructure()
if (!this.#elements.length) return
this.outputHTML = this.#prepareHtml()
if (this.#writeInFile) {
fs.writeFileSync(write.in, this.outputHTML)
}
}
#filterAstRules(astRules) {
this.#astRules = astRules.filter(
rule => {
if (
rule.type != 'rule' ||
this.#containsUnacceptableSelector(rule.selectors[0])
)
return false
return true
}
)
}
#initHTMLElements() {
for (let rule of this.#astRules) {
this.#elements.push(new ElementOfHtml(rule, rule.selectors[0]))
}
}
#createHTMLStructure() {
for (let i = 0; i < this.#elements.length; i++) {
this.#elements[i].searchInnerElements(this.#elements, i)
}
this.#elements = this.#elements.filter(el => el.parentSelector == '')
}
#prepareHtml() {
let newContent = ''
let [contentStartIndex, contentEndIndex] = this.#getWritingStartAndEndIndex()
if (this.#writeInFile) {
newContent = this.#html.substring(0, contentStartIndex)
if (contentStartIndex)
newContent += '\n'
for (let element of this.#elements) {
newContent += element.string + '\n'
}
if (contentEndIndex == 0)
// Deleting an extra new line at the end
newContent = newContent.slice(0, -1)
newContent += this.#html.substring(contentEndIndex)
}
else {
for (let element of this.#elements) {
newContent += element.string + '\n'
}
// Deleting an extra new line at the end
newContent = newContent.slice(0, -1)
}
if (this.format) {
try {
return format(newContent, ' ')
}
catch (error) {
throw new Error(
`An error occurred during formatting, check your code!
Perhaps this happened because the code that was converted turned out to be incorrect.`
)
}
} else {
return newContent
}
}
#containsUnacceptableSelector(selector) {
return CssToHtml.UNACCEPTABLE_SELECTORS.some(
unSelector => selector.includes(unSelector)
)
}
#getWritingStartAndEndIndex() {
if (!this.#writeInFile) return [0, 0]
let contentStartIndex = 0, contentEndIndex = this.#html.length
if (this.#writeAfter) {
contentStartIndex = this.#html.indexOf(this.#writeAfter)
}
if (this.#writeBefore) {
contentEndIndex = this.#html.lastIndexOf(this.#writeBefore)
}
if (contentStartIndex == -1 || contentEndIndex == -1) {
throw new Error(`contentStartIndex or contentEndIndex was not found in the file ${this.#pathToHTML}!`)
}
contentStartIndex += this.#writeAfter?.length ?? 0
return [contentStartIndex, contentEndIndex]
}
}