LibHTML: Start building a simple code generator for CSS properties

Code for parsing and stringifying CSS properties is now generated based
on LibHTML/CSS/Properties.json

At the moment, the file tells us three things:

- The name of a property
- Its initial value
- Whether it's inherited

Also, for shorthand properties, it provides a list of all the longhand
properties it may expand too. This is not actually used in the engine
yet though.

This *finally* makes layout tree dumps show the names of CSS properties
in effect, instead of "CSS::PropertyID(32)" and such. :^)
This commit is contained in:
Andreas Kling 2019-11-18 11:12:58 +01:00
parent dcd10149fe
commit e6e41e4fb8
Notes: sideshowbarker 2024-07-19 11:09:57 +09:00
10 changed files with 529 additions and 115 deletions

View file

@ -0,0 +1,61 @@
#include <AK/JsonObject.h>
#include <AK/StringBuilder.h>
#include <LibCore/CFile.h>
#include <ctype.h>
#include <stdio.h>
static String title_casify(const String& dashy_name)
{
auto parts = dashy_name.split('-');
StringBuilder builder;
for (auto& part : parts) {
if (part.is_empty())
continue;
builder.append(toupper(part[0]));
if (part.length() == 1)
continue;
builder.append(part.substring_view(1, part.length() - 1));
}
return builder.to_string();
}
int main(int argc, char** argv)
{
if (argc != 2) {
fprintf(stderr, "usage: %s <path/to/CSS/Properties.json>\n", argv[0]);
return 1;
}
auto file = CFile::construct(argv[1]);
if (!file->open(CIODevice::ReadOnly))
return 1;
auto json = JsonValue::from_string(file->read_all());
ASSERT(json.is_object());
dbg() << "#pragma once";
dbg() << "#include <AK/StringView.h>";
dbg() << "#include <AK/Traits.h>";
dbg() << "namespace CSS {";
dbg() << "enum class PropertyID {";
dbg() << " Invalid,";
json.as_object().for_each_member([&](auto& name, auto& value) {
ASSERT(value.is_object());
dbg() << " " << title_casify(name) << ",";
});
dbg() << "};\n\
PropertyID property_id_from_string(const StringView&);\n\
const char* string_from_property_id(PropertyID);\n\
}\n\
\n\
namespace AK {\n\
template<>\n\
struct Traits<CSS::PropertyID> : public GenericTraits<CSS::PropertyID> {\n\
static unsigned hash(CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }\n\
};\n\
}\n";
return 0;
}

View file

@ -0,0 +1,33 @@
PROGRAM = Generate_CSS_PropertyID_h
OBJS = \
Generate_CSS_PropertyID_h.o \
$(SERENITY_ROOT)/AK/String.o \
$(SERENITY_ROOT)/AK/StringImpl.o \
$(SERENITY_ROOT)/AK/StringBuilder.o \
$(SERENITY_ROOT)/AK/StringView.o \
$(SERENITY_ROOT)/AK/JsonValue.o \
$(SERENITY_ROOT)/AK/JsonParser.o \
$(SERENITY_ROOT)/AK/LogStream.o \
$(SERENITY_ROOT)/Libraries/LibCore/CIODevice.o \
$(SERENITY_ROOT)/Libraries/LibCore/CFile.o \
$(SERENITY_ROOT)/Libraries/LibCore/CObject.o \
$(SERENITY_ROOT)/Libraries/LibCore/CEvent.o \
$(SERENITY_ROOT)/Libraries/LibCore/CSocket.o \
$(SERENITY_ROOT)/Libraries/LibCore/CLocalSocket.o \
$(SERENITY_ROOT)/Libraries/LibCore/CNotifier.o \
$(SERENITY_ROOT)/Libraries/LibCore/CLocalServer.o \
$(SERENITY_ROOT)/Libraries/LibCore/CEventLoop.o
all: $(PROGRAM)
CXXFLAGS = -std=c++17 -Wall -Wextra
%.o: %.cpp
$(PRE_CXX) $(CXX) $(CXXFLAGS) -I../ -I$(SERENITY_ROOT)/ -I$(SERENITY_ROOT)/Libraries/ -o $@ -c $<
$(PROGRAM): $(OBJS)
$(CXX) $(LDFLAGS) -I../ -I$(SERENITY_ROOT)/ -I$(SERENITY_ROOT)/Libraries/ -o $(PROGRAM) $(OBJS)
clean:
rm -f $(PROGRAM) $(OBJS)