WebCore/css/CSSSelector.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
       
     3  *               1999 Waldo Bastian (bastian@kde.org)
       
     4  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Library General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Library General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Library General Public License
       
    17  * along with this library; see the file COPYING.LIB.  If not, write to
       
    18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    19  * Boston, MA 02110-1301, USA.
       
    20  */
       
    21 
       
    22 #ifndef CSSSelector_h
       
    23 #define CSSSelector_h
       
    24 
       
    25 #include "QualifiedName.h"
       
    26 #include "RenderStyleConstants.h"
       
    27 #include <wtf/Noncopyable.h>
       
    28 #include <wtf/OwnPtr.h>
       
    29 #include <wtf/PassOwnPtr.h>
       
    30 
       
    31 namespace WebCore {
       
    32 
       
    33     // this class represents a selector for a StyleRule
       
    34     class CSSSelector : public Noncopyable {
       
    35     public:
       
    36         CSSSelector()
       
    37             : m_relation(Descendant)
       
    38             , m_match(None)
       
    39             , m_pseudoType(PseudoNotParsed)
       
    40             , m_parsedNth(false)
       
    41             , m_isLastInSelectorList(false)
       
    42             , m_hasRareData(false)
       
    43             , m_isForPage(false)
       
    44             , m_tag(anyQName())
       
    45         {
       
    46         }
       
    47 
       
    48         CSSSelector(const QualifiedName& qName)
       
    49             : m_relation(Descendant)
       
    50             , m_match(None)
       
    51             , m_pseudoType(PseudoNotParsed)
       
    52             , m_parsedNth(false)
       
    53             , m_isLastInSelectorList(false)
       
    54             , m_hasRareData(false)
       
    55             , m_isForPage(false)
       
    56             , m_tag(qName)
       
    57         {
       
    58         }
       
    59 
       
    60         ~CSSSelector();
       
    61 
       
    62         /**
       
    63          * Re-create selector text from selector's data
       
    64          */
       
    65         String selectorText() const;
       
    66 
       
    67         // checks if the 2 selectors (including sub selectors) agree.
       
    68         bool operator==(const CSSSelector&);
       
    69 
       
    70         // tag == -1 means apply to all elements (Selector = *)
       
    71 
       
    72         unsigned specificity();
       
    73 
       
    74         /* how the attribute value has to match.... Default is Exact */
       
    75         enum Match {
       
    76             None = 0,
       
    77             Id,
       
    78             Class,
       
    79             Exact,
       
    80             Set,
       
    81             List,
       
    82             Hyphen,
       
    83             PseudoClass,
       
    84             PseudoElement,
       
    85             Contain, // css3: E[foo*="bar"]
       
    86             Begin, // css3: E[foo^="bar"]
       
    87             End, // css3: E[foo$="bar"]
       
    88             PagePseudoClass
       
    89         };
       
    90 
       
    91         enum Relation {
       
    92             Descendant = 0,
       
    93             Child,
       
    94             DirectAdjacent,
       
    95             IndirectAdjacent,
       
    96             SubSelector
       
    97         };
       
    98 
       
    99         enum PseudoType {
       
   100             PseudoNotParsed = 0,
       
   101             PseudoUnknown,
       
   102             PseudoEmpty,
       
   103             PseudoFirstChild,
       
   104             PseudoFirstOfType,
       
   105             PseudoLastChild,
       
   106             PseudoLastOfType,
       
   107             PseudoOnlyChild,
       
   108             PseudoOnlyOfType,
       
   109             PseudoFirstLine,
       
   110             PseudoFirstLetter,
       
   111             PseudoNthChild,
       
   112             PseudoNthOfType,
       
   113             PseudoNthLastChild,
       
   114             PseudoNthLastOfType,
       
   115             PseudoLink,
       
   116             PseudoVisited,
       
   117             PseudoAnyLink,
       
   118             PseudoAutofill,
       
   119             PseudoHover,
       
   120             PseudoDrag,
       
   121             PseudoFocus,
       
   122             PseudoActive,
       
   123             PseudoChecked,
       
   124             PseudoEnabled,
       
   125             PseudoFullPageMedia,
       
   126             PseudoDefault,
       
   127             PseudoDisabled,
       
   128             PseudoInputPlaceholder,
       
   129             PseudoOptional,
       
   130             PseudoRequired,
       
   131             PseudoReadOnly,
       
   132             PseudoReadWrite,
       
   133             PseudoValid,
       
   134             PseudoInvalid,
       
   135             PseudoIndeterminate,
       
   136             PseudoTarget,
       
   137             PseudoBefore,
       
   138             PseudoAfter,
       
   139             PseudoLang,
       
   140             PseudoNot,
       
   141             PseudoResizer,
       
   142             PseudoRoot,
       
   143             PseudoScrollbar,
       
   144             PseudoScrollbarBack,
       
   145             PseudoScrollbarButton,
       
   146             PseudoScrollbarCorner,
       
   147             PseudoScrollbarForward,
       
   148             PseudoScrollbarThumb,
       
   149             PseudoScrollbarTrack,
       
   150             PseudoScrollbarTrackPiece,
       
   151             PseudoWindowInactive,
       
   152             PseudoCornerPresent,
       
   153             PseudoDecrement,
       
   154             PseudoIncrement,
       
   155             PseudoHorizontal,
       
   156             PseudoVertical,
       
   157             PseudoStart,
       
   158             PseudoEnd,
       
   159             PseudoDoubleButton,
       
   160             PseudoSingleButton,
       
   161             PseudoNoButton,
       
   162             PseudoSelection,
       
   163             PseudoFileUploadButton,
       
   164             PseudoSliderThumb,
       
   165             PseudoSearchCancelButton,
       
   166             PseudoSearchDecoration,
       
   167             PseudoSearchResultsDecoration,
       
   168             PseudoSearchResultsButton,
       
   169             PseudoMediaControlsPanel,
       
   170             PseudoMediaControlsMuteButton,
       
   171             PseudoMediaControlsPlayButton,
       
   172             PseudoMediaControlsTimelineContainer,
       
   173             PseudoMediaControlsVolumeSliderContainer,
       
   174             PseudoMediaControlsVolumeSliderMuteButton,
       
   175             PseudoMediaControlsCurrentTimeDisplay,
       
   176             PseudoMediaControlsTimeRemainingDisplay,
       
   177             PseudoMediaControlsToggleClosedCaptions,
       
   178             PseudoMediaControlsTimeline,
       
   179             PseudoMediaControlsVolumeSlider,
       
   180             PseudoMediaControlsSeekBackButton,
       
   181             PseudoMediaControlsSeekForwardButton,
       
   182             PseudoMediaControlsRewindButton,
       
   183             PseudoMediaControlsReturnToRealtimeButton,
       
   184             PseudoMediaControlsStatusDisplay,
       
   185             PseudoMediaControlsFullscreenButton,
       
   186             PseudoMeterHorizontalBar,
       
   187             PseudoMeterVerticalBar,
       
   188             PseudoMeterHorizontalOptimum,
       
   189             PseudoMeterHorizontalSuboptimal,
       
   190             PseudoMeterHorizontalEvenLessGood,
       
   191             PseudoMeterVerticalOptimum,
       
   192             PseudoMeterVerticalSuboptimal,
       
   193             PseudoMeterVerticalEvenLessGood,
       
   194             PseudoInputListButton,
       
   195 #if ENABLE(INPUT_SPEECH)
       
   196             PseudoInputSpeechButton,
       
   197 #endif
       
   198             PseudoInnerSpinButton,
       
   199             PseudoOuterSpinButton,
       
   200             PseudoProgressBarValue,
       
   201             PseudoLeftPage,
       
   202             PseudoRightPage,
       
   203             PseudoFirstPage,
       
   204         };
       
   205 
       
   206         enum MarginBoxType {
       
   207             TopLeftCornerMarginBox,
       
   208             TopLeftMarginBox,
       
   209             TopCenterMarginBox,
       
   210             TopRightMarginBox,
       
   211             TopRightCornerMarginBox,
       
   212             BottomLeftCornerMarginBox,
       
   213             BottomLeftMarginBox,
       
   214             BottomCenterMarginBox,
       
   215             BottomRightMarginBox,
       
   216             BottomRightCornerMarginBox,
       
   217             LeftTopMarginBox,
       
   218             LeftMiddleMarginBox,
       
   219             LeftBottomMarginBox,
       
   220             RightTopMarginBox,
       
   221             RightMiddleMarginBox,
       
   222             RightBottomMarginBox,
       
   223         };
       
   224 
       
   225         PseudoType pseudoType() const
       
   226         {
       
   227             if (m_pseudoType == PseudoNotParsed)
       
   228                 extractPseudoType();
       
   229             return static_cast<PseudoType>(m_pseudoType);
       
   230         }
       
   231 
       
   232         static PseudoType parsePseudoType(const AtomicString&);
       
   233         static PseudoId pseudoId(PseudoType);
       
   234 
       
   235         CSSSelector* tagHistory() const { return m_hasRareData ? m_data.m_rareData->m_tagHistory.get() : m_data.m_tagHistory; }
       
   236         void setTagHistory(CSSSelector* tagHistory);
       
   237 
       
   238         bool hasTag() const { return m_tag != anyQName(); }
       
   239         bool hasAttribute() const { return m_match == Id || m_match == Class || (m_hasRareData && m_data.m_rareData->m_attribute != anyQName()); }
       
   240         
       
   241         const QualifiedName& attribute() const;
       
   242         const AtomicString& argument() const { return m_hasRareData ? m_data.m_rareData->m_argument : nullAtom; }
       
   243         CSSSelector* simpleSelector() const { return m_hasRareData ? m_data.m_rareData->m_simpleSelector.get() : 0; }
       
   244         
       
   245         void setAttribute(const QualifiedName& value);
       
   246         void setArgument(const AtomicString& value);
       
   247         void setSimpleSelector(CSSSelector* value);
       
   248         
       
   249         bool parseNth();
       
   250         bool matchNth(int count);
       
   251 
       
   252         bool matchesPseudoElement() const 
       
   253         { 
       
   254             if (m_pseudoType == PseudoUnknown)
       
   255                 extractPseudoType();
       
   256             return m_match == PseudoElement;
       
   257         }
       
   258 
       
   259         Relation relation() const { return static_cast<Relation>(m_relation); }
       
   260 
       
   261         bool isLastInSelectorList() const { return m_isLastInSelectorList; }
       
   262         void setLastInSelectorList() { m_isLastInSelectorList = true; }
       
   263         bool isSimple() const;
       
   264 
       
   265         bool isForPage() const { return m_isForPage; }
       
   266         void setForPage() { m_isForPage = true; }
       
   267 
       
   268         unsigned m_relation           : 3; // enum Relation
       
   269         mutable unsigned m_match      : 4; // enum Match
       
   270         mutable unsigned m_pseudoType : 8; // PseudoType
       
   271         
       
   272     private:
       
   273         bool m_parsedNth              : 1; // Used for :nth-* 
       
   274         bool m_isLastInSelectorList   : 1;
       
   275         bool m_hasRareData            : 1;
       
   276         bool m_isForPage              : 1;
       
   277 
       
   278         unsigned specificityForPage();
       
   279         void extractPseudoType() const;
       
   280 
       
   281         struct RareData : Noncopyable {
       
   282             RareData(PassOwnPtr<CSSSelector> tagHistory)
       
   283                 : m_a(0)
       
   284                 , m_b(0)
       
   285                 , m_tagHistory(tagHistory)
       
   286                 , m_attribute(anyQName())
       
   287                 , m_argument(nullAtom)
       
   288             {
       
   289             }
       
   290 
       
   291             bool parseNth();
       
   292             bool matchNth(int count);
       
   293 
       
   294             int m_a; // Used for :nth-*
       
   295             int m_b; // Used for :nth-*
       
   296             OwnPtr<CSSSelector> m_tagHistory;
       
   297             OwnPtr<CSSSelector> m_simpleSelector; // Used for :not.
       
   298             QualifiedName m_attribute; // used for attribute selector
       
   299             AtomicString m_argument; // Used for :contains, :lang and :nth-*
       
   300         };
       
   301 
       
   302         void createRareData()
       
   303         {
       
   304             if (m_hasRareData) 
       
   305                 return;
       
   306             m_data.m_rareData = new RareData(adoptPtr(m_data.m_tagHistory));
       
   307             m_hasRareData = true;
       
   308         }
       
   309         
       
   310         union DataUnion {
       
   311             DataUnion() : m_tagHistory(0) { }
       
   312             CSSSelector* m_tagHistory;
       
   313             RareData* m_rareData;
       
   314         } m_data;
       
   315         
       
   316     public:
       
   317         mutable AtomicString m_value;
       
   318         QualifiedName m_tag;
       
   319     };
       
   320 
       
   321 } // namespace WebCore
       
   322 
       
   323 #endif // CSSSelector_h