HTMLOrSVGElement.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/Focus.h>
  7. #include <LibWeb/HTML/HTMLElement.h>
  8. #include <LibWeb/HTML/HTMLOrSVGElement.h>
  9. #include <LibWeb/MathML/MathMLElement.h>
  10. #include <LibWeb/SVG/SVGElement.h>
  11. namespace Web::HTML {
  12. // https://html.spec.whatwg.org/multipage/dom.html#dom-dataset-dev
  13. template<typename ElementBase>
  14. GC::Ref<DOMStringMap> HTMLOrSVGElement<ElementBase>::dataset()
  15. {
  16. if (!m_dataset)
  17. m_dataset = DOMStringMap::create(*static_cast<ElementBase*>(this));
  18. return *m_dataset;
  19. }
  20. // https://html.spec.whatwg.org/multipage/interaction.html#dom-focus
  21. template<typename ElementBase>
  22. void HTMLOrSVGElement<ElementBase>::focus()
  23. {
  24. // FIXME: below are the focus(options) steps, also implement focus()
  25. // 1. If the element is marked as locked for focus, then return.
  26. if (m_locked_for_focus)
  27. return;
  28. // 2. Mark the element as locked for focus.
  29. m_locked_for_focus = true;
  30. // 3. Run the focusing steps for the element.
  31. run_focusing_steps(static_cast<ElementBase*>(this));
  32. // FIXME: 4. If the value of the preventScroll dictionary member of options is false,
  33. // then scroll the element into view with scroll behavior "auto",
  34. // block flow direction position set to an implementation-defined value,
  35. // and inline base direction position set to an implementation-defined value.
  36. // 5. Unmark the element as locked for focus.
  37. m_locked_for_focus = false;
  38. }
  39. // https://html.spec.whatwg.org/multipage/interaction.html#dom-blur
  40. template<typename ElementBase>
  41. void HTMLOrSVGElement<ElementBase>::blur()
  42. {
  43. // The blur() method, when invoked, should run the unfocusing steps for the element on which the method was called.
  44. run_unfocusing_steps(static_cast<ElementBase*>(this));
  45. // User agents may selectively or uniformly ignore calls to this method for usability reasons.
  46. }
  47. // https://html.spec.whatwg.org/#dom-noncedelement-nonce
  48. template<typename ElementBase>
  49. void HTMLOrSVGElement<ElementBase>::attribute_changed(FlyString const& local_name, Optional<String> const&, Optional<String> const& value, Optional<FlyString> const& namespace_)
  50. {
  51. // 1. If element does not include HTMLOrSVGElement, then return.
  52. // 2. If localName is not nonce or namespace is not null, then return.
  53. if (local_name != HTML::AttributeNames::nonce || namespace_.has_value())
  54. return;
  55. // 3. If value is null, then set element's [[CryptographicNonce]] to the empty string.
  56. if (!value.has_value()) {
  57. m_cryptographic_nonce = {};
  58. }
  59. // 4. Otherwise, set element's [[CryptographicNonce]] to value.
  60. else {
  61. m_cryptographic_nonce = value.value();
  62. }
  63. }
  64. // https://html.spec.whatwg.org/#dom-noncedelement-nonce
  65. template<typename ElementBase>
  66. WebIDL::ExceptionOr<void> HTMLOrSVGElement<ElementBase>::cloned(DOM::Node& copy, bool)
  67. {
  68. // The cloning steps for elements that include HTMLOrSVGElement must set the
  69. // [[CryptographicNonce]] slot on the copy to the value of the slot on the element being cloned.
  70. static_cast<ElementBase&>(copy).m_cryptographic_nonce = m_cryptographic_nonce;
  71. return {};
  72. }
  73. // https://html.spec.whatwg.org/#dom-noncedelement-nonce
  74. template<typename ElementBase>
  75. void HTMLOrSVGElement<ElementBase>::inserted()
  76. {
  77. // Whenever an element including HTMLOrSVGElement becomes browsing-context connected, the user
  78. // agent must execute the following steps on the element:
  79. DOM::Element& element = *static_cast<ElementBase*>(this);
  80. // FIXME: 1. Let CSP list be element's shadow-including root's policy container's CSP list.
  81. [[maybe_unused]] auto policy_container = element.shadow_including_root().document().policy_container();
  82. // FIXME: 2. If CSP list contains a header-delivered Content Security Policy, and element has a
  83. // nonce content attribute attr whose value is not the empty string, then:
  84. if (true && element.has_attribute(HTML::AttributeNames::nonce)) {
  85. // 2.1. Let nonce be element's [[CryptographicNonce]].
  86. auto nonce = m_cryptographic_nonce;
  87. // 2.2. Set an attribute value for element using "nonce" and the empty string.
  88. element.set_attribute_value(HTML::AttributeNames::nonce, {});
  89. // 2.3. Set element's [[CryptographicNonce]] to nonce.
  90. m_cryptographic_nonce = nonce;
  91. }
  92. }
  93. template<typename ElementBase>
  94. void HTMLOrSVGElement<ElementBase>::visit_edges(JS::Cell::Visitor& visitor)
  95. {
  96. visitor.visit(m_dataset);
  97. }
  98. template class HTMLOrSVGElement<HTMLElement>;
  99. template class HTMLOrSVGElement<MathML::MathMLElement>;
  100. template class HTMLOrSVGElement<SVG::SVGElement>;
  101. }