diff options
author | bptato <nincsnevem662@gmail.com> | 2023-06-25 14:45:04 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-06-25 14:45:04 +0200 |
commit | 36b06265715bebbfaca831e517b547c87054e58c (patch) | |
tree | 526ae7867e8dbb295b93d52f57133a61ee846d79 | |
parent | bd0d0f9185fabf887202a39f9dd823c6c511f839 (diff) | |
download | chawan-36b06265715bebbfaca831e517b547c87054e58c.tar.gz |
cssparser: remove SyntaxError
return DOMException instead
-rw-r--r-- | src/css/cssparser.nim | 47 | ||||
-rw-r--r-- | src/html/dom.nim | 11 |
2 files changed, 34 insertions, 24 deletions
diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim index 57cfc87e..222ad92d 100644 --- a/src/css/cssparser.nim +++ b/src/css/cssparser.nim @@ -3,6 +3,8 @@ import streams import sugar import unicode +import js/exception +import utils/opt import utils/twtstr type @@ -74,8 +76,6 @@ type CSSAnB* = tuple[A, B: int] - SyntaxError = object of ValueError - # For debugging proc `$`*(c: CSSParsedItem): string = if c of CSSToken: @@ -519,7 +519,7 @@ proc consumeSimpleBlock(state: var CSSParseState): CSSSimpleBlock = of CSS_LBRACE_TOKEN: ending = CSS_RBRACE_TOKEN of CSS_LPAREN_TOKEN: ending = CSS_RPAREN_TOKEN of CSS_LBRACKET_TOKEN: ending = CSS_RBRACKET_TOKEN - else: raise newException(Exception, "Parse error!") + else: doAssert false result = CSSSimpleBlock(token: t) while state.at < state.tokens.len: @@ -717,45 +717,49 @@ proc parseListOfRules*(cvals: seq[CSSComponentValue]): seq[CSSRule] = CSSParsedItem(cval) return state.parseListOfRules() -proc parseRule(state: var CSSParseState): CSSRule = +proc parseRule(state: var CSSParseState): Result[CSSRule, DOMException] = while state.has() and state.peek() == CSS_WHITESPACE_TOKEN: discard state.consume() if not state.has(): - raise newException(SyntaxError, "EOF reached!") + return err(newDOMException("Unexpected EOF", "SyntaxError")) + var res: CSSRule if state.peek() == CSS_AT_KEYWORD_TOKEN: - result = state.consumeAtRule() + res = state.consumeAtRule() else: let q = state.consumeQualifiedRule() if q.isSome: - result = q.get + res = q.get else: - raise newException(SyntaxError, "No qualified rule found!") + return err(newDOMException("No qualified rule found!", "SyntaxError")) while state.has() and state.peek() == CSS_WHITESPACE_TOKEN: discard state.consume() if state.has(): - raise newException(SyntaxError, "EOF not reached!") + return err(newDOMException("EOF not reached", "SyntaxError")) + return ok(res) -proc parseRule(inputStream: Stream): CSSRule = +proc parseRule*(inputStream: Stream): Result[CSSRule, DOMException] = var state = CSSParseState() state.tokens = tokenizeCSS(inputStream) return state.parseRule() -proc parseDeclaration(state: var CSSParseState): CSSDeclaration = +proc parseDeclaration(state: var CSSParseState): + Result[CSSDeclaration, DOMException] = while state.has() and state.peek() == CSS_WHITESPACE_TOKEN: discard state.consume() if not state.has() or state.peek() != CSS_IDENT_TOKEN: - raise newException(SyntaxError, "No ident token found!") + return err(newDOMException("No ident token found", "SyntaxError")) let d = state.consumeDeclaration() if d.isSome: - return d.get + return ok(d.get) - raise newException(SyntaxError, "No declaration found!") + return err(newDOMException("No declaration found", "SyntaxError")) -proc parseDeclaration*(inputStream: Stream): CSSDeclaration = +proc parseDeclaration*(inputStream: Stream): + Result[CSSDeclaration, DOMException] = var state = CSSParseState() state.tokens = tokenizeCSS(inputStream) return state.parseDeclaration() @@ -790,20 +794,23 @@ proc parseListOfDeclarations2*(inputStream: Stream): seq[CSSDeclaration] = state.tokens = tokenizeCSS(inputStream) return state.parseListOfDeclarations2() -proc parseComponentValue(state: var CSSParseState): CSSComponentValue = +proc parseComponentValue(state: var CSSParseState): + Result[CSSComponentValue, DOMException] = while state.has() and state.peek() == CSS_WHITESPACE_TOKEN: discard state.consume() if not state.has(): - raise newException(SyntaxError, "EOF reached!") + return err(newDOMException("Unexpected EOF", "SyntaxError")) - result = state.consumeComponentValue() + let res = state.consumeComponentValue() while state.has() and state.peek() == CSS_WHITESPACE_TOKEN: discard state.consume() if state.has(): - raise newException(SyntaxError, "EOF not reached!") + return err(newDOMException("EOF not reached", "SyntaxError")) + return ok(res) -proc parseComponentValue*(inputStream: Stream): CSSComponentValue = +proc parseComponentValue*(inputStream: Stream): + Result[CSSComponentValue, DOMException] = var state: CSSParseState state.tokens = tokenizeCSS(inputStream) return state.parseComponentValue() diff --git a/src/html/dom.nim b/src/html/dom.nim index 4d71c3a3..16335399 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -1581,10 +1581,13 @@ func formmethod*(element: Element): FormMethod = return FORM_METHOD_GET proc parseColor(element: Element, s: string): RGBAColor = - return cssColor(parseComponentValue(newStringStream(s))) - #TODO TODO TODO return element style - # For now we just use white. - .get(rgb(255, 255, 255)) + let cval = parseComponentValue(newStringStream(s)) + #TODO TODO TODO return element style + # For now we just use white. + let ec = rgb(255, 255, 255) + if cval.isErr: + return ec + return cssColor(cval.get).get(ec) #TODO ?? func target0*(element: Element): string = |