layout/style/StyleRule.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * representation of CSS style rules (selectors+declaration) and CSS
michael@0 8 * selectors
michael@0 9 */
michael@0 10
michael@0 11 #ifndef mozilla_css_StyleRule_h__
michael@0 12 #define mozilla_css_StyleRule_h__
michael@0 13
michael@0 14 #include "mozilla/Attributes.h"
michael@0 15 #include "mozilla/MemoryReporting.h"
michael@0 16 #include "mozilla/css/Rule.h"
michael@0 17
michael@0 18 #include "nsString.h"
michael@0 19 #include "nsCOMPtr.h"
michael@0 20 #include "nsCSSPseudoElements.h"
michael@0 21 #include "nsCSSPseudoClasses.h"
michael@0 22
michael@0 23 class nsIAtom;
michael@0 24 class nsCSSStyleSheet;
michael@0 25 struct nsCSSSelectorList;
michael@0 26 class nsCSSCompressedDataBlock;
michael@0 27
michael@0 28 struct nsAtomList {
michael@0 29 public:
michael@0 30 nsAtomList(nsIAtom* aAtom);
michael@0 31 nsAtomList(const nsString& aAtomValue);
michael@0 32 ~nsAtomList(void);
michael@0 33
michael@0 34 /** Do a deep clone. Should be used only on the first in the linked list. */
michael@0 35 nsAtomList* Clone() const { return Clone(true); }
michael@0 36
michael@0 37 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 38
michael@0 39 nsCOMPtr<nsIAtom> mAtom;
michael@0 40 nsAtomList* mNext;
michael@0 41 private:
michael@0 42 nsAtomList* Clone(bool aDeep) const;
michael@0 43
michael@0 44 nsAtomList(const nsAtomList& aCopy) MOZ_DELETE;
michael@0 45 nsAtomList& operator=(const nsAtomList& aCopy) MOZ_DELETE;
michael@0 46 };
michael@0 47
michael@0 48 struct nsPseudoClassList {
michael@0 49 public:
michael@0 50 nsPseudoClassList(nsCSSPseudoClasses::Type aType);
michael@0 51 nsPseudoClassList(nsCSSPseudoClasses::Type aType, const char16_t *aString);
michael@0 52 nsPseudoClassList(nsCSSPseudoClasses::Type aType, const int32_t *aIntPair);
michael@0 53 nsPseudoClassList(nsCSSPseudoClasses::Type aType,
michael@0 54 nsCSSSelectorList *aSelectorList /* takes ownership */);
michael@0 55 ~nsPseudoClassList(void);
michael@0 56
michael@0 57 /** Do a deep clone. Should be used only on the first in the linked list. */
michael@0 58 nsPseudoClassList* Clone() const { return Clone(true); }
michael@0 59
michael@0 60 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 61
michael@0 62 union {
michael@0 63 // For a given value of mType, we have either:
michael@0 64 // a. no value, which means mMemory is always null
michael@0 65 // (if none of the conditions for (b), (c), or (d) is true)
michael@0 66 // b. a string value, which means mString/mMemory is non-null
michael@0 67 // (if nsCSSPseudoClasses::HasStringArg(mType))
michael@0 68 // c. an integer pair value, which means mNumbers/mMemory is non-null
michael@0 69 // (if nsCSSPseudoClasses::HasNthPairArg(mType))
michael@0 70 // d. a selector list, which means mSelectors is non-null
michael@0 71 // (if nsCSSPseudoClasses::HasSelectorListArg(mType))
michael@0 72 void* mMemory; // mString and mNumbers use NS_Alloc/NS_Free
michael@0 73 char16_t* mString;
michael@0 74 int32_t* mNumbers;
michael@0 75 nsCSSSelectorList* mSelectors;
michael@0 76 } u;
michael@0 77 nsCSSPseudoClasses::Type mType;
michael@0 78 nsPseudoClassList* mNext;
michael@0 79 private:
michael@0 80 nsPseudoClassList* Clone(bool aDeep) const;
michael@0 81
michael@0 82 nsPseudoClassList(const nsPseudoClassList& aCopy) MOZ_DELETE;
michael@0 83 nsPseudoClassList& operator=(const nsPseudoClassList& aCopy) MOZ_DELETE;
michael@0 84 };
michael@0 85
michael@0 86 #define NS_ATTR_FUNC_SET 0 // [attr]
michael@0 87 #define NS_ATTR_FUNC_EQUALS 1 // [attr=value]
michael@0 88 #define NS_ATTR_FUNC_INCLUDES 2 // [attr~=value] (space separated)
michael@0 89 #define NS_ATTR_FUNC_DASHMATCH 3 // [attr|=value] ('-' truncated)
michael@0 90 #define NS_ATTR_FUNC_BEGINSMATCH 4 // [attr^=value] (begins with)
michael@0 91 #define NS_ATTR_FUNC_ENDSMATCH 5 // [attr$=value] (ends with)
michael@0 92 #define NS_ATTR_FUNC_CONTAINSMATCH 6 // [attr*=value] (contains substring)
michael@0 93
michael@0 94 struct nsAttrSelector {
michael@0 95 public:
michael@0 96 nsAttrSelector(int32_t aNameSpace, const nsString& aAttr);
michael@0 97 nsAttrSelector(int32_t aNameSpace, const nsString& aAttr, uint8_t aFunction,
michael@0 98 const nsString& aValue, bool aCaseSensitive);
michael@0 99 nsAttrSelector(int32_t aNameSpace, nsIAtom* aLowercaseAttr,
michael@0 100 nsIAtom* aCasedAttr, uint8_t aFunction,
michael@0 101 const nsString& aValue, bool aCaseSensitive);
michael@0 102 ~nsAttrSelector(void);
michael@0 103
michael@0 104 /** Do a deep clone. Should be used only on the first in the linked list. */
michael@0 105 nsAttrSelector* Clone() const { return Clone(true); }
michael@0 106
michael@0 107 nsString mValue;
michael@0 108 nsAttrSelector* mNext;
michael@0 109 nsCOMPtr<nsIAtom> mLowercaseAttr;
michael@0 110 nsCOMPtr<nsIAtom> mCasedAttr;
michael@0 111 int32_t mNameSpace;
michael@0 112 uint8_t mFunction;
michael@0 113 bool mCaseSensitive; // If we are in an HTML document,
michael@0 114 // is the value case sensitive?
michael@0 115 private:
michael@0 116 nsAttrSelector* Clone(bool aDeep) const;
michael@0 117
michael@0 118 nsAttrSelector(const nsAttrSelector& aCopy) MOZ_DELETE;
michael@0 119 nsAttrSelector& operator=(const nsAttrSelector& aCopy) MOZ_DELETE;
michael@0 120 };
michael@0 121
michael@0 122 struct nsCSSSelector {
michael@0 123 public:
michael@0 124 nsCSSSelector(void);
michael@0 125 ~nsCSSSelector(void);
michael@0 126
michael@0 127 /** Do a deep clone. Should be used only on the first in the linked list. */
michael@0 128 nsCSSSelector* Clone() const { return Clone(true, true); }
michael@0 129
michael@0 130 void Reset(void);
michael@0 131 void SetNameSpace(int32_t aNameSpace);
michael@0 132 void SetTag(const nsString& aTag);
michael@0 133 void AddID(const nsString& aID);
michael@0 134 void AddClass(const nsString& aClass);
michael@0 135 void AddPseudoClass(nsCSSPseudoClasses::Type aType);
michael@0 136 void AddPseudoClass(nsCSSPseudoClasses::Type aType, const char16_t* aString);
michael@0 137 void AddPseudoClass(nsCSSPseudoClasses::Type aType, const int32_t* aIntPair);
michael@0 138 // takes ownership of aSelectorList
michael@0 139 void AddPseudoClass(nsCSSPseudoClasses::Type aType,
michael@0 140 nsCSSSelectorList* aSelectorList);
michael@0 141 void AddAttribute(int32_t aNameSpace, const nsString& aAttr);
michael@0 142 void AddAttribute(int32_t aNameSpace, const nsString& aAttr, uint8_t aFunc,
michael@0 143 const nsString& aValue, bool aCaseSensitive);
michael@0 144 void SetOperator(char16_t aOperator);
michael@0 145
michael@0 146 inline bool HasTagSelector() const {
michael@0 147 return !!mCasedTag;
michael@0 148 }
michael@0 149
michael@0 150 inline bool IsPseudoElement() const {
michael@0 151 return mLowercaseTag && !mCasedTag;
michael@0 152 }
michael@0 153
michael@0 154 // Calculate the specificity of this selector (not including its mNext!).
michael@0 155 int32_t CalcWeight() const;
michael@0 156
michael@0 157 void ToString(nsAString& aString, nsCSSStyleSheet* aSheet,
michael@0 158 bool aAppend = false) const;
michael@0 159
michael@0 160 private:
michael@0 161 void AddPseudoClassInternal(nsPseudoClassList *aPseudoClass);
michael@0 162 nsCSSSelector* Clone(bool aDeepNext, bool aDeepNegations) const;
michael@0 163
michael@0 164 void AppendToStringWithoutCombinators(nsAString& aString,
michael@0 165 nsCSSStyleSheet* aSheet) const;
michael@0 166 void AppendToStringWithoutCombinatorsOrNegations(nsAString& aString,
michael@0 167 nsCSSStyleSheet* aSheet,
michael@0 168 bool aIsNegated)
michael@0 169 const;
michael@0 170 // Returns true if this selector can have a namespace specified (which
michael@0 171 // happens if and only if the default namespace would apply to this
michael@0 172 // selector).
michael@0 173 bool CanBeNamespaced(bool aIsNegated) const;
michael@0 174 // Calculate the specificity of this selector (not including its mNext
michael@0 175 // or its mNegations).
michael@0 176 int32_t CalcWeightWithoutNegations() const;
michael@0 177
michael@0 178 public:
michael@0 179 // Get and set the selector's pseudo type
michael@0 180 nsCSSPseudoElements::Type PseudoType() const {
michael@0 181 return static_cast<nsCSSPseudoElements::Type>(mPseudoType);
michael@0 182 }
michael@0 183 void SetPseudoType(nsCSSPseudoElements::Type aType) {
michael@0 184 NS_ASSERTION(static_cast<int32_t>(aType) >= INT16_MIN &&
michael@0 185 static_cast<int32_t>(aType) <= INT16_MAX,
michael@0 186 "Out of bounds - this will overflow mPseudoType");
michael@0 187 mPseudoType = static_cast<int16_t>(aType);
michael@0 188 }
michael@0 189
michael@0 190 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 191
michael@0 192 // For case-sensitive documents, mLowercaseTag is the same as mCasedTag,
michael@0 193 // but in case-insensitive documents (HTML) mLowercaseTag is lowercase.
michael@0 194 // Also, for pseudo-elements mCasedTag will be null but mLowercaseTag
michael@0 195 // contains their name.
michael@0 196 nsCOMPtr<nsIAtom> mLowercaseTag;
michael@0 197 nsCOMPtr<nsIAtom> mCasedTag;
michael@0 198 nsAtomList* mIDList;
michael@0 199 nsAtomList* mClassList;
michael@0 200 nsPseudoClassList* mPseudoClassList; // atom for the pseudo, string for
michael@0 201 // the argument to functional pseudos
michael@0 202 nsAttrSelector* mAttrList;
michael@0 203 nsCSSSelector* mNegations;
michael@0 204 nsCSSSelector* mNext;
michael@0 205 int32_t mNameSpace;
michael@0 206 char16_t mOperator;
michael@0 207 private:
michael@0 208 // int16_t to make sure it packs well with mOperator
michael@0 209 int16_t mPseudoType;
michael@0 210
michael@0 211 nsCSSSelector(const nsCSSSelector& aCopy) MOZ_DELETE;
michael@0 212 nsCSSSelector& operator=(const nsCSSSelector& aCopy) MOZ_DELETE;
michael@0 213 };
michael@0 214
michael@0 215 /**
michael@0 216 * A selector list is the unit of selectors that each style rule has.
michael@0 217 * For example, "P B, H1 B { ... }" would be a selector list with two
michael@0 218 * items (where each |nsCSSSelectorList| object's |mSelectors| has
michael@0 219 * an |mNext| for the P or H1). We represent them as linked lists.
michael@0 220 */
michael@0 221 class inDOMUtils;
michael@0 222
michael@0 223 struct nsCSSSelectorList {
michael@0 224 nsCSSSelectorList(void);
michael@0 225 ~nsCSSSelectorList(void);
michael@0 226
michael@0 227 /**
michael@0 228 * Create a new selector and push it onto the beginning of |mSelectors|,
michael@0 229 * setting its |mNext| to the current value of |mSelectors|. If there is an
michael@0 230 * earlier selector, set its |mOperator| to |aOperator|; else |aOperator|
michael@0 231 * must be char16_t(0).
michael@0 232 * Returns the new selector.
michael@0 233 * The list owns the new selector.
michael@0 234 * The caller is responsible for updating |mWeight|.
michael@0 235 */
michael@0 236 nsCSSSelector* AddSelector(char16_t aOperator);
michael@0 237
michael@0 238 /**
michael@0 239 * Should be used only on the first in the list
michael@0 240 */
michael@0 241 void ToString(nsAString& aResult, nsCSSStyleSheet* aSheet);
michael@0 242
michael@0 243 /**
michael@0 244 * Do a deep clone. Should be used only on the first in the list.
michael@0 245 */
michael@0 246 nsCSSSelectorList* Clone() const { return Clone(true); }
michael@0 247
michael@0 248 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 249
michael@0 250 nsCSSSelector* mSelectors;
michael@0 251 int32_t mWeight;
michael@0 252 nsCSSSelectorList* mNext;
michael@0 253 protected:
michael@0 254 friend class inDOMUtils;
michael@0 255 nsCSSSelectorList* Clone(bool aDeep) const;
michael@0 256
michael@0 257 private:
michael@0 258 nsCSSSelectorList(const nsCSSSelectorList& aCopy) MOZ_DELETE;
michael@0 259 nsCSSSelectorList& operator=(const nsCSSSelectorList& aCopy) MOZ_DELETE;
michael@0 260 };
michael@0 261
michael@0 262 // 464bab7a-2fce-4f30-ab44-b7a5f3aae57d
michael@0 263 #define NS_CSS_STYLE_RULE_IMPL_CID \
michael@0 264 { 0x464bab7a, 0x2fce, 0x4f30, \
michael@0 265 { 0xab, 0x44, 0xb7, 0xa5, 0xf3, 0xaa, 0xe5, 0x7d } }
michael@0 266
michael@0 267 namespace mozilla {
michael@0 268 namespace css {
michael@0 269
michael@0 270 class Declaration;
michael@0 271 class DOMCSSStyleRule;
michael@0 272
michael@0 273 class StyleRule;
michael@0 274
michael@0 275 class ImportantRule : public nsIStyleRule {
michael@0 276 public:
michael@0 277 ImportantRule(Declaration *aDeclaration);
michael@0 278
michael@0 279 NS_DECL_ISUPPORTS
michael@0 280
michael@0 281 // nsIStyleRule interface
michael@0 282 virtual void MapRuleInfoInto(nsRuleData* aRuleData) MOZ_OVERRIDE;
michael@0 283 #ifdef DEBUG
michael@0 284 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const MOZ_OVERRIDE;
michael@0 285 #endif
michael@0 286
michael@0 287 protected:
michael@0 288 virtual ~ImportantRule();
michael@0 289
michael@0 290 // Not an owning reference; the StyleRule that owns this
michael@0 291 // ImportantRule also owns the mDeclaration, and any rule node
michael@0 292 // pointing to this rule keeps that StyleRule alive as well.
michael@0 293 Declaration* mDeclaration;
michael@0 294
michael@0 295 friend class StyleRule;
michael@0 296 };
michael@0 297
michael@0 298 class StyleRule MOZ_FINAL : public Rule
michael@0 299 {
michael@0 300 public:
michael@0 301 StyleRule(nsCSSSelectorList* aSelector,
michael@0 302 Declaration *aDeclaration);
michael@0 303 private:
michael@0 304 // for |Clone|
michael@0 305 StyleRule(const StyleRule& aCopy);
michael@0 306 // for |DeclarationChanged|
michael@0 307 StyleRule(StyleRule& aCopy,
michael@0 308 Declaration *aDeclaration);
michael@0 309 public:
michael@0 310 NS_DECLARE_STATIC_IID_ACCESSOR(NS_CSS_STYLE_RULE_IMPL_CID)
michael@0 311
michael@0 312 NS_DECL_ISUPPORTS
michael@0 313
michael@0 314 // null for style attribute
michael@0 315 nsCSSSelectorList* Selector() { return mSelector; }
michael@0 316
michael@0 317 uint32_t GetLineNumber() const { return mLineNumber; }
michael@0 318 uint32_t GetColumnNumber() const { return mColumnNumber; }
michael@0 319 void SetLineNumberAndColumnNumber(uint32_t aLineNumber,
michael@0 320 uint32_t aColumnNumber)
michael@0 321 { mLineNumber = aLineNumber; mColumnNumber = aColumnNumber; }
michael@0 322
michael@0 323 Declaration* GetDeclaration() const { return mDeclaration; }
michael@0 324
michael@0 325 /**
michael@0 326 * Return a new |nsIStyleRule| instance that replaces the current
michael@0 327 * one, with |aDecl| replacing the previous declaration. Due to the
michael@0 328 * |nsIStyleRule| contract of immutability, this must be called if
michael@0 329 * the declaration is modified.
michael@0 330 *
michael@0 331 * |DeclarationChanged| handles replacing the object in the container
michael@0 332 * sheet or group rule if |aHandleContainer| is true.
michael@0 333 */
michael@0 334 already_AddRefed<StyleRule>
michael@0 335 DeclarationChanged(Declaration* aDecl, bool aHandleContainer);
michael@0 336
michael@0 337 nsIStyleRule* GetImportantRule() const { return mImportantRule; }
michael@0 338
michael@0 339 /**
michael@0 340 * The rule processor must call this method before calling
michael@0 341 * nsRuleWalker::Forward on this rule during rule matching.
michael@0 342 */
michael@0 343 void RuleMatched();
michael@0 344
michael@0 345 // hooks for DOM rule
michael@0 346 void GetCssText(nsAString& aCssText);
michael@0 347 void SetCssText(const nsAString& aCssText);
michael@0 348 void GetSelectorText(nsAString& aSelectorText);
michael@0 349 void SetSelectorText(const nsAString& aSelectorText);
michael@0 350
michael@0 351 virtual int32_t GetType() const;
michael@0 352
michael@0 353 virtual already_AddRefed<Rule> Clone() const;
michael@0 354
michael@0 355 virtual nsIDOMCSSRule* GetDOMRule();
michael@0 356
michael@0 357 virtual nsIDOMCSSRule* GetExistingDOMRule();
michael@0 358
michael@0 359 // The new mapping function.
michael@0 360 virtual void MapRuleInfoInto(nsRuleData* aRuleData) MOZ_OVERRIDE;
michael@0 361
michael@0 362 #ifdef DEBUG
michael@0 363 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const MOZ_OVERRIDE;
michael@0 364 #endif
michael@0 365
michael@0 366 virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 367
michael@0 368 private:
michael@0 369 ~StyleRule();
michael@0 370
michael@0 371 private:
michael@0 372 nsCSSSelectorList* mSelector; // null for style attribute
michael@0 373 Declaration* mDeclaration;
michael@0 374 ImportantRule* mImportantRule; // initialized by RuleMatched
michael@0 375 DOMCSSStyleRule* mDOMRule;
michael@0 376 // Keep the same type so that MSVC packs them.
michael@0 377 uint32_t mLineNumber;
michael@0 378 uint32_t mColumnNumber : 31;
michael@0 379 uint32_t mWasMatched : 1;
michael@0 380
michael@0 381 private:
michael@0 382 StyleRule& operator=(const StyleRule& aCopy) MOZ_DELETE;
michael@0 383 };
michael@0 384
michael@0 385 NS_DEFINE_STATIC_IID_ACCESSOR(StyleRule, NS_CSS_STYLE_RULE_IMPL_CID)
michael@0 386
michael@0 387 } // namespace css
michael@0 388 } // namespace mozilla
michael@0 389
michael@0 390 #endif /* mozilla_css_StyleRule_h__ */

mercurial