|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Luke Wilde <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "GeneratorUtil.h" |
| 8 | +#include <AK/SourceGenerator.h> |
| 9 | +#include <AK/StringBuilder.h> |
| 10 | +#include <LibCore/ArgsParser.h> |
| 11 | +#include <LibMain/Main.h> |
| 12 | + |
| 13 | +ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file); |
| 14 | +ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& file); |
| 15 | +ErrorOr<void> generate_idl_file(JsonObject& properties, Core::File& file); |
| 16 | + |
| 17 | +ErrorOr<int> serenity_main(Main::Arguments arguments) |
| 18 | +{ |
| 19 | + StringView generated_header_path; |
| 20 | + StringView generated_implementation_path; |
| 21 | + StringView generated_idl_path; |
| 22 | + StringView properties_json_path; |
| 23 | + |
| 24 | + Core::ArgsParser args_parser; |
| 25 | + args_parser.add_option(generated_header_path, "Path to the CSSStyleProperties header file to generate", "generated-header-path", 'h', "generated-header-path"); |
| 26 | + args_parser.add_option(generated_implementation_path, "Path to the CSSStyleProperties implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path"); |
| 27 | + args_parser.add_option(generated_idl_path, "Path to the CSSStyleProperties IDL file to generate", "generated-idl-path", 'i', "generated-idl-path"); |
| 28 | + args_parser.add_option(properties_json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path"); |
| 29 | + args_parser.parse(arguments); |
| 30 | + |
| 31 | + auto json = TRY(read_entire_file_as_json(properties_json_path)); |
| 32 | + VERIFY(json.is_object()); |
| 33 | + auto properties = json.as_object(); |
| 34 | + |
| 35 | + auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write)); |
| 36 | + auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write)); |
| 37 | + auto generated_idl_file = TRY(Core::File::open(generated_idl_path, Core::File::OpenMode::Write)); |
| 38 | + |
| 39 | + TRY(generate_header_file(properties, *generated_header_file)); |
| 40 | + TRY(generate_implementation_file(properties, *generated_implementation_file)); |
| 41 | + TRY(generate_idl_file(properties, *generated_idl_file)); |
| 42 | + |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +static String get_snake_case_function_name_for_css_property_name(ByteString const& name) |
| 47 | +{ |
| 48 | + auto snake_case_name = snake_casify(name); |
| 49 | + if (snake_case_name.starts_with('_')) |
| 50 | + return MUST(snake_case_name.substring_from_byte_offset(1)); |
| 51 | + |
| 52 | + return snake_case_name; |
| 53 | +} |
| 54 | + |
| 55 | +static String make_name_acceptable_cpp(String const& name) |
| 56 | +{ |
| 57 | + if (name.is_one_of("float")) { |
| 58 | + StringBuilder builder; |
| 59 | + builder.append(name); |
| 60 | + builder.append('_'); |
| 61 | + return MUST(builder.to_string()); |
| 62 | + } |
| 63 | + |
| 64 | + return name; |
| 65 | +} |
| 66 | + |
| 67 | +ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file) |
| 68 | +{ |
| 69 | + StringBuilder builder; |
| 70 | + SourceGenerator generator { builder }; |
| 71 | + |
| 72 | + generator.append(R"~~~( |
| 73 | +#pragma once |
| 74 | +
|
| 75 | +#include <AK/String.h> |
| 76 | +#include <LibWeb/Forward.h> |
| 77 | +
|
| 78 | +namespace Web::Bindings { |
| 79 | +
|
| 80 | +class GeneratedCSSStyleProperties { |
| 81 | +public: |
| 82 | +)~~~"); |
| 83 | + |
| 84 | + properties.for_each_member([&](auto& name, auto&) { |
| 85 | + auto declaration_generator = generator.fork(); |
| 86 | + auto snake_case_name = get_snake_case_function_name_for_css_property_name(name); |
| 87 | + declaration_generator.set("name:acceptable_cpp", make_name_acceptable_cpp(snake_case_name)); |
| 88 | + |
| 89 | + declaration_generator.append(R"~~~( |
| 90 | + WebIDL::ExceptionOr<void> set_@name:acceptable_cpp@(StringView value); |
| 91 | + String @name:acceptable_cpp@() const; |
| 92 | +)~~~"); |
| 93 | + }); |
| 94 | + |
| 95 | + generator.append(R"~~~( |
| 96 | +protected: |
| 97 | + GeneratedCSSStyleProperties() = default; |
| 98 | + virtual ~GeneratedCSSStyleProperties() = default; |
| 99 | +
|
| 100 | + virtual CSS::CSSStyleDeclaration& generated_style_properties_to_css_style_declaration() = 0; |
| 101 | + CSS::CSSStyleDeclaration const& generated_style_properties_to_css_style_declaration() const { return const_cast<GeneratedCSSStyleProperties&>(*this).generated_style_properties_to_css_style_declaration(); } |
| 102 | +}; // class GeneratedCSSStyleProperties |
| 103 | +
|
| 104 | +} // namespace Web::Bindings |
| 105 | +)~~~"); |
| 106 | + |
| 107 | + TRY(file.write_until_depleted(generator.as_string_view().bytes())); |
| 108 | + return {}; |
| 109 | +} |
| 110 | + |
| 111 | +ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& file) |
| 112 | +{ |
| 113 | + StringBuilder builder; |
| 114 | + SourceGenerator generator { builder }; |
| 115 | + |
| 116 | + generator.append(R"~~~( |
| 117 | +#include <LibWeb/CSS/CSSStyleDeclaration.h> |
| 118 | +#include <LibWeb/CSS/GeneratedCSSStyleProperties.h> |
| 119 | +#include <LibWeb/WebIDL/ExceptionOr.h> |
| 120 | +
|
| 121 | +namespace Web::Bindings { |
| 122 | +)~~~"); |
| 123 | + |
| 124 | + properties.for_each_member([&](auto& name, auto&) { |
| 125 | + auto definition_generator = generator.fork(); |
| 126 | + definition_generator.set("name", name); |
| 127 | + |
| 128 | + auto snake_case_name = get_snake_case_function_name_for_css_property_name(name); |
| 129 | + definition_generator.set("name:acceptable_cpp", make_name_acceptable_cpp(snake_case_name)); |
| 130 | + |
| 131 | + definition_generator.append(R"~~~( |
| 132 | +WebIDL::ExceptionOr<void> GeneratedCSSStyleProperties::set_@name:acceptable_cpp@(StringView value) |
| 133 | +{ |
| 134 | + return generated_style_properties_to_css_style_declaration().set_property("@name@"sv, value, ""sv); |
| 135 | +} |
| 136 | +
|
| 137 | +String GeneratedCSSStyleProperties::@name:acceptable_cpp@() const |
| 138 | +{ |
| 139 | + return generated_style_properties_to_css_style_declaration().get_property_value("@name@"sv); |
| 140 | +} |
| 141 | +)~~~"); |
| 142 | + }); |
| 143 | + |
| 144 | + generator.append(R"~~~( |
| 145 | +} // namespace Web::Bindings |
| 146 | +)~~~"); |
| 147 | + |
| 148 | + TRY(file.write_until_depleted(generator.as_string_view().bytes())); |
| 149 | + return {}; |
| 150 | +} |
| 151 | + |
| 152 | +ErrorOr<void> generate_idl_file(JsonObject& properties, Core::File& file) |
| 153 | +{ |
| 154 | + StringBuilder builder; |
| 155 | + SourceGenerator generator { builder }; |
| 156 | + |
| 157 | + generator.append(R"~~~( |
| 158 | +interface mixin GeneratedCSSStyleProperties { |
| 159 | +)~~~"); |
| 160 | + |
| 161 | + properties.for_each_member([&](auto& name, auto&) { |
| 162 | + auto member_generator = generator.fork(); |
| 163 | + |
| 164 | + member_generator.set("name", name); |
| 165 | + |
| 166 | + auto snake_case_name = get_snake_case_function_name_for_css_property_name(name); |
| 167 | + member_generator.set("name:snakecase", snake_case_name); |
| 168 | + member_generator.set("name:acceptable_cpp", make_name_acceptable_cpp(snake_case_name)); |
| 169 | + |
| 170 | + // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-camel-cased-attribute |
| 171 | + // For each CSS property property that is a supported CSS property, the following partial interface applies |
| 172 | + // where camel-cased attribute is obtained by running the CSS property to IDL attribute algorithm for property. |
| 173 | + // partial interface CSSStyleProperties { |
| 174 | + // [CEReactions] attribute [LegacyNullToEmptyString] CSSOMString _camel_cased_attribute; |
| 175 | + // }; |
| 176 | + member_generator.set("name:camelcase", css_property_to_idl_attribute(name)); |
| 177 | + |
| 178 | + member_generator.append(R"~~~( |
| 179 | + [CEReactions, LegacyNullToEmptyString, AttributeCallbackName=@name:snakecase@_regular, ImplementedAs=@name:acceptable_cpp@] attribute CSSOMString @name:camelcase@; |
| 180 | +)~~~"); |
| 181 | + |
| 182 | + // For each CSS property property that is a supported CSS property and that begins with the string -webkit-, |
| 183 | + // the following partial interface applies where webkit-cased attribute is obtained by running the CSS property |
| 184 | + // to IDL attribute algorithm for property, with the lowercase first flag set. |
| 185 | + if (name.starts_with("-webkit-"sv)) { |
| 186 | + member_generator.set("name:webkit", css_property_to_idl_attribute(name, /* lowercase_first= */ true)); |
| 187 | + member_generator.append(R"~~~( |
| 188 | + [CEReactions, LegacyNullToEmptyString, AttributeCallbackName=@name:snakecase@_webkit, ImplementedAs=@name:acceptable_cpp@] attribute CSSOMString @name:webkit@; |
| 189 | +)~~~"); |
| 190 | + } |
| 191 | + |
| 192 | + // For each CSS property property that is a supported CSS property, except for properties that have no |
| 193 | + // "-" (U+002D) in the property name, the following partial interface applies where dashed attribute is |
| 194 | + // property. |
| 195 | + // partial interface CSSStyleProperties { |
| 196 | + // [CEReactions] attribute [LegacyNullToEmptyString] CSSOMString _dashed_attribute; |
| 197 | + // }; |
| 198 | + if (name.contains('-')) { |
| 199 | + member_generator.append(R"~~~( |
| 200 | + [CEReactions, LegacyNullToEmptyString, AttributeCallbackName=@name:snakecase@_dashed, ImplementedAs=@name:acceptable_cpp@] attribute CSSOMString @name@; |
| 201 | +)~~~"); |
| 202 | + } |
| 203 | + }); |
| 204 | + |
| 205 | + generator.append(R"~~~( |
| 206 | +}; |
| 207 | +)~~~"); |
| 208 | + |
| 209 | + TRY(file.write_until_depleted(generator.as_string_view().bytes())); |
| 210 | + return {}; |
| 211 | +} |
0 commit comments