Issue #2135 - Bug 1410578: Make <link rel="stylesheet"> work in shadow trees

This commit is contained in:
FranklinDM
2023-03-04 00:53:46 +08:00
committed by roytam1
parent 45c179d01b
commit 588c2154a5
4 changed files with 21 additions and 49 deletions
+8 -16
View File
@@ -304,14 +304,6 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument,
return NS_OK;
}
// Check for a ShadowRoot because link elements are inert in a
// ShadowRoot.
ShadowRoot* containingShadow = thisContent->GetContainingShadow();
if (thisContent->IsHTMLElement(nsGkAtoms::link) &&
(aOldShadowRoot || containingShadow)) {
return NS_OK;
}
// XXXheycam ServoStyleSheets do not support <style scoped>.
Element* oldScopeElement = nullptr;
if (mStyleSheet) {
@@ -346,15 +338,16 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument,
}
}
// When static documents are created, stylesheets are cloned manually.
if (mDontLoadStyle || !mUpdatesEnabled ||
thisContent->OwnerDoc()->IsStaticDocument()) {
nsCOMPtr<nsIDocument> doc = thisContent->IsInShadowTree() ?
thisContent->OwnerDoc() : thisContent->GetUncomposedDoc();
// Loader could be null during unlink, see bug 1425866.
if (!doc || !doc->CSSLoader() || !doc->CSSLoader()->GetEnabled()) {
return NS_OK;
}
nsCOMPtr<nsIDocument> doc = thisContent->IsInShadowTree() ?
thisContent->OwnerDoc() : thisContent->GetUncomposedDoc();
if (!doc || !doc->CSSLoader()->GetEnabled()) {
// When static documents are created, stylesheets are cloned manually.
if (mDontLoadStyle || !mUpdatesEnabled || doc->IsStaticDocument()) {
return NS_OK;
}
@@ -426,8 +419,7 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument,
rv = doc->CSSLoader()->
LoadInlineStyle(thisContent, text, mLineNumber, title, media,
scopeElement, aObserver, &doneLoading, &isAlternate, &isExplicitlyEnabled);
}
else {
} else {
nsAutoString integrity;
thisContent->GetAttr(kNameSpaceID_None, nsGkAtoms::integrity, integrity);
if (!integrity.IsEmpty()) {
+5 -12
View File
@@ -162,7 +162,8 @@ HTMLLinkElement::HasDeferredDNSPrefetchRequest()
}
nsresult
HTMLLinkElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
HTMLLinkElement::BindToTree(nsIDocument* aDocument,
nsIContent* aParent,
nsIContent* aBindingParent,
bool aCompileEventHandlers)
{
@@ -173,12 +174,8 @@ HTMLLinkElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
aCompileEventHandlers);
NS_ENSURE_SUCCESS(rv, rv);
// Link must be inert in ShadowRoot.
if (aDocument && !GetContainingShadow()) {
aDocument->RegisterPendingLinkUpdate(this);
}
if (IsInComposedDoc()) {
if (nsIDocument* doc = GetComposedDoc()) {
doc->RegisterPendingLinkUpdate(this);
TryDNSPrefetchPreconnectOrPrefetch();
}
@@ -222,11 +219,7 @@ HTMLLinkElement::UnbindFromTree(bool aDeep, bool aNullParent)
// If this is reinserted back into the document it will not be
// from the parser.
nsCOMPtr<nsIDocument> oldDoc = GetUncomposedDoc();
// Check for a ShadowRoot because link elements are inert in a
// ShadowRoot.
ShadowRoot* oldShadowRoot = GetBindingParent() ?
GetBindingParent()->GetShadowRoot() : nullptr;
ShadowRoot* oldShadowRoot = GetContainingShadow();
OwnerDoc()->UnregisterPendingLinkUpdate(this);
@@ -22,22 +22,7 @@ isnot(document.baseURI, "http://www.example.org/", "Base element should be inert
SimpleTest.waitForExplicitFinish();
// Check that <link> is inert.
var numStyleBeforeLoad = document.styleSheets.length;
shadow.innerHTML = '<link id="shadowlink" rel="stylesheet" type="text/css" href="inert_style.css" /><span id="shadowspan"></span>';
shadow.applyAuthorStyles = true;
var shadowSpan = shadow.getElementById("shadowspan");
var shadowStyle = shadow.getElementById("shadowlink");
function runChecks() {
isnot(getComputedStyle(shadowSpan, null).getPropertyValue("padding-top"), "10px", "Link element should be inert.");
is(document.styleSheets.length, numStyleBeforeLoad, "Document style count should remain the same because the style should not be in the doucment.");
is(shadow.styleSheets.length, 0, "Inert link should not add style to ShadowRoot.");
// Remove link to make sure we don't get assertions.
shadow.removeChild(shadowStyle);
SimpleTest.finish();
};
SimpleTest.finish();
</script>
</body>
+7 -5
View File
@@ -2024,9 +2024,7 @@ Loader::LoadInlineStyle(nsIContent* aElement,
PrepareSheet(sheet, aTitle, aMedia, nullptr, aScopeElement, *aIsAlternate, *aIsExplicitlyEnabled);
if (aElement->HasFlag(NODE_IS_IN_SHADOW_TREE)) {
ShadowRoot* containingShadow = aElement->GetContainingShadow();
MOZ_ASSERT(containingShadow);
containingShadow->InsertSheet(sheet, aElement);
aElement->GetContainingShadow()->InsertSheet(sheet, aElement);
} else {
rv = InsertSheetInDoc(sheet, aElement, mDocument);
NS_ENSURE_SUCCESS(rv, rv);
@@ -2120,8 +2118,12 @@ Loader::LoadStyleLink(nsIContent* aElement,
PrepareSheet(sheet, aTitle, aMedia, nullptr, nullptr, *aIsAlternate, *aIsExplicitlyEnabled);
rv = InsertSheetInDoc(sheet, aElement, mDocument);
NS_ENSURE_SUCCESS(rv, rv);
if (aElement->HasFlag(NODE_IS_IN_SHADOW_TREE)) {
aElement->GetContainingShadow()->InsertSheet(sheet, aElement);
} else {
rv = InsertSheetInDoc(sheet, aElement, mDocument);
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsIStyleSheetLinkingElement> owningElement(do_QueryInterface(aElement));