mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
LibWeb: Add name validation and document check to setAttribute
Co-authored-by: Rayyan Hamid <rayyanbwp@gmail.com> Co-authored-by: Christian Ewing <u1273549@utah.edu> Co-authored-by: Austin Fashimpaur <austin.fashimpaur@gmail.com>
This commit is contained in:
parent
5ed7cd6e32
commit
13abb1b2c7
Notes:
sideshowbarker
2024-07-17 07:16:27 +09:00
Author: https://github.com/alextrnnn 🔰 Commit: https://github.com/SerenityOS/serenity/commit/13abb1b2c7 Pull-request: https://github.com/SerenityOS/serenity/pull/24000 Reviewed-by: https://github.com/trflynn89 ✅
@ -0,0 +1 @@
|
||||
<root><div xmlns="http://www.w3.org/1999/xhtml" FoO="bar"></div></root>
|
@ -0,0 +1,4 @@
|
||||
OK: InvalidCharacterError: Attribute name must not be empty or contain invalid characters
|
||||
OK: InvalidCharacterError: Attribute name must not be empty or contain invalid characters
|
||||
OK: InvalidCharacterError: Attribute name must not be empty or contain invalid characters
|
||||
OK: InvalidCharacterError: Attribute name must not be empty or contain invalid characters
|
@ -0,0 +1,12 @@
|
||||
<script src="../include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
var xmlDocument = document.implementation.createDocument(null, "root", null);
|
||||
var divElement = xmlDocument.createElementNS("http://www.w3.org/1999/xhtml", "div");
|
||||
divElement.setAttribute("FoO", "bar");
|
||||
|
||||
xmlDocument.documentElement.appendChild(divElement);
|
||||
|
||||
println(new XMLSerializer().serializeToString(xmlDocument));
|
||||
});
|
||||
</script>
|
@ -0,0 +1,21 @@
|
||||
<script src="../include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
let e = document.createElement('div');
|
||||
function testInvalidQualifiedName(name) {
|
||||
try {
|
||||
e.setAttribute(name, 'bar');
|
||||
println('FAIL')
|
||||
} catch (e) {
|
||||
println('OK: ' + e);
|
||||
}
|
||||
}
|
||||
|
||||
invalidNames =
|
||||
['', '1foo', 'f@oo', 'foo!']
|
||||
|
||||
for (let i = 0; i < invalidNames.length; i++) {
|
||||
testInvalidQualifiedName(invalidNames[i])
|
||||
}
|
||||
});
|
||||
</script>
|
@ -2628,12 +2628,13 @@ static inline bool is_valid_name_character(u32 code_point)
|
||||
|| (code_point >= 0x203f && code_point <= 0x2040);
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/xml/#NT-Name
|
||||
bool Document::is_valid_name(String const& name)
|
||||
{
|
||||
auto code_points = Utf8View { name };
|
||||
auto it = code_points.begin();
|
||||
if (code_points.is_empty())
|
||||
if (name.is_empty())
|
||||
return false;
|
||||
auto code_points = name.code_points();
|
||||
auto it = code_points.begin();
|
||||
|
||||
if (!is_valid_name_start_character(*it))
|
||||
return false;
|
||||
|
@ -167,13 +167,11 @@ JS::GCPtr<Attr> Element::get_attribute_node_ns(Optional<FlyString> const& namesp
|
||||
WebIDL::ExceptionOr<void> Element::set_attribute(FlyString const& name, String const& value)
|
||||
{
|
||||
// 1. If qualifiedName does not match the Name production in XML, then throw an "InvalidCharacterError" DOMException.
|
||||
// FIXME: Proper name validation
|
||||
if (name.is_empty())
|
||||
return WebIDL::InvalidCharacterError::create(realm(), "Attribute name must not be empty"_fly_string);
|
||||
if (!Document::is_valid_name(name.to_string()))
|
||||
return WebIDL::InvalidCharacterError::create(realm(), "Attribute name must not be empty or contain invalid characters"_fly_string);
|
||||
|
||||
// 2. If this is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
|
||||
// FIXME: Handle the second condition, assume it is an HTML document for now.
|
||||
bool insert_as_lowercase = namespace_uri() == Namespace::HTML;
|
||||
bool insert_as_lowercase = namespace_uri() == Namespace::HTML && document().document_type() == Document::Type::HTML;
|
||||
|
||||
// 3. Let attribute be the first attribute in this’s attribute list whose qualified name is qualifiedName, and null otherwise.
|
||||
auto* attribute = m_attributes->get_attribute(name);
|
||||
|
Loading…
Reference in New Issue
Block a user