CSSNamespaceRule.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. CSSNamespaceRule::CSSNamespaceRule(JS::Realm& realm, Optional<StringView> prefix, StringView namespace_uri)
  15. : CSSRule(realm)
  16. , m_namespace_uri(namespace_uri)
  17. , m_prefix(prefix.has_value() ? prefix.value() : ""sv)
  18. {
  19. }
  20. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSNamespaceRule>> CSSNamespaceRule::create(JS::Realm& realm, Optional<AK::StringView> prefix, AK::StringView namespace_uri)
  21. {
  22. return MUST_OR_THROW_OOM(realm.heap().allocate<CSSNamespaceRule>(realm, realm, prefix, namespace_uri));
  23. }
  24. void CSSNamespaceRule::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSNamespaceRulePrototype>(realm, "CSSNamespaceRule"));
  28. }
  29. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  30. DeprecatedString CSSNamespaceRule::serialized() const
  31. {
  32. StringBuilder builder;
  33. // The literal string "@namespace", followed by a single SPACE (U+0020),
  34. builder.append("@namespace "sv);
  35. // followed by the serialization as an identifier of the prefix attribute (if any),
  36. if (!m_prefix.is_empty() && !m_prefix.is_null()) {
  37. serialize_an_identifier(builder, m_prefix).release_value_but_fixme_should_propagate_errors();
  38. // followed by a single SPACE (U+0020) if there is a prefix,
  39. builder.append(" "sv);
  40. }
  41. // followed by the serialization as URL of the namespaceURI attribute,
  42. serialize_a_url(builder, m_namespace_uri).release_value_but_fixme_should_propagate_errors();
  43. // followed the character ";" (U+003B).
  44. builder.append(";"sv);
  45. return builder.to_deprecated_string();
  46. }
  47. }