diff options
| -rw-r--r-- | res/ua.css | 49 | ||||
| -rw-r--r-- | src/css/cascade.nim | 108 | ||||
| -rw-r--r-- | src/css/cssvalues.nim | 7 | ||||
| -rw-r--r-- | test/layout/acid1.color.expected | 4 | ||||
| -rw-r--r-- | test/layout/button-text.color.expected | 4 | ||||
| -rw-r--r-- | test/layout/button-text.expected | 4 | ||||
| -rw-r--r-- | test/layout/button-text.html | 5 | ||||
| -rw-r--r-- | test/layout/select-multiple.color.expected | 6 | ||||
| -rw-r--r-- | test/layout/select-multiple.expected | 6 | ||||
| -rw-r--r-- | test/layout/select-multiple.html | 2 |
10 files changed, 116 insertions, 79 deletions
@@ -59,35 +59,40 @@ caption { display: table-caption; text-align: center } caption[align=top] { caption-side: top } caption[align=bottom] { caption-side: bottom } -input { margin-right: 1ch; white-space: pre; color: red; text-decoration: underline } +input { + margin-right: 1ch; + white-space: pre !important; + color: -cha-ansi(red); + text-decoration: underline +} input[type=hidden i] { display: none } -input::before { content: '['; text-decoration: none; color: initial } -input::after { content: ']'; text-decoration: none; color: initial } +input::before { content: '[' !important; text-decoration: none; color: initial } +input::after { content: ']' !important; text-decoration: none; color: initial } -input:is([type=submit i], [type=button i], [type=reset i], [type=radio i], +input:is([type=button i], [type=submit i], [type=reset i], [type=radio i], [type=checkbox i]) { text-decoration: none } input[type=radio i]::before { - content: '('; + content: '(' !important; text-decoration: none; color: initial } input[type=radio i]::after { - content: ')'; + content: ')' !important; text-decoration: none; color: initial } input:is([type=submit i], [type=button i], [type=reset i])::before, input:is([type=submit i], [type=button i], [type=reset i])::after, button { - color: red; + color: -cha-ansi(red); text-decoration: none } -textarea { color: red; white-space: pre } +textarea { color: -cha-ansi(red); white-space: pre } li, summary { display: list-item } @@ -131,30 +136,26 @@ dt { font-weight: bold } dd { margin-left: 4ch } dl[compact] dt + br { display: none } -select { display: inline-flex !important; white-space: nowrap } -select::before { content: '['; color: initial } -select::after { content: ']'; color: initial } -select > :is(option, optgroup > option) { display: none } -select > :is(option, optgroup > option):checked { display: block; color: red } +select, optgroup { display: inline-flex !important; white-space: nowrap !important } +select::before { content: '[' !important; color: initial } +select::after { content: ']' !important; color: initial } +:is(select, select > optgroup) > option { display: none } +:is(select, select > optgroup) > option:checked { + display: block; + color: -cha-ansi(red) +} select[multiple] { overflow: auto; display: flow-root !important; margin-top: 1em } -select[multiple]::before, select[multiple]::after { content: '' } -select[multiple] > :is(option, optgroup > option) { - display: flex; +select[multiple]::before, select[multiple]::after { content: '' !important } +select[multiple] > optgroup { display: flow-root !important } +:is(select[multiple], select[multiple] > optgroup) > option { + display: flex !important; color: initial } -select[multiple] > :is(option, optgroup > option)::before { - display: block; - content: '[ ]' -} -select[multiple] > :is(option, optgroup > option):checked::before { - content: '[*]'; - color: red -} center { text-align: -cha-center } diff --git a/src/css/cascade.nim b/src/css/cascade.nim index 09789eba..0d999fb1 100644 --- a/src/css/cascade.nim +++ b/src/css/cascade.nim @@ -397,11 +397,12 @@ type StyledNode* = object element*: Element pseudo*: PseudoElement + computed*: CSSValues case t*: StyledType of stText: text*: CharacterData of stElement: - discard + anonChildren: seq[StyledNode] of stReplacement: # replaced elements: quotes, images, or (TODO) markers content*: CSSContent @@ -418,13 +419,10 @@ when defined(debug): of stReplacement: return "#replacement" -template computed*(styledNode: StyledNode): CSSValues = - styledNode.element.computedMap[styledNode.pseudo] - proc initStyledElement*(element: Element): StyledNode = if element.computed == nil: element.applyStyle() - return StyledNode(t: stElement, element: element) + return StyledNode(t: stElement, element: element, computed: element.computed) proc initStyledReplacement(parent: Element; content: sink CSSContent): StyledNode = @@ -434,51 +432,89 @@ proc initStyledImage(parent: Element; bmp: NetworkBitmap): StyledNode = return initStyledReplacement(parent, CSSContent(t: ContentImage, bmp: bmp)) proc initStyledPseudo(parent: Element; pseudo: PseudoElement): StyledNode = - return StyledNode(t: stElement, pseudo: pseudo, element: parent) + return StyledNode( + t: stElement, + pseudo: pseudo, + element: parent, + computed: parent.computedMap[pseudo] + ) proc initStyledText(parent: Element; text: CharacterData): StyledNode = return StyledNode(t: stText, element: parent, text: text) proc initStyledText(parent: Element; s: sink string): StyledNode = + #TODO should probably cache these... return initStyledText(parent, newCharacterData(s)) +proc initStyledAnon(parent: Element; children: sink seq[StyledNode]; + computed: CSSValues): StyledNode = + return StyledNode( + t: stElement, + element: parent, + anonChildren: children, + computed: computed + ) + +iterator optionChildren(styledNode: StyledNode): StyledNode {.inline.} = + let option = HTMLOptionElement(styledNode.element) + if option.select != nil and option.select.attrb(satMultiple): + yield initStyledText(option, "[") + let cdata = newCharacterData(if option.selected: "*" else: " ") + let computed = option.computed.inheritProperties() + computed{"color"} = cssColor(ANSIColor(1)) # red + yield initStyledAnon(option, @[initStyledText(option, cdata)], computed) + yield initStyledText(option, "]") + for it in option.childList: + if it of Element: + yield initStyledElement(Element(it)) + elif it of Text: + yield initStyledText(option, Text(it)) + # Many yields; we use a closure iterator to avoid bloating the code. iterator children*(styledNode: StyledNode): StyledNode {.closure.} = if styledNode.t != stElement: return if styledNode.pseudo == peNone: let parent = styledNode.element - if parent.computedMap[peBefore] != nil and - parent.computedMap[peBefore]{"content"}.len > 0: - yield initStyledPseudo(parent, peBefore) - case parent.tagType - of TAG_INPUT: - let cdata = HTMLInputElement(parent).inputString() - if cdata != nil and cdata.data.len != 0: - yield initStyledText(parent, cdata) - of TAG_TEXTAREA: - #TODO cache (do the same as with input, and add borders in render) - yield initStyledText(parent, HTMLTextAreaElement(parent).textAreaString()) - of TAG_IMG: yield initStyledImage(parent, HTMLImageElement(parent).bitmap) - of TAG_CANVAS: - yield initStyledImage(parent, HTMLCanvasElement(parent).bitmap) - of TAG_VIDEO: yield initStyledText(parent, "[video]") - of TAG_AUDIO: yield initStyledText(parent, "[audio]") - of TAG_BR: - yield initStyledReplacement(parent, CSSContent(t: ContentNewline)) - of TAG_IFRAME: yield initStyledText(parent, "[iframe]") - of TAG_FRAME: yield initStyledText(parent, "[frame]") - elif parent.tagType(Namespace.SVG) == TAG_SVG: - yield initStyledImage(parent, SVGSVGElement(parent).bitmap) + if styledNode.anonChildren.len > 0: + for it in styledNode.anonChildren: + yield it else: - for it in parent.childList: - if it of Element: - yield initStyledElement(Element(it)) - elif it of Text: - yield initStyledText(parent, Text(it)) - if parent.computedMap[peAfter] != nil and - parent.computedMap[peAfter]{"content"}.len > 0: - yield initStyledPseudo(parent, peAfter) + if parent.computedMap[peBefore] != nil and + parent.computedMap[peBefore]{"content"}.len > 0: + yield initStyledPseudo(parent, peBefore) + case parent.tagType + of TAG_INPUT: + let cdata = HTMLInputElement(parent).inputString() + if cdata != nil and cdata.data.len != 0: + yield initStyledText(parent, cdata) + of TAG_TEXTAREA: + #TODO cache (do the same as with input, and add borders in render) + let cdata = HTMLTextAreaElement(parent).textAreaString() + yield initStyledText(parent, cdata) + of TAG_IMG: yield initStyledImage(parent, HTMLImageElement(parent).bitmap) + of TAG_CANVAS: + yield initStyledImage(parent, HTMLCanvasElement(parent).bitmap) + of TAG_VIDEO: yield initStyledText(parent, "[video]") + of TAG_AUDIO: yield initStyledText(parent, "[audio]") + of TAG_BR: + yield initStyledReplacement(parent, CSSContent(t: ContentNewline)) + of TAG_IFRAME: yield initStyledText(parent, "[iframe]") + of TAG_FRAME: yield initStyledText(parent, "[frame]") + of TAG_OPTION: + for it in styledNode.optionChildren: + yield it + elif parent.tagType(Namespace.SVG) == TAG_SVG: + yield initStyledImage(parent, SVGSVGElement(parent).bitmap) + else: + for it in parent.childList: + if it of Element: + yield initStyledElement(Element(it)) + elif it of Text: + yield initStyledText(parent, Text(it)) + if parent.computedMap[peAfter] != nil and + parent.computedMap[peAfter]{"content"}.len > 0: + yield initStyledPseudo(parent, peAfter) else: let parent = styledNode.element for content in parent.computedMap[styledNode.pseudo]{"content"}: diff --git a/src/css/cssvalues.nim b/src/css/cssvalues.nim index 2c02ce14..887689a8 100644 --- a/src/css/cssvalues.nim +++ b/src/css/cssvalues.nim @@ -694,13 +694,10 @@ macro `{}=`*(vals: CSSValues; s: static string, val: typed) = case t.reprType of cprtBit: return quote do: - `vals`.bits[CSSPropertyType(`t`)].dummy = uint8(`val`) + `vals`.bits[CSSPropertyType(`t`)] = CSSValueBit(`vs`: `val`) of cprtWord: return quote do: - `vals`.objs[CSSPropertyType(`t`)] = CSSValue( - v: CSSValueType(`v`), - `vs`: `val` - ) + `vals`.words[CSSPropertyType(`t`)] = CSSValueWord(`vs`: `val`) of cprtObject: return quote do: `vals`.objs[CSSPropertyType(`t`)] = CSSValue( diff --git a/test/layout/acid1.color.expected b/test/layout/acid1.color.expected index 2463efd2..24016847 100644 --- a/test/layout/acid1.color.expected +++ b/test/layout/acid1.color.expected @@ -3,8 +3,8 @@ [48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m [48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [1m[38;2;255;255;255mtoggle[22m[39m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m [48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;0;0;0mthe way[38;2;80;80;80m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;255;255;255mthe world ends[39m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;0;0;0mi grow old[38;2;80;80;80m [38;2;139;139;139m[48;2;255;255;255m [39m[49m -[48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;255;255;255mbang [39m([38;2;255;40;40m [39m) [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m -[48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;255;255;255mwhimper [39m([38;2;255;40;40m [39m) [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m +[48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;255;255;255mbang [39m([38;2;255;55;56m [39m) [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m +[48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;255;255;255mwhimper [39m([38;2;255;55;56m [39m) [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m [48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m [48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m [48;2;0;0;255m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;204;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[48;2;0;0;0m [38;2;139;139;139m[48;2;255;255;255m [38;2;80;80;80m[48;2;255;204;0m [38;2;139;139;139m[48;2;255;255;255m [39m[49m diff --git a/test/layout/button-text.color.expected b/test/layout/button-text.color.expected new file mode 100644 index 00000000..6a1b42cc --- /dev/null +++ b/test/layout/button-text.color.expected @@ -0,0 +1,4 @@ +[38;2;255;55;56m[SUBMIT][39m +[38;2;255;55;56m[][39m +[38;2;255;55;56m[RESET][39m +[38;2;255;55;56m[][39m diff --git a/test/layout/button-text.expected b/test/layout/button-text.expected deleted file mode 100644 index b0e9318e..00000000 --- a/test/layout/button-text.expected +++ /dev/null @@ -1,4 +0,0 @@ -[SUBMIT] -[] -[RESET] -[] diff --git a/test/layout/button-text.html b/test/layout/button-text.html index a379055b..43914a93 100644 --- a/test/layout/button-text.html +++ b/test/layout/button-text.html @@ -1,4 +1,7 @@ -<input type=submit><br> +<style> +#x::before { content: "test" } +</style> +<input id=x type=submit><br> <input type=submit value=><br> <input type=reset><br> <input type=reset value=><br> diff --git a/test/layout/select-multiple.color.expected b/test/layout/select-multiple.color.expected new file mode 100644 index 00000000..4e677a5c --- /dev/null +++ b/test/layout/select-multiple.color.expected @@ -0,0 +1,6 @@ + +[[38;2;255;55;56m [39m]test test test test test test test test test +[[38;2;255;55;56m*[39m]test +[[38;2;255;55;56m [39m]test +[[38;2;255;55;56m [39m]test +test diff --git a/test/layout/select-multiple.expected b/test/layout/select-multiple.expected deleted file mode 100644 index 388c6ee8..00000000 --- a/test/layout/select-multiple.expected +++ /dev/null @@ -1,6 +0,0 @@ - -[ ]test test test test test test test test test -[ ]test -[ ]test -[ ]test -test diff --git a/test/layout/select-multiple.html b/test/layout/select-multiple.html index 39097020..7f73bb85 100644 --- a/test/layout/select-multiple.html +++ b/test/layout/select-multiple.html @@ -1,7 +1,7 @@ <!DOCTYPE html> <select multiple> <option>test test test test test test test test test</option> -<option>test</option> +<option selected>test</option> <option>test</option> <option>test</option> </select> |