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

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

Issue 1185343003: Implements the ability to get and set the caret position and the current selection from anywhere in… (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fixed retrieving the selection in text controls. Created 5 years, 6 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 15 matching lines...) Expand all
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "config.h" 29 #include "config.h"
30 #include "modules/accessibility/AXLayoutObject.h" 30 #include "modules/accessibility/AXLayoutObject.h"
31 31
32 #include "bindings/core/v8/ExceptionStatePlaceholder.h" 32 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
33 #include "core/CSSPropertyNames.h" 33 #include "core/CSSPropertyNames.h"
34 #include "core/InputTypeNames.h" 34 #include "core/InputTypeNames.h"
35 #include "core/dom/ElementTraversal.h" 35 #include "core/dom/ElementTraversal.h"
36 #include "core/dom/Range.h"
36 #include "core/dom/shadow/ShadowRoot.h" 37 #include "core/dom/shadow/ShadowRoot.h"
37 #include "core/editing/FrameSelection.h" 38 #include "core/editing/FrameSelection.h"
38 #include "core/editing/RenderedPosition.h" 39 #include "core/editing/RenderedPosition.h"
39 #include "core/editing/VisibleUnits.h" 40 #include "core/editing/VisibleUnits.h"
40 #include "core/editing/htmlediting.h" 41 #include "core/editing/htmlediting.h"
41 #include "core/editing/iterators/CharacterIterator.h" 42 #include "core/editing/iterators/CharacterIterator.h"
42 #include "core/editing/iterators/TextIterator.h" 43 #include "core/editing/iterators/TextIterator.h"
43 #include "core/frame/LocalFrame.h" 44 #include "core/frame/LocalFrame.h"
44 #include "core/frame/Settings.h" 45 #include "core/frame/Settings.h"
45 #include "core/html/HTMLImageElement.h" 46 #include "core/html/HTMLImageElement.h"
46 #include "core/html/HTMLLabelElement.h" 47 #include "core/html/HTMLLabelElement.h"
47 #include "core/html/HTMLOptionElement.h" 48 #include "core/html/HTMLOptionElement.h"
48 #include "core/html/HTMLSelectElement.h" 49 #include "core/html/HTMLSelectElement.h"
49 #include "core/html/HTMLTextAreaElement.h" 50 #include "core/html/HTMLTextAreaElement.h"
50 #include "core/html/shadow/ShadowElementNames.h" 51 #include "core/html/shadow/ShadowElementNames.h"
51 #include "core/layout/HitTestResult.h" 52 #include "core/layout/HitTestResult.h"
52 #include "core/layout/LayoutFieldset.h" 53 #include "core/layout/LayoutFieldset.h"
53 #include "core/layout/LayoutFileUploadControl.h" 54 #include "core/layout/LayoutFileUploadControl.h"
54 #include "core/layout/LayoutHTMLCanvas.h" 55 #include "core/layout/LayoutHTMLCanvas.h"
55 #include "core/layout/LayoutImage.h" 56 #include "core/layout/LayoutImage.h"
56 #include "core/layout/LayoutInline.h" 57 #include "core/layout/LayoutInline.h"
57 #include "core/layout/LayoutListMarker.h" 58 #include "core/layout/LayoutListMarker.h"
58 #include "core/layout/LayoutMenuList.h" 59 #include "core/layout/LayoutMenuList.h"
59 #include "core/layout/LayoutPart.h" 60 #include "core/layout/LayoutPart.h"
61 #include "core/layout/LayoutTextControl.h"
60 #include "core/layout/LayoutTextControlSingleLine.h" 62 #include "core/layout/LayoutTextControlSingleLine.h"
61 #include "core/layout/LayoutTextFragment.h" 63 #include "core/layout/LayoutTextFragment.h"
62 #include "core/layout/LayoutView.h" 64 #include "core/layout/LayoutView.h"
63 #include "core/loader/ProgressTracker.h" 65 #include "core/loader/ProgressTracker.h"
64 #include "core/page/Page.h" 66 #include "core/page/Page.h"
65 #include "core/paint/DeprecatedPaintLayer.h" 67 #include "core/paint/DeprecatedPaintLayer.h"
66 #include "core/style/ComputedStyleConstants.h" 68 #include "core/style/ComputedStyleConstants.h"
67 #include "core/svg/SVGDocumentExtensions.h" 69 #include "core/svg/SVGDocumentExtensions.h"
68 #include "core/svg/SVGSVGElement.h" 70 #include "core/svg/SVGSVGElement.h"
69 #include "core/svg/graphics/SVGImage.h" 71 #include "core/svg/graphics/SVGImage.h"
(...skipping 1757 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 } 1829 }
1828 1830
1829 Widget* AXLayoutObject::widgetForAttachmentView() const 1831 Widget* AXLayoutObject::widgetForAttachmentView() const
1830 { 1832 {
1831 if (!isAttachment()) 1833 if (!isAttachment())
1832 return 0; 1834 return 0;
1833 return toLayoutPart(m_layoutObject)->widget(); 1835 return toLayoutPart(m_layoutObject)->widget();
1834 } 1836 }
1835 1837
1836 // 1838 //
1837 // Selected text. 1839 // Get the current selection.
1838 // 1840 //
1839 1841
1840 AXObject::PlainTextRange AXLayoutObject::selectedTextRange() const 1842 AXObject::AXRange AXLayoutObject::selection() const
1841 { 1843 {
1842 if (!isTextControl()) 1844 AXRange textSelection = textControlSelection();
1843 return PlainTextRange(); 1845 if (!textSelection.isNull())
1846 return textSelection;
1844 1847
1845 if (m_layoutObject->isTextControl()) { 1848 if (!layoutObject() || !layoutObject()->frame())
1846 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1849 return AXRange();
1847 return PlainTextRange(textControl->selectionStart(), textControl->select ionEnd() - textControl->selectionStart()); 1850
1851 VisibleSelection selection = layoutObject()->frame()->selection().selection( );
1852 RefPtrWillBeRawPtr<Range> selectionRange = selection.firstRange();
1853 if (!selectionRange)
1854 return AXRange();
1855
1856 Node* anchorNode = selectionRange->startContainer();
1857 ASSERT(anchorNode);
1858
1859 RefPtrWillBeRawPtr<AXObject> anchorObject = nullptr;
1860 // Find the closest node that has a corresponding AXObject.
1861 while (anchorNode && !(anchorObject = axObjectCache().getOrCreate(anchorNode ))
1862 && (!anchorObject->isAXLayoutObject() || !anchorObject->node() || anchor Object->accessibilityIsIgnored())) {
1863 if (anchorNode->nextSibling())
dmazzoni 2015/06/26 06:14:38 I think you should update anchorOffset here. To t
1864 anchorNode = anchorNode->nextSibling();
1865 else
1866 anchorNode = anchorNode->parentNode();
1848 } 1867 }
1849 1868
1850 return visibleSelectionUnderObject(); 1869 Node* focusNode = selectionRange->endContainer();
1870 ASSERT(focusNode);
1871
1872 RefPtrWillBeRawPtr<AXObject> focusObject = nullptr;
1873 while (focusNode && !(focusObject = axObjectCache().getOrCreate(focusNode))
1874 && (!focusObject->isAXLayoutObject() || !focusObject->node() || focusObj ect->accessibilityIsIgnored())) {
1875 if (focusNode->previousSibling())
1876 focusNode = focusNode->previousSibling();
1877 else
1878 focusNode = focusNode->parentNode();
1879 }
1880
1881 if (!anchorObject || !focusObject)
1882 return AXRange();
1883
1884 int anchorOffset = selectionRange->startOffset();
1885 ASSERT(anchorOffset >= 0);
1886 int focusOffset = selectionRange->endOffset();
1887 ASSERT(focusOffset >= 0);
1888
1889 return AXRange(
1890 anchorObject, anchorOffset,
1891 focusObject, focusOffset);
1851 } 1892 }
1852 1893
1853 VisibleSelection AXLayoutObject::selection() const 1894 // Gets only the start and end offsets of the selection computed using the
1895 // current object as the starting point. Returns a null selection if there is
1896 // no selection in the subtree rooted at this object.
1897 AXObject::AXRange AXLayoutObject::selectionUnderObject() const
1854 { 1898 {
1855 return m_layoutObject->frame()->selection().selection(); 1899 AXRange textSelection = textControlSelection();
1900 if (!textSelection.isNull())
1901 return textSelection;
1902
1903 if (!layoutObject() || !layoutObject()->frame())
1904 return AXRange();
1905
1906 VisibleSelection selection = layoutObject()->frame()->selection().selection( );
1907 RefPtrWillBeRawPtr<Range> selectionRange = selection.firstRange();
1908 if (!selectionRange
1909 || selectionRange->compareNode(node(), IGNORE_EXCEPTION) != Range::NODE_ BEFORE_AND_AFTER)
1910 return AXRange();
1911
1912 int start = indexForVisiblePosition(selection.visibleStart());
1913 ASSERT(start >= 0);
1914 int end = indexForVisiblePosition(selection.visibleEnd());
1915 ASSERT(end >= 0);
1916
1917 return AXRange(start, end);
1918 }
1919
1920 AXObject::AXRange AXLayoutObject::textControlSelection() const
1921 {
1922 if (!layoutObject())
1923 return AXRange();
1924
1925 LayoutObject* layout = nullptr;
1926 if (layoutObject()->isTextControl()) {
1927 layout = layoutObject();
1928 } else {
1929 Element* focusedElement = document()->focusedElement();
1930 if (focusedElement && focusedElement->layoutObject()
1931 && focusedElement->layoutObject()->isTextControl())
1932 layout = focusedElement->layoutObject();
1933 }
1934
1935 if (!layout)
1936 return AXRange();
1937
1938 AXObject* axObject = axObjectCache().getOrCreate(layout);
1939 if (!axObject || !axObject->isAXLayoutObject())
1940 return AXRange();
1941
1942 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1943 layout)->textFormControlElement();
1944 ASSERT(textControl);
1945 int start = textControl->selectionStart();
1946 int end = textControl->selectionEnd();
1947 return AXRange(axObject, start, axObject, end);
1948 }
1949
1950 int AXLayoutObject::indexForVisiblePosition(const VisiblePosition& position) con st
1951 {
1952 if (layoutObject() && layoutObject()->isTextControl()) {
1953 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1954 layoutObject())->textFormControlElement();
1955 return textControl->indexForVisiblePosition(position);
1956 }
1957
1958 if (!node())
1959 return 0;
1960
1961 Position indexPosition = position.deepEquivalent();
1962 if (indexPosition.isNull())
1963 return 0;
1964
1965 RefPtrWillBeRawPtr<Range> range = Range::create(*document());
1966 range->setStart(node(), 0, IGNORE_EXCEPTION);
1967 range->setEnd(indexPosition, IGNORE_EXCEPTION);
1968
1969 return TextIterator::rangeLength(range->startPosition(), range->endPosition( ));
1856 } 1970 }
1857 1971
1858 // 1972 //
1859 // Modify or take an action on an object. 1973 // Modify or take an action on an object.
1860 // 1974 //
1861 1975
1862 void AXLayoutObject::setSelectedTextRange(const PlainTextRange& range) 1976 void AXLayoutObject::setSelection(const AXRange& selection)
1863 { 1977 {
1864 if (m_layoutObject->isTextControl()) { 1978 if (!layoutObject() || selection.isNull())
1865 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1979 return;
1866 textControl->setSelectionRange(range.start, range.start + range.length, SelectionHasNoDirection, NotDispatchSelectEvent); 1980
1981 if (selection.anchorObject
dmazzoni 2015/06/26 06:14:38 Nit: this could be a helper function like IsValidS
1982 && (selection.anchorObject->isDetached()
1983 || !selection.anchorObject->isAXLayoutObject()
1984 || selection.anchorObject->layoutObject()->frame() != layoutObject()->fr ame()
1985 || &selection.anchorObject->axObjectCache() != &axObjectCache()))
1986 return;
1987
1988 if (selection.focusObject
1989 && (selection.focusObject->isDetached()
1990 || !selection.focusObject->isAXLayoutObject()
1991 || selection.focusObject->layoutObject()->frame() != layoutObject()->fra me()
1992 || &selection.focusObject->axObjectCache() != &axObjectCache()))
1993 return;
1994
1995 AXObject* anchorObject = selection.anchorObject ?
dmazzoni 2015/06/26 06:14:38 You could maybe combine this too - have the helper
1996 selection.anchorObject.get() : this;
1997 AXObject* focusObject = selection.focusObject ?
1998 selection.focusObject.get() : this;
1999
2000 if (anchorObject == this && anchorObject == focusObject
2001 && layoutObject()->isTextControl()) {
2002 HTMLTextFormControlElement* textControl = toLayoutTextControl(
2003 layoutObject())->textFormControlElement();
dmazzoni 2015/06/26 06:14:38 When the indentation is 4 spaces, I think the line
2004 textControl->setSelectionRange(selection.anchorOffset, selection.focusOf fset,
2005 SelectionHasNoDirection, NotDispatchSelectEvent);
1867 return; 2006 return;
1868 } 2007 }
1869 2008
1870 Document& document = m_layoutObject->document(); 2009 Node* anchorNode = nullptr;
dmazzoni 2015/06/26 06:14:38 Possibly a helper here, like nearestNodeFromAXObje
1871 LocalFrame* frame = document.frame(); 2010 while (anchorObject && !anchorNode) {
2011 anchorNode = anchorObject->node();
2012 anchorObject = anchorObject->parentObject();
2013 }
2014
2015 Node* focusNode = nullptr;
2016 while (focusObject && !focusNode) {
2017 focusNode = focusObject->node();
2018 focusObject = focusObject->parentObject();
2019 }
2020
2021 if (!anchorNode || !focusNode)
2022 return;
2023
2024 LocalFrame* frame = layoutObject()->frame();
1872 if (!frame) 2025 if (!frame)
1873 return; 2026 return;
1874 Node* node = m_layoutObject->node(); 2027
1875 frame->selection().setSelection(VisibleSelection(Position(node, range.start, Position::PositionIsOffsetInAnchor), 2028 frame->selection().setSelection(VisibleSelection(Position(anchorNode,
1876 Position(node, range.start + range.length, Position::PositionIsOffsetInA nchor), DOWNSTREAM)); 2029 selection.anchorOffset, Position::PositionIsOffsetInAnchor),
2030 Position(focusNode, selection.focusOffset,
2031 Position::PositionIsOffsetInAnchor), DOWNSTREAM));
1877 } 2032 }
1878 2033
1879 void AXLayoutObject::setValue(const String& string) 2034 void AXLayoutObject::setValue(const String& string)
1880 { 2035 {
1881 if (!node() || !node()->isElementNode()) 2036 if (!node() || !node()->isElementNode())
1882 return; 2037 return;
1883 if (!m_layoutObject || !m_layoutObject->isBoxModelObject()) 2038 if (!m_layoutObject || !m_layoutObject->isBoxModelObject())
1884 return; 2039 return;
1885 2040
1886 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject); 2041 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2001 Position start, end; 2156 Position start, end;
2002 bool selected = Range::selectNodeContents(node, start, end); 2157 bool selected = Range::selectNodeContents(node, start, end);
2003 if (!selected) 2158 if (!selected)
2004 return VisiblePosition(); 2159 return VisiblePosition();
2005 2160
2006 CharacterIterator it(start, end); 2161 CharacterIterator it(start, end);
2007 it.advance(index - 1); 2162 it.advance(index - 1);
2008 return VisiblePosition(Position(it.currentContainer(), it.endOffset(), Posit ion::PositionIsOffsetInAnchor), UPSTREAM); 2163 return VisiblePosition(Position(it.currentContainer(), it.endOffset(), Posit ion::PositionIsOffsetInAnchor), UPSTREAM);
2009 } 2164 }
2010 2165
2011 int AXLayoutObject::indexForVisiblePosition(const VisiblePosition& pos) const
2012 {
2013 if (m_layoutObject->isTextControl()) {
2014 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement();
2015 return textControl->indexForVisiblePosition(pos);
2016 }
2017
2018 if (!isTextControl())
2019 return 0;
2020
2021 Node* node = m_layoutObject->node();
2022 if (!node)
2023 return 0;
2024
2025 Position indexPosition = pos.deepEquivalent();
2026 if (indexPosition.isNull()
2027 || (highestEditableRoot(indexPosition) != node
2028 && highestEditableRoot(indexPosition, HasEditableAXRole) != node))
2029 return 0;
2030
2031 RefPtrWillBeRawPtr<Range> range = Range::create(m_layoutObject->document());
2032 range->setStart(node, 0, IGNORE_EXCEPTION);
2033 range->setEnd(indexPosition, IGNORE_EXCEPTION);
2034
2035 return TextIterator::rangeLength(range->startPosition(), range->endPosition( ));
2036 }
2037
2038 void AXLayoutObject::addInlineTextBoxChildren(bool force) 2166 void AXLayoutObject::addInlineTextBoxChildren(bool force)
2039 { 2167 {
2040 Settings* settings = document()->settings(); 2168 Settings* settings = document()->settings();
2041 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled())) 2169 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled()))
2042 return; 2170 return;
2043 2171
2044 if (!layoutObject() || !layoutObject()->isText()) 2172 if (!layoutObject() || !layoutObject()->isText())
2045 return; 2173 return;
2046 2174
2047 if (layoutObject()->needsLayout()) { 2175 if (layoutObject()->needsLayout()) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2109 for (const auto& child : children()) { 2237 for (const auto& child : children()) {
2110 // Every child should have aria-role option, and if so, check for select ed attribute/state. 2238 // Every child should have aria-role option, and if so, check for select ed attribute/state.
2111 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) { 2239 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) {
2112 result.append(child); 2240 result.append(child);
2113 if (!isMulti) 2241 if (!isMulti)
2114 return; 2242 return;
2115 } 2243 }
2116 } 2244 }
2117 } 2245 }
2118 2246
2119 AXObject::PlainTextRange AXLayoutObject::visibleSelectionUnderObject() const
2120 {
2121 Node* node = m_layoutObject->node();
2122 if (!node)
2123 return PlainTextRange();
2124
2125 VisibleSelection visibleSelection = selection();
2126 RefPtrWillBeRawPtr<Range> currentSelectionRange = visibleSelection.toNormali zedRange();
2127 if (!currentSelectionRange || !currentSelectionRange->intersectsNode(node, I GNORE_EXCEPTION))
2128 return PlainTextRange();
2129
2130 int start = indexForVisiblePosition(visibleSelection.visibleStart());
2131 int end = indexForVisiblePosition(visibleSelection.visibleEnd());
2132
2133 return PlainTextRange(start, end - start);
2134 }
2135
2136 bool AXLayoutObject::nodeIsTextControl(const Node* node) const 2247 bool AXLayoutObject::nodeIsTextControl(const Node* node) const
2137 { 2248 {
2138 if (!node) 2249 if (!node)
2139 return false; 2250 return false;
2140 2251
2141 const AXObject* axObjectForNode = axObjectCache().getOrCreate(const_cast<Nod e*>(node)); 2252 const AXObject* axObjectForNode = axObjectCache().getOrCreate(const_cast<Nod e*>(node));
2142 if (!axObjectForNode) 2253 if (!axObjectForNode)
2143 return false; 2254 return false;
2144 2255
2145 return axObjectForNode->isTextControl(); 2256 return axObjectForNode->isTextControl();
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 if (label && label->layoutObject()) { 2618 if (label && label->layoutObject()) {
2508 LayoutRect labelRect = axObjectCache().getOrCreate(label)->elementRe ct(); 2619 LayoutRect labelRect = axObjectCache().getOrCreate(label)->elementRe ct();
2509 result.unite(labelRect); 2620 result.unite(labelRect);
2510 } 2621 }
2511 } 2622 }
2512 2623
2513 return result; 2624 return result;
2514 } 2625 }
2515 2626
2516 } // namespace blink 2627 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698