LibWeb: Add actual document loading for the CSS (at)import rule

This commit is contained in:
Sviatoslav Peleshko 2021-02-21 18:44:17 +02:00 committed by Andreas Kling
parent 54617e1a91
commit 183ebaee91
11 changed files with 229 additions and 56 deletions

View file

@ -78,6 +78,10 @@
#cmakedefine01 CSOCKET_DEBUG
#endif
#ifndef CSS_LOADER_DEBUG
#cmakedefine01 CSS_LOADER_DEBUG
#endif
#ifndef CURSOR_TOOL_DEBUG
#cmakedefine01 CURSOR_TOOL_DEBUG
#endif

View file

@ -45,6 +45,7 @@ set(FILE_CONTENT_DEBUG ON)
set(GZIP_DEBUG ON)
set(CNETWORKJOB_DEBUG ON)
set(CSOCKET_DEBUG ON)
set(CSS_LOADER_DEBUG ON)
set(SAFE_SYSCALL_DEBUG ON)
set(GHASH_PROCESS_DEBUG ON)
set(NT_DEBUG ON)

View file

@ -176,6 +176,7 @@ set(SOURCES
Layout/TreeBuilder.cpp
LayoutTreeModel.cpp
Loader/ContentFilter.cpp
Loader/CSSLoader.cpp
Loader/FrameLoader.cpp
Loader/ImageLoader.cpp
Loader/ImageResource.cpp

View file

@ -45,6 +45,11 @@ public:
const URL& url() const { return m_url; }
bool has_import_result() const { return !m_style_sheet.is_null(); }
RefPtr<StyleSheet> loaded_style_sheet() { return m_style_sheet; }
const RefPtr<StyleSheet> loaded_style_sheet() const { return m_style_sheet; }
void set_style_sheet(const RefPtr<StyleSheet>& style_sheet) { m_style_sheet = style_sheet; }
virtual StringView class_name() const { return "CSSImportRule"; };
virtual Type type() const { return Type::Import; };
@ -52,6 +57,7 @@ private:
CSSImportRule(URL);
URL m_url;
RefPtr<StyleSheet> m_style_sheet;
};
}

View file

@ -29,6 +29,7 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/Loader/Resource.h>
@ -52,9 +53,32 @@ public:
for (auto& rule : m_rules)
if (rule.type() == CSSRule::Type::Style) {
callback(downcast<StyleRule>(rule));
} else if (rule.type() == CSSRule::Type::Import) {
const CSSImportRule& import_rule = downcast<CSSImportRule>(rule);
if (import_rule.has_import_result())
import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
}
}
template<typename Callback>
bool for_first_not_loaded_import_rule(Callback callback)
{
for (auto& rule : m_rules)
if (rule.type() == CSSRule::Type::Import) {
CSSImportRule& import_rule = downcast<CSSImportRule>(rule);
if (!import_rule.has_import_result()) {
callback(import_rule);
return true;
}
if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
return true;
}
}
return false;
}
private:
explicit StyleSheet(NonnullRefPtrVector<CSSRule>&&);

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -35,7 +36,11 @@ namespace Web::HTML {
HTMLLinkElement::HTMLLinkElement(DOM::Document& document, QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
, m_css_loader(document)
{
m_css_loader.on_load = [&] {
document.update_style();
};
}
HTMLLinkElement::~HTMLLinkElement()
@ -46,44 +51,10 @@ void HTMLLinkElement::inserted_into(Node& node)
{
HTMLElement::inserted_into(node);
if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate))
load_stylesheet(document().complete_url(href()));
}
void HTMLLinkElement::resource_did_fail()
{
}
void HTMLLinkElement::resource_did_load()
{
VERIFY(resource());
if (!resource()->has_encoded_data())
return;
dbgln("HTMLLinkElement: Resource did load, looks good! {}", href());
auto sheet = parse_css(CSS::ParsingContext(document()), resource()->encoded_data());
if (!sheet) {
dbgln("HTMLLinkElement: Failed to parse stylesheet: {}", href());
return;
if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) {
m_css_loader.load_from_url(document().complete_url(href()));
document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull());
}
// Transfer the rules from the successfully parsed sheet into the sheet we've already inserted.
m_style_sheet->rules() = sheet->rules();
document().update_style();
}
void HTMLLinkElement::load_stylesheet(const URL& url)
{
// First insert an empty style sheet in the document sheet list.
// There's probably a nicer way to do this, but this ensures that sheets are in document order.
m_style_sheet = CSS::StyleSheet::create({});
document().style_sheets().add_sheet(*m_style_sheet);
LoadRequest request;
request.set_url(url);
set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
}
void HTMLLinkElement::parse_attribute(const FlyString& name, const String& value)

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,13 +28,11 @@
#pragma once
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/Loader/Resource.h>
#include <LibWeb/Loader/CSSLoader.h>
namespace Web::HTML {
class HTMLLinkElement final
: public HTMLElement
, public ResourceClient {
class HTMLLinkElement final : public HTMLElement {
public:
using WrapperType = Bindings::HTMLLinkElementWrapper;
@ -47,14 +46,8 @@ public:
String href() const { return attribute(HTML::AttributeNames::href); }
private:
// ^ResourceClient
virtual void resource_did_fail() override;
virtual void resource_did_load() override;
void parse_attribute(const FlyString&, const String&) override;
void load_stylesheet(const URL&);
struct Relationship {
enum {
Alternate = 1 << 0,
@ -63,7 +56,7 @@ private:
};
unsigned m_relationship { 0 };
RefPtr<CSS::StyleSheet> m_style_sheet;
CSSLoader m_css_loader;
};
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -25,7 +26,6 @@
*/
#include <AK/StringBuilder.h>
#include <LibWeb/CSS/Parser/CSSParser.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLStyleElement.h>
@ -34,7 +34,11 @@ namespace Web::HTML {
HTMLStyleElement::HTMLStyleElement(DOM::Document& document, QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
, m_css_loader(document)
{
m_css_loader.on_load = [&] {
document.update_style();
};
}
HTMLStyleElement::~HTMLStyleElement()
@ -48,17 +52,15 @@ void HTMLStyleElement::children_changed()
if (is<DOM::Text>(child))
builder.append(downcast<DOM::Text>(child).text_content());
});
m_stylesheet = parse_css(CSS::ParsingContext(document()), builder.to_string());
if (m_stylesheet)
document().style_sheets().add_sheet(*m_stylesheet);
else
document().style_sheets().add_sheet(CSS::StyleSheet::create({}));
m_css_loader.load_from_text(builder.to_string());
document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull());
HTMLElement::children_changed();
}
void HTMLStyleElement::removed_from(Node& old_parent)
{
if (m_stylesheet) {
if (m_css_loader.style_sheet()) {
// FIXME: Remove the sheet from the document
}
return HTMLElement::removed_from(old_parent);

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,6 +28,7 @@
#pragma once
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/Loader/CSSLoader.h>
namespace Web::HTML {
@ -41,7 +43,7 @@ public:
virtual void removed_from(Node&) override;
private:
RefPtr<CSS::StyleSheet> m_stylesheet;
CSSLoader m_css_loader;
};
}

View file

@ -0,0 +1,111 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Debug.h>
#include <AK/URL.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/Parser/CSSParser.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/Loader/CSSLoader.h>
#include <LibWeb/Loader/ResourceLoader.h>
namespace Web {
CSSLoader::CSSLoader(DOM::Document& document)
: m_document(&document)
{
}
void CSSLoader::load_from_text(const String& text)
{
m_style_sheet = parse_css(CSS::ParsingContext(*m_document), text);
if (!m_style_sheet)
m_style_sheet = CSS::StyleSheet::create({});
load_next_import_if_needed();
}
void CSSLoader::load_from_url(const URL& url)
{
m_style_sheet = CSS::StyleSheet::create({});
LoadRequest request;
request.set_url(url);
set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
}
void CSSLoader::resource_did_load()
{
VERIFY(resource());
if (!resource()->has_encoded_data()) {
dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, no encoded data. URL: {}", resource()->url());
} else {
dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, has encoded data. URL: {}", resource()->url());
}
auto sheet = parse_css(CSS::ParsingContext(*m_document), resource()->encoded_data());
if (!sheet) {
dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Failed to parse stylesheet: {}", resource()->url());
return;
}
bool was_imported = m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) {
rule.set_style_sheet(sheet);
});
// Transfer the rules from the successfully parsed sheet into the sheet we've already inserted.
if (!was_imported) {
m_style_sheet->rules() = sheet->rules();
}
if (on_load)
on_load();
load_next_import_if_needed();
}
void CSSLoader::resource_did_fail()
{
dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did fail. URL: {}", resource()->url());
load_next_import_if_needed();
}
void CSSLoader::load_next_import_if_needed()
{
// Create load request for the first import which isn't loaded.
// TODO: We need to somehow handle infinite cycles in imports.
m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) {
dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Loading @import {}", rule.url());
LoadRequest request;
request.set_url(rule.url());
set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
});
}
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Function.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/Loader/Resource.h>
namespace Web {
class CSSLoader : public ResourceClient {
public:
CSSLoader(DOM::Document& document);
void load_from_text(const String&);
void load_from_url(const URL&);
void load_next_import_if_needed();
RefPtr<CSS::StyleSheet> style_sheet() const { return m_style_sheet; };
Function<void()> on_load;
Function<void()> on_fail;
private:
// ^ResourceClient
virtual void resource_did_load() override;
virtual void resource_did_fail() override;
RefPtr<CSS::StyleSheet> m_style_sheet;
const DOM::Document* m_document;
};
}