fix parsing comments

This commit is contained in:
Will McGugan
2021-10-03 21:42:09 +01:00
parent 01bb02c84f
commit f7ee796991
3 changed files with 8 additions and 3 deletions

View File

@@ -93,7 +93,8 @@ def parse(css: str) -> Iterable[RuleSet]:
if __name__ == "__main__":
test = """
.foo.bar baz:focus, #egg {
display: block
/* ignore me, I'm a comment */
display: block;
visibility: visible;
border: solid green !important;
outline: red;
@@ -103,7 +104,10 @@ if __name__ == "__main__":
from .stylesheet import Stylesheet
print(test)
print()
stylesheet = Stylesheet()
stylesheet.parse(test)
print(stylesheet)
print()
print(stylesheet.css)

View File

@@ -180,7 +180,7 @@ class Styles:
yield "display", self.display, "block"
yield "visibility", self.visibility, "visible"
yield "padding", self.padding, NULL_SPACING
yield "margin", self.padding, NULL_SPACING
yield "margin", self.margin, NULL_SPACING
yield "border_top", self.border_top, None
yield "border_right", self.border_right, None
@@ -274,7 +274,6 @@ class Styles:
raise DeclarationError(name, token, f"{msg} (line {line + 1}, col {col + 1})")
def add_declaration(self, declaration: Declaration) -> None:
if not declaration.tokens:
return
process_method = getattr(self, f"process_{declaration.name.replace('-', '_')}")

View File

@@ -76,9 +76,11 @@ def tokenize(code: str) -> Iterable[Token]:
get_state = _STATES.get
while True:
token = get_token(expect)
name = token.name
if name == "comment_start":
tokenizer.skip_to(expect_comment_end)
continue
elif name == "eof":
break
expect = get_state(name, expect)