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
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
* Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc.
* All rights reserved.
* Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
* Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
* Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
* (http://www.torchmobile.com/)
* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "core/css/SelectorChecker.h"
#include "core/HTMLNames.h"
#include "core/css/CSSSelectorList.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/Fullscreen.h"
#include "core/dom/NodeComputedStyle.h"
#include "core/dom/NthIndexCache.h"
#include "core/dom/StyleEngine.h"
#include "core/dom/Text.h"
#include "core/dom/shadow/FlatTreeTraversal.h"
#include "core/dom/shadow/InsertionPoint.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/editing/FrameSelection.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLDocument.h"
#include "core/html/HTMLFrameElementBase.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLOptionElement.h"
#include "core/html/HTMLSelectElement.h"
#include "core/html/HTMLSlotElement.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "core/html/track/vtt/VTTElement.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/page/FocusController.h"
#include "core/page/Page.h"
#include "core/style/ComputedStyle.h"
#include "platform/scroll/ScrollableArea.h"
#include "platform/scroll/ScrollbarTheme.h"
#include "wtf/AutoReset.h"
namespace blink {
using namespace HTMLNames;
static bool isFrameFocused(const Element& element) {
return element.document().frame() &&
element.document().frame()->selection().isFocusedAndActive();
}
static bool matchesSpatialNavigationFocusPseudoClass(const Element& element) {
return isHTMLOptionElement(element) &&
toHTMLOptionElement(element).spatialNavigationFocused() &&
isFrameFocused(element);
}
static bool matchesListBoxPseudoClass(const Element& element) {
return isHTMLSelectElement(element) &&
!toHTMLSelectElement(element).usesMenuList();
}
static bool matchesTagName(const Element& element,
const QualifiedName& tagQName) {
if (tagQName == anyQName())
return true;
const AtomicString& localName = tagQName.localName();
if (localName != starAtom && localName != element.localName()) {
if (element.isHTMLElement() || !element.document().isHTMLDocument())
return false;
// Non-html elements in html documents are normalized to their camel-cased
// version during parsing if applicable. Yet, type selectors are lower-cased
// for selectors in html documents. Compare the upper case converted names
// instead to allow matching SVG elements like foreignObject.
if (element.tagQName().localNameUpper() != tagQName.localNameUpper())
return false;
}
const AtomicString& namespaceURI = tagQName.namespaceURI();
return namespaceURI == starAtom || namespaceURI == element.namespaceURI();
}
static Element* parentElement(
const SelectorChecker::SelectorCheckingContext& context) {
// - If context.scope is a shadow root, we should walk up to its shadow host.
// - If context.scope is some element in some shadow tree and querySelector
// initialized the context, e.g. shadowRoot.querySelector(':host *'),
// (a) context.element has the same treescope as context.scope, need to walk
// up to its shadow host.
// (b) Otherwise, should not walk up from a shadow root to a shadow host.
if (context.scope &&
(context.scope == context.element->containingShadowRoot() ||
context.scope->treeScope() == context.element->treeScope()))
return context.element->parentOrShadowHostElement();
return context.element->parentElement();
}
// If context has scope, return slot that matches the scope, otherwise return
// the assigned slot for scope-less matching of ::slotted pseudo element.
static const HTMLSlotElement* findSlotElementInScope(
const SelectorChecker::SelectorCheckingContext& context) {
if (!context.scope)
return context.element->assignedSlot();
for (const HTMLSlotElement* slot = context.element->assignedSlot(); slot;
slot = slot->assignedSlot()) {
if (slot->treeScope() == context.scope->treeScope())
return slot;
}
return nullptr;
}
static bool scopeContainsLastMatchedElement(
const SelectorChecker::SelectorCheckingContext& context) {
// If this context isn't scoped, skip checking.
if (!context.scope)
return true;
if (context.scope->treeScope() == context.element->treeScope())
return true;
// Because Blink treats a shadow host's TreeScope as a separate one from its
// descendent shadow roots, if the last matched element is a shadow host, the
// condition above isn't met, even though it should be.
return context.element == context.scope->ownerShadowHost() &&
(!context.previousElement ||
context.previousElement->isInDescendantTreeOf(context.element));
}
static inline bool nextSelectorExceedsScope(
const SelectorChecker::SelectorCheckingContext& context) {
if (context.scope && context.scope->isInShadowTree())
return context.element == context.scope->ownerShadowHost();
return false;
}
static bool shouldMatchHoverOrActive(
const SelectorChecker::SelectorCheckingContext& context) {
// If we're in quirks mode, then :hover and :active should never match anchors
// with no href and *:hover and *:active should not match anything. This is
// specified in https://quirks.spec.whatwg.org/#the-:active-and-:hover-quirk
if (!context.element->document().inQuirksMode())
return true;
if (context.isSubSelector)
return true;
if (context.element->isLink())
return true;
const CSSSelector* selector = context.selector;
while (selector->relation() == CSSSelector::SubSelector &&
selector->tagHistory()) {
selector = selector->tagHistory();
if (selector->match() != CSSSelector::PseudoClass)
return true;
if (selector->getPseudoType() != CSSSelector::PseudoHover &&
selector->getPseudoType() != CSSSelector::PseudoActive)
return true;
}
return false;
}
static bool isFirstChild(Element& element) {
return !ElementTraversal::previousSibling(element);
}
static bool isLastChild(Element& element) {
return !ElementTraversal::nextSibling(element);
}
static bool isFirstOfType(Element& element, const QualifiedName& type) {
return !ElementTraversal::previousSibling(element, HasTagName(type));
}
static bool isLastOfType(Element& element, const QualifiedName& type) {
return !ElementTraversal::nextSibling(element, HasTagName(type));
}
// Recursive check of selectors and combinators
// It can return 4 different values:
// * SelectorMatches - the selector matches the element e
// * SelectorFailsLocally - the selector fails for the element e
// * SelectorFailsAllSiblings - the selector fails for e and any sibling of e
// * SelectorFailsCompletely - the selector fails for e and any sibling or
// ancestor of e
SelectorChecker::MatchStatus SelectorChecker::matchSelector(
const SelectorCheckingContext& context,
MatchResult& result) const {
MatchResult subResult;
if (!checkOne(context, subResult))
return SelectorFailsLocally;
if (subResult.dynamicPseudo != PseudoIdNone)
result.dynamicPseudo = subResult.dynamicPseudo;
if (context.selector->isLastInTagHistory()) {
if (scopeContainsLastMatchedElement(context)) {
result.specificity += subResult.specificity;
return SelectorMatches;
}
return SelectorFailsLocally;
}
MatchStatus match;
if (context.selector->relation() != CSSSelector::SubSelector) {
if (nextSelectorExceedsScope(context))
return SelectorFailsCompletely;
if (context.pseudoId != PseudoIdNone &&
context.pseudoId != result.dynamicPseudo)
return SelectorFailsCompletely;
AutoReset<PseudoId> dynamicPseudoScope(&result.dynamicPseudo, PseudoIdNone);
match = matchForRelation(context, result);
} else {
match = matchForSubSelector(context, result);
}
if (match == SelectorMatches)
result.specificity += subResult.specificity;
return match;
}
static inline SelectorChecker::SelectorCheckingContext
prepareNextContextForRelation(
const SelectorChecker::SelectorCheckingContext& context) {
SelectorChecker::SelectorCheckingContext nextContext(context);
DCHECK(context.selector->tagHistory());
nextContext.selector = context.selector->tagHistory();
return nextContext;
}
SelectorChecker::MatchStatus SelectorChecker::matchForSubSelector(
const SelectorCheckingContext& context,
MatchResult& result) const {
SelectorCheckingContext nextContext = prepareNextContextForRelation(context);
PseudoId dynamicPseudo = result.dynamicPseudo;
nextContext.hasScrollbarPseudo =
dynamicPseudo != PseudoIdNone &&
(m_scrollbar || dynamicPseudo == PseudoIdScrollbarCorner ||
dynamicPseudo == PseudoIdResizer);
nextContext.hasSelectionPseudo = dynamicPseudo == PseudoIdSelection;
nextContext.isSubSelector = true;
return matchSelector(nextContext, result);
}
static inline bool isV0ShadowRoot(const Node* node) {
return node && node->isShadowRoot() &&
toShadowRoot(node)->type() == ShadowRootType::V0;
}
SelectorChecker::MatchStatus SelectorChecker::matchForPseudoShadow(
const SelectorCheckingContext& context,
const ContainerNode* node,
MatchResult& result) const {
if (!isV0ShadowRoot(node))
return SelectorFailsCompletely;
if (!context.previousElement)
return SelectorFailsCompletely;
return matchSelector(context, result);
}
static inline Element* parentOrV0ShadowHostElement(const Element& element) {
if (element.parentNode() && element.parentNode()->isShadowRoot()) {
if (toShadowRoot(element.parentNode())->type() != ShadowRootType::V0)
return nullptr;
}
return element.parentOrShadowHostElement();
}
static inline Element* parentOrOpenShadowHostElement(const Element& element) {
if (element.parentNode() && element.parentNode()->isShadowRoot()) {
if (toShadowRoot(element.parentNode())->type() != ShadowRootType::Open)
return nullptr;
}
return element.parentOrShadowHostElement();
}
SelectorChecker::MatchStatus SelectorChecker::matchForRelation(
const SelectorCheckingContext& context,
MatchResult& result) const {
SelectorCheckingContext nextContext = prepareNextContextForRelation(context);
CSSSelector::RelationType relation = context.selector->relation();
// Disable :visited matching when we see the first link or try to match
// anything else than an ancestors.
if (!context.isSubSelector &&
(context.element->isLink() ||
(relation != CSSSelector::Descendant && relation != CSSSelector::Child)))
nextContext.visitedMatchType = VisitedMatchDisabled;
nextContext.inRightmostCompound = false;
nextContext.isSubSelector = false;
nextContext.previousElement = context.element;
nextContext.pseudoId = PseudoIdNone;
switch (relation) {
case CSSSelector::Descendant:
if (context.selector->relationIsAffectedByPseudoContent()) {
for (Element* element = context.element; element;
element = element->parentElement()) {
if (matchForPseudoContent(nextContext, *element, result) ==
SelectorMatches)
return SelectorMatches;
}
return SelectorFailsCompletely;
}
if (nextContext.selector->getPseudoType() == CSSSelector::PseudoShadow)
return matchForPseudoShadow(
nextContext, context.element->containingShadowRoot(), result);
for (nextContext.element = parentElement(context); nextContext.element;
nextContext.element = parentElement(nextContext)) {
MatchStatus match = matchSelector(nextContext, result);
if (match == SelectorMatches || match == SelectorFailsCompletely)
return match;
if (nextSelectorExceedsScope(nextContext))
return SelectorFailsCompletely;
}
return SelectorFailsCompletely;
case CSSSelector::Child: {
if (context.selector->relationIsAffectedByPseudoContent())
return matchForPseudoContent(nextContext, *context.element, result);
if (nextContext.selector->getPseudoType() == CSSSelector::PseudoShadow)
return matchForPseudoShadow(nextContext, context.element->parentNode(),
result);
nextContext.element = parentElement(context);
if (!nextContext.element)
return SelectorFailsCompletely;
return matchSelector(nextContext, result);
}
case CSSSelector::DirectAdjacent:
// Shadow roots can't have sibling elements
if (nextContext.selector->getPseudoType() == CSSSelector::PseudoShadow)
return SelectorFailsCompletely;
if (m_mode == ResolvingStyle) {
if (ContainerNode* parent =
context.element->parentElementOrShadowRoot())
parent->setChildrenAffectedByDirectAdjacentRules();
}
nextContext.element = ElementTraversal::previousSibling(*context.element);
if (!nextContext.element)
return SelectorFailsAllSiblings;
return matchSelector(nextContext, result);
case CSSSelector::IndirectAdjacent:
// Shadow roots can't have sibling elements
if (nextContext.selector->getPseudoType() == CSSSelector::PseudoShadow)
return SelectorFailsCompletely;
if (m_mode == ResolvingStyle) {
if (ContainerNode* parent =
context.element->parentElementOrShadowRoot())
parent->setChildrenAffectedByIndirectAdjacentRules();
}
nextContext.element = ElementTraversal::previousSibling(*context.element);
for (; nextContext.element;
nextContext.element =
ElementTraversal::previousSibling(*nextContext.element)) {
MatchStatus match = matchSelector(nextContext, result);
if (match == SelectorMatches || match == SelectorFailsAllSiblings ||
match == SelectorFailsCompletely)
return match;
}
return SelectorFailsAllSiblings;
case CSSSelector::ShadowPseudo: {
if (!m_isUARule && !m_isQuerySelector &&
context.selector->getPseudoType() == CSSSelector::PseudoShadow)
Deprecation::countDeprecation(context.element->document(),
UseCounter::CSSSelectorPseudoShadow);
// If we're in the same tree-scope as the scoping element, then following
// a shadow descendant combinator would escape that and thus the scope.
if (context.scope && context.scope->ownerShadowHost() &&
context.scope->ownerShadowHost()->treeScope() ==
context.element->treeScope())
return SelectorFailsCompletely;
Element* shadowHost = context.element->ownerShadowHost();
if (!shadowHost)
return SelectorFailsCompletely;
nextContext.element = shadowHost;
return matchSelector(nextContext, result);
}
case CSSSelector::ShadowDeep: {
if (!m_isUARule && !m_isQuerySelector)
Deprecation::countDeprecation(context.element->document(),
UseCounter::CSSDeepCombinator);
if (ShadowRoot* root = context.element->containingShadowRoot()) {
if (root->type() == ShadowRootType::UserAgent)
return SelectorFailsCompletely;
}
if (context.selector->relationIsAffectedByPseudoContent()) {
// TODO(kochi): closed mode tree should be handled as well for
// ::content.
for (Element* element = context.element; element;
element = element->parentOrShadowHostElement()) {
if (matchForPseudoContent(nextContext, *element, result) ==
SelectorMatches) {
if (context.element->isInShadowTree())
UseCounter::count(context.element->document(),
UseCounter::CSSDeepCombinatorAndShadow);
return SelectorMatches;
}
}
return SelectorFailsCompletely;
}
for (nextContext.element = parentOrV0ShadowHostElement(*context.element);
nextContext.element;
nextContext.element =
parentOrV0ShadowHostElement(*nextContext.element)) {
MatchStatus match = matchSelector(nextContext, result);
if (match == SelectorMatches && context.element->isInShadowTree())
UseCounter::count(context.element->document(),
UseCounter::CSSDeepCombinatorAndShadow);
if (match == SelectorMatches || match == SelectorFailsCompletely)
return match;
if (nextSelectorExceedsScope(nextContext))
return SelectorFailsCompletely;
}
return SelectorFailsCompletely;
}
case CSSSelector::ShadowPiercingDescendant: {
DCHECK(m_isQuerySelector);
UseCounter::count(context.element->document(),
UseCounter::CSSShadowPiercingDescendantCombinator);
// TODO(kochi): parentOrOpenShadowHostElement() is necessary because
// SelectorQuery can pass V0 shadow roots. All closed shadow roots are
// already filtered out, thus once V0 is removed this logic can use
// parentOrShadowHostElement() instead.
for (nextContext.element =
parentOrOpenShadowHostElement(*context.element);
nextContext.element;
nextContext.element =
parentOrOpenShadowHostElement(*nextContext.element)) {
MatchStatus match = matchSelector(nextContext, result);
if (match == SelectorMatches || match == SelectorFailsCompletely)
return match;
if (nextSelectorExceedsScope(nextContext))
break;
}
return SelectorFailsCompletely;
}
case CSSSelector::ShadowSlot: {
const HTMLSlotElement* slot = findSlotElementInScope(context);
if (!slot)
return SelectorFailsCompletely;
nextContext.element = const_cast<HTMLSlotElement*>(slot);
return matchSelector(nextContext, result);
}
case CSSSelector::SubSelector:
break;
}
NOTREACHED();
return SelectorFailsCompletely;
}
SelectorChecker::MatchStatus SelectorChecker::matchForPseudoContent(
const SelectorCheckingContext& context,
const Element& element,
MatchResult& result) const {
HeapVector<Member<InsertionPoint>, 8> insertionPoints;
collectDestinationInsertionPoints(element, insertionPoints);
SelectorCheckingContext nextContext(context);
for (const auto& insertionPoint : insertionPoints) {
nextContext.element = insertionPoint;
// TODO(esprehn): Why does SharingRules have a special case?
if (m_mode == SharingRules)
nextContext.scope = insertionPoint->containingShadowRoot();
if (match(nextContext, result))
return SelectorMatches;
}
return SelectorFailsLocally;
}
static bool attributeValueMatches(const Attribute& attributeItem,
CSSSelector::MatchType match,
const AtomicString& selectorValue,
TextCaseSensitivity caseSensitivity) {
// TODO(esprehn): How do we get here with a null value?
const AtomicString& value = attributeItem.value();
if (value.isNull())
return false;
switch (match) {
case CSSSelector::AttributeExact:
if (caseSensitivity == TextCaseSensitive)
return selectorValue == value;
return equalIgnoringASCIICase(selectorValue, value);
case CSSSelector::AttributeSet:
return true;
case CSSSelector::AttributeList: {
// Ignore empty selectors or selectors containing HTML spaces
if (selectorValue.isEmpty() ||
selectorValue.find(&isHTMLSpace<UChar>) != kNotFound)
return false;
unsigned startSearchAt = 0;
while (true) {
size_t foundPos =
value.find(selectorValue, startSearchAt, caseSensitivity);
if (foundPos == kNotFound)
return false;
if (!foundPos || isHTMLSpace<UChar>(value[foundPos - 1])) {
unsigned endStr = foundPos + selectorValue.length();
if (endStr == value.length() || isHTMLSpace<UChar>(value[endStr]))
break; // We found a match.
}
// No match. Keep looking.
startSearchAt = foundPos + 1;
}
return true;
}
case CSSSelector::AttributeContain:
if (selectorValue.isEmpty())
return false;
return value.contains(selectorValue, caseSensitivity);
case CSSSelector::AttributeBegin:
if (selectorValue.isEmpty())
return false;
return value.startsWith(selectorValue, caseSensitivity);
case CSSSelector::AttributeEnd:
if (selectorValue.isEmpty())
return false;
return value.endsWith(selectorValue, caseSensitivity);
case CSSSelector::AttributeHyphen:
if (value.length() < selectorValue.length())
return false;
if (!value.startsWith(selectorValue, caseSensitivity))
return false;
// It they start the same, check for exact match or following '-':
if (value.length() != selectorValue.length() &&
value[selectorValue.length()] != '-')
return false;
return true;
default:
NOTREACHED();
return false;
}
}
static bool anyAttributeMatches(Element& element,
CSSSelector::MatchType match,
const CSSSelector& selector) {
const QualifiedName& selectorAttr = selector.attribute();
// Should not be possible from the CSS grammar.
DCHECK_NE(selectorAttr.localName(), starAtom);
// Synchronize the attribute in case it is lazy-computed.
// Currently all lazy properties have a null namespace, so only pass
// localName().
element.synchronizeAttribute(selectorAttr.localName());
const AtomicString& selectorValue = selector.value();
TextCaseSensitivity caseSensitivity =
(selector.attributeMatch() == CSSSelector::CaseInsensitive)
? TextCaseASCIIInsensitive
: TextCaseSensitive;
AttributeCollection attributes = element.attributesWithoutUpdate();
for (const auto& attributeItem : attributes) {
if (!attributeItem.matches(selectorAttr)) {
if (element.isHTMLElement() || !element.document().isHTMLDocument())
continue;
// Non-html attributes in html documents are normalized to their camel-
// cased version during parsing if applicable. Yet, attribute selectors
// are lower-cased for selectors in html documents. Compare the selector
// and the attribute local name insensitively to e.g. allow matching SVG
// attributes like viewBox.
if (!attributeItem.matchesCaseInsensitive(selectorAttr))
continue;
}
if (attributeValueMatches(attributeItem, match, selectorValue,
caseSensitivity))
return true;
if (caseSensitivity == TextCaseASCIIInsensitive) {
if (selectorAttr.namespaceURI() != starAtom)
return false;
continue;
}
// Legacy dictates that values of some attributes should be compared in
// a case-insensitive manner regardless of whether the case insensitive
// flag is set or not.
bool legacyCaseInsensitive =
element.document().isHTMLDocument() &&
!HTMLDocument::isCaseSensitiveAttribute(selectorAttr);
// If case-insensitive, re-check, and count if result differs.
// See http://code.google.com/p/chromium/issues/detail?id=327060
if (legacyCaseInsensitive &&
attributeValueMatches(attributeItem, match, selectorValue,
TextCaseASCIIInsensitive)) {
UseCounter::count(element.document(),
UseCounter::CaseInsensitiveAttrSelectorMatch);
return true;
}
if (selectorAttr.namespaceURI() != starAtom)
return false;
}
return false;
}
bool SelectorChecker::checkOne(const SelectorCheckingContext& context,
MatchResult& result) const {
DCHECK(context.element);
Element& element = *context.element;
DCHECK(context.selector);
const CSSSelector& selector = *context.selector;
// Only :host and :host-context() should match the host:
// http://drafts.csswg.org/css-scoping/#host-element
if (context.scope && context.scope->ownerShadowHost() == element &&
(!selector.isHostPseudoClass() && !context.treatShadowHostAsNormalScope &&
selector.match() != CSSSelector::PseudoElement))
return false;
switch (selector.match()) {
case CSSSelector::Tag:
return matchesTagName(element, selector.tagQName());
case CSSSelector::Class:
return element.hasClass() &&
element.classNames().contains(selector.value());
case CSSSelector::Id:
return element.hasID() &&
element.idForStyleResolution() == selector.value();
// Attribute selectors
case CSSSelector::AttributeExact:
case CSSSelector::AttributeSet:
case CSSSelector::AttributeHyphen:
case CSSSelector::AttributeList:
case CSSSelector::AttributeContain:
case CSSSelector::AttributeBegin:
case CSSSelector::AttributeEnd:
return anyAttributeMatches(element, selector.match(), selector);
case CSSSelector::PseudoClass:
return checkPseudoClass(context, result);
case CSSSelector::PseudoElement:
return checkPseudoElement(context, result);
default:
NOTREACHED();
return false;
}
}
bool SelectorChecker::checkPseudoNot(const SelectorCheckingContext& context,
MatchResult& result) const {
const CSSSelector& selector = *context.selector;
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
DCHECK(selector.selectorList());
for (subContext.selector = selector.selectorList()->first();
subContext.selector;
subContext.selector = subContext.selector->tagHistory()) {
// :not cannot nest. I don't really know why this is a
// restriction in CSS3, but it is, so let's honor it.
// the parser enforces that this never occurs
DCHECK_NE(subContext.selector->getPseudoType(), CSSSelector::PseudoNot);
// We select between :visited and :link when applying. We don't know which
// one applied (or not) yet.
if (subContext.selector->getPseudoType() == CSSSelector::PseudoVisited ||
(subContext.selector->getPseudoType() == CSSSelector::PseudoLink &&
subContext.visitedMatchType == VisitedMatchEnabled))
return true;
if (m_mode == SharingRules) {
// context.scope is not available if m_mode == SharingRules.
// We cannot determine whether :host or :scope matches a given element or
// not.
if (subContext.selector->isHostPseudoClass() ||
subContext.selector->getPseudoType() == CSSSelector::PseudoScope)
return true;
// :hover, :active, :focus, :-webkit-drag relies on setting flags on
// ComputedStyle even if the whole selector may not match. That
// means we cannot share style between elements which may fail
// matching the same selector for different reasons. An example is
// [attr]:hover which both fail for :hover, but an element without
// attr won't reach the :hover selector, hence not setting the bit.
if (subContext.selector->isUserActionPseudoClass())
return true;
}
if (!checkOne(subContext, result))
return true;
}
return false;
}
bool SelectorChecker::checkPseudoClass(const SelectorCheckingContext& context,
MatchResult& result) const {
Element& element = *context.element;
const CSSSelector& selector = *context.selector;
if (context.hasScrollbarPseudo) {
// CSS scrollbars match a specific subset of pseudo classes, and they have
// specialized rules for each
// (since there are no elements involved).
return checkScrollbarPseudoClass(context, result);
}
switch (selector.getPseudoType()) {
case CSSSelector::PseudoNot:
return checkPseudoNot(context, result);
case CSSSelector::PseudoEmpty: {
bool result = true;
for (Node* n = element.firstChild(); n; n = n->nextSibling()) {
if (n->isElementNode()) {
result = false;
break;
}
if (n->isTextNode()) {
Text* textNode = toText(n);
if (!textNode->data().isEmpty()) {
result = false;
break;
}
}
}
if (m_mode == ResolvingStyle) {
element.setStyleAffectedByEmpty();
if (context.inRightmostCompound)
m_elementStyle->setEmptyState(result);
else if (element.computedStyle() &&
(element.document().styleEngine().usesSiblingRules() ||
element.computedStyle()->unique()))
element.mutableComputedStyle()->setEmptyState(result);
}
return result;
}
case CSSSelector::PseudoFirstChild:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle) {
parent->setChildrenAffectedByFirstChildRules();
element.setAffectedByFirstChildRules();
}
return isFirstChild(element);
}
break;
case CSSSelector::PseudoFirstOfType:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByForwardPositionalRules();
return isFirstOfType(element, element.tagQName());
}
break;
case CSSSelector::PseudoLastChild:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle) {
parent->setChildrenAffectedByLastChildRules();
element.setAffectedByLastChildRules();
}
if (!m_isQuerySelector && !parent->isFinishedParsingChildren())
return false;
return isLastChild(element);
}
break;
case CSSSelector::PseudoLastOfType:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByBackwardPositionalRules();
if (!m_isQuerySelector && !parent->isFinishedParsingChildren())
return false;
return isLastOfType(element, element.tagQName());
}
break;
case CSSSelector::PseudoOnlyChild:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle) {
parent->setChildrenAffectedByFirstChildRules();
parent->setChildrenAffectedByLastChildRules();
element.setAffectedByFirstChildRules();
element.setAffectedByLastChildRules();
}
if (!m_isQuerySelector && !parent->isFinishedParsingChildren())
return false;
return isFirstChild(element) && isLastChild(element);
}
break;
case CSSSelector::PseudoOnlyOfType:
// FIXME: This selector is very slow.
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle) {
parent->setChildrenAffectedByForwardPositionalRules();
parent->setChildrenAffectedByBackwardPositionalRules();
}
if (!m_isQuerySelector && !parent->isFinishedParsingChildren())
return false;
return isFirstOfType(element, element.tagQName()) &&
isLastOfType(element, element.tagQName());
}
break;
case CSSSelector::PseudoPlaceholderShown:
if (isTextControlElement(element))
return toTextControlElement(element).isPlaceholderVisible();
break;
case CSSSelector::PseudoNthChild:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByForwardPositionalRules();
return selector.matchNth(NthIndexCache::nthChildIndex(element));
}
break;
case CSSSelector::PseudoNthOfType:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByForwardPositionalRules();
return selector.matchNth(NthIndexCache::nthOfTypeIndex(element));
}
break;
case CSSSelector::PseudoNthLastChild:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByBackwardPositionalRules();
if (!m_isQuerySelector && !parent->isFinishedParsingChildren())
return false;
return selector.matchNth(NthIndexCache::nthLastChildIndex(element));
}
break;
case CSSSelector::PseudoNthLastOfType:
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByBackwardPositionalRules();
if (!m_isQuerySelector && !parent->isFinishedParsingChildren())
return false;
return selector.matchNth(NthIndexCache::nthLastOfTypeIndex(element));
}
break;
case CSSSelector::PseudoTarget:
return element == element.document().cssTarget();
case CSSSelector::PseudoAny: {
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
DCHECK(selector.selectorList());
for (subContext.selector = selector.selectorList()->first();
subContext.selector;
subContext.selector = CSSSelectorList::next(*subContext.selector)) {
if (match(subContext))
return true;
}
} break;
case CSSSelector::PseudoAutofill:
return element.isFormControlElement() &&
toHTMLFormControlElement(element).isAutofilled();
case CSSSelector::PseudoAnyLink:
case CSSSelector::PseudoLink:
return element.isLink();
case CSSSelector::PseudoVisited:
return element.isLink() &&
context.visitedMatchType == VisitedMatchEnabled;
case CSSSelector::PseudoDrag:
if (m_mode == SharingRules)
return true;
if (m_mode == ResolvingStyle) {
if (context.inRightmostCompound) {
m_elementStyle->setAffectedByDrag();
} else {
m_elementStyle->setUnique();
element.setChildrenOrSiblingsAffectedByDrag();
}
}
return element.isDragged();
case CSSSelector::PseudoFocus:
if (m_mode == SharingRules)
return true;
if (m_mode == ResolvingStyle) {
if (context.inRightmostCompound) {
m_elementStyle->setAffectedByFocus();
} else {
m_elementStyle->setUnique();
element.setChildrenOrSiblingsAffectedByFocus();
}
}
return matchesFocusPseudoClass(element);
case CSSSelector::PseudoHover:
if (m_mode == SharingRules)
return true;
if (m_mode == ResolvingStyle) {
if (context.inRightmostCompound) {
m_elementStyle->setAffectedByHover();
} else {
m_elementStyle->setUnique();
element.setChildrenOrSiblingsAffectedByHover();
}
}
if (!shouldMatchHoverOrActive(context))
return false;
if (InspectorInstrumentation::forcePseudoState(&element,
CSSSelector::PseudoHover))
return true;
return element.isHovered();
case CSSSelector::PseudoActive:
if (m_mode == SharingRules)
return true;
if (m_mode == ResolvingStyle) {
if (context.inRightmostCompound) {
m_elementStyle->setAffectedByActive();
} else {
m_elementStyle->setUnique();
element.setChildrenOrSiblingsAffectedByActive();
}
}
if (!shouldMatchHoverOrActive(context))
return false;
if (InspectorInstrumentation::forcePseudoState(&element,
CSSSelector::PseudoActive))
return true;
return element.isActive();
case CSSSelector::PseudoEnabled:
return element.matchesEnabledPseudoClass();
case CSSSelector::PseudoFullPageMedia:
return element.document().isMediaDocument();
case CSSSelector::PseudoDefault:
return element.matchesDefaultPseudoClass();
case CSSSelector::PseudoDisabled:
return element.isDisabledFormControl();
case CSSSelector::PseudoReadOnly:
return element.matchesReadOnlyPseudoClass();
case CSSSelector::PseudoReadWrite:
return element.matchesReadWritePseudoClass();
case CSSSelector::PseudoOptional:
return element.isOptionalFormControl();
case CSSSelector::PseudoRequired:
return element.isRequiredFormControl();
case CSSSelector::PseudoValid:
if (m_mode == ResolvingStyle)
element.document().setContainsValidityStyleRules();
return element.matchesValidityPseudoClasses() && element.isValidElement();
case CSSSelector::PseudoInvalid:
if (m_mode == ResolvingStyle)
element.document().setContainsValidityStyleRules();
return element.matchesValidityPseudoClasses() &&
!element.isValidElement();
case CSSSelector::PseudoChecked: {
if (isHTMLInputElement(element)) {
HTMLInputElement& inputElement = toHTMLInputElement(element);
// Even though WinIE allows checked and indeterminate to
// co-exist, the CSS selector spec says that you can't be
// both checked and indeterminate. We will behave like WinIE
// behind the scenes and just obey the CSS spec here in the
// test for matching the pseudo.
if (inputElement.shouldAppearChecked() &&
!inputElement.shouldAppearIndeterminate())
return true;
} else if (isHTMLOptionElement(element) &&
toHTMLOptionElement(element).selected()) {
return true;
}
break;
}
case CSSSelector::PseudoIndeterminate:
return element.shouldAppearIndeterminate();
case CSSSelector::PseudoRoot:
return element == element.document().documentElement();
case CSSSelector::PseudoLang: {
AtomicString value;
if (element.isVTTElement())
value = toVTTElement(element).language();
else
value = element.computeInheritedLanguage();
const AtomicString& argument = selector.argument();
if (value.isEmpty() ||
!value.startsWith(argument, TextCaseASCIIInsensitive))
break;
if (value.length() != argument.length() &&
value[argument.length()] != '-')
break;
return true;
}
case CSSSelector::PseudoFullScreen:
// While a Document is in the fullscreen state, and the document's current
// fullscreen element is an element in the document, the 'full-screen'
// pseudoclass applies to that element. Also, an <iframe>, <object> or
// <embed> element whose child browsing context's Document is in the
// fullscreen state has the 'full-screen' pseudoclass applied.
if (isHTMLFrameElementBase(element) &&
element.containsFullScreenElement())
return true;
return Fullscreen::isCurrentFullScreenElement(element);
case CSSSelector::PseudoFullScreenAncestor:
return element.containsFullScreenElement();
case CSSSelector::PseudoInRange:
if (m_mode == ResolvingStyle)
element.document().setContainsValidityStyleRules();
return element.isInRange();
case CSSSelector::PseudoOutOfRange:
if (m_mode == ResolvingStyle)
element.document().setContainsValidityStyleRules();
return element.isOutOfRange();
case CSSSelector::PseudoFutureCue:
return element.isVTTElement() && !toVTTElement(element).isPastNode();
case CSSSelector::PseudoPastCue:
return element.isVTTElement() && toVTTElement(element).isPastNode();
case CSSSelector::PseudoScope:
if (m_mode == SharingRules)
return true;
if (context.scope == &element.document())
return element == element.document().documentElement();
return context.scope == &element;
case CSSSelector::PseudoUnresolved:
return element.isUnresolvedV0CustomElement();
case CSSSelector::PseudoDefined:
DCHECK(RuntimeEnabledFeatures::customElementsV1Enabled());
return element.isDefined();
case CSSSelector::PseudoHost:
case CSSSelector::PseudoHostContext:
return checkPseudoHost(context, result);
case CSSSelector::PseudoSpatialNavigationFocus:
return m_isUARule && matchesSpatialNavigationFocusPseudoClass(element);
case CSSSelector::PseudoListBox:
return m_isUARule && matchesListBoxPseudoClass(element);
case CSSSelector::PseudoHostHasAppearance:
if (!m_isUARule)
return false;
if (ShadowRoot* root = element.containingShadowRoot()) {
if (root->type() != ShadowRootType::UserAgent)
return false;
const ComputedStyle* style = root->host().computedStyle();
return style && style->hasAppearance();
}
return false;
case CSSSelector::PseudoWindowInactive:
if (!context.hasSelectionPseudo)
return false;
return !element.document().page()->focusController().isActive();
case CSSSelector::PseudoHorizontal:
case CSSSelector::PseudoVertical:
case CSSSelector::PseudoDecrement:
case CSSSelector::PseudoIncrement:
case CSSSelector::PseudoStart:
case CSSSelector::PseudoEnd:
case CSSSelector::PseudoDoubleButton:
case CSSSelector::PseudoSingleButton:
case CSSSelector::PseudoNoButton:
case CSSSelector::PseudoCornerPresent:
return false;
case CSSSelector::PseudoUnknown:
default:
NOTREACHED();
break;
}
return false;
}
bool SelectorChecker::checkPseudoElement(const SelectorCheckingContext& context,
MatchResult& result) const {
const CSSSelector& selector = *context.selector;
Element& element = *context.element;
switch (selector.getPseudoType()) {
case CSSSelector::PseudoCue: {
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
subContext.scope = nullptr;
subContext.treatShadowHostAsNormalScope = false;
for (subContext.selector = selector.selectorList()->first();
subContext.selector;
subContext.selector = CSSSelectorList::next(*subContext.selector)) {
if (match(subContext))
return true;
}
return false;
}
case CSSSelector::PseudoPlaceholder:
if (ShadowRoot* root = element.containingShadowRoot()) {
return root->type() == ShadowRootType::UserAgent &&
element.shadowPseudoId() == "-webkit-input-placeholder";
}
return false;
case CSSSelector::PseudoWebKitCustomElement: {
if (ShadowRoot* root = element.containingShadowRoot())
return root->type() == ShadowRootType::UserAgent &&
element.shadowPseudoId() == selector.value();
return false;
}
case CSSSelector::PseudoBlinkInternalElement:
if (!m_isUARule)
return false;
if (ShadowRoot* root = element.containingShadowRoot())
return root->type() == ShadowRootType::UserAgent &&
element.shadowPseudoId() == selector.value();
return false;
case CSSSelector::PseudoSlotted: {
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
subContext.scope = nullptr;
subContext.treatShadowHostAsNormalScope = false;
// ::slotted() only allows one compound selector.
DCHECK(selector.selectorList()->first());
DCHECK(!CSSSelectorList::next(*selector.selectorList()->first()));
subContext.selector = selector.selectorList()->first();
return match(subContext);
}
case CSSSelector::PseudoContent:
return element.isInShadowTree() && element.isInsertionPoint();
case CSSSelector::PseudoShadow:
return element.isInShadowTree() && context.previousElement;
default:
if (m_mode == SharingRules)
return true;
DCHECK_NE(m_mode, QueryingRules);
result.dynamicPseudo = CSSSelector::pseudoId(selector.getPseudoType());
DCHECK_NE(result.dynamicPseudo, PseudoIdNone);
return true;
}
}
bool SelectorChecker::checkPseudoHost(const SelectorCheckingContext& context,
MatchResult& result) const {
const CSSSelector& selector = *context.selector;
Element& element = *context.element;
if (m_mode == SharingRules)
return true;
// :host only matches a shadow host when :host is in a shadow tree of the
// shadow host.
if (!context.scope)
return false;
const ContainerNode* shadowHost = context.scope->ownerShadowHost();
if (!shadowHost || shadowHost != element)
return false;
DCHECK(element.shadow());
// For the case with no parameters, i.e. just :host.
if (!selector.selectorList())
return true;
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
bool matched = false;
unsigned maxSpecificity = 0;
// If one of simple selectors matches an element, returns SelectorMatches.
// Just "OR".
for (subContext.selector = selector.selectorList()->first();
subContext.selector;
subContext.selector = CSSSelectorList::next(*subContext.selector)) {
subContext.treatShadowHostAsNormalScope = true;
subContext.scope = context.scope;
// Use FlatTreeTraversal to traverse a composed ancestor list of a given
// element.
Element* nextElement = &element;
SelectorCheckingContext hostContext(subContext);
do {
MatchResult subResult;
hostContext.element = nextElement;
if (match(hostContext, subResult)) {
matched = true;
// Consider div:host(div:host(div:host(div:host...))).
maxSpecificity =
std::max(maxSpecificity, hostContext.selector->specificity() +
subResult.specificity);
break;
}
hostContext.treatShadowHostAsNormalScope = false;
hostContext.scope = nullptr;
if (selector.getPseudoType() == CSSSelector::PseudoHost)
break;
hostContext.inRightmostCompound = false;
nextElement = FlatTreeTraversal::parentElement(*nextElement);
} while (nextElement);
}
if (matched) {
result.specificity += maxSpecificity;
return true;
}
// FIXME: this was a fallthrough condition.
return false;
}
bool SelectorChecker::checkScrollbarPseudoClass(
const SelectorCheckingContext& context,
MatchResult& result) const {
const CSSSelector& selector = *context.selector;
if (selector.getPseudoType() == CSSSelector::PseudoNot)
return checkPseudoNot(context, result);
// FIXME: This is a temporary hack for resizers and scrollbar corners.
// Eventually :window-inactive should become a real
// pseudo class and just apply to everything.
if (selector.getPseudoType() == CSSSelector::PseudoWindowInactive)
return !context.element->document().page()->focusController().isActive();
if (!m_scrollbar)
return false;
switch (selector.getPseudoType()) {
case CSSSelector::PseudoEnabled:
return m_scrollbar->enabled();
case CSSSelector::PseudoDisabled:
return !m_scrollbar->enabled();
case CSSSelector::PseudoHover: {
ScrollbarPart hoveredPart = m_scrollbar->hoveredPart();
if (m_scrollbarPart == ScrollbarBGPart)
return hoveredPart != NoPart;
if (m_scrollbarPart == TrackBGPart)
return hoveredPart == BackTrackPart ||
hoveredPart == ForwardTrackPart || hoveredPart == ThumbPart;
return m_scrollbarPart == hoveredPart;
}
case CSSSelector::PseudoActive: {
ScrollbarPart pressedPart = m_scrollbar->pressedPart();
if (m_scrollbarPart == ScrollbarBGPart)
return pressedPart != NoPart;
if (m_scrollbarPart == TrackBGPart)
return pressedPart == BackTrackPart ||
pressedPart == ForwardTrackPart || pressedPart == ThumbPart;
return m_scrollbarPart == pressedPart;
}
case CSSSelector::PseudoHorizontal:
return m_scrollbar->orientation() == HorizontalScrollbar;
case CSSSelector::PseudoVertical:
return m_scrollbar->orientation() == VerticalScrollbar;
case CSSSelector::PseudoDecrement:
return m_scrollbarPart == BackButtonStartPart ||
m_scrollbarPart == BackButtonEndPart ||
m_scrollbarPart == BackTrackPart;
case CSSSelector::PseudoIncrement:
return m_scrollbarPart == ForwardButtonStartPart ||
m_scrollbarPart == ForwardButtonEndPart ||
m_scrollbarPart == ForwardTrackPart;
case CSSSelector::PseudoStart:
return m_scrollbarPart == BackButtonStartPart ||
m_scrollbarPart == ForwardButtonStartPart ||
m_scrollbarPart == BackTrackPart;
case CSSSelector::PseudoEnd:
return m_scrollbarPart == BackButtonEndPart ||
m_scrollbarPart == ForwardButtonEndPart ||
m_scrollbarPart == ForwardTrackPart;
case CSSSelector::PseudoDoubleButton: {
WebScrollbarButtonsPlacement buttonsPlacement =
m_scrollbar->theme().buttonsPlacement();
if (m_scrollbarPart == BackButtonStartPart ||
m_scrollbarPart == ForwardButtonStartPart ||
m_scrollbarPart == BackTrackPart)
return buttonsPlacement == WebScrollbarButtonsPlacementDoubleStart ||
buttonsPlacement == WebScrollbarButtonsPlacementDoubleBoth;
if (m_scrollbarPart == BackButtonEndPart ||
m_scrollbarPart == ForwardButtonEndPart ||
m_scrollbarPart == ForwardTrackPart)
return buttonsPlacement == WebScrollbarButtonsPlacementDoubleEnd ||
buttonsPlacement == WebScrollbarButtonsPlacementDoubleBoth;
return false;
}
case CSSSelector::PseudoSingleButton: {
WebScrollbarButtonsPlacement buttonsPlacement =
m_scrollbar->theme().buttonsPlacement();
if (m_scrollbarPart == BackButtonStartPart ||
m_scrollbarPart == ForwardButtonEndPart ||
m_scrollbarPart == BackTrackPart ||
m_scrollbarPart == ForwardTrackPart)
return buttonsPlacement == WebScrollbarButtonsPlacementSingle;
return false;
}
case CSSSelector::PseudoNoButton: {
WebScrollbarButtonsPlacement buttonsPlacement =
m_scrollbar->theme().buttonsPlacement();
if (m_scrollbarPart == BackTrackPart)
return buttonsPlacement == WebScrollbarButtonsPlacementNone ||
buttonsPlacement == WebScrollbarButtonsPlacementDoubleEnd;
if (m_scrollbarPart == ForwardTrackPart)
return buttonsPlacement == WebScrollbarButtonsPlacementNone ||
buttonsPlacement == WebScrollbarButtonsPlacementDoubleStart;
return false;
}
case CSSSelector::PseudoCornerPresent:
return m_scrollbar->getScrollableArea() &&
m_scrollbar->getScrollableArea()->isScrollCornerVisible();
default:
return false;
}
}
bool SelectorChecker::matchesFocusPseudoClass(const Element& element) {
if (InspectorInstrumentation::forcePseudoState(const_cast<Element*>(&element),
CSSSelector::PseudoFocus))
return true;
return element.isFocused() && isFrameFocused(element);
}
} // namespace blink
|