-
-
Notifications
You must be signed in to change notification settings - Fork 168
feat: add page.accessibility CDP domain API
#595
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
Open
myabc
wants to merge
1
commit into
rubycdp:main
Choose a base branch
from
myabc:feature/accessibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "ferrum/accessibility/ax_node" | ||
|
|
||
| module Ferrum | ||
| # | ||
| # Wraps the CDP [Accessibility](https://chromedevtools.github.io/devtools-protocol/tot/Accessibility/) | ||
| # domain. The query commands work without `enable`; `enable`/`disable` are | ||
| # provided for completeness (live AX events). | ||
| # | ||
| # @note The node-scoped methods (`node_for`, `partial_tree`, `query` with a | ||
| # `node:`) issue the command against the node's owning page session. They | ||
| # support same-process (same-target) iframes; nodes living in an | ||
| # out-of-process iframe (OOPIF, separate CDP target) are not resolvable and | ||
| # will error or return an empty result. | ||
| # | ||
| class Accessibility | ||
| def initialize(page) | ||
| @page = page | ||
| end | ||
|
|
||
| # | ||
| # The single non-ignored AXNode for a DOM node, or `nil`. | ||
| # | ||
| # @param [Ferrum::Node] node | ||
| # @return [AXNode, nil] | ||
| # | ||
| def node_for(node) | ||
| partial_tree(node: node).find { |ax_node| !ax_node.ignored? } | ||
| end | ||
|
|
||
| # | ||
| # The partial AX tree for a DOM node. | ||
| # | ||
| # @param [Ferrum::Node] node | ||
| # @param [Boolean] fetch_relatives | ||
| # @return [Array<AXNode>] | ||
| # | ||
| def partial_tree(node:, fetch_relatives: false) | ||
| nodes = node.page.command("Accessibility.getPartialAXTree", | ||
| nodeId: node.node_id, | ||
| fetchRelatives: fetch_relatives)["nodes"] | ||
| build(nodes) | ||
| end | ||
|
|
||
| # | ||
| # The full AX tree for the page. | ||
| # | ||
| # @param [Integer, nil] depth | ||
| # @param [String, nil] frame_id | ||
| # @return [Array<AXNode>] | ||
| # | ||
| def snapshot(depth: nil, frame_id: nil) | ||
| params = { depth: depth, frameId: frame_id }.compact | ||
| build(@page.command("Accessibility.getFullAXTree", **params)["nodes"]) | ||
| end | ||
|
|
||
| # | ||
| # Query the AX tree by accessible name and/or role. | ||
| # | ||
| # @param [String, nil] name | ||
| # @param [String, nil] role | ||
| # @param [Ferrum::Node, nil] node Scope the query to this node's subtree. | ||
| # @return [Array<AXNode>] | ||
| # | ||
| def query(name: nil, role: nil, node: nil) | ||
| page = node ? node.page : @page | ||
| params = { accessibleName: name, role: role }.compact | ||
| params[:nodeId] = node ? node.node_id : page.document_node_id | ||
| build(page.command("Accessibility.queryAXTree", **params)["nodes"]) | ||
| end | ||
|
|
||
| # | ||
| # The root AXNode of the (optionally framed) document. | ||
| # | ||
| # @param [String, nil] frame_id | ||
| # @return [AXNode, nil] | ||
| # | ||
| def root(frame_id: nil) | ||
| params = { depth: 1, frameId: frame_id }.compact | ||
| build(@page.command("Accessibility.getFullAXTree", **params)["nodes"]).first | ||
| end | ||
|
|
||
| # @return [self] | ||
| def enable | ||
| @page.command("Accessibility.enable") | ||
| self | ||
| end | ||
|
|
||
| # @return [self] | ||
| def disable | ||
| @page.command("Accessibility.disable") | ||
| self | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def build(nodes) | ||
| Array(nodes).map { |node| AXNode.new(node) } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Ferrum | ||
| class Accessibility | ||
| # | ||
| # Represents an [AXNode](https://chromedevtools.github.io/devtools-protocol/tot/Accessibility/#type-AXNode) | ||
| # from the CDP Accessibility domain. | ||
| # | ||
| class AXNode | ||
| # | ||
| # @param [Hash{String => Object}] params | ||
| # The parsed CDP AXNode attributes. | ||
| # | ||
| def initialize(params) | ||
| @params = deep_freeze(params) | ||
| end | ||
|
|
||
| # @return [String, nil] | ||
| def role | ||
| @params.dig("role", "value") | ||
| end | ||
|
|
||
| # @return [String, nil] | ||
| def name | ||
| @params.dig("name", "value") | ||
| end | ||
|
|
||
| # @return [String, nil] | ||
| def description | ||
| @params.dig("description", "value") | ||
| end | ||
|
|
||
| # @return [String, Numeric, Boolean, nil] raw CDP AXValue.value; type varies by control | ||
| def value | ||
| @params.dig("value", "value") | ||
| end | ||
|
|
||
| # @return [Hash{String => Object}] | ||
| # ARIA/computed properties flattened to `name => value`. | ||
| def properties | ||
| @properties ||= Array(@params["properties"]).to_h do |property| | ||
| [property["name"], property.dig("value", "value")] | ||
| end.freeze | ||
| end | ||
|
|
||
| # @return [Boolean] | ||
| def ignored? | ||
| @params["ignored"] == true | ||
| end | ||
|
|
||
| # @return [Array, nil] | ||
| def ignored_reasons | ||
| @params["ignoredReasons"] | ||
| end | ||
|
|
||
| # @return [String, nil] | ||
| def node_id | ||
| @params["nodeId"] | ||
| end | ||
|
|
||
| # @return [Integer, nil] | ||
| def backend_dom_node_id | ||
| @params["backendDOMNodeId"] | ||
| end | ||
|
|
||
| # @return [Array, nil] | ||
| def child_ids | ||
| @params["childIds"] | ||
| end | ||
|
|
||
| # @return [Hash] | ||
| # The raw CDP AXNode hash. | ||
| def to_h | ||
| @params | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def deep_freeze(object) | ||
| case object | ||
| when Hash then object.each { |key, value| deep_freeze(key.freeze) && deep_freeze(value) } | ||
| when Array then object.each { |value| deep_freeze(value) } | ||
| end | ||
| object.freeze | ||
| end | ||
|
myabc marked this conversation as resolved.
|
||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| module Ferrum | ||
| class Accessibility | ||
| @page: Page | ||
|
|
||
| def initialize: (Page page) -> void | ||
|
|
||
| def node_for: (Node node) -> AXNode? | ||
|
|
||
| def partial_tree: (node: Node, ?fetch_relatives: bool) -> Array[AXNode] | ||
|
|
||
| def snapshot: (?depth: Integer?, ?frame_id: String?) -> Array[AXNode] | ||
|
|
||
| def query: (?name: String?, ?role: String?, ?node: Node?) -> Array[AXNode] | ||
|
|
||
| def root: (?frame_id: String?) -> AXNode? | ||
|
|
||
| def enable: () -> self | ||
|
|
||
| def disable: () -> self | ||
|
|
||
| private | ||
|
|
||
| def build: (Array[untyped]? nodes) -> Array[AXNode] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| module Ferrum | ||
| class Accessibility | ||
| class AXNode | ||
| @params: Hash[String, untyped] | ||
|
|
||
| def initialize: (Hash[String, untyped] params) -> void | ||
|
|
||
| def role: () -> String? | ||
|
|
||
| def name: () -> String? | ||
|
|
||
| def description: () -> String? | ||
|
|
||
| def value: () -> (String | Numeric | bool | nil) | ||
|
|
||
| def properties: () -> Hash[String, untyped] | ||
|
|
||
| def ignored?: () -> bool | ||
|
|
||
| def ignored_reasons: () -> Array[untyped]? | ||
|
|
||
| def node_id: () -> String? | ||
|
|
||
| def backend_dom_node_id: () -> Integer? | ||
|
|
||
| def child_ids: () -> Array[String]? | ||
|
|
||
| def to_h: () -> Hash[String, untyped] | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.