diff --git a/dom/webidl/CSSStyleSheet.webidl b/dom/webidl/CSSStyleSheet.webidl index 45ef840208..3cd4575934 100644 --- a/dom/webidl/CSSStyleSheet.webidl +++ b/dom/webidl/CSSStyleSheet.webidl @@ -13,6 +13,7 @@ enum CSSStyleSheetParsingMode { "agent" }; +[Constructor] interface CSSStyleSheet : StyleSheet { [Pure] readonly attribute CSSRule? ownerRule; diff --git a/layout/style/StyleSheet.cpp b/layout/style/StyleSheet.cpp index adcd0fc01b..3c0856d840 100644 --- a/layout/style/StyleSheet.cpp +++ b/layout/style/StyleSheet.cpp @@ -12,8 +12,10 @@ #include "mozilla/CSSStyleSheet.h" #include "mozAutoDocUpdate.h" +#include "nsContentUtils.h" #include "nsIMediaList.h" #include "nsNullPrincipal.h" +#include "nsPIDOMWindow.h" using namespace mozilla::dom; @@ -222,6 +224,40 @@ StyleSheet::DeleteRule(uint32_t aIndex) // WebIDL CSSStyleSheet API +/* static */ already_AddRefed +StyleSheet::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) +{ + nsCOMPtr window = + do_QueryInterface(aGlobal.GetAsSupports()); + if (!window) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + nsCOMPtr document = window->GetDoc(); + if (!document) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + nsCOMPtr documentURI = document->GetDocumentURI(); + nsCOMPtr baseURI = document->GetBaseURI(); + nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal.Get()); + if (!documentURI || !baseURI || !principal) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + RefPtr sheet = + new CSSStyleSheet(css::eAuthorSheetFeatures, CORS_NONE, + document->GetReferrerPolicy()); + sheet->SetURIs(documentURI, nullptr, baseURI); + sheet->SetPrincipal(principal); + sheet->SetComplete(); + + return sheet.forget(); +} + dom::CSSRuleList* StyleSheet::GetCssRules(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) diff --git a/layout/style/StyleSheet.h b/layout/style/StyleSheet.h index fd1e9b3652..99ed270127 100644 --- a/layout/style/StyleSheet.h +++ b/layout/style/StyleSheet.h @@ -149,6 +149,8 @@ public: // The XPCOM SetDisabled is fine for WebIDL. // WebIDL CSSStyleSheet API + static already_AddRefed Constructor(const dom::GlobalObject& aGlobal, + ErrorResult& aRv); virtual css::Rule* GetDOMOwnerRule() const = 0; dom::CSSRuleList* GetCssRules(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv); diff --git a/layout/style/test/mochitest.ini b/layout/style/test/mochitest.ini index 1e4900e62f..e0eaf7ae1c 100644 --- a/layout/style/test/mochitest.ini +++ b/layout/style/test/mochitest.ini @@ -167,6 +167,7 @@ prefs = layout.css.filters.enabled=true [test_condition_text.html] [test_condition_text_assignment.html] [test_contain_formatting_context.html] +[test_constructed_stylesheet.html] [test_counter_descriptor_storage.html] [test_counter_style.html] [test_css_cross_domain.html] diff --git a/layout/style/test/test_constructed_stylesheet.html b/layout/style/test/test_constructed_stylesheet.html new file mode 100644 index 0000000000..685faf3e48 --- /dev/null +++ b/layout/style/test/test_constructed_stylesheet.html @@ -0,0 +1,39 @@ + + + + + Test CSSStyleSheet constructor + + + + +

+
+
+