2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-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.
|
|
|
|
*/
|
|
|
|
|
2019-10-05 10:16:27 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/Timer.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Application.h>
|
2020-03-22 21:15:49 +01:00
|
|
|
#include <LibGUI/DisplayLink.h>
|
2020-03-14 12:41:51 +01:00
|
|
|
#include <LibGUI/MessageBox.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
2020-04-04 22:12:37 +02:00
|
|
|
#include <LibJS/Parser.h>
|
2020-03-21 18:55:37 +01:00
|
|
|
#include <LibJS/Runtime/Function.h>
|
2020-03-14 13:15:11 +01:00
|
|
|
#include <LibWeb/Bindings/DocumentWrapper.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
#include <LibWeb/Bindings/WindowObject.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/CSS/StyleResolver.h>
|
2020-08-17 19:14:30 +01:00
|
|
|
#include <LibWeb/DOM/Comment.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-08-17 19:14:30 +01:00
|
|
|
#include <LibWeb/DOM/DocumentFragment.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/DOM/DocumentType.h>
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
|
|
|
#include <LibWeb/DOM/ElementFactory.h>
|
2020-08-31 13:56:16 +01:00
|
|
|
#include <LibWeb/DOM/Event.h>
|
2020-07-28 19:20:11 +02:00
|
|
|
#include <LibWeb/DOM/Text.h>
|
|
|
|
#include <LibWeb/DOM/Window.h>
|
2020-08-12 14:46:53 +02:00
|
|
|
#include <LibWeb/HTML/AttributeNames.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
#include <LibWeb/HTML/HTMLBodyElement.h>
|
2020-08-17 19:14:30 +01:00
|
|
|
#include <LibWeb/HTML/HTMLFrameSetElement.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
#include <LibWeb/HTML/HTMLHeadElement.h>
|
|
|
|
#include <LibWeb/HTML/HTMLHtmlElement.h>
|
|
|
|
#include <LibWeb/HTML/HTMLScriptElement.h>
|
|
|
|
#include <LibWeb/HTML/HTMLTitleElement.h>
|
2020-09-18 09:49:51 +02:00
|
|
|
#include <LibWeb/InProcessWebView.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/Layout/LayoutDocument.h>
|
|
|
|
#include <LibWeb/Layout/LayoutTreeBuilder.h>
|
2020-04-07 22:56:13 +02:00
|
|
|
#include <LibWeb/Origin.h>
|
2020-07-28 19:27:41 +02:00
|
|
|
#include <LibWeb/Page/Frame.h>
|
2020-07-23 09:44:42 -07:00
|
|
|
#include <LibWeb/SVG/TagNames.h>
|
2019-06-15 22:49:44 +02:00
|
|
|
#include <stdio.h>
|
2019-06-15 18:55:47 +02:00
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
namespace Web::DOM {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2020-04-07 22:56:13 +02:00
|
|
|
Document::Document(const URL& url)
|
2019-09-29 11:43:07 +02:00
|
|
|
: ParentNode(*this, NodeType::DOCUMENT_NODE)
|
2020-07-26 20:01:35 +02:00
|
|
|
, m_style_resolver(make<CSS::StyleResolver>(*this))
|
2020-06-04 16:06:32 +02:00
|
|
|
, m_style_sheets(CSS::StyleSheetList::create(*this))
|
2020-04-07 22:56:13 +02:00
|
|
|
, m_url(url)
|
2020-04-01 18:53:28 +02:00
|
|
|
, m_window(Window::create_with_document(*this))
|
2019-06-15 18:55:47 +02:00
|
|
|
{
|
2020-04-07 22:15:22 +02:00
|
|
|
m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
|
2019-10-19 18:57:02 +02:00
|
|
|
update_style();
|
2020-04-07 22:15:22 +02:00
|
|
|
});
|
2019-06-15 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Document::~Document()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:56:13 +02:00
|
|
|
Origin Document::origin() const
|
|
|
|
{
|
|
|
|
if (!m_url.is_valid())
|
|
|
|
return {};
|
|
|
|
return { m_url.protocol(), m_url.host(), m_url.port() };
|
|
|
|
}
|
|
|
|
|
2019-10-19 18:57:02 +02:00
|
|
|
void Document::schedule_style_update()
|
|
|
|
{
|
|
|
|
if (m_style_update_timer->is_active())
|
|
|
|
return;
|
|
|
|
m_style_update_timer->start();
|
|
|
|
}
|
|
|
|
|
2019-10-12 23:26:47 +02:00
|
|
|
bool Document::is_child_allowed(const Node& node) const
|
|
|
|
{
|
|
|
|
switch (node.type()) {
|
|
|
|
case NodeType::DOCUMENT_NODE:
|
|
|
|
case NodeType::TEXT_NODE:
|
|
|
|
return false;
|
|
|
|
case NodeType::COMMENT_NODE:
|
|
|
|
return true;
|
|
|
|
case NodeType::DOCUMENT_TYPE_NODE:
|
|
|
|
return !first_child_of_type<DocumentType>();
|
|
|
|
case NodeType::ELEMENT_NODE:
|
|
|
|
return !first_child_of_type<Element>();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:30:18 +02:00
|
|
|
const Element* Document::document_element() const
|
2019-09-29 16:24:57 +02:00
|
|
|
{
|
2020-08-03 13:30:18 +02:00
|
|
|
return first_child_of_type<Element>();
|
2019-09-29 16:24:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 21:23:40 +01:00
|
|
|
const HTML::HTMLHtmlElement* Document::html_element() const
|
2019-09-29 16:24:57 +02:00
|
|
|
{
|
|
|
|
auto* html = document_element();
|
2020-08-03 21:23:40 +01:00
|
|
|
if (is<HTML::HTMLHtmlElement>(html))
|
|
|
|
return downcast<HTML::HTMLHtmlElement>(html);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HTML::HTMLHeadElement* Document::head() const
|
|
|
|
{
|
|
|
|
auto* html = html_element();
|
2019-09-29 16:24:57 +02:00
|
|
|
if (!html)
|
|
|
|
return nullptr;
|
2020-07-28 18:20:36 +02:00
|
|
|
return html->first_child_of_type<HTML::HTMLHeadElement>();
|
2019-09-29 16:24:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
const HTML::HTMLElement* Document::body() const
|
2019-10-04 21:05:52 +02:00
|
|
|
{
|
2020-08-03 21:23:40 +01:00
|
|
|
auto* html = html_element();
|
2019-10-04 21:05:52 +02:00
|
|
|
if (!html)
|
|
|
|
return nullptr;
|
2020-08-17 19:14:30 +01:00
|
|
|
auto* first_body = html->first_child_of_type<HTML::HTMLBodyElement>();
|
|
|
|
if (first_body)
|
|
|
|
return first_body;
|
|
|
|
auto* first_frameset = html->first_child_of_type<HTML::HTMLFrameSetElement>();
|
|
|
|
if (first_frameset)
|
|
|
|
return first_frameset;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::set_body(HTML::HTMLElement& new_body)
|
|
|
|
{
|
|
|
|
if (!is<HTML::HTMLBodyElement>(new_body) && !is<HTML::HTMLFrameSetElement>(new_body)) {
|
|
|
|
// FIXME: throw a "HierarchyRequestError" DOMException.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* existing_body = body();
|
|
|
|
if (existing_body) {
|
|
|
|
TODO();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* html = document_element();
|
|
|
|
if (!html) {
|
|
|
|
// FIXME: throw a "HierarchyRequestError" DOMException.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Implement this once there's a non-const first_child_of_type:
|
|
|
|
// "Otherwise, the body element is null, but there's a document element. Append the new value to the document element."
|
|
|
|
TODO();
|
2019-10-04 21:05:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 16:24:57 +02:00
|
|
|
String Document::title() const
|
|
|
|
{
|
|
|
|
auto* head_element = head();
|
|
|
|
if (!head_element)
|
|
|
|
return {};
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
auto* title_element = head_element->first_child_of_type<HTML::HTMLTitleElement>();
|
2019-09-29 16:24:57 +02:00
|
|
|
if (!title_element)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return title_element->text_content();
|
|
|
|
}
|
2019-10-04 15:50:04 +02:00
|
|
|
|
|
|
|
void Document::attach_to_frame(Badge<Frame>, Frame& frame)
|
|
|
|
{
|
|
|
|
m_frame = frame.make_weak_ptr();
|
2020-06-06 15:07:07 +02:00
|
|
|
for_each_in_subtree([&](auto& node) {
|
|
|
|
node.document_did_attach_to_frame(frame);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-10-13 12:34:25 +02:00
|
|
|
layout();
|
2019-10-04 15:50:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 15:07:07 +02:00
|
|
|
void Document::detach_from_frame(Badge<Frame>, Frame& frame)
|
2019-10-04 15:50:04 +02:00
|
|
|
{
|
2020-06-06 15:07:07 +02:00
|
|
|
for_each_in_subtree([&](auto& node) {
|
|
|
|
node.document_will_detach_from_frame(frame);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-10-13 12:34:25 +02:00
|
|
|
m_layout_root = nullptr;
|
2019-10-09 21:52:38 +02:00
|
|
|
m_frame = nullptr;
|
2019-10-04 15:50:04 +02:00
|
|
|
}
|
2019-10-04 21:05:52 +02:00
|
|
|
|
2020-01-05 00:09:35 +03:00
|
|
|
Color Document::background_color(const Palette& palette) const
|
2019-10-04 21:05:52 +02:00
|
|
|
{
|
2020-01-05 00:09:35 +03:00
|
|
|
auto default_color = palette.base();
|
2019-10-04 21:05:52 +02:00
|
|
|
auto* body_element = body();
|
|
|
|
if (!body_element)
|
2020-01-05 00:09:35 +03:00
|
|
|
return default_color;
|
2019-10-04 21:05:52 +02:00
|
|
|
|
|
|
|
auto* body_layout_node = body_element->layout_node();
|
|
|
|
if (!body_layout_node)
|
2020-01-05 00:09:35 +03:00
|
|
|
return default_color;
|
2019-10-04 21:05:52 +02:00
|
|
|
|
2020-06-24 13:51:14 +02:00
|
|
|
auto background_color = body_layout_node->specified_style().property(CSS::PropertyID::BackgroundColor);
|
2019-10-04 21:05:52 +02:00
|
|
|
if (!background_color.has_value() || !background_color.value()->is_color())
|
2020-01-05 00:09:35 +03:00
|
|
|
return default_color;
|
2019-10-04 21:05:52 +02:00
|
|
|
|
2019-10-06 10:11:54 +02:00
|
|
|
return background_color.value()->to_color(*this);
|
2019-10-04 21:05:52 +02:00
|
|
|
}
|
2019-10-05 10:16:27 +02:00
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
RefPtr<Gfx::Bitmap> Document::background_image() const
|
2019-10-19 11:49:46 +02:00
|
|
|
{
|
|
|
|
auto* body_element = body();
|
|
|
|
if (!body_element)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto* body_layout_node = body_element->layout_node();
|
|
|
|
if (!body_layout_node)
|
|
|
|
return {};
|
|
|
|
|
2020-06-24 13:51:14 +02:00
|
|
|
auto background_image = body_layout_node->specified_style().property(CSS::PropertyID::BackgroundImage);
|
2019-10-19 11:49:46 +02:00
|
|
|
if (!background_image.has_value() || !background_image.value()->is_image())
|
|
|
|
return {};
|
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
auto& image_value = static_cast<const CSS::ImageStyleValue&>(*background_image.value());
|
2019-10-19 11:49:46 +02:00
|
|
|
if (!image_value.bitmap())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return *image_value.bitmap();
|
|
|
|
}
|
|
|
|
|
2019-10-05 10:16:27 +02:00
|
|
|
URL Document::complete_url(const String& string) const
|
|
|
|
{
|
2019-11-18 22:04:39 +01:00
|
|
|
return m_url.complete_url(string);
|
2019-10-05 10:16:27 +02:00
|
|
|
}
|
2019-10-05 22:27:52 +02:00
|
|
|
|
2020-03-25 18:51:04 +01:00
|
|
|
void Document::invalidate_layout()
|
2019-10-28 20:51:45 +01:00
|
|
|
{
|
|
|
|
m_layout_root = nullptr;
|
2020-03-25 18:51:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Document::force_layout()
|
|
|
|
{
|
|
|
|
invalidate_layout();
|
2019-10-28 20:51:45 +01:00
|
|
|
layout();
|
|
|
|
}
|
|
|
|
|
2019-10-13 12:34:25 +02:00
|
|
|
void Document::layout()
|
|
|
|
{
|
2020-03-25 18:51:04 +01:00
|
|
|
if (!frame())
|
|
|
|
return;
|
|
|
|
|
2019-10-15 12:22:41 +02:00
|
|
|
if (!m_layout_root) {
|
|
|
|
LayoutTreeBuilder tree_builder;
|
2020-04-05 11:11:07 +02:00
|
|
|
m_layout_root = static_ptr_cast<LayoutDocument>(tree_builder.build(*this));
|
2019-10-15 12:22:41 +02:00
|
|
|
}
|
2019-10-13 12:34:25 +02:00
|
|
|
m_layout_root->layout();
|
2019-10-28 20:51:45 +01:00
|
|
|
m_layout_root->set_needs_display();
|
2020-06-23 18:02:08 +02:00
|
|
|
|
2020-07-07 17:58:48 +02:00
|
|
|
if (frame()->is_main_frame())
|
|
|
|
frame()->page().client().page_did_layout();
|
2019-10-13 12:34:25 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 17:56:19 +02:00
|
|
|
void Document::update_style()
|
2019-10-13 12:49:43 +02:00
|
|
|
{
|
2019-12-18 21:34:03 +01:00
|
|
|
for_each_in_subtree_of_type<Element>([&](auto& element) {
|
|
|
|
if (element.needs_style_update())
|
|
|
|
element.recompute_style();
|
2019-10-21 12:01:30 +02:00
|
|
|
return IterationDecision::Continue;
|
2019-10-19 18:57:02 +02:00
|
|
|
});
|
2019-10-14 17:56:19 +02:00
|
|
|
update_layout();
|
2019-10-13 12:49:43 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 17:56:19 +02:00
|
|
|
void Document::update_layout()
|
2019-10-06 22:58:18 +11:00
|
|
|
{
|
2019-11-30 11:57:41 +01:00
|
|
|
if (!frame())
|
|
|
|
return;
|
|
|
|
|
2019-10-13 12:34:25 +02:00
|
|
|
layout();
|
2019-10-06 22:58:18 +11:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
RefPtr<LayoutNode> Document::create_layout_node(const CSS::StyleProperties*)
|
2019-10-05 22:27:52 +02:00
|
|
|
{
|
2020-07-26 20:01:35 +02:00
|
|
|
return adopt(*new LayoutDocument(*this, CSS::StyleProperties::create()));
|
2019-10-05 22:27:52 +02:00
|
|
|
}
|
2019-10-06 10:11:54 +02:00
|
|
|
|
|
|
|
void Document::set_link_color(Color color)
|
|
|
|
{
|
|
|
|
m_link_color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::set_active_link_color(Color color)
|
|
|
|
{
|
|
|
|
m_active_link_color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::set_visited_link_color(Color color)
|
|
|
|
{
|
|
|
|
m_visited_link_color = color;
|
|
|
|
}
|
2019-10-13 12:34:25 +02:00
|
|
|
|
|
|
|
const LayoutDocument* Document::layout_node() const
|
|
|
|
{
|
|
|
|
return static_cast<const LayoutDocument*>(Node::layout_node());
|
|
|
|
}
|
2019-10-14 17:54:17 +02:00
|
|
|
|
2019-11-04 19:37:52 +01:00
|
|
|
LayoutDocument* Document::layout_node()
|
|
|
|
{
|
|
|
|
return static_cast<LayoutDocument*>(Node::layout_node());
|
|
|
|
}
|
|
|
|
|
2019-11-09 11:58:50 +01:00
|
|
|
void Document::set_inspected_node(Node* node)
|
|
|
|
{
|
|
|
|
if (m_inspected_node == node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_inspected_node && m_inspected_node->layout_node())
|
|
|
|
m_inspected_node->layout_node()->set_needs_display();
|
|
|
|
|
|
|
|
m_inspected_node = node;
|
|
|
|
|
|
|
|
if (m_inspected_node && m_inspected_node->layout_node())
|
|
|
|
m_inspected_node->layout_node()->set_needs_display();
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:54:17 +02:00
|
|
|
void Document::set_hovered_node(Node* node)
|
|
|
|
{
|
|
|
|
if (m_hovered_node == node)
|
|
|
|
return;
|
|
|
|
|
2019-10-14 18:32:02 +02:00
|
|
|
RefPtr<Node> old_hovered_node = move(m_hovered_node);
|
2019-10-14 17:54:17 +02:00
|
|
|
m_hovered_node = node;
|
2019-10-14 18:32:02 +02:00
|
|
|
|
2019-10-19 18:57:02 +02:00
|
|
|
invalidate_style();
|
2019-10-14 17:54:17 +02:00
|
|
|
}
|
2019-10-21 12:01:30 +02:00
|
|
|
|
2020-10-07 12:47:17 +02:00
|
|
|
NonnullRefPtrVector<Element> Document::get_elements_by_name(const String& name) const
|
2019-10-21 12:01:30 +02:00
|
|
|
{
|
2020-10-07 12:47:17 +02:00
|
|
|
NonnullRefPtrVector<Element> elements;
|
2019-12-18 21:34:03 +01:00
|
|
|
for_each_in_subtree_of_type<Element>([&](auto& element) {
|
2020-06-03 20:51:28 +02:00
|
|
|
if (element.attribute(HTML::AttributeNames::name) == name)
|
2020-10-07 12:47:17 +02:00
|
|
|
elements.append(element);
|
2019-10-21 12:01:30 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return elements;
|
|
|
|
}
|
2020-01-13 20:33:15 +01:00
|
|
|
|
2020-07-23 18:18:13 +02:00
|
|
|
NonnullRefPtrVector<Element> Document::get_elements_by_tag_name(const FlyString& tag_name) const
|
2020-06-25 22:23:33 +02:00
|
|
|
{
|
|
|
|
NonnullRefPtrVector<Element> elements;
|
|
|
|
for_each_in_subtree_of_type<Element>([&](auto& element) {
|
2020-07-23 18:18:13 +02:00
|
|
|
if (element.local_name() == tag_name)
|
2020-06-25 22:23:33 +02:00
|
|
|
elements.append(element);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
|
2020-01-13 20:33:15 +01:00
|
|
|
Color Document::link_color() const
|
|
|
|
{
|
|
|
|
if (m_link_color.has_value())
|
|
|
|
return m_link_color.value();
|
|
|
|
if (!frame())
|
|
|
|
return Color::Blue;
|
2020-06-08 20:31:49 +02:00
|
|
|
return frame()->page().palette().link();
|
2020-01-13 20:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Color Document::active_link_color() const
|
|
|
|
{
|
|
|
|
if (m_active_link_color.has_value())
|
|
|
|
return m_active_link_color.value();
|
|
|
|
if (!frame())
|
|
|
|
return Color::Red;
|
2020-06-08 20:31:49 +02:00
|
|
|
return frame()->page().palette().active_link();
|
2020-01-13 20:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Color Document::visited_link_color() const
|
|
|
|
{
|
|
|
|
if (m_visited_link_color.has_value())
|
|
|
|
return m_visited_link_color.value();
|
|
|
|
if (!frame())
|
|
|
|
return Color::Magenta;
|
2020-06-08 20:31:49 +02:00
|
|
|
return frame()->page().palette().visited_link();
|
2020-01-13 20:33:15 +01:00
|
|
|
}
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2020-09-20 19:24:44 +02:00
|
|
|
static JS::VM& main_thread_vm()
|
|
|
|
{
|
|
|
|
static RefPtr<JS::VM> vm;
|
|
|
|
if (!vm)
|
|
|
|
vm = JS::VM::create();
|
|
|
|
return *vm;
|
|
|
|
}
|
|
|
|
|
2020-03-14 12:41:51 +01:00
|
|
|
JS::Interpreter& Document::interpreter()
|
|
|
|
{
|
2020-04-01 21:04:51 +02:00
|
|
|
if (!m_interpreter)
|
2020-09-20 19:24:44 +02:00
|
|
|
m_interpreter = JS::Interpreter::create<Bindings::WindowObject>(main_thread_vm(), *m_window);
|
2020-03-14 12:41:51 +01:00
|
|
|
return *m_interpreter;
|
|
|
|
}
|
|
|
|
|
2020-04-04 22:12:37 +02:00
|
|
|
JS::Value Document::run_javascript(const StringView& source)
|
|
|
|
{
|
2020-04-13 02:05:21 +02:00
|
|
|
auto parser = JS::Parser(JS::Lexer(source));
|
|
|
|
auto program = parser.parse_program();
|
|
|
|
if (parser.has_errors()) {
|
2020-05-14 16:26:01 +01:00
|
|
|
parser.print_errors();
|
2020-04-13 02:05:21 +02:00
|
|
|
return JS::js_undefined();
|
|
|
|
}
|
2020-08-11 17:42:18 +02:00
|
|
|
auto& interpreter = document().interpreter();
|
|
|
|
auto result = interpreter.run(interpreter.global_object(), *program);
|
|
|
|
if (interpreter.exception())
|
2020-09-21 15:28:09 +02:00
|
|
|
interpreter.vm().clear_exception();
|
2020-08-11 17:42:18 +02:00
|
|
|
return result;
|
2020-04-04 22:12:37 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 20:43:54 +02:00
|
|
|
NonnullRefPtr<Element> Document::create_element(const String& tag_name)
|
|
|
|
{
|
2020-07-26 19:37:56 +02:00
|
|
|
return DOM::create_element(*this, tag_name);
|
2020-05-10 20:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 19:14:30 +01:00
|
|
|
NonnullRefPtr<DocumentFragment> Document::create_document_fragment()
|
|
|
|
{
|
|
|
|
return adopt(*new DocumentFragment(*this));
|
|
|
|
}
|
|
|
|
|
2020-05-10 20:43:54 +02:00
|
|
|
NonnullRefPtr<Text> Document::create_text_node(const String& data)
|
|
|
|
{
|
|
|
|
return adopt(*new Text(*this, data));
|
2020-08-17 19:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<Comment> Document::create_comment(const String& data)
|
|
|
|
{
|
|
|
|
return adopt(*new Comment(*this, data));
|
2020-05-10 20:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
void Document::set_pending_parsing_blocking_script(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement* script)
|
2020-05-24 22:00:46 +02:00
|
|
|
{
|
|
|
|
m_pending_parsing_blocking_script = script;
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
NonnullRefPtr<HTML::HTMLScriptElement> Document::take_pending_parsing_blocking_script(Badge<HTML::HTMLDocumentParser>)
|
2020-05-27 23:01:04 +02:00
|
|
|
{
|
|
|
|
return m_pending_parsing_blocking_script.release_nonnull();
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
void Document::add_script_to_execute_when_parsing_has_finished(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement& script)
|
2020-05-30 12:26:15 +02:00
|
|
|
{
|
|
|
|
m_scripts_to_execute_when_parsing_has_finished.append(script);
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
NonnullRefPtrVector<HTML::HTMLScriptElement> Document::take_scripts_to_execute_when_parsing_has_finished(Badge<HTML::HTMLDocumentParser>)
|
2020-05-30 12:26:15 +02:00
|
|
|
{
|
|
|
|
return move(m_scripts_to_execute_when_parsing_has_finished);
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
void Document::add_script_to_execute_as_soon_as_possible(Badge<HTML::HTMLScriptElement>, HTML::HTMLScriptElement& script)
|
2020-05-30 12:26:15 +02:00
|
|
|
{
|
|
|
|
m_scripts_to_execute_as_soon_as_possible.append(script);
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
NonnullRefPtrVector<HTML::HTMLScriptElement> Document::take_scripts_to_execute_as_soon_as_possible(Badge<HTML::HTMLDocumentParser>)
|
2020-05-30 12:26:15 +02:00
|
|
|
{
|
|
|
|
return move(m_scripts_to_execute_as_soon_as_possible);
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:42:08 +02:00
|
|
|
void Document::adopt_node(Node& subtree_root)
|
|
|
|
{
|
|
|
|
subtree_root.for_each_in_subtree([&](auto& node) {
|
|
|
|
node.set_document({}, *this);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-18 21:17:17 +01:00
|
|
|
const DocumentType* Document::doctype() const
|
|
|
|
{
|
|
|
|
return first_child_of_type<DocumentType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
const String& Document::compat_mode() const
|
|
|
|
{
|
|
|
|
static String back_compat = "BackCompat";
|
|
|
|
static String css1_compat = "CSS1Compat";
|
|
|
|
|
|
|
|
if (m_quirks_mode == QuirksMode::Yes)
|
|
|
|
return back_compat;
|
|
|
|
|
|
|
|
return css1_compat;
|
|
|
|
}
|
|
|
|
|
2020-08-02 16:15:15 +02:00
|
|
|
bool Document::is_editable() const
|
|
|
|
{
|
|
|
|
return m_editable;
|
|
|
|
}
|
|
|
|
|
2020-08-14 19:40:37 +02:00
|
|
|
void Document::set_focused_element(Element* element)
|
|
|
|
{
|
|
|
|
if (m_focused_element == element)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (element)
|
|
|
|
m_focused_element = element->make_weak_ptr();
|
|
|
|
else
|
|
|
|
m_focused_element = nullptr;
|
|
|
|
|
|
|
|
if (m_layout_root)
|
|
|
|
m_layout_root->set_needs_display();
|
|
|
|
}
|
|
|
|
|
2020-08-31 13:56:16 +01:00
|
|
|
void Document::set_ready_state(const String& ready_state)
|
|
|
|
{
|
|
|
|
m_ready_state = ready_state;
|
|
|
|
dispatch_event(Event::create("readystatechange"));
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|