CSSNamespaceRule.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/CSSNamespaceRulePrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/CSS/CSSNamespaceRule.h>
  11. #include <LibWeb/CSS/Serialize.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::CSS {
  14. JS_DEFINE_ALLOCATOR(CSSNamespaceRule);
  15. CSSNamespaceRule::CSSNamespaceRule(JS::Realm& realm, Optional<FlyString> prefix, FlyString namespace_uri)
  16. : CSSRule(realm)
  17. , m_namespace_uri(move(namespace_uri))
  18. , m_prefix(prefix.value_or(""_fly_string))
  19. {
  20. }
  21. JS::NonnullGCPtr<CSSNamespaceRule> CSSNamespaceRule::create(JS::Realm& realm, Optional<FlyString> prefix, FlyString namespace_uri)
  22. {
  23. return realm.heap().allocate<CSSNamespaceRule>(realm, realm, move(prefix), move(namespace_uri));
  24. }
  25. void CSSNamespaceRule::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSNamespaceRule);
  29. }
  30. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  31. String CSSNamespaceRule::serialized() const
  32. {
  33. StringBuilder builder;
  34. // The literal string "@namespace", followed by a single SPACE (U+0020),
  35. builder.append("@namespace "sv);
  36. // followed by the serialization as an identifier of the prefix attribute (if any),
  37. if (!m_prefix.is_empty()) {
  38. serialize_an_identifier(builder, m_prefix);
  39. // followed by a single SPACE (U+0020) if there is a prefix,
  40. builder.append(" "sv);
  41. }
  42. // followed by the serialization as URL of the namespaceURI attribute,
  43. serialize_a_url(builder, m_namespace_uri);
  44. // followed the character ";" (U+003B).
  45. builder.append(";"sv);
  46. return MUST(builder.to_string());
  47. }
  48. }