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