Skip to content

Commit

Permalink
Add all html attributes to AssistData
Browse files Browse the repository at this point in the history
Bug: 1175288
Change-Id: I920a920ed072c989242196ab726558962f600097
AX-Relnotes: N/A
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2870490
Reviewed-by: Bo <boliu@chromium.org>
Commit-Queue: Dominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#880078}
  • Loading branch information
minorninth authored and Chromium LUCI CQ committed May 6, 2021
1 parent 6e2c07b commit 41e79b9
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 23 deletions.
20 changes: 12 additions & 8 deletions content/browser/web_contents/web_contents_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
#include "url/gurl.h"

using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertJavaStringToUTF16;
using base::android::ConvertUTF8ToJavaString;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF16ToJavaString;
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
using base::android::ToJavaArrayOfStringArray;
using base::android::ToJavaIntArray;

namespace content {
Expand Down Expand Up @@ -102,20 +103,23 @@ ScopedJavaLocalRef<jobject> JNI_WebContentsImpl_CreateJavaAXSnapshot(
// HTML/CSS attributes.
ScopedJavaLocalRef<jstring> j_html_tag =
ConvertUTF8ToJavaString(env, node->html_tag);
ScopedJavaLocalRef<jstring> j_html_id =
ConvertUTF8ToJavaString(env, node->html_id);
ScopedJavaLocalRef<jstring> j_html_class =
ConvertUTF8ToJavaString(env, node->html_class);
ScopedJavaLocalRef<jstring> j_css_display =
ConvertUTF8ToJavaString(env, node->css_display);

std::vector<std::vector<std::u16string>> html_attrs;
for (const auto& attr : node->html_attributes) {
html_attrs.push_back(
{base::UTF8ToUTF16(attr.first), base::UTF8ToUTF16(attr.second)});
}
ScopedJavaLocalRef<jobjectArray> j_attrs =
ToJavaArrayOfStringArray(env, html_attrs);

ScopedJavaLocalRef<jobject> j_node =
Java_WebContentsImpl_createAccessibilitySnapshotNode(
env, node->rect.x(), node->rect.y(), node->rect.width(),
node->rect.height(), is_root, j_text, node->color, node->bgcolor,
node->text_size, node->bold, node->italic, node->underline,
node->line_through, j_class, j_html_tag, j_html_id, j_html_class,
j_css_display);
node->line_through, j_class, j_html_tag, j_css_display, j_attrs);

if (node->selection.has_value()) {
Java_WebContentsImpl_setAccessibilitySnapshotSelection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ protected void createVirtualStructure(ViewStructure viewNode, AccessibilitySnaps
// Store the tag name in HtmlInfo.
ViewStructure.HtmlInfo.Builder htmlBuilder = viewNode.newHtmlInfoBuilder(node.htmlTag);
if (htmlBuilder != null) {
htmlBuilder.addAttribute("id", node.htmlId);
htmlBuilder.addAttribute("class", node.htmlClass);
htmlBuilder.addAttribute("display", node.cssDisplay);
for (String[] attr : node.htmlAttributes) htmlBuilder.addAttribute(attr[0], attr[1]);
viewNode.setHtmlInfo(htmlBuilder.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ protected void createVirtualStructure(ViewStructure viewNode, AccessibilitySnaps
// versions, and for scenarios where HtmlInfo is stripped.
Bundle extras = viewNode.getExtras();
extras.putCharSequence("htmlTag", node.htmlTag);
for (String[] attr : node.htmlAttributes) extras.putCharSequence(attr[0], attr[1]);

for (int i = 0; i < node.children.size(); i++) {
createVirtualStructure(viewNode.asyncNewChild(i), node.children.get(i), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,16 +802,16 @@ private static void addAccessibilityNodeAsChild(AccessibilitySnapshotNode parent
private static AccessibilitySnapshotNode createAccessibilitySnapshotNode(int parentRelativeLeft,
int parentRelativeTop, int width, int height, boolean isRootNode, String text,
int color, int bgcolor, float size, boolean bold, boolean italic, boolean underline,
boolean lineThrough, String className, String htmlTag, String htmlId, String htmlClass,
String cssDisplay) {
boolean lineThrough, String className, String htmlTag, String cssDisplay,
String[][] htmlAttributes) {
AccessibilitySnapshotNode node = new AccessibilitySnapshotNode(text, className);

// if size is smaller than 0, then style information does not exist.
if (size >= 0.0) {
node.setStyle(color, bgcolor, size, bold, italic, underline, lineThrough);
}
node.setLocationInfo(parentRelativeLeft, parentRelativeTop, width, height, isRootNode);
node.setHtmlInfo(htmlTag, htmlId, htmlClass, cssDisplay);
node.setHtmlInfo(htmlTag, cssDisplay, htmlAttributes);
return node;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public class AccessibilitySnapshotNode {
public int endSelection;

public String htmlTag;
public String htmlId;
public String htmlClass;
public String cssDisplay;

// HTML attributes: array of [lowercase HTML attribute name, value].
public String[][] htmlAttributes;

public ArrayList<AccessibilitySnapshotNode> children =
new ArrayList<AccessibilitySnapshotNode>();

Expand Down Expand Up @@ -74,11 +75,10 @@ public void setLocationInfo(int x, int y, int width, int height, boolean isRootN
this.isRootNode = isRootNode;
}

public void setHtmlInfo(String htmlTag, String htmlId, String htmlClass, String cssDisplay) {
public void setHtmlInfo(String htmlTag, String cssDisplay, String[][] htmlAttributes) {
this.htmlTag = htmlTag;
this.htmlId = htmlId;
this.htmlClass = htmlClass;
this.cssDisplay = cssDisplay;
this.htmlAttributes = htmlAttributes;
}

public void addChild(AccessibilitySnapshotNode node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,29 @@ public void testHtmlTagNames() throws Throwable {
+ " android.widget.EditText htmlTag='input'\n"
+ " android.view.View htmlTag='div'\n");
}

/**
* Test that the snapshot contains HTML attributes.
*/
@Test
@MediumTest
@MinAndroidSdkLevel(Build.VERSION_CODES.M)
@TargetApi(Build.VERSION_CODES.M)
public void testHtmlAttributes() throws Throwable {
TestViewStructureInterface node =
getViewStructureFromHtml("<button id='a' class='b' aria-label='c'>D</button>");

while (node != null
&& (node.getClassName() == null
|| !node.getClassName().equals("android.widget.Button"))) {
node = node.getChild(0);
}

Bundle extras = node.getExtras();
Assert.assertEquals("a", extras.getCharSequence("id").toString());
Assert.assertEquals("b", extras.getCharSequence("class").toString());
Assert.assertEquals("c", extras.getCharSequence("aria-label").toString());
Assert.assertNull(extras.getCharSequence("disabled"));
Assert.assertNull(extras.getCharSequence("onclick"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public String toString() {
return builder.toString();
}

@Override
public String getClassName() {
return mClassName;
}

private void recursiveDumpToString(StringBuilder builder, int indent, boolean dumpHtmlTags) {
for (int i = 0; i < indent; i++) {
builder.append(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ interface TestViewStructureInterface {
* Dump HTML tag names in toString.
*/
public void dumpHtmlTags();

/**
* Get the Android View-style class name.
*/
public String getClassName();
}
9 changes: 6 additions & 3 deletions ui/accessibility/ax_assistant_structure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,14 @@ void WalkAXTreeDepthFirst(const AXNode* node,

result->html_tag =
node->GetStringAttribute(ax::mojom::StringAttribute::kHtmlTag);
node->GetHtmlAttribute("id", &result->html_id);
result->html_class =
node->GetStringAttribute(ax::mojom::StringAttribute::kClassName);
result->css_display =
node->GetStringAttribute(ax::mojom::StringAttribute::kDisplay);
result->html_attributes = node->data().html_attributes;

std::string class_name =
node->GetStringAttribute(ax::mojom::StringAttribute::kClassName);
if (!class_name.empty())
result->html_attributes.push_back({"class", class_name});

for (size_t i = 0; i < node->GetUnignoredChildCount(); ++i) {
AXNode* child = node->GetUnignoredChildAtIndex(i);
Expand Down
5 changes: 3 additions & 2 deletions ui/accessibility/ax_assistant_structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ struct AssistantNode {

// HTML and CSS attributes.
std::string html_tag;
std::string html_id;
std::string html_class;
std::string css_display;

// HTML attributes: map from lowercase ASCII HTML attribute name to value.
base::StringPairs html_attributes;

// Accessibility functionality of the node inferred from DOM or based on HTML
// role attribute.
base::Optional<std::string> role;
Expand Down

0 comments on commit 41e79b9

Please sign in to comment.