mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 16:47:42 +00:00
LibWeb: Validate the qualified name in createDocumentType()
1% progression on ACID3. :^)
This commit is contained in:
parent
f855cbac92
commit
262488ea33
4 changed files with 61 additions and 3 deletions
|
@ -89,9 +89,11 @@ NonnullRefPtr<Document> DOMImplementation::create_html_document(const String& ti
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
|
// https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
|
||||||
NonnullRefPtr<DocumentType> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
|
ExceptionOr<NonnullRefPtr<DocumentType>> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
|
||||||
{
|
{
|
||||||
// FIXME: Validate qualified_name.
|
auto result = Document::validate_qualified_name(qualified_name);
|
||||||
|
if (result.is_exception())
|
||||||
|
return result.exception();
|
||||||
auto document_type = DocumentType::create(document());
|
auto document_type = DocumentType::create(document());
|
||||||
document_type->set_name(qualified_name);
|
document_type->set_name(qualified_name);
|
||||||
document_type->set_public_id(public_id);
|
document_type->set_public_id(public_id);
|
||||||
|
|
|
@ -30,7 +30,7 @@ public:
|
||||||
// FIXME: Add optional DocumentType once supported by IDL
|
// FIXME: Add optional DocumentType once supported by IDL
|
||||||
ExceptionOr<NonnullRefPtr<Document>> create_document(const String&, const String&) const;
|
ExceptionOr<NonnullRefPtr<Document>> create_document(const String&, const String&) const;
|
||||||
NonnullRefPtr<Document> create_html_document(const String& title) const;
|
NonnullRefPtr<Document> create_html_document(const String& title) const;
|
||||||
NonnullRefPtr<DocumentType> create_document_type(String const& qualified_name, String const& public_id, String const& system_id);
|
ExceptionOr<NonnullRefPtr<DocumentType>> create_document_type(String const& qualified_name, String const& public_id, String const& system_id);
|
||||||
|
|
||||||
// https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
|
// https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
|
||||||
bool has_feature() const { return true; }
|
bool has_feature() const { return true; }
|
||||||
|
|
|
@ -1390,4 +1390,54 @@ bool Document::is_valid_name(String const& name)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://dom.spec.whatwg.org/#validate
|
||||||
|
ExceptionOr<Document::PrefixAndTagName> Document::validate_qualified_name(String const& qualified_name)
|
||||||
|
{
|
||||||
|
if (qualified_name.is_empty())
|
||||||
|
return InvalidCharacterError::create("Empty string is not a valid qualified name.");
|
||||||
|
|
||||||
|
Utf8View utf8view { qualified_name };
|
||||||
|
if (!utf8view.validate())
|
||||||
|
return InvalidCharacterError::create("Invalid qualified name.");
|
||||||
|
|
||||||
|
Optional<size_t> colon_offset;
|
||||||
|
|
||||||
|
bool in_name = false;
|
||||||
|
|
||||||
|
for (auto it = utf8view.begin(); it != utf8view.end(); ++it) {
|
||||||
|
auto code_point = *it;
|
||||||
|
if (code_point == ':') {
|
||||||
|
if (colon_offset.has_value())
|
||||||
|
return InvalidCharacterError::create("More than one colon (:) in qualified name.");
|
||||||
|
colon_offset = utf8view.byte_offset_of(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (in_name) {
|
||||||
|
if (!is_valid_name_start_character(code_point))
|
||||||
|
return InvalidCharacterError::create("Invalid start of qualified name.");
|
||||||
|
in_name = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!is_valid_name_character(code_point))
|
||||||
|
return InvalidCharacterError::create("Invalid character in qualified name.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!colon_offset.has_value())
|
||||||
|
return Document::PrefixAndTagName {
|
||||||
|
.prefix = {},
|
||||||
|
.tag_name = qualified_name,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (*colon_offset == 0)
|
||||||
|
return InvalidCharacterError::create("Qualified name can't start with colon (:).");
|
||||||
|
|
||||||
|
if (*colon_offset >= (qualified_name.length() - 1))
|
||||||
|
return InvalidCharacterError::create("Qualified name can't end with colon (:).");
|
||||||
|
|
||||||
|
return Document::PrefixAndTagName {
|
||||||
|
.prefix = qualified_name.substring_view(0, *colon_offset),
|
||||||
|
.tag_name = qualified_name.substring_view(*colon_offset + 1),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -318,6 +318,12 @@ public:
|
||||||
|
|
||||||
static bool is_valid_name(String const&);
|
static bool is_valid_name(String const&);
|
||||||
|
|
||||||
|
struct PrefixAndTagName {
|
||||||
|
FlyString prefix;
|
||||||
|
FlyString tag_name;
|
||||||
|
};
|
||||||
|
static ExceptionOr<PrefixAndTagName> validate_qualified_name(String const& qualified_name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit Document(const AK::URL&);
|
explicit Document(const AK::URL&);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue