Skip to content

Commit f3bfa36

Browse files
committed
fix: style and inline style printing
1 parent 3e60a5b commit f3bfa36

File tree

1 file changed

+95
-95
lines changed

1 file changed

+95
-95
lines changed

src/twcss-to-sass.ts

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const defaultOptions: ITwToSassOptions = {
4545

4646
let _defaultOptions: ITwToSassOptions = defaultOptions
4747

48+
let styles: IHtmlNode[] = []
49+
4850
/**
4951
* Get style and class attributes
5052
*
@@ -103,8 +105,6 @@ function getStyleContents(element: IHtmlNode): IHtmlNode {
103105
})
104106
}
105107

106-
let styles: IHtmlNode[] = []
107-
108108
/**
109109
* Filter IHtmlNode array by node type and tagName
110110
*
@@ -119,51 +119,42 @@ function filterHtmlData(nodeTree: IHtmlNode[], deepth = 0): IHtmlNode[] {
119119
(x: IHtmlNode) => x.content !== ' ' && x.tagName != '!doctype'
120120
)
121121

122-
if (nodeTree && nodeTree.length) {
123-
const elementList: IHtmlNode[] | null = []
124-
125-
nodeTree.forEach((node: IHtmlNode, index) => {
126-
if (node.type == 'element') {
127-
if (node.tagName == 'style') {
128-
styles.push(getStyleContents(node))
129-
} else {
130-
node.filterAttributes = getAttributes(node.attributes, [
131-
'class',
132-
'style',
133-
])
134-
}
135-
136-
// find element's comment in previous node
137-
node.comment =
138-
nodeTree[index - 1] && nodeTree[index - 1].type == 'comment'
139-
? Utils.cleanText(nodeTree[index - 1].content, true)
140-
: null
122+
nodeTree.forEach((node: IHtmlNode, index) => {
123+
if (node.type == 'element') {
124+
if (node.tagName == 'style') {
125+
styles.push(getStyleContents(node))
126+
} else {
127+
node.filterAttributes = getAttributes(node.attributes, [
128+
'class',
129+
'style',
130+
])
141131
}
142132

143-
// allow only html elements and texts
144-
if (node.type != 'comment' && node.tagName !== 'style') {
145-
elementList.push(node)
146-
}
133+
// find element's comment in previous node
134+
node.comment =
135+
nodeTree[index - 1] && nodeTree[index - 1].type == 'comment'
136+
? Utils.cleanText(nodeTree[index - 1].content, true)
137+
: null
138+
}
147139

148-
// let's go deeper
149-
if (Array.isArray(node.children) && node.children.length > 0) {
150-
node.order = ++deepth
140+
// let's go deeper
141+
if (Array.isArray(node.children) && node.children.length > 0) {
142+
node.order = ++deepth
151143

152-
const childNodes: IHtmlNode[] = filterHtmlData(node.children, deepth)
144+
const childNodes: IHtmlNode[] = filterHtmlData(node.children, deepth)
153145

154-
if (childNodes && childNodes.length) {
155-
node.children = childNodes.filter(
156-
(x: IHtmlNode) => x.tagName != 'style' && x.type != 'comment'
157-
)
146+
if (childNodes && childNodes.length) {
147+
node.children = childNodes.filter(
148+
(x: IHtmlNode) => x.tagName != 'style' && x.type != 'comment'
149+
)
158150

159-
node.hasElementChildren =
160-
node.children.filter((x) => x.type == 'element').length > 0
161-
}
151+
node.hasElementChildren =
152+
node.children.filter((x) => x.type == 'element').length > 0
162153
}
163-
})
154+
}
155+
})
164156

165-
return nodeTree
166-
}
157+
return nodeTree
167158
}
168159

169160
return []
@@ -211,6 +202,27 @@ function getClassName(node: IHtmlNode, deepth: number): string {
211202
return className
212203
}
213204

205+
/**
206+
* Get CSS class name from node details
207+
*
208+
* @param styles IHtmlNode[]
209+
*
210+
* @returns string
211+
*/
212+
function getCssTree(cssTree: IHtmlNode[]): string {
213+
let css = ''
214+
215+
if (styles.length > 0) {
216+
cssTree.forEach((style: IHtmlNode, index) => {
217+
css += `// #region Style Group ${index + 1}\n\n`
218+
css += `${style.content}\n`
219+
css += `// #endregion\n\n`
220+
})
221+
}
222+
223+
return css
224+
}
225+
214226
/**
215227
* Extract SASS tree from HTML JSON tree
216228
*
@@ -219,70 +231,54 @@ function getClassName(node: IHtmlNode, deepth: number): string {
219231
*
220232
* @returns string
221233
*/
222-
function getSassTree(nodeTree: IHtmlNode[] | IHtmlNode, deepth = 0) {
223-
if (nodeTree) {
224-
let styleCount = 0
234+
function getSassTree(nodeTree: IHtmlNode[], deepth = 0) {
235+
return nodeTree
236+
.map((node: IHtmlNode) => {
237+
let treeString = '',
238+
subTreeString = ''
225239

226-
if (!Array.isArray(nodeTree)) {
227-
nodeTree = nodeTree.children
228-
}
229-
230-
return nodeTree
231-
.map((node: IHtmlNode) => {
232-
let treeString = '',
233-
subTreeString = ''
240+
if (Array.isArray(node.children) && node.children.length) {
241+
++deepth
234242

235-
if (Array.isArray(node.children) && node.children.length) {
236-
++deepth
243+
subTreeString = getSassTree(node.children, deepth)
244+
}
237245

238-
subTreeString = getSassTree(node, deepth)
246+
if (node.filterAttributes) {
247+
// print tailwind class names
248+
if (node.filterAttributes.class) {
249+
treeString += node.filterAttributes.class
250+
? `@apply ${node.filterAttributes.class};`
251+
: ''
239252
}
240253

241-
if (node.tagName == 'style' && node.filterAttributes) {
242-
styleCount += 1
243-
244-
let result = `// #region STYLE #${styleCount}\n`
245-
result += `\n${node.filterAttributes.style}\n`
246-
result += '// #endregion\n\n'
254+
// inline style printing
255+
if (node.filterAttributes.style) {
256+
node.filterAttributes.style = Utils.addMissingSuffix(
257+
node.filterAttributes.style,
258+
';'
259+
)
247260

248-
return result
249-
} else {
250-
if (node.filterAttributes) {
251-
if (node.filterAttributes.class) {
252-
treeString += node.filterAttributes.class
253-
? `@apply ${node.filterAttributes.class};`
254-
: ''
255-
}
256-
257-
if (node.filterAttributes.style) {
258-
node.filterAttributes.style = Utils.addMissingSuffix(
259-
node.filterAttributes.style,
260-
';'
261-
)
262-
263-
treeString += node.filterAttributes.style
264-
? `\n${node.filterAttributes.style}\n`
265-
: ''
266-
}
267-
}
261+
treeString += node.filterAttributes.style
262+
? `\n${node.filterAttributes.style}\n`
263+
: ''
264+
}
265+
}
268266

269-
if (treeString.length || subTreeString.length) {
270-
const classComment = _defaultOptions.printComments
271-
? `/* ${node.comment ? node.comment : node.tagName} -> ${
272-
node.order
273-
} */`
274-
: ''
267+
if (treeString.length || subTreeString.length) {
268+
const classComment = _defaultOptions.printComments
269+
? `/* ${node.comment ? node.comment : node.tagName} -> ${
270+
node.order
271+
} */`
272+
: ''
275273

276-
const className = getClassName(node, deepth)
274+
const className = getClassName(node, deepth)
277275

278-
return `${classComment}${className}{${treeString}${subTreeString}}`
279-
}
280-
}
276+
return `${classComment}${className}{${treeString}${subTreeString}}`
277+
}
281278

282-
return null
283-
})
284-
.join('')
285-
}
279+
return null
280+
})
281+
.join('')
286282

287283
return ''
288284
}
@@ -408,11 +404,15 @@ export function convertToSass(
408404
const filteredHtmlData = filterHtmlData(htmlJson)
409405

410406
if (filteredHtmlData) {
411-
const sassTreeResult = getSassTree(filteredHtmlData)
412-
let htmlTreeResult = ''
407+
let sassTreeResult = getSassTree(filteredHtmlData),
408+
htmlTreeResult = ''
413409

414410
if (sassTreeResult) {
415411
htmlTreeResult = getHtmlTree(filteredHtmlData)
412+
413+
const cssTreeResult = getCssTree(styles)
414+
415+
sassTreeResult = cssTreeResult + sassTreeResult
416416
}
417417

418418
// export with formatted output

0 commit comments

Comments
 (0)