LibWeb: Allow moving StyleSheets between documents without falling apart

We have to unregister link element stylesheets from the old document's
StyleSheetList when moving them into a new document.

This makes it possible to load GitHub contributor graphs. :^)
This commit is contained in:
Andreas Kling 2024-04-21 19:46:37 +02:00
parent 511e411def
commit 8e56367092
Notes: sideshowbarker 2024-07-18 00:54:03 +09:00
4 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,3 @@
Sheets in old doc: 0
Sheets in new doc: 1
PASS (didn't crash)

View File

@ -0,0 +1,11 @@
<script src="../include.js"></script>
<script>
let doc = new DOMParser().parseFromString(`<link rel="stylesheet" href="data:text/css,div{}">`, `text/html`);
let link = doc.head.firstChild;
document.head.appendChild(link);
test(() => {
println("Sheets in old doc: " + doc.styleSheets.length)
println("Sheets in new doc: " + document.styleSheets.length)
println("PASS (didn't crash)");
})
</script>

View File

@ -46,6 +46,15 @@ void HTMLLinkElement::initialize(JS::Realm& realm)
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLLinkElement);
}
void HTMLLinkElement::removed_from(Node* old_parent)
{
Base::removed_from(old_parent);
if (m_loaded_style_sheet) {
document_or_shadow_root_style_sheets().remove_a_css_style_sheet(*m_loaded_style_sheet);
m_loaded_style_sheet = nullptr;
}
}
void HTMLLinkElement::inserted()
{
HTMLElement::inserted();

View File

@ -28,6 +28,7 @@ public:
virtual ~HTMLLinkElement() override;
virtual void inserted() override;
virtual void removed_from(Node* old_parent) override;
String rel() const { return get_attribute_value(HTML::AttributeNames::rel); }
String type() const { return get_attribute_value(HTML::AttributeNames::type); }