Bug 1418245 - Move @-moz-document matching function out of css::DocumentRule. r=TYLin
authorCameron McCormack <cam@mcc.id.au>
Fri, 17 Nov 2017 17:31:22 +0800
changeset 437470 c59c3dbe979ba85a7b89b484924334d89f2457c0
parent 437469 a540835514ed2986ae0cf7c30c6eb6f3da0bf437
child 437471 a6339ec0511bd3bdff7b3fa91226930657cbdb48
push id117
push userfmarier@mozilla.com
push dateTue, 28 Nov 2017 20:17:16 +0000
reviewersTYLin
bugs1418245
milestone59.0a1
Bug 1418245 - Move @-moz-document matching function out of css::DocumentRule. r=TYLin MozReview-Commit-ID: 81dCiFosF0V
layout/style/CSSMozDocumentRule.cpp
layout/style/CSSMozDocumentRule.h
layout/style/ServoBindings.cpp
layout/style/nsCSSRules.cpp
layout/style/nsCSSRules.h
--- a/layout/style/CSSMozDocumentRule.cpp
+++ b/layout/style/CSSMozDocumentRule.cpp
@@ -5,16 +5,18 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/CSSMozDocumentRule.h"
 #include "mozilla/dom/CSSMozDocumentRuleBinding.h"
 
 namespace mozilla {
 namespace dom {
 
+using namespace mozilla::css;
+
 NS_IMPL_ADDREF_INHERITED(CSSMozDocumentRule, css::ConditionRule)
 NS_IMPL_RELEASE_INHERITED(CSSMozDocumentRule, css::ConditionRule)
 
 // QueryInterface implementation for CSSMozDocumentRule
 NS_INTERFACE_MAP_BEGIN(CSSMozDocumentRule)
   NS_INTERFACE_MAP_ENTRY(nsIDOMCSSGroupingRule)
   NS_INTERFACE_MAP_ENTRY(nsIDOMCSSConditionRule)
   NS_INTERFACE_MAP_ENTRY(nsIDOMCSSMozDocumentRule)
@@ -49,10 +51,57 @@ CSSMozDocumentRule::SetConditionText(con
 
 /* virtual */ JSObject*
 CSSMozDocumentRule::WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto)
 {
   return CSSMozDocumentRuleBinding::Wrap(aCx, this, aGivenProto);
 }
 
+bool
+CSSMozDocumentRule::Match(nsIDocument* aDoc,
+                          nsIURI* aDocURI,
+                          const nsACString& aDocURISpec,
+                          const nsACString& aPattern,
+                          URLMatchingFunction aUrlMatchingFunction)
+{
+  switch (aUrlMatchingFunction) {
+    case URLMatchingFunction::eURL: {
+      if (aDocURISpec == aPattern) {
+        return true;
+      }
+    } break;
+    case URLMatchingFunction::eURLPrefix: {
+      if (StringBeginsWith(aDocURISpec, aPattern)) {
+        return true;
+      }
+    } break;
+    case URLMatchingFunction::eDomain: {
+      nsAutoCString host;
+      if (aDocURI) {
+        aDocURI->GetHost(host);
+      }
+      int32_t lenDiff = host.Length() - aPattern.Length();
+      if (lenDiff == 0) {
+        if (host == aPattern) {
+          return true;
+        }
+      } else {
+        if (StringEndsWith(host, aPattern) &&
+            host.CharAt(lenDiff - 1) == '.') {
+          return true;
+        }
+      }
+    } break;
+    case URLMatchingFunction::eRegExp: {
+      NS_ConvertUTF8toUTF16 spec(aDocURISpec);
+      NS_ConvertUTF8toUTF16 regex(aPattern);
+      if (nsContentUtils::IsPatternMatching(spec, regex, aDoc)) {
+        return true;
+      }
+    } break;
+  }
+
+  return false;
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/layout/style/CSSMozDocumentRule.h
+++ b/layout/style/CSSMozDocumentRule.h
@@ -3,16 +3,17 @@
 /* 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/. */
 
 #ifndef mozilla_dom_CSSMozDocumentRule_h
 #define mozilla_dom_CSSMozDocumentRule_h
 
 #include "mozilla/css/GroupRule.h"
+#include "mozilla/css/URLMatchingFunction.h"
 #include "nsIDOMCSSMozDocumentRule.h"
 
 namespace mozilla {
 namespace dom {
 
 class CSSMozDocumentRule : public css::ConditionRule
                          , public nsIDOMCSSMozDocumentRule
 {
@@ -21,16 +22,22 @@ protected:
   virtual ~CSSMozDocumentRule() {}
 
 public:
   NS_DECL_ISUPPORTS_INHERITED
 
   int32_t GetType() const final override { return css::Rule::DOCUMENT_RULE; }
   using Rule::GetType;
 
+  static bool Match(nsIDocument* aDoc,
+                    nsIURI* aDocURI,
+                    const nsACString& aDocURISpec,
+                    const nsACString& aPattern,
+                    css::URLMatchingFunction aUrlMatchingFunction);
+
   // nsIDOMCSSGroupingRule interface
   NS_DECL_NSIDOMCSSGROUPINGRULE
 
   // nsIDOMCSSConditionRule interface
   NS_IMETHOD SetConditionText(const nsAString& aConditionText) override = 0;
 
   // nsIDOMCSSMozDocumentRule interface
   NS_DECL_NSIDOMCSSMOZDOCUMENTRULE
--- a/layout/style/ServoBindings.cpp
+++ b/layout/style/ServoBindings.cpp
@@ -2732,18 +2732,18 @@ Gecko_DocumentRule_UseForPresentation(Ra
   nsIURI *docURI = doc->GetDocumentURI();
   nsAutoCString docURISpec;
   if (docURI) {
     // If GetSpec fails (due to OOM) just skip these URI-specific CSS rules.
     nsresult rv = docURI->GetSpec(docURISpec);
     NS_ENSURE_SUCCESS(rv, false);
   }
 
-  return css::DocumentRule::UseForPresentation(doc, docURI, docURISpec,
-                                               *aPattern, aURLMatchingFunction);
+  return CSSMozDocumentRule::Match(doc, docURI, docURISpec, *aPattern,
+                                   aURLMatchingFunction);
 }
 
 void
 Gecko_SetJemallocThreadLocalArena(bool enabled)
 {
 #if defined(MOZ_MEMORY)
   jemalloc_thread_local_arena(enabled);
 #endif
--- a/layout/style/nsCSSRules.cpp
+++ b/layout/style/nsCSSRules.cpp
@@ -586,71 +586,24 @@ DocumentRule::UseForPresentation(nsPresC
   nsAutoCString docURISpec;
   if (docURI) {
     // If GetSpec fails (due to OOM) just skip these URI-specific CSS rules.
     nsresult rv = docURI->GetSpec(docURISpec);
     NS_ENSURE_SUCCESS(rv, false);
   }
 
   for (URL *url = mURLs; url; url = url->next) {
-    if (UseForPresentation(doc, docURI, docURISpec, url->url, url->func)) {
+    if (Match(doc, docURI, docURISpec, url->url, url->func)) {
       return true;
     }
   }
 
   return false;
 }
 
-bool
-DocumentRule::UseForPresentation(nsIDocument* aDoc,
-                                 nsIURI* aDocURI,
-                                 const nsACString& aDocURISpec,
-                                 const nsACString& aPattern,
-                                 URLMatchingFunction aUrlMatchingFunction)
-{
-  switch (aUrlMatchingFunction) {
-    case URLMatchingFunction::eURL: {
-      if (aDocURISpec == aPattern) {
-        return true;
-      }
-    } break;
-    case URLMatchingFunction::eURLPrefix: {
-      if (StringBeginsWith(aDocURISpec, aPattern)) {
-        return true;
-      }
-    } break;
-    case URLMatchingFunction::eDomain: {
-      nsAutoCString host;
-      if (aDocURI) {
-        aDocURI->GetHost(host);
-      }
-      int32_t lenDiff = host.Length() - aPattern.Length();
-      if (lenDiff == 0) {
-        if (host == aPattern) {
-          return true;
-        }
-      } else {
-        if (StringEndsWith(host, aPattern) &&
-            host.CharAt(lenDiff - 1) == '.') {
-          return true;
-        }
-      }
-    } break;
-    case URLMatchingFunction::eRegExp: {
-      NS_ConvertUTF8toUTF16 spec(aDocURISpec);
-      NS_ConvertUTF8toUTF16 regex(aPattern);
-      if (nsContentUtils::IsPatternMatching(spec, regex, aDoc)) {
-        return true;
-      }
-    } break;
-  }
-
-  return false;
-}
-
 DocumentRule::URL::~URL()
 {
   NS_CSS_DELETE_LIST_MEMBER(DocumentRule::URL, this, next);
 }
 
 /* virtual */ size_t
 DocumentRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
 {
--- a/layout/style/nsCSSRules.h
+++ b/layout/style/nsCSSRules.h
@@ -116,22 +116,16 @@ public:
   NS_DECL_NSIDOMCSSCONDITIONRULE
 
   // rest of GroupRule
   virtual bool UseForPresentation(nsPresContext* aPresContext,
                                   nsMediaQueryResultCacheKey& aKey) override;
 
   bool UseForPresentation(nsPresContext* aPresContext);
 
-  static bool UseForPresentation(nsIDocument* aDoc,
-                                 nsIURI* aDocURI,
-                                 const nsACString& aDocURISpec,
-                                 const nsACString& aPattern,
-                                 URLMatchingFunction aUrlMatchingFunction);
-
   struct URL {
     URLMatchingFunction func;
     nsCString url;
     URL *next;
 
     URL() : next(nullptr) {}
     URL(const URL& aOther)
       : func(aOther.func)