Bug 1355394 part 3 - Implement CSSOM support for @supports. r?heycam draft
authorXidorn Quan <me@upsuper.org>
Wed, 26 Apr 2017 15:59:29 +0800
changeset 569905 13fefbf221e93b3393ff9c81a0af4c2e0b03a918
parent 569904 ad36d0770f6d915e71ef51885a5a954a653aee60
child 569906 0b0464058363a83b6136a2992d03c03badb18532
push id56304
push userxquan@mozilla.com
push dateFri, 28 Apr 2017 01:10:56 +0000
reviewersheycam
bugs1355394
milestone55.0a1
Bug 1355394 part 3 - Implement CSSOM support for @supports. r?heycam MozReview-Commit-ID: AV1t6mIFF0J
layout/style/ServoArcTypeList.h
layout/style/ServoBindingList.h
layout/style/ServoCSSRuleList.cpp
layout/style/ServoSupportsRule.cpp
layout/style/ServoSupportsRule.h
layout/style/moz.build
--- a/layout/style/ServoArcTypeList.h
+++ b/layout/style/ServoArcTypeList.h
@@ -13,8 +13,9 @@ SERVO_ARC_TYPE(DeclarationBlock, RawServ
 SERVO_ARC_TYPE(StyleRule, RawServoStyleRule)
 SERVO_ARC_TYPE(ImportRule, RawServoImportRule)
 SERVO_ARC_TYPE(AnimationValue, RawServoAnimationValue)
 SERVO_ARC_TYPE(AnimationValueMap, RawServoAnimationValueMap)
 SERVO_ARC_TYPE(MediaList, RawServoMediaList)
 SERVO_ARC_TYPE(MediaRule, RawServoMediaRule)
 SERVO_ARC_TYPE(NamespaceRule, RawServoNamespaceRule)
 SERVO_ARC_TYPE(PageRule, RawServoPageRule)
+SERVO_ARC_TYPE(SupportsRule, RawServoSupportsRule)
--- a/layout/style/ServoBindingList.h
+++ b/layout/style/ServoBindingList.h
@@ -101,16 +101,17 @@ SERVO_BINDING_FUNC(Servo_CssRules_Delete
 #define GROUP_RULE_FUNCS(type_) \
   BASIC_RULE_FUNCS(type_) \
   SERVO_BINDING_FUNC(Servo_##type_##Rule_GetRules, ServoCssRulesStrong, \
                      RawServo##type_##RuleBorrowed rule)
 BASIC_RULE_FUNCS(Style)
 GROUP_RULE_FUNCS(Media)
 BASIC_RULE_FUNCS(Namespace)
 BASIC_RULE_FUNCS(Page)
+GROUP_RULE_FUNCS(Supports)
 #undef GROUP_RULE_FUNCS
 #undef BASIC_RULE_FUNCS
 SERVO_BINDING_FUNC(Servo_CssRules_GetFontFaceRuleAt, nsCSSFontFaceRule*,
                    ServoCssRulesBorrowed rules, uint32_t index)
 SERVO_BINDING_FUNC(Servo_StyleRule_GetStyle, RawServoDeclarationBlockStrong,
                    RawServoStyleRuleBorrowed rule)
 SERVO_BINDING_FUNC(Servo_StyleRule_SetStyle, void,
                    RawServoStyleRuleBorrowed rule,
@@ -123,16 +124,18 @@ SERVO_BINDING_FUNC(Servo_NamespaceRule_G
                    RawServoNamespaceRuleBorrowed rule)
 SERVO_BINDING_FUNC(Servo_NamespaceRule_GetURI, nsIAtom*,
                    RawServoNamespaceRuleBorrowed rule)
 SERVO_BINDING_FUNC(Servo_PageRule_GetStyle, RawServoDeclarationBlockStrong,
                    RawServoPageRuleBorrowed rule)
 SERVO_BINDING_FUNC(Servo_PageRule_SetStyle, void,
                    RawServoPageRuleBorrowed rule,
                    RawServoDeclarationBlockBorrowed declarations)
+SERVO_BINDING_FUNC(Servo_SupportsRule_GetConditionText, void,
+                   RawServoSupportsRuleBorrowed rule, nsAString* result)
 
 // Animations API
 SERVO_BINDING_FUNC(Servo_ParseProperty,
                    RawServoDeclarationBlockStrong,
                    nsCSSPropertyID property, const nsACString* value,
                    RawGeckoURLExtraData* data)
 SERVO_BINDING_FUNC(Servo_ParseEasing, bool,
                    const nsAString* easing,
--- a/layout/style/ServoCSSRuleList.cpp
+++ b/layout/style/ServoCSSRuleList.cpp
@@ -8,16 +8,17 @@
 
 #include "mozilla/ServoCSSRuleList.h"
 
 #include "mozilla/ServoBindings.h"
 #include "mozilla/ServoStyleRule.h"
 #include "mozilla/ServoMediaRule.h"
 #include "mozilla/ServoNamespaceRule.h"
 #include "mozilla/ServoPageRule.h"
+#include "mozilla/ServoSupportsRule.h"
 
 namespace mozilla {
 
 ServoCSSRuleList::ServoCSSRuleList(already_AddRefed<ServoCssRules> aRawRules)
   : mRawRules(aRawRules)
 {
   Servo_CssRules_ListTypes(mRawRules, &mRules);
   // XXX We may want to eagerly create object for import rule, so that
@@ -84,16 +85,17 @@ ServoCSSRuleList::GetRule(uint32_t aInde
         ruleObj = new Servo##name_##Rule(                                   \
           Servo_CssRules_Get##name_##RuleAt(mRawRules, aIndex).Consume());  \
         break;                                                              \
       }
       CASE_RULE(STYLE, Style)
       CASE_RULE(MEDIA, Media)
       CASE_RULE(NAMESPACE, Namespace)
       CASE_RULE(PAGE, Page)
+      CASE_RULE(SUPPORTS, Supports)
 #undef CASE_RULE
       case nsIDOMCSSRule::FONT_FACE_RULE: {
         // Returns a borrowed nsCSSFontFaceRule object directly, so we
         // don't need to create anything here, but we still need to have
         // the style sheet and parent rule set properly.
         ruleObj = Servo_CssRules_GetFontFaceRuleAt(mRawRules, aIndex);
         break;
       }
@@ -208,18 +210,19 @@ void
 ServoCSSRuleList::FillStyleRuleHashtable(StyleRuleHashtable& aTable)
 {
   for (uint32_t i = 0; i < mRules.Length(); i++) {
     uint16_t type = GetRuleType(i);
     if (type == nsIDOMCSSRule::STYLE_RULE) {
       ServoStyleRule* castedRule = static_cast<ServoStyleRule*>(GetRule(i));
       RawServoStyleRule* rawRule = castedRule->Raw();
       aTable.Put(rawRule, castedRule);
-    } else if (type == nsIDOMCSSRule::MEDIA_RULE) {
-      ServoMediaRule* castedRule = static_cast<ServoMediaRule*>(GetRule(i));
+    } else if (type == nsIDOMCSSRule::MEDIA_RULE ||
+               type == nsIDOMCSSRule::SUPPORTS_RULE) {
+      GroupRule* castedRule = static_cast<GroupRule*>(GetRule(i));
 
       // Call this method recursively on the ServoCSSRuleList in the rule.
       ServoCSSRuleList* castedRuleList = static_cast<ServoCSSRuleList*>(
         castedRule->CssRules());
       castedRuleList->FillStyleRuleHashtable(aTable);
     }
   }
 }
new file mode 100644
--- /dev/null
+++ b/layout/style/ServoSupportsRule.cpp
@@ -0,0 +1,96 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* representation of CSSSupportsRule for stylo */
+
+#include "mozilla/ServoSupportsRule.h"
+
+#include "mozilla/ServoBindings.h"
+
+using namespace mozilla::dom;
+
+namespace mozilla {
+
+ServoSupportsRule::ServoSupportsRule(RefPtr<RawServoSupportsRule> aRawRule)
+  : CSSSupportsRule(Servo_SupportsRule_GetRules(aRawRule).Consume())
+  , mRawRule(Move(aRawRule))
+{
+}
+
+ServoSupportsRule::~ServoSupportsRule()
+{
+}
+
+NS_IMPL_ADDREF_INHERITED(ServoSupportsRule, CSSSupportsRule)
+NS_IMPL_RELEASE_INHERITED(ServoSupportsRule, CSSSupportsRule)
+
+// QueryInterface implementation for SupportsRule
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServoSupportsRule)
+NS_INTERFACE_MAP_END_INHERITING(CSSSupportsRule)
+
+/* virtual */ already_AddRefed<css::Rule>
+ServoSupportsRule::Clone() const
+{
+  // Rule::Clone is only used when CSSStyleSheetInner is cloned in
+  // preparation of being mutated. However, ServoStyleSheet never clones
+  // anything, so this method should never be called.
+  MOZ_ASSERT_UNREACHABLE("Shouldn't be cloning ServoSupportsRule");
+  return nullptr;
+}
+
+/* virtual */ bool
+ServoSupportsRule::UseForPresentation(nsPresContext* aPresContext,
+                                      nsMediaQueryResultCacheKey& aKey)
+{
+  // GroupRule::UseForPresentation is only used in nsCSSRuleProcessor,
+  // so this should never be called.
+  MOZ_ASSERT_UNREACHABLE("Shouldn't be calling UseForPresentation");
+  return false;
+}
+
+#ifdef DEBUG
+/* virtual */ void
+ServoSupportsRule::List(FILE* out, int32_t aIndent) const
+{
+  nsAutoCString str;
+  for (int32_t i = 0; i < aIndent; i++) {
+    str.AppendLiteral("  ");
+  }
+  Servo_SupportsRule_Debug(mRawRule, &str);
+  fprintf_stderr(out, "%s\n", str.get());
+}
+#endif
+
+// nsIDOMCSSConditionRule methods
+
+NS_IMETHODIMP
+ServoSupportsRule::GetConditionText(nsAString& aConditionText)
+{
+  Servo_SupportsRule_GetConditionText(mRawRule, &aConditionText);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+ServoSupportsRule::SetConditionText(const nsAString& aConditionText)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+/* virtual */ void
+ServoSupportsRule::GetCssTextImpl(nsAString& aCssText) const
+{
+  Servo_SupportsRule_GetCssText(mRawRule, &aCssText);
+}
+
+/* virtual */ size_t
+ServoSupportsRule::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
+  const
+{
+  // TODO Implement this!
+  return aMallocSizeOf(this);
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/layout/style/ServoSupportsRule.h
@@ -0,0 +1,51 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* representation of CSSSupportsRule for stylo */
+
+#ifndef mozilla_ServoSupportsRule_h
+#define mozilla_ServoSupportsRule_h
+
+#include "mozilla/dom/CSSSupportsRule.h"
+#include "mozilla/ServoBindingTypes.h"
+
+namespace mozilla {
+
+class ServoSupportsRule final : public dom::CSSSupportsRule
+{
+public:
+  explicit ServoSupportsRule(RefPtr<RawServoSupportsRule> aRawRule);
+
+  NS_DECL_ISUPPORTS_INHERITED
+
+  already_AddRefed<css::Rule> Clone() const override;
+  bool UseForPresentation(nsPresContext* aPresContext,
+                          nsMediaQueryResultCacheKey& aKey) final;
+#ifdef DEBUG
+  void List(FILE* out = stdout, int32_t aIndent = 0) const final;
+#endif
+
+  RawServoSupportsRule* Raw() const { return mRawRule; }
+
+  // nsIDOMCSSConditionRule interface
+  NS_DECL_NSIDOMCSSCONDITIONRULE
+
+  // WebIDL interface
+  void GetCssTextImpl(nsAString& aCssText) const override;
+  using CSSSupportsRule::SetConditionText;
+
+  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
+    const override;
+
+private:
+  virtual ~ServoSupportsRule();
+
+  RefPtr<RawServoSupportsRule> mRawRule;
+};
+
+} // namespace mozilla
+
+#endif // mozilla_ServoSupportsRule_h
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -107,16 +107,17 @@ EXPORTS.mozilla += [
     'ServoMediaRule.h',
     'ServoNamespaceRule.h',
     'ServoPageRule.h',
     'ServoPropPrefList.h',
     'ServoSpecifiedValues.h',
     'ServoStyleRule.h',
     'ServoStyleSet.h',
     'ServoStyleSheet.h',
+    'ServoSupportsRule.h',
     'ServoTypes.h',
     'ServoUtils.h',
     'SheetType.h',
     'StyleAnimationValue.h',
     'StyleBackendType.h',
     'StyleComplexColor.h',
     'StyleContextSource.h',
     'StyleSetHandle.h',
@@ -228,16 +229,17 @@ UNIFIED_SOURCES += [
     'ServoMediaList.cpp',
     'ServoMediaRule.cpp',
     'ServoNamespaceRule.cpp',
     'ServoPageRule.cpp',
     'ServoSpecifiedValues.cpp',
     'ServoStyleRule.cpp',
     'ServoStyleSet.cpp',
     'ServoStyleSheet.cpp',
+    'ServoSupportsRule.cpp',
     'StyleAnimationValue.cpp',
     'StyleRule.cpp',
     'StyleSheet.cpp',
     'URLExtraData.cpp',
 ]
 
 # - nsLayoutStylesheetCache.cpp needs to be built separately because it uses
 # nsExceptionHandler.h, which includes windows.h.