summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-02-19 19:13:08 +0100
committerAndreas Kling <kling@serenityos.org>2021-02-20 00:09:11 +0100
commite064194061d04dd93947c079ebb3d5f69df398b7 (patch)
tree3275e12cfc16c012d276b5f0d9544f6a4b0118b5
parent3da2b51d74ba92d7d37e6e3119240b0613c85aab (diff)
downloadserenity-e064194061d04dd93947c079ebb3d5f69df398b7.zip
LibWeb: Return InvalidCharacterError from Element::set_attribute() for empty attr
This is the first user of the new DOMException, using ExceptionOr. :^)
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.cpp9
-rw-r--r--Userland/Libraries/LibWeb/DOM/Element.h3
2 files changed, 10 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp
index c40932a0f5..10e2bda733 100644
--- a/Userland/Libraries/LibWeb/DOM/Element.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Element.cpp
@@ -30,8 +30,10 @@
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleInvalidator.h>
#include <LibWeb/CSS/StyleResolver.h>
+#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
+#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
@@ -82,8 +84,12 @@ String Element::attribute(const FlyString& name) const
return {};
}
-void Element::set_attribute(const FlyString& name, const String& value)
+ExceptionOr<void> Element::set_attribute(const FlyString& name, const String& value)
{
+ // FIXME: Proper name validation
+ if (name.is_empty())
+ return InvalidCharacterError::create("Attribute name must not be empty");
+
CSS::StyleInvalidator style_invalidator(document());
if (auto* attribute = find_attribute(name))
@@ -92,6 +98,7 @@ void Element::set_attribute(const FlyString& name, const String& value)
m_attributes.empend(name, value);
parse_attribute(name, value);
+ return {};
}
void Element::remove_attribute(const FlyString& name)
diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h
index fd41df14d9..a9462caf5b 100644
--- a/Userland/Libraries/LibWeb/DOM/Element.h
+++ b/Userland/Libraries/LibWeb/DOM/Element.h
@@ -29,6 +29,7 @@
#include <AK/FlyString.h>
#include <AK/String.h>
#include <LibWeb/DOM/Attribute.h>
+#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/DOM/NonDocumentTypeChildNode.h>
#include <LibWeb/DOM/ParentNode.h>
#include <LibWeb/HTML/AttributeNames.h>
@@ -63,7 +64,7 @@ public:
bool has_attributes() const { return !m_attributes.is_empty(); }
String attribute(const FlyString& name) const;
String get_attribute(const FlyString& name) const { return attribute(name); }
- void set_attribute(const FlyString& name, const String& value);
+ ExceptionOr<void> set_attribute(const FlyString& name, const String& value);
void remove_attribute(const FlyString& name);
template<typename Callback>