Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: third_party/WebKit/Source/modules/accessibility/AXLayoutObject.cpp

Issue 2339093003: Support child-based node offsets when setting accessible selections (Closed)
Patch Set: More tests. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 1799 matching lines...) Expand 10 before | Expand all | Expand 10 after
1810 } 1810 }
1811 1811
1812 1812
1813 // 1813 //
1814 // Modify or take an action on an object. 1814 // Modify or take an action on an object.
1815 // 1815 //
1816 1816
1817 // Convert from an accessible object and offset to a VisiblePosition. 1817 // Convert from an accessible object and offset to a VisiblePosition.
1818 static VisiblePosition toVisiblePosition(AXObject* obj, int offset) 1818 static VisiblePosition toVisiblePosition(AXObject* obj, int offset)
1819 { 1819 {
1820 // First walk up until we find an accessible object with an associated node. 1820 if (!obj->getNode())
1821 AXObject* runner = obj;
1822 Node* node = nullptr;
1823 while (runner && !node) {
1824 node = runner->getNode();
1825 runner = runner->parentObject();
1826 }
1827
1828 if (!node)
1829 return VisiblePosition(); 1821 return VisiblePosition();
1830 1822
1831 // If it's not a text node, no conversion is necessary, just create a Visibl ePosition 1823 Node* node = obj->getNode();
1832 // with this node and offset. 1824 if (!node->isTextNode()) {
dmazzoni 2016/09/15 16:32:36 It's possible to call AXObject::setSelection on an
1833 if (!node->isTextNode()) 1825 // The offsets are child offsets over the AX tree. Note that we allow
1834 return createVisiblePosition(Position(node, offset)); 1826 // for the offset to equal the number of children as |Range| does.
1827 if (offset < 0 || static_cast<unsigned>(offset) > obj->children().size() )
1828 return VisiblePosition();
1829
1830 // Clamp to between 0 and child count - 1.
1831 int clampedOffset =
1832 static_cast<unsigned>(offset) > (obj->children().size() - 1) ? offse t - 1 : offset;
dmazzoni 2016/09/15 16:32:36 Clearer using std::min?
1833 AXObject* childObj = obj->children()[clampedOffset];
1834 Node* childNode = childObj->getNode();
1835 if (!childNode || !childNode->parentNode())
1836 return VisiblePosition();
1837
1838 // The index in parent.
1839 int adjustedOffset = -1;
1840 NodeList* childNodes = childNode->parentNode()->childNodes();
1841 for (unsigned i = 0; i < childNodes->length(); ++i) {
1842 if (childNodes->item(i) == childNode) {
1843 adjustedOffset = i;
1844 break;
1845 }
1846 }
1847
1848 if (adjustedOffset == -1)
1849 return VisiblePosition();
1850
1851 // If we had to clamp the offset above, the client wants to select the
1852 // end of the node.
1853 if (clampedOffset != offset)
1854 adjustedOffset++;
1855
1856 return createVisiblePosition(Position::editingPositionOf(childNode->pare ntNode(), adjustedOffset));
dmazzoni 2016/09/15 16:32:36 Instead of computing the index in parent in the DO
1857 }
1835 1858
1836 // If it is a text node, we need to call some utility functions that use a T extIterator 1859 // If it is a text node, we need to call some utility functions that use a T extIterator
1837 // to walk the characters of the node and figure out the position correspond ing to the 1860 // to walk the characters of the node and figure out the position correspond ing to the
1838 // visible character at position |offset|. 1861 // visible character at position |offset|.
1839 ContainerNode* parent = node->parentNode(); 1862 ContainerNode* parent = node->parentNode();
1840 if (!parent) 1863 if (!parent)
1841 return VisiblePosition(); 1864 return VisiblePosition();
1842 1865
1843 VisiblePosition nodePosition = blink::visiblePositionBeforeNode(*node); 1866 VisiblePosition nodePosition = blink::visiblePositionBeforeNode(*node);
1844 int nodeIndex = blink::indexForVisiblePosition(nodePosition, parent); 1867 int nodeIndex = blink::indexForVisiblePosition(nodePosition, parent);
1845 return blink::visiblePositionForIndex(nodeIndex + offset, parent); 1868 return blink::visiblePositionForIndex(nodeIndex + offset, parent);
1846 } 1869 }
1847 1870
1848 void AXLayoutObject::setSelection(const AXRange& selection) 1871 void AXLayoutObject::setSelection(const AXRange& selection)
1849 { 1872 {
1850 if (!getLayoutObject() || !selection.isValid()) 1873 if (!getLayoutObject() || !selection.isValid())
1851 return; 1874 return;
1852 1875
1853 AXObject* anchorObject = selection.anchorObject ? 1876 AXObject* anchorObject = selection.anchorObject ?
1854 selection.anchorObject.get() : this; 1877 selection.anchorObject.get() : this;
1855 AXObject* focusObject = selection.focusObject ? 1878 AXObject* focusObject = selection.focusObject ?
1856 selection.focusObject.get() : this; 1879 selection.focusObject.get() : this;
1857 1880
1858 if (!isValidSelectionBound(anchorObject) 1881 if (!isValidSelectionBound(anchorObject)
1859 || !isValidSelectionBound(focusObject)) { 1882 || !isValidSelectionBound(focusObject)) {
1860 return; 1883 return;
1861 } 1884 }
1862 1885
1886 // The selection offsets are offsets into the accessible value.
1863 if (anchorObject == focusObject 1887 if (anchorObject == focusObject
1864 && anchorObject->getLayoutObject()->isTextControl()) { 1888 && anchorObject->getLayoutObject()->isTextControl()) {
1865 HTMLTextFormControlElement* textControl = toLayoutTextControl( 1889 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1866 anchorObject->getLayoutObject())->textFormControlElement(); 1890 anchorObject->getLayoutObject())->textFormControlElement();
1867 if (selection.anchorOffset <= selection.focusOffset) { 1891 if (selection.anchorOffset <= selection.focusOffset) {
1868 textControl->setSelectionRange( 1892 textControl->setSelectionRange(
1869 selection.anchorOffset, selection.focusOffset, 1893 selection.anchorOffset, selection.focusOffset,
1870 SelectionHasForwardDirection); 1894 SelectionHasForwardDirection);
1871 } else { 1895 } else {
1872 textControl->setSelectionRange( 1896 textControl->setSelectionRange(
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
2371 2395
2372 bool AXLayoutObject::elementAttributeValue(const QualifiedName& attributeName) c onst 2396 bool AXLayoutObject::elementAttributeValue(const QualifiedName& attributeName) c onst
2373 { 2397 {
2374 if (!m_layoutObject) 2398 if (!m_layoutObject)
2375 return false; 2399 return false;
2376 2400
2377 return equalIgnoringCase(getAttribute(attributeName), "true"); 2401 return equalIgnoringCase(getAttribute(attributeName), "true");
2378 } 2402 }
2379 2403
2380 } // namespace blink 2404 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698