aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-04-18 00:34:33 +0200
committerbptato <nincsnevem662@gmail.com>2025-04-18 00:34:33 +0200
commitb9ca0d24c99c3b7c07640ad7e3dc7654318da0dc (patch)
treef03f2b52d1e31a9e0579c1fb2f5b9f095539715a
parentb01220178576ee4454334c25f57963350eb11e2c (diff)
downloadchawan-b9ca0d24c99c3b7c07640ad7e3dc7654318da0dc.tar.gz
selectorparser: check type flag of hash tokens
-rw-r--r--src/css/cssparser.nim14
-rw-r--r--src/css/selectorparser.nim2
-rw-r--r--test/layout/id-selector-hash-token-type-flag.color.expected3
-rw-r--r--test/layout/id-selector-hash-token-type-flag.html9
4 files changed, 18 insertions, 10 deletions
diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim
index f5c47ffd..12e6c87b 100644
--- a/src/css/cssparser.nim
+++ b/src/css/cssparser.nim
@@ -12,15 +12,12 @@ type
cttCdc, cttColon, cttSemicolon, cttComma, cttRbracket, cttLbracket,
cttLparen, cttRparen, cttLbrace, cttRbrace
- tflaga = enum
- tflagaUnrestricted, tflagaId
-
CSSComponentValue* = ref object of RootObj
CSSToken* = ref object of CSSComponentValue
case t*: CSSTokenType
of cttIdent, cttFunction, cttAtKeyword, cttHash, cttString, cttUrl:
- tflaga*: tflaga
+ validId*: bool # type flag; "unrestricted" -> false, "id" -> true
value*: string
of cttDelim:
cvalue*: char
@@ -174,7 +171,7 @@ proc startsWithIdentSequence(iq: openArray[char]; n: int): bool =
return false
case iq[n]
of '-':
- return n + 1 < iq.len and iq[n] in IdentStart + {'-'} or
+ return n + 1 < iq.len and iq[n + 1] in IdentStart + {'-'} or
n + 2 < iq.len and iq[n + 1] == '\\' and iq[n + 2] != '\n'
of IdentStart:
return true
@@ -372,14 +369,11 @@ proc consumeToken(iq: openArray[char]; n: var int): CSSToken =
of '#':
if n < iq.len and iq[n] in Ident or
n + 1 < iq.len and iq[n] == '\\' and iq[n + 1] != '\n':
- let flag = if iq.startsWithIdentSequence(n):
- tflagaId
- else:
- tflagaUnrestricted
+ let validId = iq.startsWithIdentSequence(n)
return CSSToken(
t: cttHash,
value: iq.consumeIdentSequence(n),
- tflaga: flag
+ validId: validId
)
else:
dec n
diff --git a/src/css/selectorparser.nim b/src/css/selectorparser.nim
index 2b1fcb53..1cd8a2a1 100644
--- a/src/css/selectorparser.nim
+++ b/src/css/selectorparser.nim
@@ -446,6 +446,8 @@ proc parseCompoundSelector(state: var SelectorParser): CompoundSelector =
result.add(state.parsePseudoSelector())
of cttHash:
inc state.at
+ if not tok.validId:
+ fail
let id = tok.value.toAtomLower()
result.add(Selector(t: stId, id: id))
of cttComma: break
diff --git a/test/layout/id-selector-hash-token-type-flag.color.expected b/test/layout/id-selector-hash-token-type-flag.color.expected
new file mode 100644
index 00000000..c2feb85e
--- /dev/null
+++ b/test/layout/id-selector-hash-token-type-flag.color.expected
@@ -0,0 +1,3 @@
+test
+test
+test
diff --git a/test/layout/id-selector-hash-token-type-flag.html b/test/layout/id-selector-hash-token-type-flag.html
new file mode 100644
index 00000000..55ba1568
--- /dev/null
+++ b/test/layout/id-selector-hash-token-type-flag.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<style>
+#99 { color: red } /* wrong */
+#-- { color: blue } /* valid */
+#- { color: green } /* wrong */
+</style>
+<div id=99>test</div>
+<div id=-->test</div>
+<div id=->test</div>