1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
|
/*
* Copyright (C) 2012, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "AccessibilityNodeObject.h"
#include "AXObjectCache.h"
#include "AccessibilityImageMapLink.h"
#include "AccessibilityListBox.h"
#include "AccessibilitySpinButton.h"
#include "AccessibilityTable.h"
#include "EventNames.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameSelection.h"
#include "FrameView.h"
#include "HTMLAreaElement.h"
#include "HTMLFieldSetElement.h"
#include "HTMLFormElement.h"
#include "HTMLFrameElementBase.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLLabelElement.h"
#include "HTMLLegendElement.h"
#include "HTMLMapElement.h"
#include "HTMLNames.h"
#include "HTMLOptGroupElement.h"
#include "HTMLOptionElement.h"
#include "HTMLOptionsCollection.h"
#include "HTMLPlugInImageElement.h"
#include "HTMLSelectElement.h"
#include "HTMLTextAreaElement.h"
#include "HTMLTextFormControlElement.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "LabelableElement.h"
#include "LocalizedStrings.h"
#include "MathMLNames.h"
#include "NodeList.h"
#include "NodeTraversal.h"
#include "Page.h"
#include "ProgressTracker.h"
#include "SVGElement.h"
#include "SVGNames.h"
#include "SVGStyledElement.h"
#include "Text.h"
#include "TextControlInnerElements.h"
#include "TextIterator.h"
#include "UserGestureIndicator.h"
#include "VisibleUnits.h"
#include "Widget.h"
#include "htmlediting.h"
#include <wtf/StdLibExtras.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/unicode/CharacterNames.h>
using namespace std;
namespace WebCore {
using namespace HTMLNames;
AccessibilityNodeObject::AccessibilityNodeObject(Node* node)
: AccessibilityObject()
, m_ariaRole(UnknownRole)
, m_childrenDirty(false)
, m_roleForMSAA(UnknownRole)
#ifndef NDEBUG
, m_initialized(false)
#endif
, m_node(node)
{
}
AccessibilityNodeObject::~AccessibilityNodeObject()
{
ASSERT(isDetached());
}
void AccessibilityNodeObject::init()
{
#ifndef NDEBUG
ASSERT(!m_initialized);
m_initialized = true;
#endif
m_role = determineAccessibilityRole();
}
PassRefPtr<AccessibilityNodeObject> AccessibilityNodeObject::create(Node* node)
{
return adoptRef(new AccessibilityNodeObject(node));
}
void AccessibilityNodeObject::detach()
{
clearChildren();
AccessibilityObject::detach();
m_node = 0;
}
void AccessibilityNodeObject::childrenChanged()
{
// This method is meant as a quick way of marking a portion of the accessibility tree dirty.
if (!node() && !renderer())
return;
axObjectCache()->postNotification(this, document(), AXObjectCache::AXChildrenChanged, true);
// Go up the accessibility parent chain, but only if the element already exists. This method is
// called during render layouts, minimal work should be done.
// If AX elements are created now, they could interrogate the render tree while it's in a funky state.
// At the same time, process ARIA live region changes.
for (AccessibilityObject* parent = this; parent; parent = parent->parentObjectIfExists()) {
parent->setNeedsToUpdateChildren();
// These notifications always need to be sent because screenreaders are reliant on them to perform.
// In other words, they need to be sent even when the screen reader has not accessed this live region since the last update.
// If this element supports ARIA live regions, then notify the AT of changes.
if (parent->supportsARIALiveRegion())
axObjectCache()->postNotification(parent, parent->document(), AXObjectCache::AXLiveRegionChanged, true);
// If this element is an ARIA text control, notify the AT of changes.
if (parent->isARIATextControl() && !parent->isNativeTextControl() && !parent->node()->rendererIsEditable())
axObjectCache()->postNotification(parent, parent->document(), AXObjectCache::AXValueChanged, true);
}
}
void AccessibilityNodeObject::updateAccessibilityRole()
{
bool ignoredStatus = accessibilityIsIgnored();
m_role = determineAccessibilityRole();
// The AX hierarchy only needs to be updated if the ignored status of an element has changed.
if (ignoredStatus != accessibilityIsIgnored())
childrenChanged();
}
AccessibilityObject* AccessibilityNodeObject::firstChild() const
{
if (!node())
return 0;
Node* firstChild = node()->firstChild();
if (!firstChild)
return 0;
return axObjectCache()->getOrCreate(firstChild);
}
AccessibilityObject* AccessibilityNodeObject::lastChild() const
{
if (!node())
return 0;
Node* lastChild = node()->lastChild();
if (!lastChild)
return 0;
return axObjectCache()->getOrCreate(lastChild);
}
AccessibilityObject* AccessibilityNodeObject::previousSibling() const
{
if (!node())
return 0;
Node* previousSibling = node()->previousSibling();
if (!previousSibling)
return 0;
return axObjectCache()->getOrCreate(previousSibling);
}
AccessibilityObject* AccessibilityNodeObject::nextSibling() const
{
if (!node())
return 0;
Node* nextSibling = node()->nextSibling();
if (!nextSibling)
return 0;
return axObjectCache()->getOrCreate(nextSibling);
}
AccessibilityObject* AccessibilityNodeObject::parentObjectIfExists() const
{
return parentObject();
}
AccessibilityObject* AccessibilityNodeObject::parentObject() const
{
if (!node())
return 0;
Node* parentObj = node()->parentNode();
if (parentObj)
return axObjectCache()->getOrCreate(parentObj);
return 0;
}
LayoutRect AccessibilityNodeObject::elementRect() const
{
return boundingBoxRect();
}
LayoutRect AccessibilityNodeObject::boundingBoxRect() const
{
// AccessibilityNodeObjects have no mechanism yet to return a size or position.
// For now, let's return the position of the ancestor that does have a position,
// and make it the width of that parent, and about the height of a line of text, so that it's clear the object is a child of the parent.
LayoutRect boundingBox;
for (AccessibilityObject* positionProvider = parentObject(); positionProvider; positionProvider = positionProvider->parentObject()) {
if (positionProvider->isAccessibilityRenderObject()) {
LayoutRect parentRect = positionProvider->elementRect();
boundingBox.setSize(LayoutSize(parentRect.width(), LayoutUnit(std::min(10.0f, parentRect.height().toFloat()))));
boundingBox.setLocation(parentRect.location());
break;
}
}
return boundingBox;
}
void AccessibilityNodeObject::setNode(Node* node)
{
m_node = node;
}
Document* AccessibilityNodeObject::document() const
{
if (!node())
return 0;
return node()->document();
}
AccessibilityRole AccessibilityNodeObject::determineAccessibilityRole()
{
if (!node())
return UnknownRole;
m_ariaRole = determineAriaRoleAttribute();
AccessibilityRole ariaRole = ariaRoleAttribute();
if (ariaRole != UnknownRole)
return ariaRole;
if (node()->isLink())
return WebCoreLinkRole;
if (node()->isTextNode())
return StaticTextRole;
if (node()->hasTagName(buttonTag))
return buttonRoleType();
if (isHTMLInputElement(node())) {
HTMLInputElement* input = toHTMLInputElement(node());
if (input->isCheckbox())
return CheckBoxRole;
if (input->isRadioButton())
return RadioButtonRole;
if (input->isTextButton())
return buttonRoleType();
if (input->isRangeControl())
return SliderRole;
#if ENABLE(INPUT_TYPE_COLOR)
const AtomicString& type = input->getAttribute(typeAttr);
if (equalIgnoringCase(type, "color"))
return ColorWellRole;
#endif
return TextFieldRole;
}
if (node()->hasTagName(selectTag)) {
HTMLSelectElement* selectElement = toHTMLSelectElement(node());
return selectElement->multiple() ? ListBoxRole : PopUpButtonRole;
}
if (isHTMLTextAreaElement(node()))
return TextAreaRole;
if (headingLevel())
return HeadingRole;
if (node()->hasTagName(divTag))
return DivRole;
if (node()->hasTagName(pTag))
return ParagraphRole;
if (isHTMLLabelElement(node()))
return LabelRole;
if (node()->isElementNode() && toElement(node())->isFocusable())
return GroupRole;
return UnknownRole;
}
void AccessibilityNodeObject::insertChild(AccessibilityObject* child, unsigned index)
{
if (!child)
return;
// If the parent is asking for this child's children, then either it's the first time (and clearing is a no-op),
// or its visibility has changed. In the latter case, this child may have a stale child cached.
// This can prevent aria-hidden changes from working correctly. Hence, whenever a parent is getting children, ensure data is not stale.
child->clearChildren();
if (child->accessibilityIsIgnored()) {
AccessibilityChildrenVector children = child->children();
size_t length = children.size();
for (size_t i = 0; i < length; ++i)
m_children.insert(index + i, children[i]);
} else {
ASSERT(child->parentObject() == this);
m_children.insert(index, child);
}
}
void AccessibilityNodeObject::addChild(AccessibilityObject* child)
{
insertChild(child, m_children.size());
}
void AccessibilityNodeObject::addChildren()
{
// If the need to add more children in addition to existing children arises,
// childrenChanged should have been called, leaving the object with no children.
ASSERT(!m_haveChildren);
if (!m_node)
return;
m_haveChildren = true;
// The only time we add children from the DOM tree to a node with a renderer is when it's a canvas.
if (renderer() && !m_node->hasTagName(canvasTag))
return;
for (Node* child = m_node->firstChild(); child; child = child->nextSibling())
addChild(axObjectCache()->getOrCreate(child));
}
bool AccessibilityNodeObject::canHaveChildren() const
{
// If this is an AccessibilityRenderObject, then it's okay if this object
// doesn't have a node - there are some renderers that don't have associated
// nodes, like scroll areas and css-generated text.
if (!node() && !isAccessibilityRenderObject())
return false;
// Elements that should not have children
switch (roleValue()) {
case ImageRole:
case ButtonRole:
case PopUpButtonRole:
case CheckBoxRole:
case RadioButtonRole:
case TabRole:
case ToggleButtonRole:
case StaticTextRole:
case ListBoxOptionRole:
case ScrollBarRole:
case ProgressIndicatorRole:
return false;
default:
return true;
}
}
bool AccessibilityNodeObject::computeAccessibilityIsIgnored() const
{
#ifndef NDEBUG
// Double-check that an AccessibilityObject is never accessed before
// it's been initialized.
ASSERT(m_initialized);
#endif
// If this element is within a parent that cannot have children, it should not be exposed.
if (isDescendantOfBarrenParent())
return true;
return m_role == UnknownRole;
}
bool AccessibilityNodeObject::canvasHasFallbackContent() const
{
Node* node = this->node();
if (!node || !node->hasTagName(canvasTag))
return false;
// If it has any children that are elements, we'll assume it might be fallback
// content. If it has no children or its only children are not elements
// (e.g. just text nodes), it doesn't have fallback content.
for (Node* child = node->firstChild(); child; child = child->nextSibling()) {
if (child->isElementNode())
return true;
}
return false;
}
bool AccessibilityNodeObject::isImageButton() const
{
return isNativeImage() && isButton();
}
bool AccessibilityNodeObject::isAnchor() const
{
return !isNativeImage() && isLink();
}
bool AccessibilityNodeObject::isNativeTextControl() const
{
Node* node = this->node();
if (!node)
return false;
if (isHTMLTextAreaElement(node))
return true;
if (isHTMLInputElement(node)) {
HTMLInputElement* input = toHTMLInputElement(node);
return input->isText() || input->isNumberField();
}
return false;
}
bool AccessibilityNodeObject::isSearchField() const
{
Node* node = this->node();
if (!node)
return false;
HTMLInputElement* inputElement = node->toInputElement();
if (!inputElement)
return false;
if (inputElement->isSearchField())
return true;
// Some websites don't label their search fields as such. However, they will
// use the word "search" in either the form or input type. This won't catch every case,
// but it will catch google.com for example.
// Check the node name of the input type, sometimes it's "search".
const AtomicString& nameAttribute = getAttribute(nameAttr);
if (nameAttribute.contains("search", false))
return true;
// Check the form action and the name, which will sometimes be "search".
HTMLFormElement* form = inputElement->form();
if (form && (form->name().contains("search", false) || form->action().contains("search", false)))
return true;
return false;
}
bool AccessibilityNodeObject::isNativeImage() const
{
Node* node = this->node();
if (!node)
return false;
if (isHTMLImageElement(node))
return true;
if (node->hasTagName(appletTag) || node->hasTagName(embedTag) || node->hasTagName(objectTag))
return true;
if (isHTMLInputElement(node)) {
HTMLInputElement* input = toHTMLInputElement(node);
return input->isImageButton();
}
return false;
}
bool AccessibilityNodeObject::isImage() const
{
return roleValue() == ImageRole;
}
bool AccessibilityNodeObject::isPasswordField() const
{
Node* node = this->node();
if (!node || !node->isHTMLElement())
return false;
if (ariaRoleAttribute() != UnknownRole)
return false;
HTMLInputElement* inputElement = node->toInputElement();
if (!inputElement)
return false;
return inputElement->isPasswordField();
}
bool AccessibilityNodeObject::isInputImage() const
{
Node* node = this->node();
if (!node)
return false;
if (roleValue() == ButtonRole && isHTMLInputElement(node)) {
HTMLInputElement* input = toHTMLInputElement(node);
return input->isImageButton();
}
return false;
}
bool AccessibilityNodeObject::isProgressIndicator() const
{
return roleValue() == ProgressIndicatorRole;
}
bool AccessibilityNodeObject::isSlider() const
{
return roleValue() == SliderRole;
}
bool AccessibilityNodeObject::isMenuRelated() const
{
switch (roleValue()) {
case MenuRole:
case MenuBarRole:
case MenuButtonRole:
case MenuItemRole:
return true;
default:
return false;
}
}
bool AccessibilityNodeObject::isMenu() const
{
return roleValue() == MenuRole;
}
bool AccessibilityNodeObject::isMenuBar() const
{
return roleValue() == MenuBarRole;
}
bool AccessibilityNodeObject::isMenuButton() const
{
return roleValue() == MenuButtonRole;
}
bool AccessibilityNodeObject::isMenuItem() const
{
return roleValue() == MenuItemRole;
}
bool AccessibilityNodeObject::isNativeCheckboxOrRadio() const
{
Node* node = this->node();
if (!node)
return false;
HTMLInputElement* input = node->toInputElement();
if (input)
return input->isCheckbox() || input->isRadioButton();
return false;
}
bool AccessibilityNodeObject::isEnabled() const
{
if (equalIgnoringCase(getAttribute(aria_disabledAttr), "true"))
return false;
Node* node = this->node();
if (!node || !node->isElementNode())
return true;
return !toElement(node)->isDisabledFormControl();
}
bool AccessibilityNodeObject::isIndeterminate() const
{
Node* node = this->node();
if (!node)
return false;
HTMLInputElement* inputElement = node->toInputElement();
if (!inputElement)
return false;
return inputElement->shouldAppearIndeterminate();
}
bool AccessibilityNodeObject::isPressed() const
{
if (!isButton())
return false;
Node* node = this->node();
if (!node)
return false;
// If this is an ARIA button, check the aria-pressed attribute rather than node()->active()
if (ariaRoleAttribute() == ButtonRole) {
if (equalIgnoringCase(getAttribute(aria_pressedAttr), "true"))
return true;
return false;
}
if (!node->isElementNode())
return false;
return toElement(node)->active();
}
bool AccessibilityNodeObject::isChecked() const
{
Node* node = this->node();
if (!node)
return false;
// First test for native checkedness semantics
HTMLInputElement* inputElement = node->toInputElement();
if (inputElement)
return inputElement->shouldAppearChecked();
// Else, if this is an ARIA checkbox or radio, respect the aria-checked attribute
bool validRole = false;
switch (ariaRoleAttribute()) {
case RadioButtonRole:
case CheckBoxRole:
case MenuItemRole:
validRole = true;
break;
default:
break;
}
if (validRole && equalIgnoringCase(getAttribute(aria_checkedAttr), "true"))
return true;
return false;
}
bool AccessibilityNodeObject::isHovered() const
{
Node* node = this->node();
if (!node)
return false;
return node->isElementNode() && toElement(node)->hovered();
}
bool AccessibilityNodeObject::isMultiSelectable() const
{
const AtomicString& ariaMultiSelectable = getAttribute(aria_multiselectableAttr);
if (equalIgnoringCase(ariaMultiSelectable, "true"))
return true;
if (equalIgnoringCase(ariaMultiSelectable, "false"))
return false;
return node() && node()->hasTagName(selectTag) && toHTMLSelectElement(node())->multiple();
}
bool AccessibilityNodeObject::isReadOnly() const
{
Node* node = this->node();
if (!node)
return true;
if (isHTMLTextAreaElement(node))
return toHTMLFormControlElement(node)->isReadOnly();
if (isHTMLInputElement(node)) {
HTMLInputElement* input = toHTMLInputElement(node);
if (input->isTextField())
return input->isReadOnly();
}
return !node->rendererIsEditable();
}
bool AccessibilityNodeObject::isRequired() const
{
if (equalIgnoringCase(getAttribute(aria_requiredAttr), "true"))
return true;
Node* n = this->node();
if (n && (n->isElementNode() && toElement(n)->isFormControlElement()))
return static_cast<HTMLFormControlElement*>(n)->isRequired();
return false;
}
bool AccessibilityNodeObject::supportsRequiredAttribute() const
{
switch (roleValue()) {
case CellRole:
case CheckBoxRole:
case ComboBoxRole:
case GridRole:
case IncrementorRole:
case ListBoxRole:
case PopUpButtonRole:
case RadioButtonRole:
case RadioGroupRole:
case RowHeaderRole:
case SliderRole:
case SpinButtonRole:
case TableHeaderContainerRole:
case TextAreaRole:
case TextFieldRole:
case ToggleButtonRole:
return true;
default:
return false;
}
}
int AccessibilityNodeObject::headingLevel() const
{
// headings can be in block flow and non-block flow
Node* node = this->node();
if (!node)
return false;
if (isHeading()) {
int ariaLevel = getAttribute(aria_levelAttr).toInt();
if (ariaLevel > 0)
return ariaLevel;
}
if (node->hasTagName(h1Tag))
return 1;
if (node->hasTagName(h2Tag))
return 2;
if (node->hasTagName(h3Tag))
return 3;
if (node->hasTagName(h4Tag))
return 4;
if (node->hasTagName(h5Tag))
return 5;
if (node->hasTagName(h6Tag))
return 6;
return 0;
}
String AccessibilityNodeObject::valueDescription() const
{
if (!isRangeControl())
return String();
return getAttribute(aria_valuetextAttr).string();
}
float AccessibilityNodeObject::valueForRange() const
{
if (node() && isHTMLInputElement(node())) {
HTMLInputElement* input = toHTMLInputElement(node());
if (input->isRangeControl())
return input->valueAsNumber();
}
if (!isRangeControl())
return 0.0f;
return getAttribute(aria_valuenowAttr).toFloat();
}
float AccessibilityNodeObject::maxValueForRange() const
{
if (node() && isHTMLInputElement(node())) {
HTMLInputElement* input = toHTMLInputElement(node());
if (input->isRangeControl())
return input->maximum();
}
if (!isRangeControl())
return 0.0f;
return getAttribute(aria_valuemaxAttr).toFloat();
}
float AccessibilityNodeObject::minValueForRange() const
{
if (node() && isHTMLInputElement(node())) {
HTMLInputElement* input = toHTMLInputElement(node());
if (input->isRangeControl())
return input->minimum();
}
if (!isRangeControl())
return 0.0f;
return getAttribute(aria_valueminAttr).toFloat();
}
float AccessibilityNodeObject::stepValueForRange() const
{
return getAttribute(stepAttr).toFloat();
}
bool AccessibilityNodeObject::isHeading() const
{
return roleValue() == HeadingRole;
}
bool AccessibilityNodeObject::isLink() const
{
return roleValue() == WebCoreLinkRole;
}
bool AccessibilityNodeObject::isControl() const
{
Node* node = this->node();
if (!node)
return false;
return ((node->isElementNode() && toElement(node)->isFormControlElement())
|| AccessibilityObject::isARIAControl(ariaRoleAttribute()));
}
bool AccessibilityNodeObject::isFieldset() const
{
Node* node = this->node();
if (!node)
return false;
return node->hasTagName(fieldsetTag);
}
bool AccessibilityNodeObject::isGroup() const
{
return roleValue() == GroupRole;
}
AccessibilityObject* AccessibilityNodeObject::selectedRadioButton()
{
if (!isRadioGroup())
return 0;
AccessibilityObject::AccessibilityChildrenVector children = this->children();
// Find the child radio button that is selected (ie. the intValue == 1).
size_t size = children.size();
for (size_t i = 0; i < size; ++i) {
AccessibilityObject* object = children[i].get();
if (object->roleValue() == RadioButtonRole && object->checkboxOrRadioValue() == ButtonStateOn)
return object;
}
return 0;
}
AccessibilityObject* AccessibilityNodeObject::selectedTabItem()
{
if (!isTabList())
return 0;
// Find the child tab item that is selected (ie. the intValue == 1).
AccessibilityObject::AccessibilityChildrenVector tabs;
tabChildren(tabs);
AccessibilityObject::AccessibilityChildrenVector children = this->children();
size_t size = tabs.size();
for (size_t i = 0; i < size; ++i) {
AccessibilityObject* object = children[i].get();
if (object->isTabItem() && object->isChecked())
return object;
}
return 0;
}
AccessibilityButtonState AccessibilityNodeObject::checkboxOrRadioValue() const
{
if (isNativeCheckboxOrRadio())
return isChecked() ? ButtonStateOn : ButtonStateOff;
return AccessibilityObject::checkboxOrRadioValue();
}
Element* AccessibilityNodeObject::anchorElement() const
{
Node* node = this->node();
if (!node)
return 0;
AXObjectCache* cache = axObjectCache();
// search up the DOM tree for an anchor element
// NOTE: this assumes that any non-image with an anchor is an HTMLAnchorElement
for ( ; node; node = node->parentNode()) {
if (isHTMLAnchorElement(node) || (node->renderer() && cache->getOrCreate(node->renderer())->isAnchor()))
return toElement(node);
}
return 0;
}
Element* AccessibilityNodeObject::actionElement() const
{
Node* node = this->node();
if (!node)
return 0;
if (isHTMLInputElement(node)) {
HTMLInputElement* input = toHTMLInputElement(node);
if (!input->isDisabledFormControl() && (isCheckboxOrRadio() || input->isTextButton()))
return input;
} else if (node->hasTagName(buttonTag))
return toElement(node);
if (isFileUploadButton())
return toElement(node);
if (AccessibilityObject::isARIAInput(ariaRoleAttribute()))
return toElement(node);
if (isImageButton())
return toElement(node);
if (node->hasTagName(selectTag))
return toElement(node);
switch (roleValue()) {
case ButtonRole:
case PopUpButtonRole:
case ToggleButtonRole:
case TabRole:
case MenuItemRole:
case ListItemRole:
return toElement(node);
default:
break;
}
Element* elt = anchorElement();
if (!elt)
elt = mouseButtonListener();
return elt;
}
Element* AccessibilityNodeObject::mouseButtonListener() const
{
Node* node = this->node();
if (!node)
return 0;
// check if our parent is a mouse button listener
while (node && !node->isElementNode())
node = node->parentNode();
if (!node)
return 0;
// FIXME: Do the continuation search like anchorElement does
for (Element* element = toElement(node); element; element = element->parentElement()) {
// If we've reached the body and this is not a control element, do not expose press action for this element.
// It can cause false positives, where every piece of text is labeled as accepting press actions.
if (element->hasTagName(bodyTag) && isStaticText())
break;
if (element->hasEventListeners(eventNames().clickEvent) || element->hasEventListeners(eventNames().mousedownEvent) || element->hasEventListeners(eventNames().mouseupEvent))
return element;
}
return 0;
}
bool AccessibilityNodeObject::isDescendantOfBarrenParent() const
{
for (AccessibilityObject* object = parentObject(); object; object = object->parentObject()) {
if (!object->canHaveChildren())
return true;
}
return false;
}
void AccessibilityNodeObject::alterSliderValue(bool increase)
{
if (roleValue() != SliderRole)
return;
if (!getAttribute(stepAttr).isEmpty())
changeValueByStep(increase);
else
changeValueByPercent(increase ? 5 : -5);
}
void AccessibilityNodeObject::increment()
{
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
alterSliderValue(true);
}
void AccessibilityNodeObject::decrement()
{
UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
alterSliderValue(false);
}
void AccessibilityNodeObject::changeValueByStep(bool increase)
{
float step = stepValueForRange();
float value = valueForRange();
value += increase ? step : -step;
setValue(String::number(value));
axObjectCache()->postNotification(node(), AXObjectCache::AXValueChanged, true);
}
void AccessibilityNodeObject::changeValueByPercent(float percentChange)
{
float range = maxValueForRange() - minValueForRange();
float value = valueForRange();
value += range * (percentChange / 100);
setValue(String::number(value));
axObjectCache()->postNotification(node(), AXObjectCache::AXValueChanged, true);
}
bool AccessibilityNodeObject::isGenericFocusableElement() const
{
if (!canSetFocusAttribute())
return false;
// If it's a control, it's not generic.
if (isControl())
return false;
// If it has an aria role, it's not generic.
if (m_ariaRole != UnknownRole)
return false;
// If the content editable attribute is set on this element, that's the reason
// it's focusable, and existing logic should handle this case already - so it's not a
// generic focusable element.
if (hasContentEditableAttributeSet())
return false;
// The web area and body element are both focusable, but existing logic handles these
// cases already, so we don't need to include them here.
if (roleValue() == WebAreaRole)
return false;
if (node() && node()->hasTagName(bodyTag))
return false;
// An SVG root is focusable by default, but it's probably not interactive, so don't
// include it. It can still be made accessible by giving it an ARIA role.
if (roleValue() == SVGRootRole)
return false;
return true;
}
HTMLLabelElement* AccessibilityNodeObject::labelForElement(Element* element) const
{
if (!element->isHTMLElement() || !toHTMLElement(element)->isLabelable())
return 0;
const AtomicString& id = element->getIdAttribute();
if (!id.isEmpty()) {
if (HTMLLabelElement* label = element->treeScope()->labelElementForId(id))
return label;
}
for (Element* parent = element->parentElement(); parent; parent = parent->parentElement()) {
if (isHTMLLabelElement(parent))
return toHTMLLabelElement(parent);
}
return 0;
}
String AccessibilityNodeObject::ariaAccessibilityDescription() const
{
String ariaLabeledBy = ariaLabeledByAttribute();
if (!ariaLabeledBy.isEmpty())
return ariaLabeledBy;
const AtomicString& ariaLabel = getAttribute(aria_labelAttr);
if (!ariaLabel.isEmpty())
return ariaLabel;
return String();
}
static Element* siblingWithAriaRole(String role, Node* node)
{
Node* parent = node->parentNode();
if (!parent)
return 0;
for (Node* sibling = parent->firstChild(); sibling; sibling = sibling->nextSibling()) {
if (sibling->isElementNode()) {
const AtomicString& siblingAriaRole = toElement(sibling)->getAttribute(roleAttr);
if (equalIgnoringCase(siblingAriaRole, role))
return toElement(sibling);
}
}
return 0;
}
Element* AccessibilityNodeObject::menuElementForMenuButton() const
{
if (ariaRoleAttribute() != MenuButtonRole)
return 0;
return siblingWithAriaRole("menu", node());
}
AccessibilityObject* AccessibilityNodeObject::menuForMenuButton() const
{
return axObjectCache()->getOrCreate(menuElementForMenuButton());
}
Element* AccessibilityNodeObject::menuItemElementForMenu() const
{
if (ariaRoleAttribute() != MenuRole)
return 0;
return siblingWithAriaRole("menuitem", node());
}
AccessibilityObject* AccessibilityNodeObject::menuButtonForMenu() const
{
Element* menuItem = menuItemElementForMenu();
if (menuItem) {
// ARIA just has generic menu items. AppKit needs to know if this is a top level items like MenuBarButton or MenuBarItem
AccessibilityObject* menuItemAX = axObjectCache()->getOrCreate(menuItem);
if (menuItemAX && menuItemAX->isMenuButton())
return menuItemAX;
}
return 0;
}
void AccessibilityNodeObject::titleElementText(Vector<AccessibilityText>& textOrder)
{
Node* node = this->node();
if (!node)
return;
bool isInputTag = isHTMLInputElement(node);
if (isInputTag || AccessibilityObject::isARIAInput(ariaRoleAttribute()) || isControl()) {
HTMLLabelElement* label = labelForElement(toElement(node));
if (label) {
AccessibilityObject* labelObject = axObjectCache()->getOrCreate(label);
textOrder.append(AccessibilityText(label->innerText(), LabelByElementText, labelObject));
return;
}
}
AccessibilityObject* titleUIElement = this->titleUIElement();
if (titleUIElement)
textOrder.append(AccessibilityText(String(), LabelByElementText, titleUIElement));
}
void AccessibilityNodeObject::alternativeText(Vector<AccessibilityText>& textOrder) const
{
if (isWebArea()) {
String webAreaText = alternativeTextForWebArea();
if (!webAreaText.isEmpty())
textOrder.append(AccessibilityText(webAreaText, AlternativeText));
return;
}
ariaLabeledByText(textOrder);
const AtomicString& ariaLabel = getAttribute(aria_labelAttr);
if (!ariaLabel.isEmpty())
textOrder.append(AccessibilityText(ariaLabel, AlternativeText));
if (isImage() || isInputImage() || isNativeImage() || isCanvas()) {
// Images should use alt as long as the attribute is present, even if empty.
// Otherwise, it should fallback to other methods, like the title attribute.
const AtomicString& alt = getAttribute(altAttr);
if (!alt.isNull())
textOrder.append(AccessibilityText(alt, AlternativeText));
}
Node* node = this->node();
if (!node)
return;
#if ENABLE(SVG)
// SVG elements all can have a <svg:title> element inside which should act as the descriptive text.
if (node->isSVGElement() && toSVGElement(node)->isSVGStyledElement())
textOrder.append(AccessibilityText(toSVGStyledElement(node)->title(), AlternativeText));
#endif
#if ENABLE(MATHML)
if (node->isElementNode() && toElement(node)->isMathMLElement())
textOrder.append(AccessibilityText(getAttribute(MathMLNames::alttextAttr), AlternativeText));
#endif
}
void AccessibilityNodeObject::visibleText(Vector<AccessibilityText>& textOrder) const
{
Node* node = this->node();
if (!node)
return;
bool isInputTag = isHTMLInputElement(node);
if (isInputTag) {
HTMLInputElement* input = toHTMLInputElement(node);
if (input->isTextButton()) {
textOrder.append(AccessibilityText(input->valueWithDefault(), VisibleText));
return;
}
}
// If this node isn't rendered, there's no inner text we can extract from a select element.
if (!isAccessibilityRenderObject() && node->hasTagName(selectTag))
return;
bool useTextUnderElement = false;
switch (roleValue()) {
case PopUpButtonRole:
// Native popup buttons should not use their button children's text as a title. That value is retrieved through stringValue().
if (node->hasTagName(selectTag))
break;
case ButtonRole:
case ToggleButtonRole:
case CheckBoxRole:
case ListBoxOptionRole:
case ListItemRole:
case MenuButtonRole:
case MenuItemRole:
case RadioButtonRole:
case TabRole:
case ProgressIndicatorRole:
useTextUnderElement = true;
break;
default:
break;
}
// If it's focusable but it's not content editable or a known control type, then it will appear to
// the user as a single atomic object, so we should use its text as the default title.
if (isHeading() || isLink() || isGenericFocusableElement())
useTextUnderElement = true;
if (useTextUnderElement) {
String text = textUnderElement();
if (!text.isEmpty())
textOrder.append(AccessibilityText(text, ChildrenText));
}
}
void AccessibilityNodeObject::helpText(Vector<AccessibilityText>& textOrder) const
{
const AtomicString& ariaHelp = getAttribute(aria_helpAttr);
if (!ariaHelp.isEmpty())
textOrder.append(AccessibilityText(ariaHelp, HelpText));
String describedBy = ariaDescribedByAttribute();
if (!describedBy.isEmpty())
textOrder.append(AccessibilityText(describedBy, SummaryText));
// Add help type text that is derived from ancestors.
for (Node* curr = node(); curr; curr = curr->parentNode()) {
const AtomicString& summary = getAttribute(summaryAttr);
if (!summary.isEmpty())
textOrder.append(AccessibilityText(summary, SummaryText));
// The title attribute should be used as help text unless it is already being used as descriptive text.
const AtomicString& title = getAttribute(titleAttr);
if (!title.isEmpty())
textOrder.append(AccessibilityText(title, TitleTagText));
// Only take help text from an ancestor element if its a group or an unknown role. If help was
// added to those kinds of elements, it is likely it was meant for a child element.
AccessibilityObject* axObj = axObjectCache()->getOrCreate(curr);
if (!axObj)
return;
AccessibilityRole role = axObj->roleValue();
if (role != GroupRole && role != UnknownRole)
break;
}
}
void AccessibilityNodeObject::accessibilityText(Vector<AccessibilityText>& textOrder)
{
titleElementText(textOrder);
alternativeText(textOrder);
visibleText(textOrder);
helpText(textOrder);
String placeholder = placeholderValue();
if (!placeholder.isEmpty())
textOrder.append(AccessibilityText(placeholder, PlaceholderText));
}
void AccessibilityNodeObject::ariaLabeledByText(Vector<AccessibilityText>& textOrder) const
{
String ariaLabeledBy = ariaLabeledByAttribute();
if (!ariaLabeledBy.isEmpty()) {
Vector<Element*> elements;
ariaLabeledByElements(elements);
Vector<RefPtr<AccessibilityObject> > axElements;
unsigned length = elements.size();
for (unsigned k = 0; k < length; k++) {
RefPtr<AccessibilityObject> axElement = axObjectCache()->getOrCreate(elements[k]);
axElements.append(axElement);
}
textOrder.append(AccessibilityText(ariaLabeledBy, AlternativeText, axElements));
}
}
String AccessibilityNodeObject::alternativeTextForWebArea() const
{
// The WebArea description should follow this order:
// aria-label on the <html>
// title on the <html>
// <title> inside the <head> (of it was set through JS)
// name on the <html>
// For iframes:
// aria-label on the <iframe>
// title on the <iframe>
// name on the <iframe>
Document* document = this->document();
if (!document)
return String();
// Check if the HTML element has an aria-label for the webpage.
if (Element* documentElement = document->documentElement()) {
const AtomicString& ariaLabel = documentElement->getAttribute(aria_labelAttr);
if (!ariaLabel.isEmpty())
return ariaLabel;
}
Node* owner = document->ownerElement();
if (owner) {
if (owner->hasTagName(frameTag) || owner->hasTagName(iframeTag)) {
const AtomicString& title = toElement(owner)->getAttribute(titleAttr);
if (!title.isEmpty())
return title;
return toElement(owner)->getNameAttribute();
}
if (owner->isHTMLElement())
return toHTMLElement(owner)->getNameAttribute();
}
String documentTitle = document->title();
if (!documentTitle.isEmpty())
return documentTitle;
owner = document->body();
if (owner && owner->isHTMLElement())
return toHTMLElement(owner)->getNameAttribute();
return String();
}
String AccessibilityNodeObject::accessibilityDescription() const
{
// Static text should not have a description, it should only have a stringValue.
if (roleValue() == StaticTextRole)
return String();
String ariaDescription = ariaAccessibilityDescription();
if (!ariaDescription.isEmpty())
return ariaDescription;
if (isImage() || isInputImage() || isNativeImage() || isCanvas()) {
// Images should use alt as long as the attribute is present, even if empty.
// Otherwise, it should fallback to other methods, like the title attribute.
const AtomicString& alt = getAttribute(altAttr);
if (!alt.isNull())
return alt;
}
#if ENABLE(SVG)
// SVG elements all can have a <svg:title> element inside which should act as the descriptive text.
if (m_node && m_node->isSVGElement() && toSVGElement(m_node)->isSVGStyledElement())
return toSVGStyledElement(m_node)->title();
#endif
#if ENABLE(MATHML)
if (m_node && m_node->isElementNode() && toElement(m_node)->isMathMLElement())
return getAttribute(MathMLNames::alttextAttr);
#endif
// An element's descriptive text is comprised of title() (what's visible on the screen) and accessibilityDescription() (other descriptive text).
// Both are used to generate what a screen reader speaks.
// If this point is reached (i.e. there's no accessibilityDescription) and there's no title(), we should fallback to using the title attribute.
// The title attribute is normally used as help text (because it is a tooltip), but if there is nothing else available, this should be used (according to ARIA).
if (title().isEmpty())
return getAttribute(titleAttr);
return String();
}
String AccessibilityNodeObject::helpText() const
{
Node* node = this->node();
if (!node)
return String();
const AtomicString& ariaHelp = getAttribute(aria_helpAttr);
if (!ariaHelp.isEmpty())
return ariaHelp;
String describedBy = ariaDescribedByAttribute();
if (!describedBy.isEmpty())
return describedBy;
String description = accessibilityDescription();
for (Node* curr = node; curr; curr = curr->parentNode()) {
if (curr->isHTMLElement()) {
const AtomicString& summary = toElement(curr)->getAttribute(summaryAttr);
if (!summary.isEmpty())
return summary;
// The title attribute should be used as help text unless it is already being used as descriptive text.
const AtomicString& title = toElement(curr)->getAttribute(titleAttr);
if (!title.isEmpty() && description != title)
return title;
}
// Only take help text from an ancestor element if its a group or an unknown role. If help was
// added to those kinds of elements, it is likely it was meant for a child element.
AccessibilityObject* axObj = axObjectCache()->getOrCreate(curr);
if (axObj) {
AccessibilityRole role = axObj->roleValue();
if (role != GroupRole && role != UnknownRole)
break;
}
}
return String();
}
unsigned AccessibilityNodeObject::hierarchicalLevel() const
{
Node* node = this->node();
if (!node || !node->isElementNode())
return 0;
Element* element = toElement(node);
String ariaLevel = element->getAttribute(aria_levelAttr);
if (!ariaLevel.isEmpty())
return ariaLevel.toInt();
// Only tree item will calculate its level through the DOM currently.
if (roleValue() != TreeItemRole)
return 0;
// Hierarchy leveling starts at 1, to match the aria-level spec.
// We measure tree hierarchy by the number of groups that the item is within.
unsigned level = 1;
for (AccessibilityObject* parent = parentObject(); parent; parent = parent->parentObject()) {
AccessibilityRole parentRole = parent->roleValue();
if (parentRole == GroupRole)
level++;
else if (parentRole == TreeRole)
break;
}
return level;
}
// When building the textUnderElement for an object, determine whether or not
// we should include the inner text of this given descendant object or skip it.
static bool shouldUseAccessiblityObjectInnerText(AccessibilityObject* obj, AccessibilityTextUnderElementMode mode)
{
// Do not use any heuristic if we are explicitly asking to include all the children.
if (mode == TextUnderElementModeIncludeAllChildren)
return true;
// Consider this hypothetical example:
// <div tabindex=0>
// <h2>
// Table of contents
// </h2>
// <a href="#start">Jump to start of book</a>
// <ul>
// <li><a href="#1">Chapter 1</a></li>
// <li><a href="#1">Chapter 2</a></li>
// </ul>
// </div>
//
// The goal is to return a reasonable title for the outer container div, because
// it's focusable - but without making its title be the full inner text, which is
// quite long. As a heuristic, skip links, controls, and elements that are usually
// containers with lots of children.
if (equalIgnoringCase(obj->getAttribute(aria_hiddenAttr), "true"))
return false;
// If something doesn't expose any children, then we can always take the inner text content.
// This is what we want when someone puts an <a> inside a <button> for example.
if (obj->isDescendantOfBarrenParent())
return true;
// Skip focusable children, so we don't include the text of links and controls.
if (obj->canSetFocusAttribute())
return false;
// Skip big container elements like lists, tables, etc.
if (obj->isList() || obj->isAccessibilityTable() || obj->isTree() || obj->isCanvas())
return false;
return true;
}
String AccessibilityNodeObject::textUnderElement(AccessibilityTextUnderElementMode mode) const
{
Node* node = this->node();
if (node && node->isTextNode())
return toText(node)->wholeText();
StringBuilder builder;
for (AccessibilityObject* child = firstChild(); child; child = child->nextSibling()) {
if (!shouldUseAccessiblityObjectInnerText(child, mode))
continue;
if (child->isAccessibilityNodeObject()) {
Vector<AccessibilityText> textOrder;
toAccessibilityNodeObject(child)->alternativeText(textOrder);
if (textOrder.size() > 0 && textOrder[0].text.length()) {
if (builder.length())
builder.append(' ');
builder.append(textOrder[0].text);
continue;
}
}
String childText = child->textUnderElement(mode);
if (childText.length()) {
if (builder.length())
builder.append(' ');
builder.append(childText);
}
}
return builder.toString().stripWhiteSpace().simplifyWhiteSpace();
}
String AccessibilityNodeObject::title() const
{
Node* node = this->node();
if (!node)
return String();
bool isInputTag = isHTMLInputElement(node);
if (isInputTag) {
HTMLInputElement* input = toHTMLInputElement(node);
if (input->isTextButton())
return input->valueWithDefault();
}
if (isInputTag || AccessibilityObject::isARIAInput(ariaRoleAttribute()) || isControl()) {
HTMLLabelElement* label = labelForElement(toElement(node));
if (label && !exposesTitleUIElement())
return label->innerText();
}
// If this node isn't rendered, there's no inner text we can extract from a select element.
if (!isAccessibilityRenderObject() && node->hasTagName(selectTag))
return String();
switch (roleValue()) {
case PopUpButtonRole:
// Native popup buttons should not use their button children's text as a title. That value is retrieved through stringValue().
if (node->hasTagName(selectTag))
return String();
case ButtonRole:
case ToggleButtonRole:
case CheckBoxRole:
case ListBoxOptionRole:
case ListItemRole:
case MenuButtonRole:
case MenuItemRole:
case RadioButtonRole:
case TabRole:
return textUnderElement();
// SVGRoots should not use the text under itself as a title. That could include the text of objects like <text>.
case SVGRootRole:
return String();
default:
break;
}
if (isHeading() || isLink())
return textUnderElement();
// If it's focusable but it's not content editable or a known control type, then it will appear to
// the user as a single atomic object, so we should use its text as the default title.
if (isGenericFocusableElement())
return textUnderElement();
return String();
}
String AccessibilityNodeObject::text() const
{
// If this is a user defined static text, use the accessible name computation.
if (ariaRoleAttribute() == StaticTextRole)
return ariaAccessibilityDescription();
if (!isTextControl())
return String();
Node* node = this->node();
if (!node)
return String();
if (isNativeTextControl() && (isHTMLTextAreaElement(node) || isHTMLInputElement(node)))
return toHTMLTextFormControlElement(node)->value();
if (!node->isElementNode())
return String();
return toElement(node)->innerText();
}
String AccessibilityNodeObject::stringValue() const
{
Node* node = this->node();
if (!node)
return String();
if (ariaRoleAttribute() == StaticTextRole) {
String staticText = text();
if (!staticText.length())
staticText = textUnderElement();
return staticText;
}
if (node->isTextNode())
return textUnderElement();
if (node->hasTagName(selectTag)) {
HTMLSelectElement* selectElement = toHTMLSelectElement(node);
int selectedIndex = selectElement->selectedIndex();
const Vector<HTMLElement*> listItems = selectElement->listItems();
if (selectedIndex >= 0 && static_cast<size_t>(selectedIndex) < listItems.size()) {
const AtomicString& overriddenDescription = listItems[selectedIndex]->fastGetAttribute(aria_labelAttr);
if (!overriddenDescription.isNull())
return overriddenDescription;
}
if (!selectElement->multiple())
return selectElement->value();
return String();
}
if (isTextControl())
return text();
// FIXME: We might need to implement a value here for more types
// FIXME: It would be better not to advertise a value at all for the types for which we don't implement one;
// this would require subclassing or making accessibilityAttributeNames do something other than return a
// single static array.
return String();
}
void AccessibilityNodeObject::colorValue(int& r, int& g, int& b) const
{
r = 0;
g = 0;
b = 0;
if (!isColorWell())
return;
if (!node() || !isHTMLInputElement(node()))
return;
HTMLInputElement* input = toHTMLInputElement(node());
const AtomicString& type = input->getAttribute(typeAttr);
if (!equalIgnoringCase(type, "color"))
return;
// HTMLInputElement::value always returns a string parseable by Color().
Color color(input->value());
r = color.red();
g = color.green();
b = color.blue();
}
// This function implements the ARIA accessible name as described by the Mozilla
// ARIA Implementer's Guide.
static String accessibleNameForNode(Node* node)
{
if (node->isTextNode())
return toText(node)->data();
if (isHTMLInputElement(node))
return toHTMLInputElement(node)->value();
if (node->isHTMLElement()) {
const AtomicString& alt = toHTMLElement(node)->getAttribute(altAttr);
if (!alt.isEmpty())
return alt;
}
return String();
}
String AccessibilityNodeObject::accessibilityDescriptionForElements(Vector<Element*> &elements) const
{
StringBuilder builder;
unsigned size = elements.size();
for (unsigned i = 0; i < size; ++i) {
Element* idElement = elements[i];
builder.append(accessibleNameForNode(idElement));
for (Node* n = idElement->firstChild(); n; n = NodeTraversal::next(n, idElement))
builder.append(accessibleNameForNode(n));
if (i != size - 1)
builder.append(' ');
}
return builder.toString();
}
String AccessibilityNodeObject::ariaDescribedByAttribute() const
{
Vector<Element*> elements;
elementsFromAttribute(elements, aria_describedbyAttr);
return accessibilityDescriptionForElements(elements);
}
void AccessibilityNodeObject::elementsFromAttribute(Vector<Element*>& elements, const QualifiedName& attribute) const
{
Node* node = this->node();
if (!node || !node->isElementNode())
return;
TreeScope* scope = node->treeScope();
if (!scope)
return;
String idList = getAttribute(attribute).string();
if (idList.isEmpty())
return;
idList.replace('\n', ' ');
Vector<String> idVector;
idList.split(' ', idVector);
unsigned size = idVector.size();
for (unsigned i = 0; i < size; ++i) {
AtomicString idName(idVector[i]);
Element* idElement = scope->getElementById(idName);
if (idElement)
elements.append(idElement);
}
}
void AccessibilityNodeObject::ariaLabeledByElements(Vector<Element*>& elements) const
{
elementsFromAttribute(elements, aria_labeledbyAttr);
if (!elements.size())
elementsFromAttribute(elements, aria_labelledbyAttr);
}
String AccessibilityNodeObject::ariaLabeledByAttribute() const
{
Vector<Element*> elements;
ariaLabeledByElements(elements);
return accessibilityDescriptionForElements(elements);
}
bool AccessibilityNodeObject::canSetFocusAttribute() const
{
Node* node = this->node();
if (!node)
return false;
if (isWebArea())
return true;
// NOTE: It would be more accurate to ask the document whether setFocusedElement() would
// do anything. For example, setFocusedElement() will do nothing if the current focused
// node will not relinquish the focus.
if (!node)
return false;
if (!node->isElementNode())
return false;
Element* element = toElement(node);
if (element->isDisabledFormControl())
return false;
return element->supportsFocus();
}
AccessibilityRole AccessibilityNodeObject::determineAriaRoleAttribute() const
{
const AtomicString& ariaRole = getAttribute(roleAttr);
if (ariaRole.isNull() || ariaRole.isEmpty())
return UnknownRole;
AccessibilityRole role = ariaRoleToWebCoreRole(ariaRole);
// ARIA states if an item can get focus, it should not be presentational.
if (role == PresentationalRole && canSetFocusAttribute())
return UnknownRole;
if (role == ButtonRole)
role = buttonRoleType();
if (role == TextAreaRole && !ariaIsMultiline())
role = TextFieldRole;
role = remapAriaRoleDueToParent(role);
if (role)
return role;
return UnknownRole;
}
AccessibilityRole AccessibilityNodeObject::ariaRoleAttribute() const
{
return m_ariaRole;
}
AccessibilityRole AccessibilityNodeObject::remapAriaRoleDueToParent(AccessibilityRole role) const
{
// Some objects change their role based on their parent.
// However, asking for the unignoredParent calls accessibilityIsIgnored(), which can trigger a loop.
// While inside the call stack of creating an element, we need to avoid accessibilityIsIgnored().
// https://bugs.webkit.org/show_bug.cgi?id=65174
if (role != ListBoxOptionRole && role != MenuItemRole)
return role;
for (AccessibilityObject* parent = parentObject(); parent && !parent->accessibilityIsIgnored(); parent = parent->parentObject()) {
AccessibilityRole parentAriaRole = parent->ariaRoleAttribute();
// Selects and listboxes both have options as child roles, but they map to different roles within WebCore.
if (role == ListBoxOptionRole && parentAriaRole == MenuRole)
return MenuItemRole;
// An aria "menuitem" may map to MenuButton or MenuItem depending on its parent.
if (role == MenuItemRole && parentAriaRole == GroupRole)
return MenuButtonRole;
// If the parent had a different role, then we don't need to continue searching up the chain.
if (parentAriaRole)
break;
}
return role;
}
// If you call node->rendererIsEditable() since that will return true if an ancestor is editable.
// This only returns true if this is the element that actually has the contentEditable attribute set.
bool AccessibilityNodeObject::hasContentEditableAttributeSet() const
{
if (!hasAttribute(contenteditableAttr))
return false;
const AtomicString& contentEditableValue = getAttribute(contenteditableAttr);
// Both "true" (case-insensitive) and the empty string count as true.
return contentEditableValue.isEmpty() || equalIgnoringCase(contentEditableValue, "true");
}
bool AccessibilityNodeObject::canSetSelectedAttribute() const
{
// Elements that can be selected
switch (roleValue()) {
case CellRole:
case RadioButtonRole:
case RowHeaderRole:
case RowRole:
case TabListRole:
case TabRole:
case TreeGridRole:
case TreeItemRole:
case TreeRole:
return isEnabled();
default:
return false;
}
}
} // namespace WebCore
|