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

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: Addressed more comments from reviewer. 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 1816 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 } 1827 }
1828 1828
1829 Widget* AXLayoutObject::widgetForAttachmentView() const 1829 Widget* AXLayoutObject::widgetForAttachmentView() const
1830 { 1830 {
1831 if (!isAttachment()) 1831 if (!isAttachment())
1832 return 0; 1832 return 0;
1833 return toLayoutPart(m_layoutObject)->widget(); 1833 return toLayoutPart(m_layoutObject)->widget();
1834 } 1834 }
1835 1835
1836 // 1836 //
1837 // Selected text. 1837 // Get the current selection.
1838 // 1838 //
1839 1839
1840 AXObject::PlainTextRange AXLayoutObject::selectedTextRange() const 1840 AXObject::AXRange AXLayoutObject::selection() const
1841 { 1841 {
1842 if (!isTextControl()) 1842 AXRange textSelection = textControlSelection();
1843 return PlainTextRange(); 1843 if (!textSelection.isNull())
1844 return textSelection;
1844 1845
1845 if (m_layoutObject->isTextControl()) { 1846 if (!layoutObject() || !layoutObject()->frame())
1846 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1847 return AXRange();
1847 return PlainTextRange(textControl->selectionStart(), textControl->select ionEnd() - textControl->selectionStart()); 1848
1849 VisibleSelection selection = layoutObject()->frame()->selection().selection( );
1850 RefPtrWillBeRawPtr<Range> selectionRange = selection.firstRange();
1851 if (!selectionRange)
1852 return AXRange();
1853
1854 Node* anchorNode = selectionRange->startContainer();
1855 ASSERT(anchorNode);
1856
1857 RefPtrWillBeRawPtr<AXObject> anchorObject = nullptr;
1858 // Find the closest node that has a corresponding AXObject.
1859 while (anchorObject = axObjectCache()->get(anchorNode)
dmazzoni 2015/06/24 00:11:01 This loop doesn't seem right because if axObjectCa
1860 && (!anchorObject->isAXLayoutObject() || !anchorObject->node()
1861 || anchorObject->accessibilityIsIgnored())) {
dmazzoni 2015/06/24 00:11:01 Blink has no 80 column line limit - you should wra
1862 if (anchorNode->nextSibling())
1863 anchorNode = anchorNode->nextSibling();
1864 else
1865 anchorNode = anchorNode->parentNode();
1848 } 1866 }
1849 1867
1850 return visibleSelectionUnderObject(); 1868 Node* focusNode = selectionRange->endContainer();
1869 ASSERT(focusNode);
1870
1871 RefPtrWillBeRawPtr<AXObject> focusObject = nullptr;
1872 while (focusObject = axObjectCache()->get(focusNode)
1873 && (!focusObject->isAXLayoutObject() || !focusObject->node()
1874 || focusObject->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) != 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 Node* node = isHTMLTextFormControlElement(node()) ? node() :
1926 document()->focusedElement();
1927
1928 if (!node)
1929 return AXRange();
1930
1931 AXObject axObject = axObjectCache()->get(node);
1932 if (!axObject)
1933 return AXRange();
1934
1935 HTMLTextFormControlElement* textControl = toHTMLTextFormControlElement(node) ;
1936 int start = textControl->selectionStart();
1937 int end = textControl->selectionEnd();
1938 return AXRange(axObject, start, axObject, end);
1939 }
1940
1941 int AXLayoutObject::indexForVisiblePosition(const VisiblePosition& position) con st
1942 {
1943 if (layoutObject() && layoutObject()->isTextControl()) {
1944 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1945 layoutObject())->textFormControlElement();
1946 return textControl->indexForVisiblePosition(position);
1947 }
1948
1949 if (!node())
1950 return 0;
1951
1952 Position indexPosition = position.deepEquivalent();
1953 if (indexPosition.isNull())
1954 return 0;
1955
1956 RefPtrWillBeRawPtr<Range> range = Range::create(*document());
1957 range->setStart(node(), 0, IGNORE_EXCEPTION);
1958 range->setEnd(indexPosition, IGNORE_EXCEPTION);
1959
1960 return TextIterator::rangeLength(range->startPosition(), range->endPosition( ));
1856 } 1961 }
1857 1962
1858 // 1963 //
1859 // Modify or take an action on an object. 1964 // Modify or take an action on an object.
1860 // 1965 //
1861 1966
1862 void AXLayoutObject::setSelectedTextRange(const PlainTextRange& range) 1967 void AXLayoutObject::setSelection(const AXRange& selection)
1863 { 1968 {
1864 if (m_layoutObject->isTextControl()) { 1969 if (!layoutObject() || selection.isNull())
1865 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1970 return;
1866 textControl->setSelectionRange(range.start, range.start + range.length, SelectionHasNoDirection, NotDispatchSelectEvent); 1971
1972 if (selection.anchorObject
1973 && (selection.anchorObject->isDetached()
1974 || !selection.anchorObject->isAXLayoutObject()
1975 || selection.anchorObject->layoutObject()->frame() != layoutObject()->fr ame()
1976 || selection.anchorObject->axObjectCache() != axObjectCache()))
1977 return;
1978
1979 if (selection.focusObject
1980 && (selection.focusObject->isDetached()
1981 || !selection.focusObject->isAXLayoutObject()
1982 || selection.focusObject->layoutObject()->frame() != layoutObject()->fra me()
1983 || selection.focusObject->axObjectCache() != axObjectCache()))
1984 return;
1985
1986 AXObject* anchorObject = selection.anchorObject != null ?
1987 selection.anchorObject.get() : this;
1988 AXObject* focusObject = selection.focusObject != null ?
1989 selection.focusObject.get() : this;
1990
1991 if (anchorObject == this && anchorObject == focusObject
1992 && layoutObject()->isTextControl()) {
1993 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1994 layoutObject())->textFormControlElement();
1995 textControl->setSelectionRange(selection.anchorOffset, selection.focusOf fset,
1996 SelectionHasNoDirection, NotDispatchSelectEvent);
1867 return; 1997 return;
1868 } 1998 }
1869 1999
1870 Document& document = m_layoutObject->document(); 2000 Node* anchorNode = nullptr;
1871 LocalFrame* frame = document.frame(); 2001 while (anchorObject && !anchorNode) {
2002 anchorNode = anchorObject->node();
2003 anchorObject = anchorObject->parentObject();
2004 }
2005
2006 Node* focusNode = nullptr;
2007 while (focusObject && !focusNode) {
2008 focusNode = focusObject->node();
2009 focusObject = focusObject->parentObject();
2010 }
2011
2012 if (!anchorNode || !focusNode)
2013 return;
2014
2015 LocalFrame* frame = layoutObject()->frame();
1872 if (!frame) 2016 if (!frame)
1873 return; 2017 return;
1874 Node* node = m_layoutObject->node(); 2018
1875 frame->selection().setSelection(VisibleSelection(Position(node, range.start, Position::PositionIsOffsetInAnchor), 2019 frame->selection().setSelection(VisibleSelection(Position(anchorNode,
1876 Position(node, range.start + range.length, Position::PositionIsOffsetInA nchor), DOWNSTREAM)); 2020 selection.anchorOffset, Position::PositionIsOffsetInAnchor),
2021 Position(focusNode, selection.focusOffset,
2022 Position::PositionIsOffsetInAnchor), DOWNSTREAM));
1877 } 2023 }
1878 2024
1879 void AXLayoutObject::setValue(const String& string) 2025 void AXLayoutObject::setValue(const String& string)
1880 { 2026 {
1881 if (!node() || !node()->isElementNode()) 2027 if (!node() || !node()->isElementNode())
1882 return; 2028 return;
1883 if (!m_layoutObject || !m_layoutObject->isBoxModelObject()) 2029 if (!m_layoutObject || !m_layoutObject->isBoxModelObject())
1884 return; 2030 return;
1885 2031
1886 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject); 2032 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2001 Position start, end; 2147 Position start, end;
2002 bool selected = Range::selectNodeContents(node, start, end); 2148 bool selected = Range::selectNodeContents(node, start, end);
2003 if (!selected) 2149 if (!selected)
2004 return VisiblePosition(); 2150 return VisiblePosition();
2005 2151
2006 CharacterIterator it(start, end); 2152 CharacterIterator it(start, end);
2007 it.advance(index - 1); 2153 it.advance(index - 1);
2008 return VisiblePosition(Position(it.currentContainer(), it.endOffset(), Posit ion::PositionIsOffsetInAnchor), UPSTREAM); 2154 return VisiblePosition(Position(it.currentContainer(), it.endOffset(), Posit ion::PositionIsOffsetInAnchor), UPSTREAM);
2009 } 2155 }
2010 2156
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) 2157 void AXLayoutObject::addInlineTextBoxChildren(bool force)
2039 { 2158 {
2040 Settings* settings = document()->settings(); 2159 Settings* settings = document()->settings();
2041 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled())) 2160 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled()))
2042 return; 2161 return;
2043 2162
2044 if (!layoutObject() || !layoutObject()->isText()) 2163 if (!layoutObject() || !layoutObject()->isText())
2045 return; 2164 return;
2046 2165
2047 if (layoutObject()->needsLayout()) { 2166 if (layoutObject()->needsLayout()) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2109 for (const auto& child : children()) { 2228 for (const auto& child : children()) {
2110 // Every child should have aria-role option, and if so, check for select ed attribute/state. 2229 // Every child should have aria-role option, and if so, check for select ed attribute/state.
2111 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) { 2230 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) {
2112 result.append(child); 2231 result.append(child);
2113 if (!isMulti) 2232 if (!isMulti)
2114 return; 2233 return;
2115 } 2234 }
2116 } 2235 }
2117 } 2236 }
2118 2237
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 2238 bool AXLayoutObject::nodeIsTextControl(const Node* node) const
2137 { 2239 {
2138 if (!node) 2240 if (!node)
2139 return false; 2241 return false;
2140 2242
2141 const AXObject* axObjectForNode = axObjectCache().getOrCreate(const_cast<Nod e*>(node)); 2243 const AXObject* axObjectForNode = axObjectCache().getOrCreate(const_cast<Nod e*>(node));
2142 if (!axObjectForNode) 2244 if (!axObjectForNode)
2143 return false; 2245 return false;
2144 2246
2145 return axObjectForNode->isTextControl(); 2247 return axObjectForNode->isTextControl();
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 if (label && label->layoutObject()) { 2609 if (label && label->layoutObject()) {
2508 LayoutRect labelRect = axObjectCache().getOrCreate(label)->elementRe ct(); 2610 LayoutRect labelRect = axObjectCache().getOrCreate(label)->elementRe ct();
2509 result.unite(labelRect); 2611 result.unite(labelRect);
2510 } 2612 }
2511 } 2613 }
2512 2614
2513 return result; 2615 return result;
2514 } 2616 }
2515 2617
2516 } // namespace blink 2618 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698