|
@@ -4,6 +4,7 @@
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
*/
|
|
|
|
|
|
+#include <AK/HashTable.h>
|
|
|
#include <LibWeb/HTML/Parser/HTMLToken.h>
|
|
|
|
|
|
namespace Web::HTML {
|
|
@@ -73,4 +74,32 @@ String HTMLToken::to_string() const
|
|
|
return MUST(builder.to_string());
|
|
|
}
|
|
|
|
|
|
+void HTMLToken::normalize_attributes()
|
|
|
+{
|
|
|
+ // From AttributeNameState: https://html.spec.whatwg.org/multipage/parsing.html#attribute-name-state
|
|
|
+ //
|
|
|
+ // When the user agent leaves the attribute name state (and before emitting the tag token, if appropriate),
|
|
|
+ // the complete attribute's name must be compared to the other attributes on the same token;
|
|
|
+ // if there is already an attribute on the token with the exact same name, then this is a duplicate-attribute
|
|
|
+ // parse error and the new attribute must be removed from the token.
|
|
|
+
|
|
|
+ // NOTE: If an attribute is so removed from a token, it, and the value that gets associated with it, if any,
|
|
|
+ // are never subsequently used by the parser, and are therefore effectively discarded. Removing the attribute
|
|
|
+ // in this way does not change its status as the "current attribute" for the purposes of the tokenizer, however.
|
|
|
+
|
|
|
+ HashTable<FlyString> seen_attributes;
|
|
|
+ auto* ptr = tag_attributes();
|
|
|
+ if (!ptr)
|
|
|
+ return;
|
|
|
+ auto& tag_attributes = *ptr;
|
|
|
+ for (size_t i = 0; i < tag_attributes.size(); ++i) {
|
|
|
+ auto& attribute = tag_attributes[i];
|
|
|
+ if (seen_attributes.set(attribute.local_name, AK::HashSetExistingEntryBehavior::Keep) == AK::HashSetResult::KeptExistingEntry) {
|
|
|
+ // This is a duplicate attribute, remove it.
|
|
|
+ tag_attributes.remove(i);
|
|
|
+ --i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
}
|