diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/css/match.nim | 6 | ||||
-rw-r--r-- | src/css/stylednode.nim | 35 | ||||
-rw-r--r-- | src/html/dom.nim | 7 |
3 files changed, 23 insertions, 25 deletions
diff --git a/src/css/match.nim b/src/css/match.nim index e621b332..52a8ab7f 100644 --- a/src/css/match.nim +++ b/src/css/match.nim @@ -83,7 +83,7 @@ func pseudoSelectorMatches[T: Element|StyledNode](elem: T; sel: Selector; return elem.parentNode.firstElementChild == elem and elem.parentNode.lastElementChild == elem of pcHover: - when selem is StyledNode: felem.addDependency(selem, dtHover) + when selem is StyledNode: felem.addDependency(elem, dtHover) return elem.hover of pcRoot: return elem == elem.document.html of pcNthChild: @@ -133,14 +133,14 @@ func pseudoSelectorMatches[T: Element|StyledNode](elem: T; sel: Selector; inc i return false of pcChecked: - when selem is StyledNode: felem.addDependency(selem, dtChecked) + when selem is StyledNode: felem.addDependency(elem, dtChecked) if elem.tagType == TAG_INPUT: return HTMLInputElement(elem).checked elif elem.tagType == TAG_OPTION: return HTMLOptionElement(elem).selected return false of pcFocus: - when selem is StyledNode: felem.addDependency(selem, dtFocus) + when selem is StyledNode: felem.addDependency(elem, dtFocus) return elem.document.focus == elem of pcNot: return not selem.selectorsMatch(sel.pseudo.fsels, felem) diff --git a/src/css/stylednode.nim b/src/css/stylednode.nim index 218f330d..4d14123d 100644 --- a/src/css/stylednode.nim +++ b/src/css/stylednode.nim @@ -39,15 +39,7 @@ type StyledType* = enum stElement, stText, stReplacement - DependencyType* = enum - dtHover, dtChecked, dtFocus - - DependencyInfo* = object - # All nodes we depend on, for each dependency type d. - nodes*: array[DependencyType, seq[StyledNode]] - # Previous value. The owner StyledNode is marked as invalid when one of - # these no longer matches the DOM value. - prev: array[DependencyType, bool] + DependencyInfo = array[DependencyType, seq[Element]] StyledNode* = ref object parent*: StyledNode @@ -59,6 +51,7 @@ type of stElement: computed*: CSSComputedValues children*: seq[StyledNode] + # All elements we depend on, for each dependency type d. depends*: DependencyInfo of stReplacement: # replaced elements: quotes, or (TODO) markers, images @@ -126,33 +119,31 @@ func isValid*(styledNode: StyledNode): bool = if styledNode.node != nil and Element(styledNode.node).invalid: return false for d in DependencyType: - for child in styledNode.depends.nodes[d]: - assert child.node != nil - let elem = Element(child.node) + for elem in styledNode.depends[d]: case d of dtHover: - if child.depends.prev[d] != elem.hover: + if elem.prev[d] != elem.hover: return false of dtChecked: - if child.depends.prev[d] != elem.checked: + if elem.prev[d] != elem.checked: return false of dtFocus: let focus = elem.document.focus == elem - if child.depends.prev[d] != focus: + if elem.prev[d] != focus: return false return true proc applyDependValues*(styledNode: StyledNode) = let elem = Element(styledNode.node) - styledNode.depends.prev[dtHover] = elem.hover - styledNode.depends.prev[dtChecked] = elem.checked + elem.prev[dtHover] = elem.hover + elem.prev[dtChecked] = elem.checked let focus = elem.document.focus == elem - styledNode.depends.prev[dtFocus] = focus + elem.prev[dtFocus] = focus elem.invalid = false -proc addDependency*(styledNode, dep: StyledNode; t: DependencyType) = - if dep notin styledNode.depends.nodes[t]: - styledNode.depends.nodes[t].add(dep) +proc addDependency*(styledNode: StyledNode; dep: Element; t: DependencyType) = + if dep notin styledNode.depends[t]: + styledNode.depends[t].add(dep) func newStyledElement*(parent: StyledNode; element: Element; computed: CSSComputedValues; reg: DependencyInfo): StyledNode = @@ -172,7 +163,7 @@ func newStyledElement*(element: Element): StyledNode = return StyledNode(t: stElement, node: element) func newStyledElement*(parent: StyledNode; pseudo: PseudoElem; - computed: CSSComputedValues; reg: sink DependencyInfo): StyledNode = + computed: CSSComputedValues; reg: DependencyInfo): StyledNode = return StyledNode( t: stElement, computed: computed, diff --git a/src/html/dom.nim b/src/html/dom.nim index 14d46037..4f8b7cef 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -66,6 +66,9 @@ type DocumentReadyState* = enum rsComplete = "complete" type + DependencyType* = enum + dtHover, dtChecked, dtFocus + Location = ref object window: Window @@ -228,6 +231,10 @@ type style_cached*: CSSStyleDeclaration children_cached: HTMLCollection + # The owner StyledNode is marked as invalid when one of these no longer + # matches the DOM value. + prev*: array[DependencyType, bool] + AttrDummyElement = ref object of Element CSSStyleDeclaration* = ref object |