-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(svg): introduce style tags parsing
This commit introduces the ability to parse CSS code within `<style>` tags in SVG files. It adds a new `SvgStyleNode` class to represent `<style>` elements and stores the parsed CSS as an Abstract Syntax Tree (AST) for later use.
- Loading branch information
1 parent
37969e3
commit 9b88ed2
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgStyleNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dev.tonholo.s2c.domain.svg | ||
|
||
import dev.tonholo.s2c.domain.xml.XmlNode | ||
import dev.tonholo.s2c.domain.xml.XmlParentNode | ||
import dev.tonholo.s2c.domain.xml.XmlTextNode | ||
import dev.tonholo.s2c.extensions.firstInstanceOf | ||
import dev.tonholo.s2c.lexer.css.CssTokenizer | ||
import dev.tonholo.s2c.lexer.css.CssTokenKind | ||
import dev.tonholo.s2c.parser.ast.AstParser | ||
import dev.tonholo.s2c.parser.ast.css.syntax.node.StyleSheet | ||
|
||
class SvgStyleNode( | ||
parent: XmlParentNode, | ||
override val children: MutableSet<XmlNode>, | ||
override val attributes: MutableMap<String, String>, | ||
) : SvgElementNode<SvgStyleNode>(parent, children, attributes, tagName = TAG_NAME), SvgNode { | ||
override val constructor = ::SvgStyleNode | ||
override val tagName: String = TAG_NAME | ||
val content: String by lazy { | ||
children | ||
.firstInstanceOf<XmlTextNode>() | ||
.content | ||
} | ||
private var _tree: StyleSheet? = null | ||
val tree: StyleSheet | ||
get() = checkNotNull(_tree) { | ||
"Css tree is not resolved. Did you forget to call `resolveTree()`?" | ||
} | ||
|
||
override fun toString(): String = toJsString { | ||
} | ||
|
||
internal fun resolveTree(parser: AstParser<CssTokenKind, StyleSheet>) { | ||
val tokens = CssTokenizer().tokenize(content).toList() | ||
_tree = parser.parse(tokens) | ||
} | ||
|
||
companion object { | ||
const val TAG_NAME = "style" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters