LibWeb/CSS: Move "serialize a CSS declaration" to Serialize.{h,cpp}

We need this in other places, so make it available for reuse. Also,
update step 4 which now only appends `value` if it contains
non-whitespace.
This commit is contained in:
Sam Atkins 2025-03-26 16:20:55 +00:00
commit 2b67cb5f98
Notes: github-actions[bot] 2025-03-27 11:54:13 +00:00
3 changed files with 33 additions and 51 deletions

View file

@ -223,4 +223,32 @@ String serialize_a_srgb_value(Color color)
return MUST(builder.to_string());
}
// https://drafts.csswg.org/cssom/#serialize-a-css-declaration
String serialize_a_css_declaration(StringView property, StringView value, Important important)
{
// 1. Let s be the empty string.
StringBuilder builder;
// 2. Append property to s.
builder.append(property);
// 3. Append ": " (U+003A U+0020) to s.
builder.append(": "sv);
// 4. If value contains any non-whitespace characters, append value to s.
if (!value.is_whitespace())
builder.append(value);
// 5. If the important flag is set, append " !important" (U+0020 U+0021 U+0069 U+006D U+0070 U+006F U+0072 U+0074
// U+0061 U+006E U+0074) to s.
if (important == Important::Yes)
builder.append(" !important"sv);
// 6. Append ";" (U+003B) to s.
builder.append(';');
// 7. Return s.
return MUST(builder.to_string());
}
}