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

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: 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::AXSelection AXLayoutObject::selection() const
1841 { 1841 {
1842 if (!isTextControl()) 1842 AXSelection textSelection = textControlSelection();
dmazzoni 2015/06/16 17:24:03 This only seems to work if *this* is a text contro
1843 return PlainTextRange(); 1843 if (!textSelection.isNull())
1844 return textSelection;
1844 1845
1845 if (m_layoutObject->isTextControl()) { 1846 if (!node())
1846 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1847 return AXSelection();
1847 return PlainTextRange(textControl->selectionStart(), textControl->select ionEnd() - textControl->selectionStart()); 1848
1849 VisibleSelection selection = visibleSelection();
1850 RefPtrWillBeRawPtr<Range> selectionRange = selection.firstRange();
1851 if (!selectionRange)
1852 return AXSelection();
1853
1854 Node* anchorNode = selectionRange->startContainer();
1855 ASSERT(anchorNode);
1856 AXObject* anchorObject = axObjectCache()->get(anchorNode);
dmazzoni 2015/06/16 17:24:03 I think you need to handle the case where this obj
1857 Node* focusNode = selectionRange->endContainer();
1858 ASSERT(focusNode);
1859 AXObject* focusObject = axObjectCache()->get(focusNode);
1860
1861 if (!anchorObject || !focusObject)
1862 return AXSelection();
1863
1864 AXID anchorId = anchorObject->axObjectID();
1865 AXID focusId = focusObject->axObjectID();
1866
1867 int anchorOffset = selectionRange->startOffset();
1868 ASSERT(anchorOffset >= 0);
1869 int focusOffset = selectionRange->endOffset();
1870 ASSERT(focusOffset >= 0);
1871
1872 return AXSelection(
1873 anchorId, static_cast<unsigned>(anchorOffset),
1874 focusId, static_cast<unsigned>(focusOffset));
1875 }
1876
1877 AXObject::AXSelection AXLayoutObject::selectionUnderObject() const
dmazzoni 2015/06/16 17:24:03 Can you explain what this does?
1878 {
1879 AXSelection textSelection = textControlSelection();
1880 if (!textSelection.isNull())
1881 return textSelection;
1882
1883 if (!node())
1884 return AXSelection();
1885
1886 VisibleSelection selection = visibleSelection();
1887 RefPtrWillBeRawPtr<Range> selectionRange = selection.firstRange();
1888 if (!selectionRange)
1889 return AXSelection();
1890
1891 int start = indexForVisiblePosition(selection.visibleStart());
1892 int end = indexForVisiblePosition(selection.visibleEnd());
1893
1894 return AXSelection(start, end);
1895 }
1896
1897 AXObject::AXSelection AXLayoutObject::textControlSelection() const
1898 {
1899 if (layoutObject() && layoutObject()->isTextControl()) {
1900 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1901 layoutObject())->textFormControlElement();
1902 AXID id = axObjectID();
1903 unsigned start = textControl->selectionStart();
1904 unsigned end = textControl->selectionEnd();
1905 return AXSelection(id, start, id, end);
1848 } 1906 }
1849 1907
1850 return visibleSelectionUnderObject(); 1908 return AXSelection();
1851 } 1909 }
1852 1910
1853 VisibleSelection AXLayoutObject::selection() const 1911 VisibleSelection AXLayoutObject::visibleSelection() const
1854 { 1912 {
1855 return m_layoutObject->frame()->selection().selection(); 1913 return layoutObject() ?
1914 layoutObject()->frame()->selection().selection() : VisibleSelection();
1915 }
1916
1917 int AXLayoutObject::indexForVisiblePosition(const VisiblePosition& position) con st
1918 {
1919 if (layoutObject() && layoutObject()->isTextControl()) {
1920 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1921 layoutObject())->textFormControlElement();
1922 return textControl->indexForVisiblePosition(position);
1923 }
1924
1925 if (!node())
1926 return 0;
1927
1928 Position indexPosition = position.deepEquivalent();
1929 if (indexPosition.isNull())
1930 return 0;
1931
1932 RefPtrWillBeRawPtr<Range> range = Range::create(*document());
1933 range->setStart(node(), 0, IGNORE_EXCEPTION);
1934 range->setEnd(indexPosition, IGNORE_EXCEPTION);
1935
1936 return TextIterator::rangeLength(range->startPosition(), range->endPosition( ));
1856 } 1937 }
1857 1938
1858 // 1939 //
1859 // Modify or take an action on an object. 1940 // Modify or take an action on an object.
1860 // 1941 //
1861 1942
1862 void AXLayoutObject::setSelectedTextRange(const PlainTextRange& range) 1943 void AXLayoutObject::setSelection(const AXSelection& selection)
1863 { 1944 {
1864 if (m_layoutObject->isTextControl()) { 1945 if (selection.isNull())
1865 HTMLTextFormControlElement* textControl = toLayoutTextControl(m_layoutOb ject)->textFormControlElement(); 1946 return;
1866 textControl->setSelectionRange(range.start, range.start + range.length, SelectionHasNoDirection, NotDispatchSelectEvent); 1947
1948 AXObject* anchorObject = this;
1949 if (selection.hasAnchor())
1950 anchorObject = axObjectCache()->objectFromAXID(selection.anchorId);
1951
1952 AXObject* focusObject = this;
1953 if (selection.hasFocus())
1954 focusObject = axObjectCache()->objectFromAXID(selection.focusId);
1955
1956 if (!anchorObject || !focusObject)
1957 return;
1958
1959 if (anchorObject == this && anchorObject == focusObject
1960 && layoutObject() && layoutObject()->isTextControl()) {
1961 HTMLTextFormControlElement* textControl = toLayoutTextControl(
1962 layoutObject())->textFormControlElement();
1963 textControl->setSelectionRange(selection.anchorOffset, selection.focusOf fset,
1964 SelectionHasNoDirection, NotDispatchSelectEvent);
1867 return; 1965 return;
1868 } 1966 }
1869 1967
1870 Document& document = m_layoutObject->document(); 1968 Node* anchorNode = anchorObject->node();
1871 LocalFrame* frame = document.frame(); 1969 Node* focusNode = focusObject->node();
1970 if (!anchorNode || !focusNode)
dmazzoni 2015/06/16 17:24:03 Some objects in the tree have a layoutObject but n
1971 return;
1972
1973 Document* objectDocument = document();
1974 ASSERT(objectDocument);
1975 LocalFrame* frame = objectDocument->frame();
1872 if (!frame) 1976 if (!frame)
1873 return; 1977 return;
1874 Node* node = m_layoutObject->node(); 1978
1875 frame->selection().setSelection(VisibleSelection(Position(node, range.start, Position::PositionIsOffsetInAnchor), 1979 frame->selection().setSelection(VisibleSelection(Position(anchorNode,
dmazzoni 2015/06/16 17:24:03 I think you need to check that both the anchorNode
1876 Position(node, range.start + range.length, Position::PositionIsOffsetInA nchor), DOWNSTREAM)); 1980 selection.anchorOffset, Position::PositionIsOffsetInAnchor),
1981 Position(focusNode, selection.focusOffset,
1982 Position::PositionIsOffsetInAnchor), DOWNSTREAM));
1877 } 1983 }
1878 1984
1879 void AXLayoutObject::setValue(const String& string) 1985 void AXLayoutObject::setValue(const String& string)
1880 { 1986 {
1881 if (!node() || !node()->isElementNode()) 1987 if (!node() || !node()->isElementNode())
1882 return; 1988 return;
1883 if (!m_layoutObject || !m_layoutObject->isBoxModelObject()) 1989 if (!m_layoutObject || !m_layoutObject->isBoxModelObject())
1884 return; 1990 return;
1885 1991
1886 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject); 1992 LayoutBoxModelObject* layoutObject = toLayoutBoxModelObject(m_layoutObject);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2001 Position start, end; 2107 Position start, end;
2002 bool selected = Range::selectNodeContents(node, start, end); 2108 bool selected = Range::selectNodeContents(node, start, end);
2003 if (!selected) 2109 if (!selected)
2004 return VisiblePosition(); 2110 return VisiblePosition();
2005 2111
2006 CharacterIterator it(start, end); 2112 CharacterIterator it(start, end);
2007 it.advance(index - 1); 2113 it.advance(index - 1);
2008 return VisiblePosition(Position(it.currentContainer(), it.endOffset(), Posit ion::PositionIsOffsetInAnchor), UPSTREAM); 2114 return VisiblePosition(Position(it.currentContainer(), it.endOffset(), Posit ion::PositionIsOffsetInAnchor), UPSTREAM);
2009 } 2115 }
2010 2116
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) 2117 void AXLayoutObject::addInlineTextBoxChildren(bool force)
2039 { 2118 {
2040 Settings* settings = document()->settings(); 2119 Settings* settings = document()->settings();
2041 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled())) 2120 if (!force && (!settings || !settings->inlineTextBoxAccessibilityEnabled()))
2042 return; 2121 return;
2043 2122
2044 if (!layoutObject() || !layoutObject()->isText()) 2123 if (!layoutObject() || !layoutObject()->isText())
2045 return; 2124 return;
2046 2125
2047 if (layoutObject()->needsLayout()) { 2126 if (layoutObject()->needsLayout()) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2109 for (const auto& child : children()) { 2188 for (const auto& child : children()) {
2110 // Every child should have aria-role option, and if so, check for select ed attribute/state. 2189 // Every child should have aria-role option, and if so, check for select ed attribute/state.
2111 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) { 2190 if (child->isSelected() && child->ariaRoleAttribute() == ListBoxOptionRo le) {
2112 result.append(child); 2191 result.append(child);
2113 if (!isMulti) 2192 if (!isMulti)
2114 return; 2193 return;
2115 } 2194 }
2116 } 2195 }
2117 } 2196 }
2118 2197
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 2198 bool AXLayoutObject::nodeIsTextControl(const Node* node) const
2137 { 2199 {
2138 if (!node) 2200 if (!node)
2139 return false; 2201 return false;
2140 2202
2141 const AXObject* axObjectForNode = axObjectCache()->getOrCreate(const_cast<No de*>(node)); 2203 const AXObject* axObjectForNode = axObjectCache()->getOrCreate(const_cast<No de*>(node));
2142 if (!axObjectForNode) 2204 if (!axObjectForNode)
2143 return false; 2205 return false;
2144 2206
2145 return axObjectForNode->isTextControl(); 2207 return axObjectForNode->isTextControl();
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 if (label && label->layoutObject()) { 2569 if (label && label->layoutObject()) {
2508 LayoutRect labelRect = axObjectCache()->getOrCreate(label)->elementR ect(); 2570 LayoutRect labelRect = axObjectCache()->getOrCreate(label)->elementR ect();
2509 result.unite(labelRect); 2571 result.unite(labelRect);
2510 } 2572 }
2511 } 2573 }
2512 2574
2513 return result; 2575 return result;
2514 } 2576 }
2515 2577
2516 } // namespace blink 2578 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698