HTMLMetaElement.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/HTMLMetaElement.h>
  10. namespace Web::HTML {
  11. HTMLMetaElement::HTMLMetaElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : HTMLElement(document, move(qualified_name))
  13. {
  14. }
  15. HTMLMetaElement::~HTMLMetaElement() = default;
  16. JS::ThrowCompletionOr<void> HTMLMetaElement::initialize(JS::Realm& realm)
  17. {
  18. MUST_OR_THROW_OOM(Base::initialize(realm));
  19. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLMetaElementPrototype>(realm, "HTMLMetaElement"));
  20. return {};
  21. }
  22. Optional<HTMLMetaElement::HttpEquivAttributeState> HTMLMetaElement::http_equiv_state() const
  23. {
  24. auto value = attribute(HTML::AttributeNames::http_equiv);
  25. #define __ENUMERATE_HTML_META_HTTP_EQUIV_ATTRIBUTE(keyword, state) \
  26. if (value.equals_ignoring_ascii_case(#keyword##sv)) \
  27. return HTMLMetaElement::HttpEquivAttributeState::state;
  28. ENUMERATE_HTML_META_HTTP_EQUIV_ATTRIBUTES
  29. #undef __ENUMERATE_HTML_META_HTTP_EQUIV_ATTRIBUTE
  30. return OptionalNone {};
  31. }
  32. void HTMLMetaElement::inserted()
  33. {
  34. Base::inserted();
  35. // https://html.spec.whatwg.org/multipage/semantics.html#pragma-directives
  36. // When a meta element is inserted into the document, if its http-equiv attribute is present and represents one of
  37. // the above states, then the user agent must run the algorithm appropriate for that state, as described in the
  38. // following list:
  39. auto http_equiv = http_equiv_state();
  40. if (http_equiv.has_value()) {
  41. switch (http_equiv.value()) {
  42. case HttpEquivAttributeState::Refresh: {
  43. // https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh
  44. // 1. If the meta element has no content attribute, or if that attribute's value is the empty string, then return.
  45. // 2. Let input be the value of the element's content attribute.
  46. if (!has_attribute(AttributeNames::content))
  47. break;
  48. auto input = attribute(AttributeNames::content);
  49. if (input.is_empty())
  50. break;
  51. // 3. Run the shared declarative refresh steps with the meta element's node document, input, and the meta element.
  52. document().shared_declarative_refresh_steps(input, this);
  53. break;
  54. }
  55. default:
  56. dbgln("FIXME: Implement '{}' http-equiv state", attribute(AttributeNames::http_equiv));
  57. break;
  58. }
  59. }
  60. }
  61. }