CSSPropertyRule.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2024, Alex Ungurianu <alex@ungurianu.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/CSSPropertyRulePrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/CSS/CSSPropertyRule.h>
  9. #include <LibWeb/CSS/Serialize.h>
  10. namespace Web::CSS {
  11. JS_DEFINE_ALLOCATOR(CSSPropertyRule);
  12. JS::NonnullGCPtr<CSSPropertyRule> CSSPropertyRule::create(JS::Realm& realm, FlyString name, FlyString syntax, bool inherits, Optional<String> initial_value)
  13. {
  14. return realm.heap().allocate<CSSPropertyRule>(realm, realm, move(name), move(syntax), inherits, move(initial_value));
  15. }
  16. CSSPropertyRule::CSSPropertyRule(JS::Realm& realm, FlyString name, FlyString syntax, bool inherits, Optional<String> initial_value)
  17. : CSSRule(realm, Type::Property)
  18. , m_name(move(name))
  19. , m_syntax(move(syntax))
  20. , m_inherits(inherits)
  21. , m_initial_value(move(initial_value))
  22. {
  23. }
  24. void CSSPropertyRule::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSPropertyRule);
  28. }
  29. // https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
  30. String CSSPropertyRule::serialized() const
  31. {
  32. StringBuilder builder;
  33. // Serialization algorithm is defined in the spec below
  34. // https://drafts.css-houdini.org/css-properties-values-api/#the-css-property-rule-interface
  35. // To serialize a CSSPropertyRule, return the concatenation of the following:
  36. // 1. The string "@property" followed by a single SPACE (U+0020).
  37. // 2. The result of performing serialize an identifier on the rule’s name, followed by a single SPACE (U+0020).
  38. builder.appendff("@property {} ", serialize_an_identifier(name()));
  39. // 3. The string "{ ", i.e., a single LEFT CURLY BRACKET (U+007B), followed by a SPACE (U+0020).
  40. builder.append("{ "sv);
  41. // 4. The string "syntax:", followed by a single SPACE (U+0020).
  42. // 5. The result of performing serialize a string on the rule’s syntax, followed by a single SEMICOLON (U+003B), followed by a SPACE (U+0020).
  43. builder.appendff("syntax: {}; ", serialize_a_string(syntax()));
  44. // 6. The string "inherits:", followed by a single SPACE (U+0020).
  45. // 7. For the rule’s inherits attribute, one of the following depending on the attribute’s value:
  46. // true: The string "true" followed by a single SEMICOLON (U+003B), followed by a SPACE (U+0020).
  47. // false: The string "false" followed by a single SEMICOLON (U+003B), followed by a SPACE (U+0020).
  48. builder.appendff("inherits: {}; ", inherits());
  49. // 8. If the rule’s initial-value is present, follow these substeps:
  50. if (initial_value().has_value()) {
  51. // 1. The string "initial-value:".
  52. // 2. The result of performing serialize a CSS value in the rule’s initial-value followed by a single SEMICOLON (U+003B), followed by a SPACE (U+0020).
  53. // FIXME: Follow the spec for serializing the value whenever we actually have a CSS value here.
  54. builder.appendff("initial-value: {}; ", initial_value());
  55. }
  56. // 9. A single RIGHT CURLY BRACKET (U+007D).
  57. builder.append("}"sv);
  58. return MUST(builder.to_string());
  59. }
  60. }