about summary refs log tree commit diff stats
path: root/src/css
diff options
context:
space:
mode:
Diffstat (limited to 'src/css')
-rw-r--r--src/css/select.nim3
-rw-r--r--src/css/selectorparser.nim4
-rw-r--r--src/css/stylednode.nim8
3 files changed, 13 insertions, 2 deletions
diff --git a/src/css/select.nim b/src/css/select.nim
index f47df9af..0d142558 100644
--- a/src/css/select.nim
+++ b/src/css/select.nim
@@ -47,6 +47,9 @@ func pseudoSelectorMatches[T: Element|StyledNode](elem: T, sel: Selector, felem:
     elif elem.tagType == TAG_OPTION:
       return HTMLOptionElement(elem).selected
     return false
+  of PSEUDO_FOCUS:
+    when selem is StyledNode: felem.addDependency(selem, DEPEND_FOCUS)
+    return elem.document.focus == elem
 
 func selectorsMatch*[T: Element|StyledNode](elem: T, selectors: SelectorList, felem: T = nil): bool
 
diff --git a/src/css/selectorparser.nim b/src/css/selectorparser.nim
index 012d6e68..846aa865 100644
--- a/src/css/selectorparser.nim
+++ b/src/css/selectorparser.nim
@@ -21,7 +21,7 @@ type
 
   PseudoClass* = enum
     PSEUDO_FIRST_CHILD, PSEUDO_LAST_CHILD, PSEUDO_ONLY_CHILD, PSEUDO_HOVER,
-    PSEUDO_ROOT, PSEUDO_NTH_CHILD, PSEUDO_CHECKED
+    PSEUDO_ROOT, PSEUDO_NTH_CHILD, PSEUDO_CHECKED, PSEUDO_FOCUS
 
   CombinatorType* = enum
     DESCENDANT_COMBINATOR, CHILD_COMBINATOR, NEXT_SIBLING_COMBINATOR,
@@ -212,6 +212,8 @@ proc parseSelectorToken(state: var SelectorParser, csstoken: CSSToken) =
         state.addSelector(Selector(t: PSEUDO_SELECTOR, pseudo: PSEUDO_ROOT))
       of "checked":
         state.addSelector(Selector(t: PSEUDO_SELECTOR, pseudo: PSEUDO_CHECKED))
+      of "focus":
+        state.addSelector(Selector(t: PSEUDO_SELECTOR, pseudo: PSEUDO_FOCUS))
     of QUERY_PSELEM:
       case csstoken.value
       of "before":
diff --git a/src/css/stylednode.nim b/src/css/stylednode.nim
index 52a13771..f75e3f66 100644
--- a/src/css/stylednode.nim
+++ b/src/css/stylednode.nim
@@ -39,7 +39,7 @@ type
     STYLED_ELEMENT, STYLED_TEXT
 
   DependencyType* = enum
-    DEPEND_HOVER, DEPEND_CHECKED
+    DEPEND_HOVER, DEPEND_CHECKED, DEPEND_FOCUS
 
   DependencyInfo* = object
     # All nodes we depend on, for each dependency type d.
@@ -91,12 +91,18 @@ func isValid*(styledNode: StyledNode): bool =
       of DEPEND_CHECKED:
         if child.depends.prev[d] != elem.checked:
           return false
+      of DEPEND_FOCUS:
+        let focus = elem.document.focus == elem
+        if child.depends.prev[d] != focus:
+          return false
   return true
 
 proc applyDependValues*(styledNode: StyledNode) =
   let elem = Element(styledNode.node)
   styledNode.depends.prev[DEPEND_HOVER] = elem.hover
   styledNode.depends.prev[DEPEND_CHECKED] = elem.checked
+  let focus = elem.document.focus == elem
+  styledNode.depends.prev[DEPEND_FOCUS] = focus
   elem.invalid = false
 
 proc addDependency*(styledNode, dep: StyledNode, t: DependencyType) =