diff --git a/Libraries/LibWeb/Bindings/ElementWrapper.cpp b/Libraries/LibWeb/Bindings/ElementWrapper.cpp index 9475bbf0dd..8e53ef46c9 100644 --- a/Libraries/LibWeb/Bindings/ElementWrapper.cpp +++ b/Libraries/LibWeb/Bindings/ElementWrapper.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include namespace Web { @@ -131,7 +132,7 @@ void ElementWrapper::inner_html_setter(JS::Interpreter& interpreter, JS::Value v JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter) { if (auto* impl = impl_from(interpreter)) - return JS::js_string(interpreter, impl->attribute("id")); + return JS::js_string(interpreter, impl->attribute(HTML::AttributeNames::id)); return {}; } @@ -141,7 +142,7 @@ void ElementWrapper::id_setter(JS::Interpreter& interpreter, JS::Value value) auto string = value.to_string(interpreter); if (interpreter.exception()) return; - impl->set_attribute("id", string); + impl->set_attribute(HTML::AttributeNames::id, string); } } diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 540958cf12..a37702ec04 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -25,6 +25,7 @@ set(SOURCES CSS/StyleRule.cpp CSS/StyleSheet.cpp CSS/StyleValue.cpp + DOM/AttributeNames.cpp DOM/CanvasRenderingContext2D.cpp DOM/CharacterData.cpp DOM/Comment.cpp diff --git a/Libraries/LibWeb/CSS/SelectorEngine.cpp b/Libraries/LibWeb/CSS/SelectorEngine.cpp index 8b26beff74..12470c5d8a 100644 --- a/Libraries/LibWeb/CSS/SelectorEngine.cpp +++ b/Libraries/LibWeb/CSS/SelectorEngine.cpp @@ -25,6 +25,7 @@ */ #include +#include #include #include #include @@ -98,7 +99,7 @@ bool matches(const Selector::SimpleSelector& component, const Element& element) case Selector::SimpleSelector::Type::Universal: return true; case Selector::SimpleSelector::Type::Id: - return component.value == element.attribute("id"); + return component.value == element.attribute(HTML::AttributeNames::id); case Selector::SimpleSelector::Type::Class: return element.has_class(component.value); case Selector::SimpleSelector::Type::TagName: diff --git a/Libraries/LibWeb/DOM/AttributeNames.cpp b/Libraries/LibWeb/DOM/AttributeNames.cpp new file mode 100644 index 0000000000..59188f0e7e --- /dev/null +++ b/Libraries/LibWeb/DOM/AttributeNames.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020, Andreas Kling + * 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 + +namespace Web { +namespace HTML { +namespace AttributeNames { + +FlyString id; +FlyString class_; + +void initialize() +{ + bool s_initialized = false; + if (s_initialized) + return; + id = "id"; + class_ = "class"; + s_initialized = true; +} + +} +} +} diff --git a/Libraries/LibWeb/DOM/AttributeNames.h b/Libraries/LibWeb/DOM/AttributeNames.h new file mode 100644 index 0000000000..a2e6ff4fe5 --- /dev/null +++ b/Libraries/LibWeb/DOM/AttributeNames.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020, Andreas Kling + * 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 + +namespace Web { +namespace HTML { +namespace AttributeNames { + +void initialize(); + +extern FlyString id; +extern FlyString class_; + +} +} +} diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 714782898a..78f0f8d7c0 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -65,6 +66,8 @@ Document::Document(const URL& url) , m_url(url) , m_window(Window::create_with_document(*this)) { + HTML::AttributeNames::initialize(); + m_style_update_timer = Core::Timer::create_single_shot(0, [this] { update_style(); }); diff --git a/Libraries/LibWeb/DOM/NonElementParentNode.h b/Libraries/LibWeb/DOM/NonElementParentNode.h index 1b06098846..837d9ffe3b 100644 --- a/Libraries/LibWeb/DOM/NonElementParentNode.h +++ b/Libraries/LibWeb/DOM/NonElementParentNode.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include #include @@ -39,7 +40,7 @@ public: { const Element* found_element = nullptr; static_cast(this)->template for_each_in_subtree_of_type([&](auto& element) { - if (element.attribute("id") == id) { + if (element.attribute(HTML::AttributeNames::id) == id) { found_element = &element; return IterationDecision::Break; } @@ -53,7 +54,7 @@ public: } protected: - NonElementParentNode() {} + NonElementParentNode() { } }; }