Changeset 106626 in webkit


Ignore:
Timestamp:
Feb 2, 2012 9:29:33 PM (12 years ago)
Author:
rolandsteiner@chromium.org
Message:

Simplify SelectorChecker::checkSelector and checkOneSelector
https://bugs.webkit.org/show_bug.cgi?id=77697

Make use of Element::previous/nextElementSibling.
Made those methods inline.
Simplify code in checkSelector and checkOneSelector, esp. for first/nth/nth-last/last/only-child implementations.

Reviewed by Andreas Kling.

No new tests. (refactoring)

  • css/SelectorChecker.cpp:

(WebCore::SelectorChecker::checkSelector):
(WebCore::SelectorChecker::checkOneSelector):

  • dom/Element.cpp:
  • dom/Element.h:

(WebCore::Element::previousElementSibling):
(WebCore):
(WebCore::Element::nextElementSibling):

Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r106625 r106626  
     12012-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
    1232012-02-02  Keishi Hattori  <keishi@webkit.org>
    224
  • trunk/Source/WebCore/css/SelectorChecker.cpp

    r106515 r106626  
    501501                    parentStyle->setChildrenAffectedByDirectAdjacentRules();
    502502            }
    503             Node* n = e->previousSibling();
    504             while (n && !n->isElementNode())
    505                 n = n->previousSibling();
    506             if (!n)
     503            e = e->previousElementSibling();
     504            if (!e)
    507505                return SelectorFailsAllSiblings;
    508             e = static_cast<Element*>(n);
    509506            return checkSelector(sel, e, dynamicPseudo, false, visitedMatchType);
    510507        }
     
    516513        }
    517514        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)
    522517                return SelectorFailsAllSiblings;
    523             e = static_cast<Element*>(n);
    524518            SelectorMatch match = checkSelector(sel, e, dynamicPseudo, false, visitedMatchType);
    525519            if (match == SelectorMatches || match == SelectorFailsAllSiblings || match == SelectorFailsCompletely)
     
    773767        case CSSSelector::PseudoFirstChild:
    774768            // first-child matches the first child that is an element
    775             if (e->parentNode() && e->parentNode()->isElementNode()) {
     769            if (e->parentElement()) {
    776770                bool result = false;
    777                 Node* n = e->previousSibling();
    778                 while (n && !n->isElementNode())
    779                     n = n->previousSibling();
    780                 if (!n)
     771                if (!e->previousElementSibling())
    781772                    result = true;
    782773                if (!m_isCollectingRulesOnly) {
     
    793784        case CSSSelector::PseudoFirstOfType:
    794785            // first-of-type matches the first element of its type
    795             if (e->parentNode() && e->parentNode()->isElementNode()) {
    796                 bool result = false;
     786            if (e->parentElement()) {
     787                bool result = true;
    797788                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;
    801792                        break;
    802                     n = n->previousSibling();
    803                 }
    804                 if (!n)
    805                     result = true;
     793                    }
     794                }
    806795                if (!m_isCollectingRulesOnly) {
    807796                    RenderStyle* parentStyle = elementStyle ? elementParentStyle : e->parentNode()->renderStyle();
     
    815804            // last-child matches the last child that is an element
    816805            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();
    825807                if (!m_isCollectingRulesOnly) {
    826808                    RenderStyle* childStyle = elementStyle ? elementStyle : e->renderStyle();
     
    844826                if (!parentElement->isFinishedParsingChildren())
    845827                    return false;
    846                 bool result = false;
    847828                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;
    857834            }
    858835            break;
    859836        case CSSSelector::PseudoOnlyChild:
    860837            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
    876841                if (!m_isCollectingRulesOnly) {
    877842                    RenderStyle* childStyle = elementStyle ? elementStyle : e->renderStyle();
     
    883848                    if (firstChild && childStyle)
    884849                        childStyle->setFirstChildState();
    885                     if (lastChild && childStyle)
     850                    if (onlyChild && childStyle)
    886851                        childStyle->setLastChildState();
    887852                }
    888                 return firstChild && lastChild;
     853                return onlyChild;
    889854            }
    890855            break;
     
    901866                if (!parentElement->isFinishedParsingChildren())
    902867                    return false;
    903                 bool firstChild = false;
    904                 bool lastChild = false;
    905868                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;
    925878            }
    926879            break;
     
    930883            if (Element* parentElement = e->parentElement()) {
    931884                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;
    942891                    }
    943                     n = n->previousSibling();
     892                    count++;
    944893                }
    945894
     
    963912                int count = 1;
    964913                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                }
    972918                if (!m_isCollectingRulesOnly) {
    973919                    RenderStyle* parentStyle = elementStyle ? elementParentStyle : parentElement->renderStyle();
     
    992938                    return false;
    993939                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;
    1000942                if (sel->matchNth(count))
    1001943                    return true;
     
    1015957                int count = 1;
    1016958                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;
    1022962                }
    1023963                if (sel->matchNth(count))
  • trunk/Source/WebCore/dom/Element.cpp

    r106613 r106626  
    17411741}
    17421742
    1743 Element* Element::previousElementSibling() const
    1744 {
    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() const
    1752 {
    1753     Node* n = nextSibling();
    1754     while (n && !n->isElementNode())
    1755         n = n->nextSibling();
    1756     return static_cast<Element*>(n);
    1757 }
    1758 
    17591743unsigned Element::childElementCount() const
    17601744{
  • trunk/Source/WebCore/dom/Element.h

    r106515 r106626  
    507507}
    508508
     509inline 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
     517inline 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
    509525inline NamedNodeMap* Element::ensureUpdatedAttributes() const
    510526{
Note: See TracChangeset for help on using the changeset viewer.