LibWeb: Add namespace qualifier to type names equal to a C++ namespace

For example, Document.getSelection returns Selection, which is in the
Selection namespace.

Namespaces.h has Linus' copyright since he changed the "is_one_of" list
to an Array.
This commit is contained in:
Luke Wilde 2022-12-09 18:58:52 +00:00 committed by Linus Groh
parent 34c130b336
commit 565dc0f296
Notes: sideshowbarker 2024-07-17 03:35:05 +09:00
3 changed files with 53 additions and 27 deletions

View File

@ -7,6 +7,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Namespaces.h"
#include <AK/LexicalPath.h>
#include <AK/Queue.h>
#include <AK/QuickSort.h>
@ -1377,7 +1378,16 @@ static void generate_wrap_statement(SourceGenerator& generator, DeprecatedString
{
auto scoped_generator = generator.fork();
scoped_generator.set("value", value);
scoped_generator.set("type", type.name());
if (!libweb_interface_namespaces.span().contains_slow(type.name())) {
scoped_generator.set("type", type.name());
} else {
// e.g. Document.getSelection which returns Selection, which is in the Selection namespace.
StringBuilder builder;
builder.append(type.name());
builder.append("::"sv);
builder.append(type.name());
scoped_generator.set("type", builder.to_deprecated_string());
}
scoped_generator.set("result_expression", result_expression);
scoped_generator.set("recursion_depth", DeprecatedString::number(recursion_depth));

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <AK/StringView.h>
namespace IDL {
static constexpr Array libweb_interface_namespaces = {
"CSS"sv,
"Crypto"sv,
"DOM"sv,
"DOMParsing"sv,
"Encoding"sv,
"Fetch"sv,
"FileAPI"sv,
"Geometry"sv,
"HTML"sv,
"HighResolutionTime"sv,
"IntersectionObserver"sv,
"NavigationTiming"sv,
"RequestIdleCallback"sv,
"ResizeObserver"sv,
"SVG"sv,
"Selection"sv,
"UIEvents"sv,
"URL"sv,
"WebGL"sv,
"WebIDL"sv,
"WebSockets"sv,
"XHR"sv,
};
}

View File

@ -7,6 +7,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Namespaces.h"
#include <AK/Debug.h>
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
@ -74,32 +75,7 @@ int main(int argc, char** argv)
IDL::Parser parser(path, data, import_base_path);
auto& interface = parser.parse();
static constexpr Array libweb_interface_namespaces = {
"CSS"sv,
"Crypto"sv,
"DOM"sv,
"DOMParsing"sv,
"Encoding"sv,
"Fetch"sv,
"FileAPI"sv,
"Geometry"sv,
"HTML"sv,
"HighResolutionTime"sv,
"IntersectionObserver"sv,
"NavigationTiming"sv,
"RequestIdleCallback"sv,
"ResizeObserver"sv,
"SVG"sv,
"Selection"sv,
"UIEvents"sv,
"URL"sv,
"WebGL"sv,
"WebIDL"sv,
"WebSockets"sv,
"XHR"sv,
};
if (libweb_interface_namespaces.span().contains_slow(namespace_)) {
if (IDL::libweb_interface_namespaces.span().contains_slow(namespace_)) {
StringBuilder builder;
builder.append(namespace_);
builder.append("::"sv);