mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-23 04:55:15 +00:00
LibWeb: Port HTMLLinkElement to the ResourceClient interface
This commit is contained in:
parent
b32761f2e0
commit
7197adbd55
Notes:
sideshowbarker
2024-07-19 05:53:36 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/7197adbd551
2 changed files with 44 additions and 18 deletions
|
@ -29,8 +29,8 @@
|
|||
#include <LibCore/File.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/HTMLLinkElement.h>
|
||||
#include <LibWeb/Parser/CSSParser.h>
|
||||
#include <LibWeb/Loader/ResourceLoader.h>
|
||||
#include <LibWeb/Parser/CSSParser.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
|
@ -43,24 +43,40 @@ HTMLLinkElement::~HTMLLinkElement()
|
|||
{
|
||||
}
|
||||
|
||||
void HTMLLinkElement::inserted_into(Node&)
|
||||
void HTMLLinkElement::inserted_into(Node& node)
|
||||
{
|
||||
if (rel() == "stylesheet") {
|
||||
URL url = document().complete_url(href());
|
||||
ResourceLoader::the().load(url, [&](auto data, auto&) {
|
||||
if (data.is_null()) {
|
||||
dbg() << "HTMLLinkElement: Failed to load stylesheet: " << href();
|
||||
return;
|
||||
}
|
||||
auto sheet = parse_css(data);
|
||||
if (!sheet) {
|
||||
dbg() << "HTMLLinkElement: Failed to parse stylesheet: " << href();
|
||||
return;
|
||||
}
|
||||
document().add_sheet(*sheet);
|
||||
document().update_style();
|
||||
});
|
||||
HTMLElement::inserted_into(node);
|
||||
|
||||
if (rel() == "stylesheet")
|
||||
load_stylesheet(document().complete_url(href()));
|
||||
}
|
||||
|
||||
void HTMLLinkElement::resource_did_fail()
|
||||
{
|
||||
}
|
||||
|
||||
void HTMLLinkElement::resource_did_load()
|
||||
{
|
||||
ASSERT(resource());
|
||||
if (!resource()->has_encoded_data())
|
||||
return;
|
||||
|
||||
dbg() << "HTMLLinkElement: Resource did load, looks good! " << href();
|
||||
|
||||
auto sheet = parse_css(resource()->encoded_data());
|
||||
if (!sheet) {
|
||||
dbg() << "HTMLLinkElement: Failed to parse stylesheet: " << href();
|
||||
return;
|
||||
}
|
||||
document().add_sheet(*sheet);
|
||||
document().update_style();
|
||||
}
|
||||
|
||||
void HTMLLinkElement::load_stylesheet(const URL& url)
|
||||
{
|
||||
LoadRequest request;
|
||||
request.set_url(url);
|
||||
set_resource(ResourceLoader::the().load_resource(request));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,10 +27,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibWeb/DOM/HTMLElement.h>
|
||||
#include <LibWeb/Loader/Resource.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
class HTMLLinkElement final : public HTMLElement {
|
||||
class HTMLLinkElement final
|
||||
: public HTMLElement
|
||||
, public ResourceClient {
|
||||
public:
|
||||
HTMLLinkElement(Document&, const FlyString& tag_name);
|
||||
virtual ~HTMLLinkElement() override;
|
||||
|
@ -40,6 +43,13 @@ public:
|
|||
String rel() const { return attribute("rel"); }
|
||||
String type() const { return attribute("type"); }
|
||||
String href() const { return attribute("href"); }
|
||||
|
||||
private:
|
||||
// ^ResourceClient
|
||||
virtual void resource_did_fail() override;
|
||||
virtual void resource_did_load() override;
|
||||
|
||||
void load_stylesheet(const URL&);
|
||||
};
|
||||
|
||||
template<>
|
||||
|
|
Loading…
Add table
Reference in a new issue