mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 06:37:40 +00:00
LibWeb: Port HTMLLinkElement to the ResourceClient interface
This commit is contained in:
parent
b32761f2e0
commit
7197adbd55
2 changed files with 44 additions and 18 deletions
|
@ -29,8 +29,8 @@
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibWeb/DOM/Document.h>
|
#include <LibWeb/DOM/Document.h>
|
||||||
#include <LibWeb/DOM/HTMLLinkElement.h>
|
#include <LibWeb/DOM/HTMLLinkElement.h>
|
||||||
#include <LibWeb/Parser/CSSParser.h>
|
|
||||||
#include <LibWeb/Loader/ResourceLoader.h>
|
#include <LibWeb/Loader/ResourceLoader.h>
|
||||||
|
#include <LibWeb/Parser/CSSParser.h>
|
||||||
|
|
||||||
namespace Web {
|
namespace Web {
|
||||||
|
|
||||||
|
@ -43,24 +43,40 @@ HTMLLinkElement::~HTMLLinkElement()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTMLLinkElement::inserted_into(Node&)
|
void HTMLLinkElement::inserted_into(Node& node)
|
||||||
{
|
{
|
||||||
if (rel() == "stylesheet") {
|
HTMLElement::inserted_into(node);
|
||||||
URL url = document().complete_url(href());
|
|
||||||
ResourceLoader::the().load(url, [&](auto data, auto&) {
|
if (rel() == "stylesheet")
|
||||||
if (data.is_null()) {
|
load_stylesheet(document().complete_url(href()));
|
||||||
dbg() << "HTMLLinkElement: Failed to load stylesheet: " << href();
|
}
|
||||||
return;
|
|
||||||
}
|
void HTMLLinkElement::resource_did_fail()
|
||||||
auto sheet = parse_css(data);
|
{
|
||||||
if (!sheet) {
|
}
|
||||||
dbg() << "HTMLLinkElement: Failed to parse stylesheet: " << href();
|
|
||||||
return;
|
void HTMLLinkElement::resource_did_load()
|
||||||
}
|
{
|
||||||
document().add_sheet(*sheet);
|
ASSERT(resource());
|
||||||
document().update_style();
|
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
|
#pragma once
|
||||||
|
|
||||||
#include <LibWeb/DOM/HTMLElement.h>
|
#include <LibWeb/DOM/HTMLElement.h>
|
||||||
|
#include <LibWeb/Loader/Resource.h>
|
||||||
|
|
||||||
namespace Web {
|
namespace Web {
|
||||||
|
|
||||||
class HTMLLinkElement final : public HTMLElement {
|
class HTMLLinkElement final
|
||||||
|
: public HTMLElement
|
||||||
|
, public ResourceClient {
|
||||||
public:
|
public:
|
||||||
HTMLLinkElement(Document&, const FlyString& tag_name);
|
HTMLLinkElement(Document&, const FlyString& tag_name);
|
||||||
virtual ~HTMLLinkElement() override;
|
virtual ~HTMLLinkElement() override;
|
||||||
|
@ -40,6 +43,13 @@ public:
|
||||||
String rel() const { return attribute("rel"); }
|
String rel() const { return attribute("rel"); }
|
||||||
String type() const { return attribute("type"); }
|
String type() const { return attribute("type"); }
|
||||||
String href() const { return attribute("href"); }
|
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<>
|
template<>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue