Skip to content

Commit

Permalink
Bug 1492326, use Element helper methods in accessibility instead of Q…
Browse files Browse the repository at this point in the history
…ueryInterface to get interface implementations that might be implemented by custom elements, r=surkov
  • Loading branch information
EnnDeakin2 committed Dec 4, 2018
1 parent 90cfd34 commit 9397165
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 92 deletions.
7 changes: 4 additions & 3 deletions accessible/base/nsAccUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ int32_t nsAccUtils::GetARIAOrDefaultLevel(const Accessible* aAccessible) {
}

int32_t nsAccUtils::GetLevelForXULContainerItem(nsIContent* aContent) {
nsCOMPtr<nsIDOMXULContainerItemElement> item(do_QueryInterface(aContent));
nsCOMPtr<nsIDOMXULContainerItemElement> item =
aContent->AsElement()->AsXULContainerItem();
if (!item) return 0;

nsCOMPtr<Element> containerElement;
item->GetParentContainer(getter_AddRefs(containerElement));
nsCOMPtr<nsIDOMXULContainerElement> container =
do_QueryInterface(containerElement);
containerElement ? containerElement->AsXULContainer() : nullptr;
if (!container) return 0;

// Get level of the item.
Expand All @@ -107,7 +108,7 @@ int32_t nsAccUtils::GetLevelForXULContainerItem(nsIContent* aContent) {
level++;

container->GetParentContainer(getter_AddRefs(containerElement));
container = do_QueryInterface(containerElement);
container = containerElement ? containerElement->AsXULContainer() : nullptr;
}

return level;
Expand Down
27 changes: 20 additions & 7 deletions accessible/generic/Accessible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,13 +734,15 @@ void Accessible::XULElmName(DocAccessible* aDocument, nsIContent* aElm,
*/

// CASE #1 (via label attribute) -- great majority of the cases
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemEl = do_QueryInterface(aElm);
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemEl =
aElm->AsElement()->AsXULSelectControlItem();
if (itemEl) {
itemEl->GetLabel(aName);
} else {
// Use @label if this is not a select control element, which uses label
// attribute to indicate, which option is selected.
nsCOMPtr<nsIDOMXULSelectControlElement> select = do_QueryInterface(aElm);
nsCOMPtr<nsIDOMXULSelectControlElement> select =
aElm->AsElement()->AsXULSelectControl();
if (!select) {
aElm->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, aName);
}
Expand Down Expand Up @@ -1673,7 +1675,7 @@ Relation Accessible::RelationByType(RelationType aType) const {
} else {
// In XUL, use first <button default="true" .../> in the document
nsIDocument* doc = mContent->OwnerDoc();
nsCOMPtr<nsIDOMXULButtonElement> buttonEl;
nsIContent* buttonEl = nullptr;
if (doc->IsXULDocument()) {
dom::XULDocument* xulDoc = doc->AsXULDocument();
nsCOMPtr<nsIHTMLCollection> possibleDefaultButtons =
Expand All @@ -1683,7 +1685,13 @@ Relation Accessible::RelationByType(RelationType aType) const {
uint32_t length = possibleDefaultButtons->Length();
// Check for button in list of default="true" elements
for (uint32_t count = 0; count < length && !buttonEl; count++) {
buttonEl = do_QueryInterface(possibleDefaultButtons->Item(count));
nsIContent* item = possibleDefaultButtons->Item(count);
RefPtr<nsIDOMXULButtonElement> button =
item->IsElement() ? item->AsElement()->AsXULButton()
: nullptr;
if (button) {
buttonEl = item;
}
}
}
if (!buttonEl) { // Check for anonymous accept button in <dialog>
Expand All @@ -1692,11 +1700,16 @@ Relation Accessible::RelationByType(RelationType aType) const {
nsIContent* possibleButtonEl =
rootElm->OwnerDoc()->GetAnonymousElementByAttribute(
rootElm, nsGkAtoms::_default, NS_LITERAL_STRING("true"));
buttonEl = do_QueryInterface(possibleButtonEl);
if (possibleButtonEl && possibleButtonEl->IsElement()) {
RefPtr<nsIDOMXULButtonElement> button =
possibleButtonEl->AsElement()->AsXULButton();
if (button) {
buttonEl = possibleButtonEl;
}
}
}
}
nsCOMPtr<nsIContent> relatedContent(do_QueryInterface(buttonEl));
return Relation(mDoc, relatedContent);
return Relation(mDoc, buttonEl);
}
}
return Relation();
Expand Down
2 changes: 1 addition & 1 deletion accessible/generic/RootAccessible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void RootAccessible::ProcessDOMEvent(Event* aDOMEvent) {
// If multiselect tree, we should fire selectionadd or selection removed
if (FocusMgr()->HasDOMFocus(targetNode)) {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSel =
do_QueryInterface(targetNode);
targetNode->AsElement()->AsXULMultiSelectControl();
nsAutoString selType;
multiSel->GetSelType(selType);
if (selType.IsEmpty() || !selType.EqualsLiteral("single")) {
Expand Down
12 changes: 6 additions & 6 deletions accessible/xul/XULComboboxAccessible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ uint64_t XULComboboxAccessible::NativeState() const {
// Get focus status from base class
uint64_t state = Accessible::NativeState();

nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList();
if (menuList) {
bool isOpen = false;
menuList->GetOpen(&isOpen);
Expand All @@ -70,7 +70,7 @@ uint64_t XULComboboxAccessible::NativeState() const {
void XULComboboxAccessible::Description(nsString& aDescription) {
aDescription.Truncate();
// Use description of currently focused option
nsCOMPtr<nsIDOMXULMenuListElement> menuListElm(do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = Elm()->AsXULMenuList();
if (!menuListElm) return;

nsCOMPtr<Element> focusedOptionItem;
Expand All @@ -85,7 +85,7 @@ void XULComboboxAccessible::Value(nsString& aValue) const {
aValue.Truncate();

// The value is the option or text shown entered in the combobox.
nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList();
if (menuList) menuList->GetLabel(aValue);
}

Expand All @@ -98,7 +98,7 @@ bool XULComboboxAccessible::DoAction(uint8_t aIndex) const {
if (aIndex != XULComboboxAccessible::eAction_Click) return false;

// Programmaticaly toggle the combo box.
nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList();
if (!menuList) return false;

bool isDroppedDown = false;
Expand All @@ -111,7 +111,7 @@ void XULComboboxAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) {
aName.Truncate();
if (aIndex != XULComboboxAccessible::eAction_Click) return;

nsCOMPtr<nsIDOMXULMenuListElement> menuList(do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList();
if (!menuList) return;

bool isDroppedDown = false;
Expand Down Expand Up @@ -153,7 +153,7 @@ bool XULComboboxAccessible::AreItemsOperable() const {
return false;
}

nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = do_QueryInterface(mContent);
nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = Elm()->AsXULMenuList();
if (menuListElm) {
bool isOpen = false;
menuListElm->GetOpen(&isOpen);
Expand Down
9 changes: 4 additions & 5 deletions accessible/xul/XULFormControlAccessible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ uint64_t XULButtonAccessible::NativeState() const {
uint64_t state = Accessible::NativeState();

// Buttons can be checked -- they simply appear pressed in rather than checked
nsCOMPtr<nsIDOMXULButtonElement> xulButtonElement(
do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULButtonElement> xulButtonElement = Elm()->AsXULButton();
if (xulButtonElement) {
nsAutoString type;
xulButtonElement->GetType(type);
Expand Down Expand Up @@ -157,15 +156,15 @@ bool XULDropmarkerAccessible::DropmarkerOpen(bool aToggleOpen) const {

while (parent) {
nsCOMPtr<nsIDOMXULButtonElement> parentButtonElement =
do_QueryInterface(parent);
parent->AsElement()->AsXULButton();
if (parentButtonElement) {
parentButtonElement->GetOpen(&isOpen);
if (aToggleOpen) parentButtonElement->SetOpen(!isOpen);
return isOpen;
}

nsCOMPtr<nsIDOMXULMenuListElement> parentMenuListElement =
do_QueryInterface(parent);
parent->AsElement()->AsXULMenuList();
if (parentMenuListElement) {
parentMenuListElement->GetOpen(&isOpen);
if (aToggleOpen) parentMenuListElement->SetOpen(!isOpen);
Expand Down Expand Up @@ -247,7 +246,7 @@ uint64_t XULRadioButtonAccessible::NativeState() const {
state |= states::CHECKABLE;

nsCOMPtr<nsIDOMXULSelectControlItemElement> radioButton =
do_QueryInterface(mContent);
Elm()->AsXULSelectControlItem();
if (radioButton) {
bool selected = false; // Radio buttons can be selected
radioButton->GetSelected(&selected);
Expand Down
52 changes: 27 additions & 25 deletions accessible/xul/XULListboxAccessible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ uint64_t XULListboxAccessible::NativeState() const {
void XULListboxAccessible::Value(nsString& aValue) const {
aValue.Truncate();

nsCOMPtr<nsIDOMXULSelectControlElement> select(do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULSelectControlElement> select = Elm()->AsXULSelectControl();
if (select) {
RefPtr<Element> element;
select->GetSelectedItem(getter_AddRefs(element));

nsCOMPtr<nsIDOMXULSelectControlItemElement> selectedItem =
do_QueryInterface(element);
element->AsXULSelectControlItem();
if (selectedItem) selectedItem->GetLabel(aValue);
}
}
Expand All @@ -133,7 +133,7 @@ role XULListboxAccessible::NativeRole() const {
uint32_t XULListboxAccessible::ColCount() const { return 0; }

uint32_t XULListboxAccessible::RowCount() {
nsCOMPtr<nsIDOMXULSelectControlElement> element(do_QueryInterface(mContent));
nsCOMPtr<nsIDOMXULSelectControlElement> element = Elm()->AsXULSelectControl();

uint32_t itemCount = 0;
if (element) element->GetItemCount(&itemCount);
Expand All @@ -143,7 +143,7 @@ uint32_t XULListboxAccessible::RowCount() {

Accessible* XULListboxAccessible::CellAt(uint32_t aRowIndex,
uint32_t aColumnIndex) {
nsCOMPtr<nsIDOMXULSelectControlElement> control = do_QueryInterface(mContent);
nsCOMPtr<nsIDOMXULSelectControlElement> control = Elm()->AsXULSelectControl();
NS_ENSURE_TRUE(control, nullptr);

RefPtr<Element> element;
Expand All @@ -158,7 +158,7 @@ Accessible* XULListboxAccessible::CellAt(uint32_t aRowIndex,

bool XULListboxAccessible::IsColSelected(uint32_t aColIdx) {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

Expand All @@ -170,14 +170,15 @@ bool XULListboxAccessible::IsColSelected(uint32_t aColIdx) {
}

bool XULListboxAccessible::IsRowSelected(uint32_t aRowIdx) {
nsCOMPtr<nsIDOMXULSelectControlElement> control = do_QueryInterface(mContent);
nsCOMPtr<nsIDOMXULSelectControlElement> control = Elm()->AsXULSelectControl();
NS_ASSERTION(control, "Doesn't implement nsIDOMXULSelectControlElement.");

RefPtr<Element> element;
nsresult rv = control->GetItemAtIndex(aRowIdx, getter_AddRefs(element));
NS_ENSURE_SUCCESS(rv, false);

nsCOMPtr<nsIDOMXULSelectControlItemElement> item = do_QueryInterface(element);
nsCOMPtr<nsIDOMXULSelectControlItemElement> item =
element->AsXULSelectControlItem();

bool isSelected = false;
item->GetSelected(&isSelected);
Expand All @@ -190,7 +191,7 @@ bool XULListboxAccessible::IsCellSelected(uint32_t aRowIdx, uint32_t aColIdx) {

uint32_t XULListboxAccessible::SelectedCellCount() {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

Expand All @@ -205,7 +206,7 @@ uint32_t XULListboxAccessible::SelectedCellCount() {

uint32_t XULListboxAccessible::SelectedColCount() {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

Expand All @@ -221,7 +222,7 @@ uint32_t XULListboxAccessible::SelectedColCount() {

uint32_t XULListboxAccessible::SelectedRowCount() {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

Expand All @@ -234,7 +235,7 @@ uint32_t XULListboxAccessible::SelectedRowCount() {

void XULListboxAccessible::SelectedCells(nsTArray<Accessible*>* aCells) {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

Expand All @@ -260,7 +261,7 @@ void XULListboxAccessible::SelectedCells(nsTArray<Accessible*>* aCells) {

void XULListboxAccessible::SelectedCellIndices(nsTArray<uint32_t>* aCells) {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

Expand All @@ -277,9 +278,9 @@ void XULListboxAccessible::SelectedCellIndices(nsTArray<uint32_t>* aCells) {
for (uint32_t selItemsIdx = 0, cellsIdx = 0; selItemsIdx < selectedItemsCount;
selItemsIdx++) {
nsIContent* itemContent = selectedItems->Item(selItemsIdx);
nsCOMPtr<nsIDOMXULSelectControlItemElement> item =
do_QueryInterface(itemContent);

nsCOMPtr<nsIDOMXULSelectControlItemElement> item =
itemContent->AsElement()->AsXULSelectControlItem();
if (item) {
int32_t itemIdx = -1;
control->GetIndexOfItem(item, &itemIdx);
Expand All @@ -300,7 +301,7 @@ void XULListboxAccessible::SelectedColIndices(nsTArray<uint32_t>* aCols) {

void XULListboxAccessible::SelectedRowIndices(nsTArray<uint32_t>* aRows) {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

Expand All @@ -318,7 +319,7 @@ void XULListboxAccessible::SelectedRowIndices(nsTArray<uint32_t>* aRows) {
for (uint32_t rowIdx = 0; rowIdx < rowCount; rowIdx++) {
nsIContent* itemContent = selectedItems->Item(rowIdx);
nsCOMPtr<nsIDOMXULSelectControlItemElement> item =
do_QueryInterface(itemContent);
itemContent->AsElement()->AsXULSelectControlItem();

if (item) {
int32_t itemIdx = -1;
Expand All @@ -330,27 +331,29 @@ void XULListboxAccessible::SelectedRowIndices(nsTArray<uint32_t>* aRows) {

void XULListboxAccessible::SelectRow(uint32_t aRowIdx) {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

RefPtr<Element> item;
control->GetItemAtIndex(aRowIdx, getter_AddRefs(item));

nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm = do_QueryInterface(item);
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm =
item->AsXULSelectControlItem();
control->SelectItem(itemElm);
}

void XULListboxAccessible::UnselectRow(uint32_t aRowIdx) {
nsCOMPtr<nsIDOMXULMultiSelectControlElement> control =
do_QueryInterface(mContent);
Elm()->AsXULMultiSelectControl();
NS_ASSERTION(control,
"Doesn't implement nsIDOMXULMultiSelectControlElement.");

RefPtr<Element> item;
control->GetItemAtIndex(aRowIdx, getter_AddRefs(item));

nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm = do_QueryInterface(item);
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm =
item->AsXULSelectControlItem();
control->RemoveItemFromSelection(itemElm);
}

Expand Down Expand Up @@ -388,13 +391,13 @@ bool XULListboxAccessible::AreItemsOperable() const {
}

Accessible* XULListboxAccessible::ContainerWidget() const {
if (IsAutoCompletePopup()) {
if (IsAutoCompletePopup() && mContent->GetParent()) {
// This works for XUL autocompletes. It doesn't work for HTML forms
// autocomplete because of potential crossprocess calls (when autocomplete
// lives in content process while popup lives in chrome process). If that's
// a problem then rethink Widgets interface.
nsCOMPtr<nsIDOMXULMenuListElement> menuListElm =
do_QueryInterface(mContent->GetParent());
mContent->GetParent()->AsElement()->AsXULMenuList();
if (menuListElm) {
RefPtr<mozilla::dom::Element> inputElm;
menuListElm->GetInputField(getter_AddRefs(inputElm));
Expand Down Expand Up @@ -429,7 +432,7 @@ Accessible* XULListitemAccessible::GetListAccessible() const {
if (IsDefunct()) return nullptr;

nsCOMPtr<nsIDOMXULSelectControlItemElement> listItem =
do_QueryInterface(mContent);
Elm()->AsXULSelectControlItem();
if (!listItem) return nullptr;

RefPtr<Element> listElement;
Expand Down Expand Up @@ -479,8 +482,7 @@ uint64_t XULListitemAccessible::NativeState() const {
uint64_t states = NativeInteractiveState();

nsCOMPtr<nsIDOMXULSelectControlItemElement> listItem =
do_QueryInterface(mContent);

Elm()->AsXULSelectControlItem();
if (listItem) {
bool isSelected;
listItem->GetSelected(&isSelected);
Expand Down
Loading

0 comments on commit 9397165

Please sign in to comment.