Issue #3053 - Implement CSSStyleSheet constructor

This commit is contained in:
Basilisk-Dev
2026-05-22 12:35:37 -04:00
committed by roytam1
parent 0915736715
commit f5a84f4ad2
5 changed files with 79 additions and 0 deletions
+1
View File
@@ -13,6 +13,7 @@ enum CSSStyleSheetParsingMode {
"agent"
};
[Constructor]
interface CSSStyleSheet : StyleSheet {
[Pure]
readonly attribute CSSRule? ownerRule;
+36
View File
@@ -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>
StyleSheet::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
{
nsCOMPtr<nsPIDOMWindowInner> window =
do_QueryInterface(aGlobal.GetAsSupports());
if (!window) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
nsCOMPtr<nsIDocument> document = window->GetDoc();
if (!document) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
nsCOMPtr<nsIURI> documentURI = document->GetDocumentURI();
nsCOMPtr<nsIURI> baseURI = document->GetBaseURI();
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal.Get());
if (!documentURI || !baseURI || !principal) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<StyleSheet> 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)
+2
View File
@@ -149,6 +149,8 @@ public:
// The XPCOM SetDisabled is fine for WebIDL.
// WebIDL CSSStyleSheet API
static already_AddRefed<StyleSheet> Constructor(const dom::GlobalObject& aGlobal,
ErrorResult& aRv);
virtual css::Rule* GetDOMOwnerRule() const = 0;
dom::CSSRuleList* GetCssRules(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv);
+1
View File
@@ -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]
@@ -0,0 +1,39 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test CSSStyleSheet constructor</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test"></pre>
<script type="application/javascript">
"use strict";
let sheet;
try {
sheet = new CSSStyleSheet();
ok(true, "CSSStyleSheet constructor should not throw");
} catch (e) {
ok(false, "CSSStyleSheet constructor should not throw: " + e);
}
if (sheet) {
ok(sheet instanceof CSSStyleSheet, "Constructor should create a CSSStyleSheet");
is(sheet.type, "text/css", "Constructed sheet should have CSS type");
is(sheet.href, null, "Constructed sheet should not have an href");
is(sheet.cssRules.length, 0, "Constructed sheet should start with no rules");
is(sheet.insertRule("#test { color: rgb(1, 2, 3); }", 0), 0,
"insertRule should work on a constructed sheet");
is(sheet.cssRules.length, 1, "insertRule should append a rule");
is(sheet.cssRules[0].selectorText, "#test", "Inserted rule should be readable");
sheet.deleteRule(0);
is(sheet.cssRules.length, 0, "deleteRule should remove the rule");
}
</script>
</body>
</html>