diff options
author | bptato <nincsnevem662@gmail.com> | 2022-11-25 18:42:27 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-11-25 18:42:27 +0100 |
commit | 7ab7f28fdefe503fdde53ba9e253e308cb06b44f (patch) | |
tree | cd4cc561061c673999f9e8ecf1063a0664ba4dae /src/css | |
parent | d205ef228866cad35891ca96cacae547c02fd4ae (diff) | |
download | chawan-7ab7f28fdefe503fdde53ba9e253e308cb06b44f.tar.gz |
Add some quirks mode rules + presentational hints
Only as a POC for now.
Diffstat (limited to 'src/css')
-rw-r--r-- | src/css/cascade.nim | 44 | ||||
-rw-r--r-- | src/css/values.nim | 23 |
2 files changed, 56 insertions, 11 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim index bb4872fd..bc8db4d7 100644 --- a/src/css/cascade.nim +++ b/src/css/cascade.nim @@ -1,4 +1,5 @@ import algorithm +import options import streams import sugar @@ -11,6 +12,7 @@ import css/stylednode import css/values import html/dom import html/tags +import types/color type DeclarationList* = array[PseudoElem, seq[CSSDeclaration]] @@ -68,6 +70,44 @@ func calcRules(styledNode: StyledNode, sheet: CSSStylesheet): DeclarationList = for item in tosorts[i]: for dl in item[1]: dl + +func calcPresentationalHints(element: Element): CSSComputedValues = + template set_cv(a, b, c: untyped) = + if result == nil: + new(result) + result[a] = CSSComputedValue(t: a, v: ValueTypes[a], b: c) + template map_width = + let s = parseDimensionValues(element.attr("width")) + if s.isSome: + set_cv(PROPERTY_WIDTH, length, s.get) + template map_height = + let s = parseDimensionValues(element.attr("height")) + if s.isSome: + set_cv(PROPERTY_HEIGHT, length, s.get) + template map_width_nozero = + let s = parseDimensionValues(element.attr("width")) + if s.isSome and s.get.num != 0: + set_cv(PROPERTY_WIDTH, length, s.get) + template map_height_nozero = + let s = parseDimensionValues(element.attr("height")) + if s.isSome and s.get.num != 0: + set_cv(PROPERTY_HEIGHT, length, s.get) + template map_bgcolor = + let c = parseLegacyColor(element.attr("bgcolor")) + if c.isSome: + set_cv(PROPERTY_BACKGROUND_COLOR, color, c.get) + + case element.tagType + of TAG_TABLE, TAG_TD, TAG_TH: + map_height_nozero + map_width_nozero + map_bgcolor + of TAG_THEAD, TAG_TBODY, TAG_TFOOT, TAG_TR: + map_height + map_bgcolor + of TAG_COL: + map_width + else: discard proc applyDeclarations(styledNode: StyledNode, parent: CSSComputedValues, ua, user: DeclarationList, author: seq[DeclarationList]) = let pseudo = PSEUDO_NONE @@ -78,10 +118,12 @@ proc applyDeclarations(styledNode: StyledNode, parent: CSSComputedValues, ua, us for rule in author: builder.addValues(rule[pseudo], ORIGIN_AUTHOR) if styledNode.node != nil: - let style = Element(styledNode.node).attr("style") + let element = Element(styledNode.node) + let style = element.attr("style") if style.len > 0: let inline_rules = newStringStream(style).parseListOfDeclarations2() builder.addValues(inline_rules, ORIGIN_AUTHOR) + builder.preshints = element.calcPresentationalHints() styledNode.computed = builder.buildComputedValues() diff --git a/src/css/values.nim b/src/css/values.nim index 67200532..1e82fe3c 100644 --- a/src/css/values.nim +++ b/src/css/values.nim @@ -87,9 +87,7 @@ type unit*: CSSUnit auto*: bool - CSSColor* = object - rgba*: RGBAColor - termcolor: int + CSSColor* = RGBAColor CSSVerticalAlign* = object length*: CSSLength @@ -143,6 +141,7 @@ type parent: CSSComputedValues normalProperties: array[CSSOrigin, CSSComputedValueBuilders] importantProperties: array[CSSOrigin, CSSComputedValueBuilders] + preshints*: CSSComputedValues CSSValueError* = object of ValueError @@ -177,7 +176,7 @@ const PropertyNames = { "background-color": PROPERTY_BACKGROUND_COLOR, }.toTable() -const ValueTypes = [ +const ValueTypes* = [ PROPERTY_NONE: VALUE_NONE, PROPERTY_ALL: VALUE_NONE, PROPERTY_COLOR: VALUE_COLOR, @@ -293,8 +292,8 @@ func listMarker*(t: CSSListStyleType, i: int): string = const Colors: Table[string, CSSColor] = ((func (): Table[string, CSSColor] = for name, rgb in ColorsRGB: - result[name] = CSSColor(rgba: rgb) - result["transparent"] = CSSColor(rgba: rgba(0x00, 0x00, 0x00, 0x00)) + result[name] = CSSColor(rgb) + result["transparent"] = CSSColor(rgba(0x00, 0x00, 0x00, 0x00)) )()) const Units = { @@ -346,10 +345,10 @@ func parseDimensionValues*(s: string): Option[CSSLength] = return some(CSSLength(num: n, unit: UNIT_PX)) func color(r, g, b: int): CSSColor = - return CSSColor(rgba: rgba(r, g, b, 256)) + return CSSColor(rgba(r, g, b, 256)) func color(r, g, b, a: int): CSSColor = - return CSSColor(rgba: rgba(r, g, b, a)) + return CSSColor(rgba(r, g, b, a)) func cssColor(d: CSSDeclaration): CSSColor = if d.value.len > 0: @@ -359,7 +358,7 @@ func cssColor(d: CSSDeclaration): CSSColor = of CSS_HASH_TOKEN: let c = parseHexColor(tok.value) if c.isSome: - return CSSColor(rgba: c.get) + return CSSColor(c.get) else: raise newException(CSSValueError, "Invalid color") of CSS_IDENT_TOKEN: @@ -400,7 +399,7 @@ func cssColor(d: CSSDeclaration): CSSColor = raise newException(CSSValueError, "Invalid color") func cellColor*(color: CSSColor): CellColor = - return CellColor(rgb: true, rgbcolor: RGBColor(color.rgba)) + return CellColor(rgb: true, rgbcolor: RGBColor(color)) func isToken(d: CSSDeclaration): bool {.inline.} = d.value.len > 0 and d.value[0] of CSSToken @@ -854,6 +853,10 @@ func buildComputedValues*(builder: CSSComputedValuesBuilder): CSSComputedValues result.buildComputedValue(builder.parent, nil, build) # important, so no need to save origins # set defaults + if builder.preshints != nil: + for prop in CSSPropertyType: + if result[prop] == nil: + result[prop] = builder.preshints[prop] for prop in CSSPropertyType: if result[prop] == nil: if inherited(prop) and builder.parent != nil and builder.parent[prop] != nil: |