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

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: Moved layout tests to another CL. Created 5 years, 5 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
« no previous file with comments | « Source/modules/accessibility/AXLayoutObject.h ('k') | Source/modules/accessibility/AXObject.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Functions that retrieve 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.isValid())
1844 1846 return textSelection;
1845 if (m_layoutObject->isTextControl()) { 1847
1846 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1848 if (!layoutObject() || !layoutObject()->frame())
1847 return PlainTextRange(textControl->selectionStart(), textControl->select ionEnd() - textControl->selectionStart()); 1849 return AXRange();
1848 } 1850
1849 1851 VisibleSelection selection = layoutObject()->frame()->selection().selection( );
1850 return visibleSelectionUnderObject(); 1852 RefPtrWillBeRawPtr<Range> selectionRange = selection.firstRange();
1851 } 1853 if (!selectionRange)
1852 1854 return AXRange();
1853 VisibleSelection AXLayoutObject::selection() const 1855
1854 { 1856 int anchorOffset = selectionRange->startOffset();
1855 return m_layoutObject->frame()->selection().selection(); 1857 ASSERT(anchorOffset >= 0);
1858 int focusOffset = selectionRange->endOffset();
1859 ASSERT(focusOffset >= 0);
1860
1861 Node* anchorNode = selectionRange->startContainer();
1862 ASSERT(anchorNode);
1863
1864 RefPtrWillBeRawPtr<AXObject> anchorObject = nullptr;
1865 // Find the closest node that has a corresponding AXObject.
1866 // This is because some nodes may be aria hidden or might not even have
1867 // a layout object if they are part of the shadow DOM.
1868 while (anchorNode && !(anchorObject = axObjectCache().getOrCreate(anchorNode ))
1869 && (!anchorObject->isAXLayoutObject() || !anchorObject->node() || anchor Object->accessibilityIsIgnored())) {
1870 if (anchorNode->nextSibling())
1871 anchorNode = anchorNode->nextSibling();
1872 else
1873 anchorNode = anchorNode->parentNode();
1874 }
1875 if (anchorNode != selectionRange->startContainer())
1876 anchorOffset = 0;
1877
1878 Node* focusNode = selectionRange->endContainer();
1879 ASSERT(focusNode);
1880
1881 RefPtrWillBeRawPtr<AXObject> focusObject = nullptr;
1882 while (focusNode && !(focusObject = axObjectCache().getOrCreate(focusNode))
1883 && (!focusObject->isAXLayoutObject() || !focusObject->node() || focusObj ect->accessibilityIsIgnored())) {
1884 if (focusNode->previousSibling())
1885 focusNode = focusNode->previousSibling();
1886 else
1887 focusNode = focusNode->parentNode();
1888 }
1889 if (focusNode != selectionRange->endContainer())
1890 focusOffset = 0;
1891
1892 if (!anchorObject || !focusObject)
1893 return AXRange();
1894
1895 return AXRange(
1896 anchorObject, anchorOffset,
1897 focusObject, focusOffset);
1898 }
1899
1900 // Gets only the start and end offsets of the selection computed using the
1901 // current object as the starting point. Returns a null selection if there is
1902 // no selection in the subtree rooted at this object.
1903 AXObject::AXRange AXLayoutObject::selectionUnderObject() const
1904 {
1905 AXRange textSelection = textControlSelection();
1906 if (textSelection.isValid())
1907 return textSelection;
1908
1909 if (!layoutObject() || !layoutObject()->frame())
1910 return AXRange();
1911
1912 VisibleSelection selection = layoutObject()->frame()->selection().selection( );
1913 RefPtrWillBeRawPtr<Range> selectionRange = selection.firstRange();
1914 ContainerNode* parentNode = node()->parentNode();
1915 int nodeIndex = node()->nodeIndex();
1916 if (!selectionRange
1917 // Selection is contained in node.
1918 || !(parentNode
1919 && selectionRange->comparePoint(parentNode, nodeIndex, IGNORE_EXCEPTION) < 0
1920 && selectionRange->comparePoint(parentNode, nodeIndex + 1, IGNORE_EXCEPT ION) > 0)) {
1921 return AXRange();
1922 }
1923
1924 int start = indexForVisiblePosition(selection.visibleStart());
1925 ASSERT(start >= 0);
1926 int end = indexForVisiblePosition(selection.visibleEnd());
1927 ASSERT(end >= 0);
1928
1929 return AXRange(start, end);
1930 }
1931
1932 AXObject::AXRange AXLayoutObject::textControlSelection() const
1933 {
1934 if (!layoutObject())
1935 return AXRange();
1936
1937 LayoutObject* layout = nullptr;
1938 if (layoutObject()->isTextControl()) {
1939 layout = layoutObject();
1940 } else {
1941 Element* focusedElement = document()->focusedElement();
1942 if (focusedElement && focusedElement->layoutObject()
1943 && focusedElement->layoutObject()->isTextControl())
1944 layout = focusedElement->layoutObject();
1945 }
1946
1947 if (!layout)
1948 return AXRange();
1949
1950 AXObject* axObject = axObjectCache().getOrCreate(layout);
1951 if (!axObject || !axObject->isAXLayoutObject())
1952 return AXRange();
1953
1954 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1955 layout)->textFormControlElement();
1956 ASSERT(textControl);
1957 int start = textControl->selectionStart();
1958 int end = textControl->selectionEnd();
1959 return AXRange(axObject, start, axObject, end);
1960 }
1961
1962 int AXLayoutObject::indexForVisiblePosition(const VisiblePosition& position) con st
1963 {
1964 if (layoutObject() && layoutObject()->isTextControl()) {
1965 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1966 layoutObject())->textFormControlElement();
1967 return textControl->indexForVisiblePosition(position);
1968 }
1969
1970 if (!node())
1971 return 0;
1972
1973 Position indexPosition = position.deepEquivalent();
1974 if (indexPosition.isNull())
1975 return 0;
1976
1977 RefPtrWillBeRawPtr<Range> range = Range::create(*document());
1978 range->setStart(node(), 0, IGNORE_EXCEPTION);
1979 range->setEnd(indexPosition, IGNORE_EXCEPTION);
1980
1981 return TextIterator::rangeLength(range->startPosition(), range->endPosition( ));
1856 } 1982 }
1857 1983
1858 // 1984 //
1859 // Modify or take an action on an object. 1985 // Modify or take an action on an object.
1860 // 1986 //
1861 1987
1862 void AXLayoutObject::setSelectedTextRange(const PlainTextRange& range) 1988 void AXLayoutObject::setSelection(const AXRange& selection)
1863 { 1989 {
1864 if (m_layoutObject->isTextControl()) { 1990 if (!layoutObject() || !selection.isValid())
1865 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1991 return;
1866 textControl->setSelectionRange(range.start, range.start + range.length, SelectionHasNoDirection, NotDispatchSelectEvent); 1992
1867 return; 1993 if (selection.anchorObject && !isValidSelectionBound(selection.anchorObject. get()))
1868 } 1994 return;
1869 1995
1870 Document& document = m_layoutObject->document(); 1996 if (selection.focusObject && !isValidSelectionBound(selection.focusObject.ge t()))
1871 LocalFrame* frame = document.frame(); 1997 return;
1998
1999 AXObject* anchorObject = selection.anchorObject ?
2000 selection.anchorObject.get() : this;
2001 AXObject* focusObject = selection.focusObject ?
2002 selection.focusObject.get() : this;
2003
2004 if (anchorObject == this && anchorObject == focusObject
2005 && layoutObject()->isTextControl()) {
2006 HTMLTextFormControlElement* textControl = toLayoutTextControl(
2007 layoutObject())->textFormControlElement();
2008 textControl->setSelectionRange(selection.anchorOffset, selection.focusOf fset,
2009 SelectionHasNoDirection, NotDispatchSelectEvent);
2010 return;
2011 }
2012
2013 Node* anchorNode = nullptr;
2014 while (anchorObject && !anchorNode) {
2015 anchorNode = anchorObject->node();
2016 anchorObject = anchorObject->parentObject();
2017 }
2018
2019 Node* focusNode = nullptr;
2020 while (focusObject && !focusNode) {
2021 focusNode = focusObject->node();
2022 focusObject = focusObject->parentObject();
2023 }
2024
2025 if (!anchorNode || !focusNode)
2026 return;
2027
2028 LocalFrame* frame = layoutObject()->frame();
1872 if (!frame) 2029 if (!frame)
1873 return; 2030 return;
1874 Node* node = m_layoutObject->node(); 2031
1875 frame->selection().setSelection(VisibleSelection(Position(node, range.start) , 2032 frame->selection().setSelection(VisibleSelection(
1876 Position(node, range.start + range.length), DOWNSTREAM)); 2033 Position(anchorNode, selection.anchorOffset),
2034 Position(focusNode, selection.focusOffset),
2035 DOWNSTREAM));
2036 }
2037
2038 bool AXLayoutObject::isValidSelectionBound(const AXObject* boundObject) const
2039 {
2040 return boundObject && !boundObject->isDetached()
2041 && boundObject->isAXLayoutObject()
2042 && boundObject->layoutObject()->frame() == layoutObject()->frame()
2043 && &boundObject->axObjectCache() == &axObjectCache();
1877 } 2044 }
1878 2045
1879 void AXLayoutObject::setValue(const String& string) 2046 void AXLayoutObject::setValue(const String& string)
1880 { 2047 {
1881 if (!node() || !node()->isElementNode()) 2048 if (!node() || !node()->isElementNode())
1882 return; 2049 return;
1883 if (!m_layoutObject || !m_layoutObject->isBoxModelObject()) 2050 if (!m_layoutObject || !m_layoutObject->isBoxModelObject())
1884 return; 2051 return;
1885 2052
1886 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject); 2053 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2001 Position start, end; 2168 Position start, end;
2002 bool selected = Range::selectNodeContents(node, start, end); 2169 bool selected = Range::selectNodeContents(node, start, end);
2003 if (!selected) 2170 if (!selected)
2004 return VisiblePosition(); 2171 return VisiblePosition();
2005 2172
2006 CharacterIterator it(start, end); 2173 CharacterIterator it(start, end);
2007 it.advance(index - 1); 2174 it.advance(index - 1);
2008 return VisiblePosition(Position(it.currentContainer(), it.endOffset()), UPST REAM); 2175 return VisiblePosition(Position(it.currentContainer(), it.endOffset()), UPST REAM);
2009 } 2176 }
2010 2177
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) 2178 void AXLayoutObject::addInlineTextBoxChildren(bool force)
2039 { 2179 {
2040 Settings* settings = document()->settings(); 2180 Settings* settings = document()->settings();
2041 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled())) 2181 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled()))
2042 return; 2182 return;
2043 2183
2044 if (!layoutObject() || !layoutObject()->isText()) 2184 if (!layoutObject() || !layoutObject()->isText())
2045 return; 2185 return;
2046 2186
2047 if (layoutObject()->needsLayout()) { 2187 if (layoutObject()->needsLayout()) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2109 for (const auto& child : children()) { 2249 for (const auto& child : children()) {
2110 // Every child should have aria-role option, and if so, check for select ed attribute/state. 2250 // Every child should have aria-role option, and if so, check for select ed attribute/state.
2111 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) { 2251 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) {
2112 result.append(child); 2252 result.append(child);
2113 if (!isMulti) 2253 if (!isMulti)
2114 return; 2254 return;
2115 } 2255 }
2116 } 2256 }
2117 } 2257 }
2118 2258
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 2259 bool AXLayoutObject::nodeIsTextControl(const Node* node) const
2137 { 2260 {
2138 if (!node) 2261 if (!node)
2139 return false; 2262 return false;
2140 2263
2141 const AXObject* axObjectForNode = axObjectCache().getOrCreate(const_cast<Nod e*>(node)); 2264 const AXObject* axObjectForNode = axObjectCache().getOrCreate(const_cast<Nod e*>(node));
2142 if (!axObjectForNode) 2265 if (!axObjectForNode)
2143 return false; 2266 return false;
2144 2267
2145 return axObjectForNode->isTextControl(); 2268 return axObjectForNode->isTextControl();
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
2508 if (label && label->layoutObject()) { 2631 if (label && label->layoutObject()) {
2509 LayoutRect labelRect = axObjectCache().getOrCreate(label)->elementRe ct(); 2632 LayoutRect labelRect = axObjectCache().getOrCreate(label)->elementRe ct();
2510 result.unite(labelRect); 2633 result.unite(labelRect);
2511 } 2634 }
2512 } 2635 }
2513 2636
2514 return result; 2637 return result;
2515 } 2638 }
2516 2639
2517 } // namespace blink 2640 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/accessibility/AXLayoutObject.h ('k') | Source/modules/accessibility/AXObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698