diff options
author | stelar7 <dudedbz@gmail.com> | 2022-05-07 23:26:58 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-05-08 16:29:06 +0200 |
commit | 303b72d5169ac9e5c647920dbd85e86146076bc3 (patch) | |
tree | b1f273982071928fe2522fa7641f523dccf70cce /Userland/Libraries/LibWeb | |
parent | b751f80166b65bd58d41d7ed8fcbf878cbb0844f (diff) | |
download | serenity-303b72d5169ac9e5c647920dbd85e86146076bc3.zip |
LibWeb: Make an+b pattern selector serialization spec compliant
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Selector.h | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Selector.h b/Userland/Libraries/LibWeb/CSS/Selector.h index 1fc242e2da..47e42ac9bd 100644 --- a/Userland/Libraries/LibWeb/CSS/Selector.h +++ b/Userland/Libraries/LibWeb/CSS/Selector.h @@ -44,9 +44,38 @@ public: int step_size { 0 }; // "A" int offset = { 0 }; // "B" + // https://www.w3.org/TR/css-syntax-3/#serializing-anb String serialize() const { - return String::formatted("{}n{:+}", step_size, offset); + // 1. If A is zero, return the serialization of B. + if (step_size == 0) { + return String::formatted("{}", offset); + } + + // 2. Otherwise, let result initially be an empty string. + StringBuilder result; + + // 3. + // - A is 1: Append "n" to result. + if (step_size == 1) + result.append("n"); + // - A is -1: Append "-n" to result. + else if (step_size == -1) + result.append("-n"); + // - A is non-zero: Serialize A and append it to result, then append "n" to result. + else if (step_size != 0) + result.appendff("{}n", step_size); + + // 4. + // - B is greater than zero: Append "+" to result, then append the serialization of B to result. + if (offset > 0) + result.appendff("+{}", offset); + // - B is less than zero: Append the serialization of B to result. + if (offset < 0) + result.appendff("{}", offset); + + // 5. Return result. + return result.to_string(); } }; |