-
-
Notifications
You must be signed in to change notification settings - Fork 404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add hinting support for Shadow DOM nested elements #3001
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,25 @@ | |
(export-always 'qs-nyxt-id) | ||
(defpsmacro qs-nyxt-id (context id) | ||
"context.querySelector() tailored for Nyxt IDs." | ||
`(chain ,context (query-selector (lisp (format nil "[nyxt-identifier=\"~a\"]" ,id))))) | ||
`(chain ,context (query-selector (stringify "[nyxt-identifier=\"" ,id "\"]")))) | ||
|
||
(export-always 'rqs-nyxt-id) | ||
(defpsmacro rqs-nyxt-id (context id) | ||
"Recursive version of `qs-nyxt-id` which goes through Shadow DOMs if there's | ||
at least one." | ||
`(flet ((recursive-query-selector (context selector) | ||
(let ((node (qs context selector))) | ||
(if node | ||
node | ||
(let ((node-iterator (chain document (create-node-iterator context (@ *node #:|ELEMENT_NODE|)))) | ||
current-node) | ||
(loop while (and (setf current-node (chain node-iterator (next-node))) (not node)) | ||
do (when (@ current-node shadow-root) | ||
(setf node (recursive-query-selector (@ current-node shadow-root) selector)))) | ||
node))))) | ||
(if (chain ,context (query-selector "[nyxt-shadow-root]")) | ||
(recursive-query-selector ,context (stringify "[nyxt-identifier=\"" ,id "\"]")) | ||
(qs-nyxt-id ,context ,id)))) | ||
|
||
(export-always 'active-element) | ||
(defpsmacro active-element (context) | ||
|
@@ -151,13 +169,15 @@ | |
(radius (parse-float (chain computed-style border-top-left-radius))) | ||
(rounded-border-offset (ceiling (* radius (- 1 (sin (/ pi 4)))))) | ||
(offset (max coord-truncation-offset rounded-border-offset)) | ||
(el (chain document (element-from-point (+ (chain rect left) offset) | ||
(+ (chain rect top) offset))))) | ||
(el (chain ,element (get-root-node) (element-from-point (+ (chain rect left) offset) | ||
(+ (chain rect top) offset))))) | ||
(if (or (>= offset (chain rect width)) | ||
(>= offset (chain rect height))) | ||
t | ||
(progn (loop while (and el (not (eq el element))) | ||
do (setf el (chain el parent-node))) | ||
do (setf el (if (instanceof (chain el parent-node) *shadow-root) | ||
(chain el parent-node host) | ||
(chain el parent-node)))) | ||
(null el))))) | ||
|
||
(export-always 'element-invisible-p) | ||
|
@@ -179,3 +199,19 @@ | |
"element.classList.remove(class) tailored for Nyxt IDs." | ||
`(let ((element (nyxt/ps:qs-nyxt-id document (ps:lisp ,id)))) | ||
(ps:chain element class-list (remove ,class)))) | ||
|
||
(export-always 'rqsa) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, why define new variations for the existing Parenscript macros? Maybe simply replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. Here's an idea for optimization: test whether there are any Shadow DOM nodes in the page, and call a regular Although I'm not sure how easy that'd be. Leaving it to your judgement, @heiwiper :) EDIT: Typo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This would not be possible as far as I know, Shadow DOMs don't seem to have a straighforward API to select them from the main DOM, one would need to check every element and call the shadowRoot function to test whether it a Shadow root or not. I did some tests on pages containing Shadow DOMs and others that do not contain this type of elements using There's actually a workaround that we could implement, we could check if an element is a shadow root in the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
(defpsmacro rqsa (context selector) | ||
"Recursive version of context.querySelectorAll() which goes through | ||
Shadow DOMs if there's at least one." | ||
`(flet ((recursive-query-selector-all (context selector) | ||
(ps:let ((nodes (ps:chain *array (from (nyxt/ps:qsa context selector)))) | ||
(node-iterator (ps:chain document (create-node-iterator context (ps:@ *node #:|ELEMENT_NODE|)))) | ||
current-node) | ||
(ps:loop while (ps:setf current-node (ps:chain node-iterator (next-node))) | ||
do (ps:when (ps:@ current-node shadow-root) | ||
(ps:chain *array prototype push (apply nodes (recursive-query-selector-all (ps:@ current-node shadow-root) selector))))) | ||
nodes))) | ||
(if (chain ,context (query-selector "[nyxt-shadow-root]")) | ||
(recursive-query-selector-all ,context ,selector) | ||
(qsa ,context ,selector)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that you can also
(coerce nyxt-identifiers 'vector)
, although I'm not sure about the performance of such an approach 😛Anyway, not critical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nyxt:update-document-model
to mention the new attributenyxt-shadow-dom
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use the same code as the one used for
hint
binding