mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:27:43 +00:00
LibWeb: Add initial implementation of document.implementation
This commit is contained in:
parent
3ec54448f5
commit
dcb21b0c3a
9 changed files with 191 additions and 6 deletions
74
Libraries/LibWeb/DOM/DOMImplementation.cpp
Normal file
74
Libraries/LibWeb/DOM/DOMImplementation.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2020, 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 <LibWeb/DOM/DOMImplementation.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/DocumentType.h>
|
||||
#include <LibWeb/DOM/ElementFactory.h>
|
||||
#include <LibWeb/DOM/Text.h>
|
||||
#include <LibWeb/Namespace.h>
|
||||
#include <LibWeb/Origin.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
DOMImplementation::DOMImplementation(Document& document)
|
||||
: m_document(document)
|
||||
{
|
||||
}
|
||||
|
||||
const NonnullRefPtr<Document> DOMImplementation::create_htmldocument(const String& title) const
|
||||
{
|
||||
auto html_document = Document::create();
|
||||
|
||||
html_document->set_content_type("text/html");
|
||||
|
||||
auto doctype = adopt(*new DocumentType(html_document));
|
||||
doctype->set_name("html");
|
||||
html_document->append_child(doctype);
|
||||
|
||||
auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML);
|
||||
html_document->append_child(html_element);
|
||||
|
||||
auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML);
|
||||
html_element->append_child(head_element);
|
||||
|
||||
if (!title.is_null()) {
|
||||
auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML);
|
||||
head_element->append_child(title_element);
|
||||
|
||||
auto text_node = adopt(*new Text(html_document, title));
|
||||
title_element->append_child(text_node);
|
||||
}
|
||||
|
||||
auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
|
||||
html_element->append_child(body_element);
|
||||
|
||||
html_document->set_origin(m_document.origin());
|
||||
|
||||
return html_document;
|
||||
}
|
||||
|
||||
}
|
60
Libraries/LibWeb/DOM/DOMImplementation.h
Normal file
60
Libraries/LibWeb/DOM/DOMImplementation.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2020, 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/Weakable.h>
|
||||
#include <LibWeb/Bindings/Wrappable.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
class DOMImplementation final
|
||||
: public RefCounted<DOMImplementation>
|
||||
, public Weakable<DOMImplementation>
|
||||
, public Bindings::Wrappable {
|
||||
public:
|
||||
using WrapperType = Bindings::DOMImplementationWrapper;
|
||||
|
||||
static NonnullRefPtr<DOMImplementation> create(Document& document)
|
||||
{
|
||||
return adopt(*new DOMImplementation(document));
|
||||
}
|
||||
|
||||
// FIXME: snake_case in WrapperGenerator turns "createHTMLDocument" into "create_htmldocument"
|
||||
const NonnullRefPtr<Document> create_htmldocument(const String& title) const;
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
|
||||
bool has_feature() const { return true; }
|
||||
|
||||
private:
|
||||
explicit DOMImplementation(Document&);
|
||||
|
||||
Document& m_document;
|
||||
};
|
||||
|
||||
}
|
7
Libraries/LibWeb/DOM/DOMImplementation.idl
Normal file
7
Libraries/LibWeb/DOM/DOMImplementation.idl
Normal file
|
@ -0,0 +1,7 @@
|
|||
interface DOMImplementation {
|
||||
|
||||
Document createHTMLDocument(optional DOMString title);
|
||||
|
||||
boolean hasFeature();
|
||||
|
||||
}
|
|
@ -68,6 +68,7 @@ Document::Document(const URL& url)
|
|||
, m_style_sheets(CSS::StyleSheetList::create(*this))
|
||||
, m_url(url)
|
||||
, m_window(Window::create_with_document(*this))
|
||||
, m_implementation(DOMImplementation::create(*this))
|
||||
{
|
||||
m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
|
||||
update_style();
|
||||
|
@ -136,6 +137,13 @@ Origin Document::origin() const
|
|||
return { m_url.protocol(), m_url.host(), m_url.port() };
|
||||
}
|
||||
|
||||
void Document::set_origin(const Origin& origin)
|
||||
{
|
||||
m_url.set_protocol(origin.protocol());
|
||||
m_url.set_host(origin.host());
|
||||
m_url.set_port(origin.port());
|
||||
}
|
||||
|
||||
void Document::schedule_style_update()
|
||||
{
|
||||
if (m_style_update_timer->is_active())
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
#include <LibWeb/CSS/StyleResolver.h>
|
||||
#include <LibWeb/CSS/StyleSheet.h>
|
||||
#include <LibWeb/CSS/StyleSheetList.h>
|
||||
#include <LibWeb/DOM/DOMImplementation.h>
|
||||
#include <LibWeb/DOM/NonElementParentNode.h>
|
||||
#include <LibWeb/DOM/ParentNode.h>
|
||||
|
||||
|
@ -64,6 +65,7 @@ public:
|
|||
URL url() const { return m_url; }
|
||||
|
||||
Origin origin() const;
|
||||
void set_origin(const Origin& origin);
|
||||
|
||||
bool is_scripting_enabled() const { return true; }
|
||||
|
||||
|
@ -197,6 +199,8 @@ public:
|
|||
const String& content_type() const { return m_content_type; }
|
||||
void set_content_type(const String& content_type) { m_content_type = content_type; }
|
||||
|
||||
const NonnullRefPtr<DOMImplementation> implementation() { return m_implementation; }
|
||||
|
||||
private:
|
||||
explicit Document(const URL&);
|
||||
|
||||
|
@ -258,6 +262,8 @@ private:
|
|||
|
||||
String m_ready_state { "loading" };
|
||||
String m_content_type;
|
||||
|
||||
NonnullRefPtr<DOMImplementation> m_implementation;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
interface Document : Node {
|
||||
|
||||
readonly attribute DOMImplementation implementation;
|
||||
|
||||
readonly attribute DOMString contentType;
|
||||
|
||||
Element? getElementById(DOMString id);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue