CSSNamespaceRule.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/WebIDL/ExceptionOr.h>
  12. namespace Web::CSS {
  13. CSSNamespaceRule::CSSNamespaceRule(JS::Realm& realm, Optional<StringView> prefix, StringView namespace_uri)
  14. : CSSRule(realm)
  15. , m_namespace_uri(namespace_uri)
  16. , m_prefix(prefix.has_value() ? prefix.value() : ""sv)
  17. {
  18. }
  19. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSNamespaceRule>> CSSNamespaceRule::create(JS::Realm& realm, Optional<AK::StringView> prefix, AK::StringView namespace_uri)
  20. {
  21. return MUST_OR_THROW_OOM(realm.heap().allocate<CSSNamespaceRule>(realm, realm, prefix, namespace_uri));
  22. }
  23. JS::ThrowCompletionOr<void> CSSNamespaceRule::initialize(JS::Realm& realm)
  24. {
  25. MUST_OR_THROW_OOM(Base::initialize(realm));
  26. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSNamespaceRulePrototype>(realm, "CSSNamespaceRule"));
  27. return {};
  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. builder.append(m_prefix);
  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. builder.append(m_namespace_uri);
  43. // followed the character ";" (U+003B).
  44. builder.append(";"sv);
  45. return builder.to_deprecated_string();
  46. }
  47. }