mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-23 04:41:11 +00:00
Bug 1905211 part 10: Expose target text as an a11y text attribute. r=morgan
This is now simply a matter of leveraging the work in the previous patches to support the new selection type and map it to the correct attribute. There isn't an IAccessible2 or ATK attribute for this yet. We use mark:true, based on <mark> and role="mark" which is used for a semantic highlight. I've discussed this with Google, NV Access and Vispero and they all seem to be happy with this. Differential Revision: https://phabricator.services.mozilla.com/D217071
This commit is contained in:
parent
6652c8aa0d
commit
b453f2a8d5
@ -74,6 +74,7 @@ struct TextOffsetAttribute {
|
||||
// is -1, the attribute ends after this leaf, crossing Accessibles.
|
||||
int32_t mEndOffset;
|
||||
// The attribute:
|
||||
// nsGkAtoms::mark: Semantic highlights such as text fragments.
|
||||
// nsGkAtoms::spelling: spelling errors
|
||||
RefPtr<nsAtom> mAttribute;
|
||||
|
||||
|
@ -202,7 +202,8 @@ void SelectionManager::ProcessSelectionChanged(SelData* aSelData) {
|
||||
/* static */
|
||||
bool SelectionManager::SelectionRangeChanged(SelectionType aType,
|
||||
const dom::AbstractRange& aRange) {
|
||||
if (aType != SelectionType::eSpellCheck) {
|
||||
if (aType != SelectionType::eSpellCheck &&
|
||||
aType != SelectionType::eTargetText) {
|
||||
// We don't need to handle range changes for this selection type.
|
||||
return false;
|
||||
}
|
||||
|
@ -467,6 +467,7 @@ FindDOMTextOffsetAttributes(LocalAccessible* aAcc, int32_t aRenderedStart,
|
||||
const std::pair<mozilla::SelectionType, nsStaticAtom*>
|
||||
kSelectionTypesToAttributes[] = {
|
||||
{SelectionType::eSpellCheck, nsGkAtoms::spelling},
|
||||
{SelectionType::eTargetText, nsGkAtoms::mark},
|
||||
};
|
||||
result.SetCapacity(ArrayLength(kSelectionTypesToAttributes));
|
||||
for (auto [selType, attr] : kSelectionTypesToAttributes) {
|
||||
@ -1472,6 +1473,8 @@ void TextLeafPoint::AddTextOffsetAttributes(AccAttributes* aAttrs) const {
|
||||
auto expose = [aAttrs](nsAtom* aAttr) {
|
||||
if (aAttr == nsGkAtoms::spelling) {
|
||||
aAttrs->SetAttribute(nsGkAtoms::invalid, aAttr);
|
||||
} else if (aAttr == nsGkAtoms::mark) {
|
||||
aAttrs->SetAttribute(aAttr, true);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -5,10 +5,15 @@ support-files = [
|
||||
"!/accessible/tests/browser/shared-head.js",
|
||||
"!/accessible/tests/mochitest/*.js",
|
||||
]
|
||||
prefs = ["javascript.options.asyncstack_capture_debuggee_only=false"]
|
||||
prefs = [
|
||||
"dom.text_fragments.enabled=true",
|
||||
"javascript.options.asyncstack_capture_debuggee_only=false",
|
||||
]
|
||||
|
||||
["browser_editabletext.js"]
|
||||
|
||||
["browser_highlights.js"]
|
||||
|
||||
["browser_text.js"]
|
||||
|
||||
["browser_text_caret.js"]
|
||||
|
157
accessible/tests/browser/text/browser_highlights.js
Normal file
157
accessible/tests/browser/text/browser_highlights.js
Normal file
@ -0,0 +1,157 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
"use strict";
|
||||
|
||||
/* import-globals-from ../../mochitest/text.js */
|
||||
/* import-globals-from ../../mochitest/attributes.js */
|
||||
loadScripts({ name: "attributes.js", dir: MOCHITESTS_DIR });
|
||||
|
||||
const boldAttrs = { "font-weight": "700" };
|
||||
const fragmentAttrs = { mark: "true" };
|
||||
const snippet = `
|
||||
<p id="first">The first phrase.</p>
|
||||
<p id="second">The second <b>phrase.</b></p>
|
||||
`;
|
||||
|
||||
/**
|
||||
* Returns a promise that resolves once the attribute ranges match. If
|
||||
* shouldWaitForEvent is true, we first wait for a text attribute change event.
|
||||
*/
|
||||
async function waitForTextAttrRanges(
|
||||
acc,
|
||||
ranges,
|
||||
attrs,
|
||||
shouldWaitForEvent = true
|
||||
) {
|
||||
if (shouldWaitForEvent) {
|
||||
await waitForEvent(EVENT_TEXT_ATTRIBUTE_CHANGED);
|
||||
}
|
||||
await untilCacheOk(
|
||||
() => textAttrRangesMatch(acc, ranges, attrs),
|
||||
`Attr ranges match: ${JSON.stringify(ranges)}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a text fragment within a single node.
|
||||
*/
|
||||
addAccessibleTask(
|
||||
snippet,
|
||||
async function testTextFragmentSingleNode(browser, docAcc) {
|
||||
const first = findAccessibleChildByID(docAcc, "first");
|
||||
ok(
|
||||
textAttrRangesMatch(
|
||||
first,
|
||||
[
|
||||
[4, 16], // "first phrase"
|
||||
],
|
||||
fragmentAttrs
|
||||
),
|
||||
"first attr ranges correct"
|
||||
);
|
||||
const second = findAccessibleChildByID(docAcc, "second");
|
||||
ok(
|
||||
textAttrRangesMatch(second, [], fragmentAttrs),
|
||||
"second attr ranges correct"
|
||||
);
|
||||
},
|
||||
{ chrome: true, topLevel: true, urlSuffix: "#:~:text=first%20phrase" }
|
||||
);
|
||||
|
||||
/**
|
||||
* Test a text fragment crossing nodes.
|
||||
*/
|
||||
addAccessibleTask(
|
||||
snippet,
|
||||
async function testTextFragmentCrossNode(browser, docAcc) {
|
||||
const first = findAccessibleChildByID(docAcc, "first");
|
||||
ok(
|
||||
textAttrRangesMatch(first, [], fragmentAttrs),
|
||||
"first attr ranges correct"
|
||||
);
|
||||
const second = findAccessibleChildByID(docAcc, "second");
|
||||
ok(
|
||||
textAttrRangesMatch(
|
||||
second,
|
||||
[
|
||||
// This run is split because of the bolded word.
|
||||
[4, 11], // "second "
|
||||
[11, 17], // "phrase"
|
||||
],
|
||||
fragmentAttrs
|
||||
),
|
||||
"second attr ranges correct"
|
||||
);
|
||||
// Ensure bold is still exposed in the presence of a fragment.
|
||||
testTextAttrs(
|
||||
second,
|
||||
11,
|
||||
{ ...fragmentAttrs, ...boldAttrs },
|
||||
{},
|
||||
11,
|
||||
17,
|
||||
true
|
||||
); // "phrase"
|
||||
testTextAttrs(second, 17, boldAttrs, {}, 17, 18, true); // "."
|
||||
},
|
||||
{ chrome: true, topLevel: true, urlSuffix: "#:~:text=second%20phrase" }
|
||||
);
|
||||
|
||||
/**
|
||||
* Test scrolling to a text fragment on the same page. This also tests that the
|
||||
* scrolling start event is fired.
|
||||
*/
|
||||
add_task(async function testTextFragmentSamePage() {
|
||||
// We use add_task here because we need to verify that an
|
||||
// event is fired, but it might be fired before document load complete, so we
|
||||
// could miss it if we used addAccessibleTask.
|
||||
const docUrl = snippetToURL(snippet);
|
||||
const initialUrl = docUrl + "#:~:text=first%20phrase";
|
||||
let scrolled = waitForEvent(EVENT_SCROLLING_START, "first");
|
||||
await BrowserTestUtils.withNewTab(initialUrl, async function (browser) {
|
||||
info("Waiting for scroll to first");
|
||||
const first = (await scrolled).accessible;
|
||||
info("Checking ranges");
|
||||
await waitForTextAttrRanges(
|
||||
first,
|
||||
[
|
||||
[4, 16], // "first phrase"
|
||||
],
|
||||
fragmentAttrs,
|
||||
false
|
||||
);
|
||||
const second = first.nextSibling;
|
||||
await waitForTextAttrRanges(second, [], fragmentAttrs, false);
|
||||
|
||||
info("Navigating to second");
|
||||
scrolled = waitForEvent(EVENT_SCROLLING_START, second);
|
||||
let rangeCheck = waitForTextAttrRanges(
|
||||
second,
|
||||
[
|
||||
[4, 11], // "second "
|
||||
[11, 17], // "phrase"
|
||||
],
|
||||
fragmentAttrs,
|
||||
true
|
||||
);
|
||||
await invokeContentTask(browser, [], () => {
|
||||
content.location.hash = "#:~:text=second%20phrase";
|
||||
});
|
||||
await scrolled;
|
||||
info("Checking ranges");
|
||||
await rangeCheck;
|
||||
// XXX DOM should probably remove the highlight from "first phrase" since
|
||||
// we've navigated to "second phrase". For now, this test expects the
|
||||
// current DOM behaviour: "first" is still highlighted.
|
||||
await waitForTextAttrRanges(
|
||||
first,
|
||||
[
|
||||
[4, 16], // "first phrase"
|
||||
],
|
||||
fragmentAttrs,
|
||||
false
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user