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
|
/*
* 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 "config.h"
#include "SelectorChecker.h"
#include "CSSSelector.h"
#include "CSSSelectorList.h"
#include "Document.h"
#include "FocusController.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "HTMLAnchorElement.h"
#include "HTMLDocument.h"
#include "HTMLFrameElementBase.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "HTMLOptGroupElement.h"
#include "HTMLOptionElement.h"
#include "HTMLProgressElement.h"
#include "HTMLStyleElement.h"
#include "InsertionPoint.h"
#include "InspectorInstrumentation.h"
#include "NodeRenderStyle.h"
#include "Page.h"
#include "RenderObject.h"
#include "RenderScrollbar.h"
#include "RenderStyle.h"
#include "ScrollableArea.h"
#include "ScrollbarTheme.h"
#include "ShadowRoot.h"
#include "StyledElement.h"
#include "Text.h"
#if ENABLE(VIDEO_TRACK)
#include "WebVTTElement.h"
#endif
namespace WebCore {
using namespace HTMLNames;
static inline bool isFirstChildElement(const Element* element)
{
return !element->previousElementSibling();
}
static inline bool isLastChildElement(const Element* element)
{
return !element->nextElementSibling();
}
static inline bool isFirstOfType(const Element* element, const QualifiedName& type)
{
for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) {
if (sibling->hasTagName(type))
return false;
}
return true;
}
static inline bool isLastOfType(const Element* element, const QualifiedName& type)
{
for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
if (sibling->hasTagName(type))
return false;
}
return true;
}
static inline int countElementsBefore(const Element* element)
{
int count = 0;
for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) {
unsigned index = sibling->childIndex();
if (index) {
count += index;
break;
}
count++;
}
return count;
}
static inline int countElementsOfTypeBefore(const Element* element, const QualifiedName& type)
{
int count = 0;
for (const Element* sibling = element->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) {
if (sibling->hasTagName(type))
++count;
}
return count;
}
static inline int countElementsAfter(const Element* element)
{
int count = 0;
for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling())
++count;
return count;
}
static inline int countElementsOfTypeAfter(const Element* element, const QualifiedName& type)
{
int count = 0;
for (const Element* sibling = element->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
if (sibling->hasTagName(type))
++count;
}
return count;
}
SelectorChecker::SelectorChecker(Document* document, Mode mode)
: m_strictParsing(!document->inQuirksMode())
, m_documentIsHTML(document->isHTMLDocument())
, m_mode(mode)
{
}
// 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::Match SelectorChecker::match(const SelectorCheckingContext& context, PseudoId& dynamicPseudo) const
{
// first selector has to match
if (!checkOne(context))
return SelectorFailsLocally;
if (context.selector->m_match == CSSSelector::PseudoElement) {
if (context.selector->isCustomPseudoElement()) {
if (ShadowRoot* root = context.element->containingShadowRoot()) {
if (context.element->shadowPseudoId() != context.selector->value())
return SelectorFailsLocally;
if (context.selector->pseudoType() == CSSSelector::PseudoWebKitCustomElement && root->type() != ShadowRoot::UserAgentShadowRoot)
return SelectorFailsLocally;
} else
return SelectorFailsLocally;
} else {
if ((!context.elementStyle && m_mode == ResolvingStyle) || m_mode == QueryingRules)
return SelectorFailsLocally;
PseudoId pseudoId = CSSSelector::pseudoId(context.selector->pseudoType());
if (pseudoId == FIRST_LETTER) {
if (Document* document = context.element->document())
document->styleSheetCollection()->setUsesFirstLetterRules(true);
}
if (pseudoId != NOPSEUDO && m_mode != SharingRules)
dynamicPseudo = pseudoId;
}
}
// The rest of the selectors has to match
CSSSelector::Relation relation = context.selector->relation();
// Prepare next selector
const CSSSelector* historySelector = context.selector->tagHistory();
if (!historySelector) {
if (context.behaviorAtBoundary == CrossesBoundary) {
ASSERT(context.scope);
return context.scope->contains(context.element) ? SelectorMatches : SelectorFailsLocally;
}
return SelectorMatches;
}
SelectorCheckingContext nextContext(context);
nextContext.selector = historySelector;
PseudoId ignoreDynamicPseudo = NOPSEUDO;
if (relation != CSSSelector::SubSelector) {
// Abort if the next selector would exceed the scope.
if (context.element == context.scope && context.behaviorAtBoundary != StaysWithinTreeScope)
return SelectorFailsCompletely;
// Bail-out if this selector is irrelevant for the pseudoId
if (context.pseudoId != NOPSEUDO && context.pseudoId != dynamicPseudo)
return SelectorFailsCompletely;
// 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.pseudoId = NOPSEUDO;
}
switch (relation) {
case CSSSelector::Descendant:
nextContext.element = context.element->parentElement();
nextContext.isSubSelector = false;
nextContext.elementStyle = 0;
for (; nextContext.element; nextContext.element = nextContext.element->parentElement()) {
Match match = this->match(nextContext, ignoreDynamicPseudo);
if (match == SelectorMatches || match == SelectorFailsCompletely)
return match;
if (nextContext.element == nextContext.scope && nextContext.behaviorAtBoundary != StaysWithinTreeScope)
return SelectorFailsCompletely;
}
return SelectorFailsCompletely;
case CSSSelector::Child:
nextContext.element = context.element->parentElement();
if (!nextContext.element)
return SelectorFailsCompletely;
nextContext.isSubSelector = false;
nextContext.elementStyle = 0;
return match(nextContext, ignoreDynamicPseudo);
case CSSSelector::DirectAdjacent:
if (m_mode == ResolvingStyle) {
if (Element* parentElement = context.element->parentElement())
parentElement->setChildrenAffectedByDirectAdjacentRules();
}
nextContext.element = context.element->previousElementSibling();
if (!nextContext.element)
return SelectorFailsAllSiblings;
nextContext.isSubSelector = false;
nextContext.elementStyle = 0;
return match(nextContext, ignoreDynamicPseudo);
case CSSSelector::IndirectAdjacent:
if (m_mode == ResolvingStyle) {
if (Element* parentElement = context.element->parentElement())
parentElement->setChildrenAffectedByForwardPositionalRules();
}
nextContext.element = context.element->previousElementSibling();
nextContext.isSubSelector = false;
nextContext.elementStyle = 0;
for (; nextContext.element; nextContext.element = nextContext.element->previousElementSibling()) {
Match match = this->match(nextContext, ignoreDynamicPseudo);
if (match == SelectorMatches || match == SelectorFailsAllSiblings || match == SelectorFailsCompletely)
return match;
};
return SelectorFailsAllSiblings;
case CSSSelector::SubSelector:
// a selector is invalid if something follows a pseudo-element
// We make an exception for scrollbar pseudo elements and allow a set of pseudo classes (but nothing else)
// to follow the pseudo elements.
nextContext.hasScrollbarPseudo = dynamicPseudo != NOPSEUDO && (context.scrollbar || dynamicPseudo == SCROLLBAR_CORNER || dynamicPseudo == RESIZER);
nextContext.hasSelectionPseudo = dynamicPseudo == SELECTION;
if ((context.elementStyle || m_mode == CollectingRules || m_mode == QueryingRules) && dynamicPseudo != NOPSEUDO
&& !nextContext.hasSelectionPseudo
&& !(nextContext.hasScrollbarPseudo && nextContext.selector->m_match == CSSSelector::PseudoClass))
return SelectorFailsCompletely;
nextContext.isSubSelector = true;
return match(nextContext, dynamicPseudo);
case CSSSelector::ShadowDescendant:
{
// 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->treeScope() == context.element->treeScope() && context.behaviorAtBoundary != StaysWithinTreeScope)
return SelectorFailsCompletely;
Element* shadowHostNode = context.element->shadowHost();
if (!shadowHostNode)
return SelectorFailsCompletely;
nextContext.element = shadowHostNode;
nextContext.isSubSelector = false;
nextContext.elementStyle = 0;
return match(nextContext, ignoreDynamicPseudo);
}
}
ASSERT_NOT_REACHED();
return SelectorFailsCompletely;
}
static bool attributeValueMatches(const Attribute* attributeItem, CSSSelector::Match match, const AtomicString& selectorValue, bool caseSensitive)
{
const AtomicString& value = attributeItem->value();
if (value.isNull())
return false;
switch (match) {
case CSSSelector::Exact:
if (caseSensitive ? selectorValue != value : !equalIgnoringCase(selectorValue, value))
return false;
break;
case CSSSelector::List:
{
// Ignore empty selectors or selectors containing spaces
if (selectorValue.contains(' ') || selectorValue.isEmpty())
return false;
unsigned startSearchAt = 0;
while (true) {
size_t foundPos = value.find(selectorValue, startSearchAt, caseSensitive);
if (foundPos == notFound)
return false;
if (!foundPos || value[foundPos - 1] == ' ') {
unsigned endStr = foundPos + selectorValue.length();
if (endStr == value.length() || value[endStr] == ' ')
break; // We found a match.
}
// No match. Keep looking.
startSearchAt = foundPos + 1;
}
break;
}
case CSSSelector::Contain:
if (!value.contains(selectorValue, caseSensitive) || selectorValue.isEmpty())
return false;
break;
case CSSSelector::Begin:
if (!value.startsWith(selectorValue, caseSensitive) || selectorValue.isEmpty())
return false;
break;
case CSSSelector::End:
if (!value.endsWith(selectorValue, caseSensitive) || selectorValue.isEmpty())
return false;
break;
case CSSSelector::Hyphen:
if (value.length() < selectorValue.length())
return false;
if (!value.startsWith(selectorValue, caseSensitive))
return false;
// It they start the same, check for exact match or following '-':
if (value.length() != selectorValue.length() && value[selectorValue.length()] != '-')
return false;
break;
case CSSSelector::PseudoClass:
case CSSSelector::PseudoElement:
default:
break;
}
return true;
}
static bool anyAttributeMatches(Element* element, const CSSSelector* selector, const QualifiedName& selectorAttr, bool caseSensitive)
{
ASSERT(element->hasAttributesWithoutUpdate());
for (size_t i = 0; i < element->attributeCount(); ++i) {
const Attribute* attributeItem = element->attributeItem(i);
if (!attributeItem->matches(selectorAttr.prefix(), element->isHTMLElement() ? selector->attributeCanonicalLocalName() : selectorAttr.localName(), selectorAttr.namespaceURI()))
continue;
if (attributeValueMatches(attributeItem, static_cast<CSSSelector::Match>(selector->m_match), selector->value(), caseSensitive))
return true;
}
return false;
}
bool SelectorChecker::checkOne(const SelectorCheckingContext& context) const
{
Element* const & element = context.element;
const CSSSelector* const & selector = context.selector;
ASSERT(element);
ASSERT(selector);
if (selector->m_match == CSSSelector::Tag)
return SelectorChecker::tagMatches(element, selector->tagQName());
if (selector->m_match == CSSSelector::Class)
return element->hasClass() && element->classNames().contains(selector->value());
if (selector->m_match == CSSSelector::Id)
return element->hasID() && element->idForStyleResolution() == selector->value();
if (selector->isAttributeSelector()) {
if (!element->hasAttributes())
return false;
const QualifiedName& attr = selector->attribute();
bool caseSensitive = !m_documentIsHTML || HTMLDocument::isCaseSensitiveAttribute(attr);
if (!anyAttributeMatches(element, selector, attr, caseSensitive))
return false;
}
if (selector->m_match == CSSSelector::PseudoClass) {
// Handle :not up front.
if (selector->pseudoType() == CSSSelector::PseudoNot) {
const CSSSelectorList* selectorList = selector->selectorList();
// FIXME: We probably should fix the parser and make it never produce :not rules with missing selector list.
if (!selectorList)
return false;
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
for (subContext.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
ASSERT(subContext.selector->pseudoType() != CSSSelector::PseudoNot);
// We select between :visited and :link when applying. We don't know which one applied (or not) yet.
if (subContext.selector->pseudoType() == CSSSelector::PseudoVisited || (subContext.selector->pseudoType() == CSSSelector::PseudoLink && subContext.visitedMatchType == VisitedMatchEnabled))
return true;
if (!checkOne(subContext))
return true;
}
} else 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, element->document(), selector);
} else if (context.hasSelectionPseudo) {
if (selector->pseudoType() == CSSSelector::PseudoWindowInactive)
return !element->document()->page()->focusController()->isActive();
}
// Normal element pseudo class checking.
switch (selector->pseudoType()) {
// Pseudo classes:
case CSSSelector::PseudoNot:
break; // Already handled up above.
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.elementStyle)
context.elementStyle->setEmptyState(result);
else if (element->renderStyle() && (element->document()->styleSheetCollection()->usesSiblingRules() || element->renderStyle()->unique()))
element->renderStyle()->setEmptyState(result);
}
return result;
}
case CSSSelector::PseudoFirstChild:
// first-child matches the first child that is an element
if (Element* parentElement = element->parentElement()) {
bool result = isFirstChildElement(element);
if (m_mode == ResolvingStyle) {
RenderStyle* childStyle = context.elementStyle ? context.elementStyle : element->renderStyle();
parentElement->setChildrenAffectedByFirstChildRules();
if (result && childStyle)
childStyle->setFirstChildState();
}
return result;
}
break;
case CSSSelector::PseudoFirstOfType:
// first-of-type matches the first element of its type
if (Element* parentElement = element->parentElement()) {
bool result = isFirstOfType(element, element->tagQName());
if (m_mode == ResolvingStyle)
parentElement->setChildrenAffectedByForwardPositionalRules();
return result;
}
break;
case CSSSelector::PseudoLastChild:
// last-child matches the last child that is an element
if (Element* parentElement = element->parentElement()) {
bool result = parentElement->isFinishedParsingChildren() && isLastChildElement(element);
if (m_mode == ResolvingStyle) {
RenderStyle* childStyle = context.elementStyle ? context.elementStyle : element->renderStyle();
parentElement->setChildrenAffectedByLastChildRules();
if (result && childStyle)
childStyle->setLastChildState();
}
return result;
}
break;
case CSSSelector::PseudoLastOfType:
// last-of-type matches the last element of its type
if (Element* parentElement = element->parentElement()) {
if (m_mode == ResolvingStyle)
parentElement->setChildrenAffectedByBackwardPositionalRules();
if (!parentElement->isFinishedParsingChildren())
return false;
return isLastOfType(element, element->tagQName());
}
break;
case CSSSelector::PseudoOnlyChild:
if (Element* parentElement = element->parentElement()) {
bool firstChild = isFirstChildElement(element);
bool onlyChild = firstChild && parentElement->isFinishedParsingChildren() && isLastChildElement(element);
if (m_mode == ResolvingStyle) {
RenderStyle* childStyle = context.elementStyle ? context.elementStyle : element->renderStyle();
parentElement->setChildrenAffectedByFirstChildRules();
parentElement->setChildrenAffectedByLastChildRules();
if (firstChild && childStyle)
childStyle->setFirstChildState();
if (onlyChild && childStyle)
childStyle->setLastChildState();
}
return onlyChild;
}
break;
case CSSSelector::PseudoOnlyOfType:
// FIXME: This selector is very slow.
if (Element* parentElement = element->parentElement()) {
if (m_mode == ResolvingStyle) {
parentElement->setChildrenAffectedByForwardPositionalRules();
parentElement->setChildrenAffectedByBackwardPositionalRules();
}
if (!parentElement->isFinishedParsingChildren())
return false;
return isFirstOfType(element, element->tagQName()) && isLastOfType(element, element->tagQName());
}
break;
case CSSSelector::PseudoNthChild:
if (!selector->parseNth())
break;
if (Element* parentElement = element->parentElement()) {
int count = 1 + countElementsBefore(element);
if (m_mode == ResolvingStyle) {
RenderStyle* childStyle = context.elementStyle ? context.elementStyle : element->renderStyle();
element->setChildIndex(count);
if (childStyle)
childStyle->setUnique();
parentElement->setChildrenAffectedByForwardPositionalRules();
}
if (selector->matchNth(count))
return true;
}
break;
case CSSSelector::PseudoNthOfType:
if (!selector->parseNth())
break;
if (Element* parentElement = element->parentElement()) {
int count = 1 + countElementsOfTypeBefore(element, element->tagQName());
if (m_mode == ResolvingStyle)
parentElement->setChildrenAffectedByForwardPositionalRules();
if (selector->matchNth(count))
return true;
}
break;
case CSSSelector::PseudoNthLastChild:
if (!selector->parseNth())
break;
if (Element* parentElement = element->parentElement()) {
if (m_mode == ResolvingStyle)
parentElement->setChildrenAffectedByBackwardPositionalRules();
if (!parentElement->isFinishedParsingChildren())
return false;
int count = 1 + countElementsAfter(element);
if (selector->matchNth(count))
return true;
}
break;
case CSSSelector::PseudoNthLastOfType:
if (!selector->parseNth())
break;
if (Element* parentElement = element->parentElement()) {
if (m_mode == ResolvingStyle)
parentElement->setChildrenAffectedByBackwardPositionalRules();
if (!parentElement->isFinishedParsingChildren())
return false;
int count = 1 + countElementsOfTypeAfter(element, element->tagQName());
if (selector->matchNth(count))
return true;
}
break;
case CSSSelector::PseudoTarget:
if (element == element->document()->cssTarget())
return true;
break;
case CSSSelector::PseudoAny:
{
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
PseudoId ignoreDynamicPseudo = NOPSEUDO;
for (subContext.selector = selector->selectorList()->first(); subContext.selector; subContext.selector = CSSSelectorList::next(subContext.selector)) {
if (match(subContext, ignoreDynamicPseudo) == SelectorMatches)
return true;
}
}
break;
case CSSSelector::PseudoAutofill:
if (!element->isFormControlElement())
break;
if (HTMLInputElement* inputElement = element->toInputElement())
return inputElement->isAutofilled();
break;
case CSSSelector::PseudoAnyLink:
case CSSSelector::PseudoLink:
// :visited and :link matches are separated later when applying the style. Here both classes match all links...
return element->isLink();
case CSSSelector::PseudoVisited:
// ...except if :visited matching is disabled for ancestor/sibling matching.
return element->isLink() && context.visitedMatchType == VisitedMatchEnabled;
case CSSSelector::PseudoDrag:
if (m_mode == ResolvingStyle) {
if (context.elementStyle)
context.elementStyle->setAffectedByDrag();
else
element->setChildrenAffectedByDrag(true);
}
if (element->renderer() && element->renderer()->isDragging())
return true;
break;
case CSSSelector::PseudoFocus:
return matchesFocusPseudoClass(element);
case CSSSelector::PseudoHover:
// If we're in quirks mode, then hover should never match anchors with no
// href and *:hover should not match anything. This is important for sites like wsj.com.
if (m_strictParsing || context.isSubSelector || (selector->m_match == CSSSelector::Tag && selector->tagQName() != anyQName() && !isHTMLAnchorElement(element)) || element->isLink()) {
if (m_mode == ResolvingStyle) {
if (context.elementStyle)
context.elementStyle->setAffectedByHover();
else
element->setChildrenAffectedByHover(true);
}
if (element->hovered() || InspectorInstrumentation::forcePseudoState(element, CSSSelector::PseudoHover))
return true;
}
break;
case CSSSelector::PseudoActive:
// If we're in quirks mode, then :active should never match anchors with no
// href and *:active should not match anything.
if (m_strictParsing || context.isSubSelector || (selector->m_match == CSSSelector::Tag && selector->tagQName() != anyQName() && !isHTMLAnchorElement(element)) || element->isLink()) {
if (m_mode == ResolvingStyle) {
if (context.elementStyle)
context.elementStyle->setAffectedByActive();
else
element->setChildrenAffectedByActive(true);
}
if (element->active() || InspectorInstrumentation::forcePseudoState(element, CSSSelector::PseudoActive))
return true;
}
break;
case CSSSelector::PseudoEnabled:
if (element->isFormControlElement() || isHTMLOptionElement(element) || isHTMLOptGroupElement(element))
return !element->isDisabledFormControl();
break;
case CSSSelector::PseudoFullPageMedia:
return element->document() && element->document()->isMediaDocument();
break;
case CSSSelector::PseudoDefault:
return element->isDefaultButtonForForm();
case CSSSelector::PseudoDisabled:
if (element->isFormControlElement() || isHTMLOptionElement(element) || isHTMLOptGroupElement(element))
return element->isDisabledFormControl();
break;
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:
element->document()->setContainsValidityStyleRules();
return element->willValidate() && element->isValidFormControlElement();
case CSSSelector::PseudoInvalid:
element->document()->setContainsValidityStyleRules();
return element->willValidate() && !element->isValidFormControlElement();
case CSSSelector::PseudoChecked:
{
// 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.
HTMLInputElement* inputElement = element->toInputElement();
if (inputElement && inputElement->shouldAppearChecked() && !inputElement->shouldAppearIndeterminate())
return true;
if (isHTMLOptionElement(element) && toHTMLOptionElement(element)->selected())
return true;
break;
}
case CSSSelector::PseudoIndeterminate:
return element->shouldAppearIndeterminate();
case CSSSelector::PseudoRoot:
if (element == element->document()->documentElement())
return true;
break;
case CSSSelector::PseudoLang:
{
AtomicString value;
#if ENABLE(VIDEO_TRACK)
if (element->isWebVTTElement())
value = toWebVTTElement(element)->language();
else
#endif
value = element->computeInheritedLanguage();
const AtomicString& argument = selector->argument();
if (value.isEmpty() || !value.startsWith(argument, false))
break;
if (value.length() != argument.length() && value[argument.length()] != '-')
break;
return true;
}
#if ENABLE(FULLSCREEN_API)
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 (element->isFrameElementBase() && element->containsFullScreenElement())
return true;
if (!element->document()->webkitIsFullScreen())
return false;
return element == element->document()->webkitCurrentFullScreenElement();
case CSSSelector::PseudoAnimatingFullScreenTransition:
if (element != element->document()->webkitCurrentFullScreenElement())
return false;
return element->document()->isAnimatingFullScreen();
case CSSSelector::PseudoFullScreenAncestor:
return element->containsFullScreenElement();
case CSSSelector::PseudoFullScreenDocument:
// While a Document is in the fullscreen state, the 'full-screen-document' pseudoclass applies
// to all elements of that Document.
if (!element->document()->webkitIsFullScreen())
return false;
return true;
#endif
#if ENABLE(IFRAME_SEAMLESS)
case CSSSelector::PseudoSeamlessDocument:
// While a document is rendered in a seamless iframe, the 'seamless-document' pseudoclass applies
// to all elements of that Document.
return element->document()->shouldDisplaySeamlesslyWithParent();
#endif
case CSSSelector::PseudoInRange:
element->document()->setContainsValidityStyleRules();
return element->isInRange();
case CSSSelector::PseudoOutOfRange:
element->document()->setContainsValidityStyleRules();
return element->isOutOfRange();
#if ENABLE(VIDEO_TRACK)
case CSSSelector::PseudoFutureCue:
return (element->isWebVTTElement() && !toWebVTTElement(element)->isPastNode());
case CSSSelector::PseudoPastCue:
return (element->isWebVTTElement() && toWebVTTElement(element)->isPastNode());
#endif
case CSSSelector::PseudoScope:
{
const Node* contextualReferenceNode = !context.scope || context.behaviorAtBoundary == CrossesBoundary ? element->document()->documentElement() : context.scope;
if (element == contextualReferenceNode)
return true;
break;
}
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:
case CSSSelector::PseudoNotParsed:
default:
ASSERT_NOT_REACHED();
break;
}
return false;
}
#if ENABLE(VIDEO_TRACK)
else if (selector->m_match == CSSSelector::PseudoElement && selector->pseudoType() == CSSSelector::PseudoCue) {
SelectorCheckingContext subContext(context);
subContext.isSubSelector = true;
PseudoId ignoreDynamicPseudo = NOPSEUDO;
const CSSSelector* const & selector = context.selector;
for (subContext.selector = selector->selectorList()->first(); subContext.selector; subContext.selector = CSSSelectorList::next(subContext.selector)) {
if (match(subContext, ignoreDynamicPseudo) == SelectorMatches)
return true;
}
return false;
}
#endif
// ### add the rest of the checks...
return true;
}
bool SelectorChecker::checkScrollbarPseudoClass(const SelectorCheckingContext& context, Document* document, const CSSSelector* selector) const
{
RenderScrollbar* scrollbar = context.scrollbar;
ScrollbarPart part = context.scrollbarPart;
// 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->pseudoType() == CSSSelector::PseudoWindowInactive)
return !document->page()->focusController()->isActive();
if (!scrollbar)
return false;
ASSERT(selector->m_match == CSSSelector::PseudoClass);
switch (selector->pseudoType()) {
case CSSSelector::PseudoEnabled:
return scrollbar->enabled();
case CSSSelector::PseudoDisabled:
return !scrollbar->enabled();
case CSSSelector::PseudoHover:
{
ScrollbarPart hoveredPart = scrollbar->hoveredPart();
if (part == ScrollbarBGPart)
return hoveredPart != NoPart;
if (part == TrackBGPart)
return hoveredPart == BackTrackPart || hoveredPart == ForwardTrackPart || hoveredPart == ThumbPart;
return part == hoveredPart;
}
case CSSSelector::PseudoActive:
{
ScrollbarPart pressedPart = scrollbar->pressedPart();
if (part == ScrollbarBGPart)
return pressedPart != NoPart;
if (part == TrackBGPart)
return pressedPart == BackTrackPart || pressedPart == ForwardTrackPart || pressedPart == ThumbPart;
return part == pressedPart;
}
case CSSSelector::PseudoHorizontal:
return scrollbar->orientation() == HorizontalScrollbar;
case CSSSelector::PseudoVertical:
return scrollbar->orientation() == VerticalScrollbar;
case CSSSelector::PseudoDecrement:
return part == BackButtonStartPart || part == BackButtonEndPart || part == BackTrackPart;
case CSSSelector::PseudoIncrement:
return part == ForwardButtonStartPart || part == ForwardButtonEndPart || part == ForwardTrackPart;
case CSSSelector::PseudoStart:
return part == BackButtonStartPart || part == ForwardButtonStartPart || part == BackTrackPart;
case CSSSelector::PseudoEnd:
return part == BackButtonEndPart || part == ForwardButtonEndPart || part == ForwardTrackPart;
case CSSSelector::PseudoDoubleButton:
{
ScrollbarButtonsPlacement buttonsPlacement = scrollbar->theme()->buttonsPlacement();
if (part == BackButtonStartPart || part == ForwardButtonStartPart || part == BackTrackPart)
return buttonsPlacement == ScrollbarButtonsDoubleStart || buttonsPlacement == ScrollbarButtonsDoubleBoth;
if (part == BackButtonEndPart || part == ForwardButtonEndPart || part == ForwardTrackPart)
return buttonsPlacement == ScrollbarButtonsDoubleEnd || buttonsPlacement == ScrollbarButtonsDoubleBoth;
return false;
}
case CSSSelector::PseudoSingleButton:
{
ScrollbarButtonsPlacement buttonsPlacement = scrollbar->theme()->buttonsPlacement();
if (part == BackButtonStartPart || part == ForwardButtonEndPart || part == BackTrackPart || part == ForwardTrackPart)
return buttonsPlacement == ScrollbarButtonsSingle;
return false;
}
case CSSSelector::PseudoNoButton:
{
ScrollbarButtonsPlacement buttonsPlacement = scrollbar->theme()->buttonsPlacement();
if (part == BackTrackPart)
return buttonsPlacement == ScrollbarButtonsNone || buttonsPlacement == ScrollbarButtonsDoubleEnd;
if (part == ForwardTrackPart)
return buttonsPlacement == ScrollbarButtonsNone || buttonsPlacement == ScrollbarButtonsDoubleStart;
return false;
}
case CSSSelector::PseudoCornerPresent:
return scrollbar->scrollableArea() ? scrollbar->scrollableArea()->isScrollCornerVisible() : false;
default:
return false;
}
}
unsigned SelectorChecker::determineLinkMatchType(const CSSSelector* selector)
{
unsigned linkMatchType = MatchAll;
// Statically determine if this selector will match a link in visited, unvisited or any state, or never.
// :visited never matches other elements than the innermost link element.
for (; selector; selector = selector->tagHistory()) {
switch (selector->pseudoType()) {
case CSSSelector::PseudoNot:
{
// :not(:visited) is equivalent to :link. Parser enforces that :not can't nest.
const CSSSelectorList* selectorList = selector->selectorList();
if (!selectorList)
break;
for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = subSelector->tagHistory()) {
CSSSelector::PseudoType subType = subSelector->pseudoType();
if (subType == CSSSelector::PseudoVisited)
linkMatchType &= ~SelectorChecker::MatchVisited;
else if (subType == CSSSelector::PseudoLink)
linkMatchType &= ~SelectorChecker::MatchLink;
}
}
break;
case CSSSelector::PseudoLink:
linkMatchType &= ~SelectorChecker::MatchVisited;
break;
case CSSSelector::PseudoVisited:
linkMatchType &= ~SelectorChecker::MatchLink;
break;
default:
// We don't support :link and :visited inside :-webkit-any.
break;
}
CSSSelector::Relation relation = selector->relation();
if (relation == CSSSelector::SubSelector)
continue;
if (relation != CSSSelector::Descendant && relation != CSSSelector::Child)
return linkMatchType;
if (linkMatchType != MatchAll)
return linkMatchType;
}
return linkMatchType;
}
bool SelectorChecker::isFrameFocused(const Element* element)
{
return element->document()->frame() && element->document()->frame()->selection()->isFocusedAndActive();
}
bool SelectorChecker::matchesFocusPseudoClass(const Element* element)
{
if (InspectorInstrumentation::forcePseudoState(const_cast<Element*>(element), CSSSelector::PseudoFocus))
return true;
return element->focused() && isFrameFocused(element);
}
}
|