Changeset 106626 in webkit
- Timestamp:
- Feb 2, 2012 9:29:33 PM (12 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r106625 r106626 1 2012-02-02 Roland Steiner <rolandsteiner@chromium.org> 2 3 Simplify SelectorChecker::checkSelector and checkOneSelector 4 https://bugs.webkit.org/show_bug.cgi?id=77697 5 6 Make use of Element::previous/nextElementSibling. 7 Made those methods inline. 8 Simplify code in checkSelector and checkOneSelector, esp. for first/nth/nth-last/last/only-child implementations. 9 10 Reviewed by Andreas Kling. 11 12 No new tests. (refactoring) 13 14 * css/SelectorChecker.cpp: 15 (WebCore::SelectorChecker::checkSelector): 16 (WebCore::SelectorChecker::checkOneSelector): 17 * dom/Element.cpp: 18 * dom/Element.h: 19 (WebCore::Element::previousElementSibling): 20 (WebCore): 21 (WebCore::Element::nextElementSibling): 22 1 23 2012-02-02 Keishi Hattori <keishi@webkit.org> 2 24 -
trunk/Source/WebCore/css/SelectorChecker.cpp
r106515 r106626 501 501 parentStyle->setChildrenAffectedByDirectAdjacentRules(); 502 502 } 503 Node* n = e->previousSibling(); 504 while (n && !n->isElementNode()) 505 n = n->previousSibling(); 506 if (!n) 503 e = e->previousElementSibling(); 504 if (!e) 507 505 return SelectorFailsAllSiblings; 508 e = static_cast<Element*>(n);509 506 return checkSelector(sel, e, dynamicPseudo, false, visitedMatchType); 510 507 } … … 516 513 } 517 514 while (true) { 518 Node* n = e->previousSibling(); 519 while (n && !n->isElementNode()) 520 n = n->previousSibling(); 521 if (!n) 515 e = e->previousElementSibling(); 516 if (!e) 522 517 return SelectorFailsAllSiblings; 523 e = static_cast<Element*>(n);524 518 SelectorMatch match = checkSelector(sel, e, dynamicPseudo, false, visitedMatchType); 525 519 if (match == SelectorMatches || match == SelectorFailsAllSiblings || match == SelectorFailsCompletely) … … 773 767 case CSSSelector::PseudoFirstChild: 774 768 // first-child matches the first child that is an element 775 if (e->parent Node() && e->parentNode()->isElementNode()) {769 if (e->parentElement()) { 776 770 bool result = false; 777 Node* n = e->previousSibling(); 778 while (n && !n->isElementNode()) 779 n = n->previousSibling(); 780 if (!n) 771 if (!e->previousElementSibling()) 781 772 result = true; 782 773 if (!m_isCollectingRulesOnly) { … … 793 784 case CSSSelector::PseudoFirstOfType: 794 785 // first-of-type matches the first element of its type 795 if (e->parent Node() && e->parentNode()->isElementNode()) {796 bool result = false;786 if (e->parentElement()) { 787 bool result = true; 797 788 const QualifiedName& type = e->tagQName(); 798 Node* n = e->previousSibling();799 while (n) {800 if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type))789 for (const Element* sibling = e->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) { 790 if (sibling->hasTagName(type)) { 791 result = false; 801 792 break; 802 n = n->previousSibling(); 803 } 804 if (!n) 805 result = true; 793 } 794 } 806 795 if (!m_isCollectingRulesOnly) { 807 796 RenderStyle* parentStyle = elementStyle ? elementParentStyle : e->parentNode()->renderStyle(); … … 815 804 // last-child matches the last child that is an element 816 805 if (Element* parentElement = e->parentElement()) { 817 bool result = false; 818 if (parentElement->isFinishedParsingChildren()) { 819 Node* n = e->nextSibling(); 820 while (n && !n->isElementNode()) 821 n = n->nextSibling(); 822 if (!n) 823 result = true; 824 } 806 bool result = parentElement->isFinishedParsingChildren() && !e->nextElementSibling(); 825 807 if (!m_isCollectingRulesOnly) { 826 808 RenderStyle* childStyle = elementStyle ? elementStyle : e->renderStyle(); … … 844 826 if (!parentElement->isFinishedParsingChildren()) 845 827 return false; 846 bool result = false;847 828 const QualifiedName& type = e->tagQName(); 848 Node* n = e->nextSibling(); 849 while (n) { 850 if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type)) 851 break; 852 n = n->nextSibling(); 853 } 854 if (!n) 855 result = true; 856 return result; 829 for (const Element* sibling = e->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) { 830 if (sibling->hasTagName(type)) 831 return false; 832 } 833 return true; 857 834 } 858 835 break; 859 836 case CSSSelector::PseudoOnlyChild: 860 837 if (Element* parentElement = e->parentElement()) { 861 bool firstChild = false; 862 bool lastChild = false; 863 864 Node* n = e->previousSibling(); 865 while (n && !n->isElementNode()) 866 n = n->previousSibling(); 867 if (!n) 868 firstChild = true; 869 if (firstChild && parentElement->isFinishedParsingChildren()) { 870 n = e->nextSibling(); 871 while (n && !n->isElementNode()) 872 n = n->nextSibling(); 873 if (!n) 874 lastChild = true; 875 } 838 bool firstChild = !e->previousElementSibling(); 839 bool onlyChild = firstChild && parentElement->isFinishedParsingChildren() && !e->nextElementSibling(); 840 876 841 if (!m_isCollectingRulesOnly) { 877 842 RenderStyle* childStyle = elementStyle ? elementStyle : e->renderStyle(); … … 883 848 if (firstChild && childStyle) 884 849 childStyle->setFirstChildState(); 885 if ( lastChild && childStyle)850 if (onlyChild && childStyle) 886 851 childStyle->setLastChildState(); 887 852 } 888 return firstChild && lastChild;853 return onlyChild; 889 854 } 890 855 break; … … 901 866 if (!parentElement->isFinishedParsingChildren()) 902 867 return false; 903 bool firstChild = false;904 bool lastChild = false;905 868 const QualifiedName& type = e->tagQName(); 906 Node* n = e->previousSibling(); 907 while (n) { 908 if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type)) 909 break; 910 n = n->previousSibling(); 911 } 912 if (!n) 913 firstChild = true; 914 if (firstChild) { 915 n = e->nextSibling(); 916 while (n) { 917 if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type)) 918 break; 919 n = n->nextSibling(); 920 } 921 if (!n) 922 lastChild = true; 923 } 924 return firstChild && lastChild; 869 for (const Element* sibling = e->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) { 870 if (sibling->hasTagName(type)) 871 return false; 872 } 873 for (const Element* sibling = e->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) { 874 if (sibling->hasTagName(type)) 875 return false; 876 } 877 return true; 925 878 } 926 879 break; … … 930 883 if (Element* parentElement = e->parentElement()) { 931 884 int count = 1; 932 Node* n = e->previousSibling(); 933 while (n) { 934 if (n->isElementNode()) { 935 RenderStyle* s = n->renderStyle(); 936 unsigned index = s ? s->childIndex() : 0; 937 if (index) { 938 count += index; 939 break; 940 } 941 count++; 885 for (const Element* sibling = e->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) { 886 RenderStyle* s = sibling->renderStyle(); 887 unsigned index = s ? s->childIndex() : 0; 888 if (index) { 889 count += index; 890 break; 942 891 } 943 n = n->previousSibling();892 count++; 944 893 } 945 894 … … 963 912 int count = 1; 964 913 const QualifiedName& type = e->tagQName(); 965 Node* n = e->previousSibling(); 966 while (n) { 967 if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type)) 968 count++; 969 n = n->previousSibling(); 970 } 971 914 for (const Element* sibling = e->previousElementSibling(); sibling; sibling = sibling->previousElementSibling()) { 915 if (sibling->hasTagName(type)) 916 ++count; 917 } 972 918 if (!m_isCollectingRulesOnly) { 973 919 RenderStyle* parentStyle = elementStyle ? elementParentStyle : parentElement->renderStyle(); … … 992 938 return false; 993 939 int count = 1; 994 Node* n = e->nextSibling(); 995 while (n) { 996 if (n->isElementNode()) 997 count++; 998 n = n->nextSibling(); 999 } 940 for (const Element* sibling = e->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) 941 ++count; 1000 942 if (sel->matchNth(count)) 1001 943 return true; … … 1015 957 int count = 1; 1016 958 const QualifiedName& type = e->tagQName(); 1017 Node* n = e->nextSibling(); 1018 while (n) { 1019 if (n->isElementNode() && static_cast<Element*>(n)->hasTagName(type)) 1020 count++; 1021 n = n->nextSibling(); 959 for (const Element* sibling = e->nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) { 960 if (sibling->hasTagName(type)) 961 ++count; 1022 962 } 1023 963 if (sel->matchNth(count)) -
trunk/Source/WebCore/dom/Element.cpp
r106613 r106626 1741 1741 } 1742 1742 1743 Element* Element::previousElementSibling() const1744 {1745 Node* n = previousSibling();1746 while (n && !n->isElementNode())1747 n = n->previousSibling();1748 return static_cast<Element*>(n);1749 }1750 1751 Element* Element::nextElementSibling() const1752 {1753 Node* n = nextSibling();1754 while (n && !n->isElementNode())1755 n = n->nextSibling();1756 return static_cast<Element*>(n);1757 }1758 1759 1743 unsigned Element::childElementCount() const 1760 1744 { -
trunk/Source/WebCore/dom/Element.h
r106515 r106626 507 507 } 508 508 509 inline Element* Element::previousElementSibling() const 510 { 511 Node* n = previousSibling(); 512 while (n && !n->isElementNode()) 513 n = n->previousSibling(); 514 return static_cast<Element*>(n); 515 } 516 517 inline Element* Element::nextElementSibling() const 518 { 519 Node* n = nextSibling(); 520 while (n && !n->isElementNode()) 521 n = n->nextSibling(); 522 return static_cast<Element*>(n); 523 } 524 509 525 inline NamedNodeMap* Element::ensureUpdatedAttributes() const 510 526 {
Note: See TracChangeset
for help on using the changeset viewer.