diff options
| -rw-r--r-- | doc/css.md | 4 | ||||
| -rw-r--r-- | src/css/cssparser.nim | 1 | ||||
| -rw-r--r-- | src/css/csstree.nim | 90 | ||||
| -rw-r--r-- | src/css/cssvalues.nim | 110 | ||||
| -rw-r--r-- | src/html/catom.nim | 1 | ||||
| -rw-r--r-- | test/layout/counters.expected | 15 | ||||
| -rw-r--r-- | test/layout/counters.html | 37 |
7 files changed, 211 insertions, 47 deletions
@@ -24,8 +24,10 @@ values: * caption-side * clear * color (hex values and functions `rgb`, `rgba`, `hsl`, `hsla`) -* content +* content (string, (no-)open/close-quote, counter()) +* counter-increment * counter-reset +* counter-set * display (`block`, `inline-block`, `list-item`, `table`, `table-*`, `flex`, `inline-flex`, `flow-root`) * flex-basis (but `content` not supported) diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim index d92b4fe3..ed6e12b4 100644 --- a/src/css/cssparser.nim +++ b/src/css/cssparser.nim @@ -68,6 +68,7 @@ type cftHsl = "hsl" cftHsla = "hsla" cftCalc = "calc" + cftCounter = "counter" CSSFunction* = ref object of CSSComponentValue name*: CSSFunctionType diff --git a/src/css/csstree.nim b/src/css/csstree.nim index 95afc237..386ce6cc 100644 --- a/src/css/csstree.nim +++ b/src/css/csstree.nim @@ -29,6 +29,7 @@ import chame/tags import css/box import css/cascade import css/cssvalues +import css/lunit import css/selectorparser import html/catom import html/dom @@ -39,7 +40,7 @@ import utils/twtstr type StyledNodeType = enum - stElement, stText, stImage, stBr + stElement, stText, stMarker, stImage, stBr # Abstraction over the DOM to pretend that elements, text, replaced # and pseudo-elements are derived from the same type. @@ -57,10 +58,17 @@ type bmp: NetworkBitmap of stBr: # <br> element discard + of stMarker: # ::marker (not that it's implemented...) + discard + + CSSCounter = object + element: Element + name: CAtom + n: int32 TreeContext = object quoteLevel: int - listItemCounter: int + counters: seq[CSSCounter] rootProperties: CSSValues TreeFrame = object @@ -93,6 +101,53 @@ when defined(debug): return "#image" of stBr: return "#br" + of stMarker: + return "#marker" + +iterator mritems(counters: var seq[CSSCounter]): var CSSCounter = + for i in countdown(counters.high, 0): + yield counters[i] + +proc incCounter(ctx: var TreeContext; name: CAtom; n: int32; element: Element) = + var found = false + for counter in ctx.counters.mritems: + if counter.name == name: + let n64 = clamp(int64(counter.n) + int64(n), int32.low, int32.high) + counter.n = int32(n64) + found = true + break + if not found: # instantiate a new counter + ctx.counters.add(CSSCounter(name: name, n: n, element: element)) + +proc setCounter(ctx: var TreeContext; name: CAtom; n: int32; element: Element) = + var found = false + for counter in ctx.counters.mritems: + if counter.name == name: + counter.n = n + found = true + break + if not found: # instantiate a new counter + ctx.counters.add(CSSCounter(name: name, n: n, element: element)) + +proc resetCounter(ctx: var TreeContext; name: CAtom; n: int32; + element: Element) = + var found = false + for counter in ctx.counters.mritems: + if counter.name == name and + counter.element.parentNode == element.parentNode and + counter.element.elIndex <= element.elIndex: + counter.element = element + counter.n = n + found = true + break + if not found: + ctx.counters.add(CSSCounter(name: name, n: n, element: element)) + +proc counter(ctx: var TreeContext; name: CAtom): int = + for counter in ctx.counters.mritems: + if counter.name == name: + return counter.n + return 0 func inheritFor(frame: TreeFrame; display: CSSDisplay): CSSValues = result = frame.computed.inheritProperties() @@ -223,14 +278,11 @@ proc getParent(frame: var TreeFrame; computed: CSSValues; display: CSSDisplay): proc addListItem(frame: var TreeFrame; node: sink StyledNode) = var node = node # Generate a marker box. - inc frame.ctx.listItemCounter let computed = node.computed.inheritProperties() computed{"white-space"} = WhitespacePre - let counter = frame.ctx.listItemCounter let markerText = StyledNode( - t: stText, + t: stMarker, element: node.element, - text: newRefString(computed{"list-style-type"}.listMarker(counter)), computed: computed ) case node.computed{"list-style-position"} @@ -400,6 +452,8 @@ proc addContent(frame: var TreeFrame; content: CSSContent) = of ContentNoCloseQuote: if frame.ctx.quoteLevel > 0: dec frame.ctx.quoteLevel + of ContentCounter: + frame.addText($frame.ctx.counter(content.counter)) proc buildChildren(frame: var TreeFrame; styledNode: StyledNode) = for child in styledNode.anonChildren: @@ -436,14 +490,21 @@ proc build(ctx: var TreeContext; cached: CSSBox; styledNode: StyledNode): CSSBox = case styledNode.t of stElement: - for reset in styledNode.computed{"counter-reset"}: - if reset.name == "list-item": - ctx.listItemCounter = reset.num - let listItemCounter = ctx.listItemCounter + for counter in styledNode.computed{"counter-reset"}: + ctx.resetCounter(counter.name, counter.num, styledNode.element) + var liSeen = false + for counter in styledNode.computed{"counter-increment"}: + liSeen = counter.name == satListItem.toAtom() + ctx.incCounter(counter.name, counter.num, styledNode.element) + if not liSeen and styledNode.computed{"display"} == DisplayListItem: + ctx.incCounter(satListItem.toAtom(), 1, styledNode.element) + for counter in styledNode.computed{"counter-set"}: + ctx.setCounter(counter.name, counter.num, styledNode.element) + let countersLen = ctx.counters.len var frame = ctx.initTreeFrame(styledNode.element, styledNode.computed) frame.buildChildren(styledNode) let box = ctx.buildBox(frame, cached) - ctx.listItemCounter = listItemCounter + ctx.counters.setLen(countersLen) return box of stText: return InlineTextBox( @@ -456,6 +517,13 @@ proc build(ctx: var TreeContext; cached: CSSBox; styledNode: StyledNode): computed: styledNode.computed, element: styledNode.element ) + of stMarker: + let counter = ctx.counter(satListItem.toAtom()) + return InlineTextBox( + computed: styledNode.computed, + element: styledNode.element, + text: styledNode.computed{"list-style-type"}.listMarker(counter) + ) of stImage: return InlineImageBox( computed: styledNode.computed, diff --git a/src/css/cssvalues.nim b/src/css/cssvalues.nim index 15e81303..2cdd2df0 100644 --- a/src/css/cssvalues.nim +++ b/src/css/cssvalues.nim @@ -80,6 +80,8 @@ type cptBorderSpacing = "border-spacing" cptContent = "content" cptCounterReset = "counter-reset" + cptCounterIncrement = "counter-increment" + cptCounterSet = "counter-set" cptQuotes = "quotes" const LastBitPropType = cptWordBreak @@ -152,7 +154,7 @@ type cvtLength2 = "length2" cvtBorderCollapse = "borderCollapse" cvtQuotes = "quotes" - cvtCounterReset = "counterReset" + cvtCounterSet = "counterSet" cvtImage = "image" cvtFloat = "float" cvtVisibility = "visibility" @@ -282,8 +284,12 @@ type BorderCollapseCollapse = "collapse" CSSContentType* = enum - ContentString, ContentOpenQuote, ContentCloseQuote, ContentNoOpenQuote, - ContentNoCloseQuote + ContentString = "-cha-string" + ContentCounter = "-cha-counter" + ContentOpenQuote = "open-quote" + ContentCloseQuote = "close-quote" + ContentNoOpenQuote = "no-open-quote" + ContentNoCloseQuote = "no-close-quote" CSSFloat* = enum FloatNone = "none" @@ -354,16 +360,21 @@ type num*: float32 CSSContent* = object - t*: CSSContentType - s*: RefString + case t*: CSSContentType + of ContentString: + s*: RefString + of ContentCounter: + counter*: CAtom + else: + discard # nil -> auto CSSQuotes* = ref object qs*: seq[tuple[s, e: RefString]] - CSSCounterReset* = object - name*: string - num*: int + CSSCounterSet* = object + name*: CAtom + num*: int32 CSSLength2* = ref object a*: CSSLength @@ -408,8 +419,8 @@ type quotes*: CSSQuotes of cvtLength2: length2*: CSSLength2 - of cvtCounterReset: - counterReset*: seq[CSSCounterReset] + of cvtCounterSet: + counterSet*: seq[CSSCounterSet] of cvtImage: image*: NetworkBitmap else: discard @@ -520,7 +531,9 @@ const ValueTypes = [ cptBackgroundImage: cvtImage, cptBorderSpacing: cvtLength2, cptContent: cvtContent, - cptCounterReset: cvtCounterReset, + cptCounterReset: cvtCounterSet, + cptCounterIncrement: cvtCounterSet, + cptCounterSet: cvtCounterSet, cptQuotes: cvtQuotes, ] @@ -597,9 +610,14 @@ func `$`*(bmp: NetworkBitmap): string = return "" #TODO func `$`*(content: CSSContent): string = - if content.s != "": + case content.t + of ContentString: return content.s - return "none" + of ContentCounter: + return content.counter.toStr() + of ContentOpenQuote, ContentCloseQuote, ContentNoOpenQuote, + ContentNoCloseQuote: + return $content.t func `$`(quotes: CSSQuotes): string = if quotes == nil: @@ -608,10 +626,10 @@ func `$`(quotes: CSSQuotes): string = for (s, e) in quotes.qs: result &= "'" & ($s).cssEscape() & "' '" & ($e).cssEscape() & "'" -func `$`(counterreset: seq[CSSCounterReset]): string = +func `$`(counterreset: seq[CSSCounterSet]): string = result = "" for it in counterreset: - result &= it.name + result &= it.name.toStr() result &= ' ' result &= $it.num @@ -629,7 +647,7 @@ func serialize(val: CSSValue): string = result &= ' ' result &= $x of cvtQuotes: return $val.quotes - of cvtCounterReset: return $val.counterReset + of cvtCounterSet: return $val.counterSet else: assert false func serialize(val: CSSValueWord; t: CSSValueType): string = @@ -671,7 +689,7 @@ func serialize*(computed: CSSValues; p: CSSPropertyType): string = of cprtWord: return computed.words[p].serialize(valueType(p)) of cprtObject: return computed.objs[p].serialize() -func `$`*(computed: CSSValues): string = +proc `$`*(computed: CSSValues): string = result = "" for p in CSSPropertyType: result &= $p & ':' @@ -864,7 +882,7 @@ func japaneseNumber(i: int): string = s &= ss[j] return s -func listMarker*(t: CSSListStyleType; i: int): string = +func listMarker0(t: CSSListStyleType; i: int): string = case t of ListStyleTypeNone: return "" of ListStyleTypeDisc: return "• " # U+2022 @@ -887,6 +905,9 @@ func listMarker*(t: CSSListStyleType; i: int): string = of ListStyleTypeCjkHeavenlyStem: return numToFixed(i, HeavenlyStemMap) & "、" of ListStyleTypeJapaneseInformal: return japaneseNumber(i) & "、" +func listMarker*(t: CSSListStyleType; i: int): RefString = + return newRefString(listMarker0(t, i)) + func quoteStart*(level: int): string = if level == 0: return "“" @@ -1270,8 +1291,8 @@ func parseQuotes(cvals: openArray[CSSComponentValue]): Opt[CSSQuotes] = else: return err() -func cssContent(cvals: openArray[CSSComponentValue]): seq[CSSContent] = - result = @[] +proc parseContent(cvals: openArray[CSSComponentValue]): Opt[seq[CSSContent]] = + var res: seq[CSSContent] = @[] for cval in cvals: if cval of CSSToken: let tok = CSSToken(cval) @@ -1280,16 +1301,30 @@ func cssContent(cvals: openArray[CSSComponentValue]): seq[CSSContent] = if tok.value == "/": break elif tok.value.equalsIgnoreCase("open-quote"): - result.add(CSSContent(t: ContentOpenQuote)) + res.add(CSSContent(t: ContentOpenQuote)) elif tok.value.equalsIgnoreCase("no-open-quote"): - result.add(CSSContent(t: ContentNoOpenQuote)) + res.add(CSSContent(t: ContentNoOpenQuote)) elif tok.value.equalsIgnoreCase("close-quote"): - result.add(CSSContent(t: ContentCloseQuote)) + res.add(CSSContent(t: ContentCloseQuote)) elif tok.value.equalsIgnoreCase("no-close-quote"): - result.add(CSSContent(t: ContentNoCloseQuote)) + res.add(CSSContent(t: ContentNoCloseQuote)) of cttString: - result.add(CSSContent(t: ContentString, s: newRefString(tok.value))) - else: return + res.add(CSSContent(t: ContentString, s: newRefString(tok.value))) + of cttWhitespace: + discard + else: + return err() + elif cval of CSSFunction: + let fun = CSSFunction(cval) + if fun.name == cftCounter: + let i = fun.value.skipBlanks(0) + if i >= fun.value.len or not (fun.value[i] of CSSToken): + return err() + let tok = CSSToken(fun.value[i]) + if tok.t != cttIdent: + return err() + res.add(CSSContent(t: ContentCounter, counter: tok.value.toAtom())) + ok(res) func parseFontWeight(cval: CSSComponentValue): Opt[int32] = if cval of CSSToken: @@ -1341,13 +1376,13 @@ func cssVerticalAlign(cval: CSSComponentValue; attrs: WindowAttributes): )) return err() -func cssCounterReset(cvals: openArray[CSSComponentValue]): - Opt[seq[CSSCounterReset]] = +proc parseCounterSet(cvals: openArray[CSSComponentValue]; n: int32): + Opt[seq[CSSCounterSet]] = template die = return err() - var r = CSSCounterReset() + var r = CSSCounterSet() var s = false - var res: seq[CSSCounterReset] = @[] + var res: seq[CSSCounterSet] = @[] for cval in cvals: if cval of CSSToken: let tok = CSSToken(cval) @@ -1356,16 +1391,19 @@ func cssCounterReset(cvals: openArray[CSSComponentValue]): of cttIdent: if s: die - r.name = tok.value + r.name = tok.value.toAtom() s = true of cttNumber, cttINumber: if not s: die - r.num = int(tok.nvalue) + r.num = int32(tok.nvalue) res.add(r) s = false else: die + if s: + r.num = n + res.add(r) return ok(res) func cssMaxSize(cval: CSSComponentValue; attrs: WindowAttributes): @@ -1520,7 +1558,7 @@ proc parseValue(cvals: openArray[CSSComponentValue]; t: CSSPropertyType; #TODO content for flex-basis else: set_word length, ?parseLength(cval, attrs) - of cvtContent: set_new content, cssContent(cvals) + of cvtContent: set_new content, ?parseContent(cvals) of cvtInteger: case t of cptFontWeight: set_word integer, ?parseFontWeight(cval) @@ -1543,7 +1581,9 @@ proc parseValue(cvals: openArray[CSSComponentValue]; t: CSSPropertyType; let b = if i >= cvals.len: a else: ?cssAbsoluteLength(cvals[i], attrs) set_new length2, CSSLength2(a: a, b: b) of cvtQuotes: set_new quotes, ?parseQuotes(cvals) - of cvtCounterReset: set_new counterReset, ?cssCounterReset(cvals) + of cvtCounterSet: + let n = if t == cptCounterIncrement: 1i32 else: 0i32 + set_new counterSet, ?parseCounterSet(cvals, n) of cvtImage: set_new image, ?parseImage(cval) of cvtFloat: set_bit float, ?parseIdent[CSSFloat](cval) of cvtVisibility: set_bit visibility, ?parseIdent[CSSVisibility](cval) @@ -1879,7 +1919,7 @@ func splitTable*(computed: CSSValues): tuple[outer, innner: CSSValues] = return (outer, inner) when defined(debug): - func `serializeEmpty`*(computed: CSSValues): string = + func serializeEmpty*(computed: CSSValues): string = let default = rootProperties() result = "" for p in CSSPropertyType: diff --git a/src/html/catom.nim b/src/html/catom.nim index c888333e..33041dd7 100644 --- a/src/html/catom.nim +++ b/src/html/catom.nim @@ -62,6 +62,7 @@ macro makeStaticAtom = satIntegrity = "integrity" satIsmap = "ismap" satLanguage = "language" + satListItem = "list-item" satLoad = "load" satLoadend = "loadend" satLoadstart = "loadstart" diff --git a/test/layout/counters.expected b/test/layout/counters.expected new file mode 100644 index 00000000..0bab8ed4 --- /dev/null +++ b/test/layout/counters.expected @@ -0,0 +1,15 @@ +[1]test + 1. 1 1 +[2]test + 2. 2 2 + 3. 3 3 + 4. 4 4 + 5. 5 5 + 6. 6 6 + 7. 7 7 + 1. 1 1 + 2. 2 2 + 3. 3 3 + 4. 4 4 + 5. 5 5 +[3]test diff --git a/test/layout/counters.html b/test/layout/counters.html new file mode 100644 index 00000000..ac410e73 --- /dev/null +++ b/test/layout/counters.html @@ -0,0 +1,37 @@ +<!DOCTYPE html> +<!-- Once again, Blink disagrees with Gecko. This time though, it's + Blink being weird: it resets list-item counters per div level. + That feels wrong, so we follow Gecko. --> +<style> +myli { display: list-item; margin-left: 5ch; list-style-type: decimal } +myli { counter-increment: asdf 1 } +myli::before { content: counter(asdf) ' ' } +:root { counter-set: my-link-counter } +a[href]::before{ + content: '['counter(my-link-counter)']'; + counter-increment: my-link-counter +} +</style> +<a href=https://example.org>test</a> +<div> + <myli>1</myli> + <div> + <a href=https://example.org>test</a> + <myli>2</myli> + <myli>3</myli> + <myli>4</myli> + <myli>5</myli> + </div> + <myli>6</myli> + <myli>7</myli> +</div> +<div> + <myli>1</myli> + <myli>2</myli> + <myli>3</myli> + <div> + <myli>4</myli> + </div> + <myli>5</myli> +</div> +<a href=https://example.org>test</a> |