HTMLMetaElement.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/CSS/Parser/Parser.h>
  9. #include <LibWeb/CSS/Parser/ParsingContext.h>
  10. #include <LibWeb/CSS/PropertyID.h>
  11. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/HTMLMetaElement.h>
  14. #include <LibWeb/Infra/CharacterTypes.h>
  15. #include <LibWeb/Page/Page.h>
  16. namespace Web::HTML {
  17. HTMLMetaElement::HTMLMetaElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : HTMLElement(document, move(qualified_name))
  19. {
  20. }
  21. HTMLMetaElement::~HTMLMetaElement() = default;
  22. void HTMLMetaElement::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLMetaElementPrototype>(realm, "HTMLMetaElement"));
  26. }
  27. Optional<HTMLMetaElement::HttpEquivAttributeState> HTMLMetaElement::http_equiv_state() const
  28. {
  29. auto value = deprecated_attribute(HTML::AttributeNames::http_equiv);
  30. #define __ENUMERATE_HTML_META_HTTP_EQUIV_ATTRIBUTE(keyword, state) \
  31. if (value.equals_ignoring_ascii_case(#keyword##sv)) \
  32. return HTMLMetaElement::HttpEquivAttributeState::state;
  33. ENUMERATE_HTML_META_HTTP_EQUIV_ATTRIBUTES
  34. #undef __ENUMERATE_HTML_META_HTTP_EQUIV_ATTRIBUTE
  35. return OptionalNone {};
  36. }
  37. void HTMLMetaElement::inserted()
  38. {
  39. Base::inserted();
  40. // https://html.spec.whatwg.org/multipage/semantics.html#meta-theme-color
  41. // 1. To obtain a page's theme color, user agents must run the following steps:
  42. // * The element is in a document tree
  43. // * The element has a name attribute, whose value is an ASCII case-insensitive match for theme-color
  44. // * The element has a content attribute
  45. auto name = attribute(AttributeNames::name);
  46. auto content = attribute(AttributeNames::content);
  47. if (name.has_value() && name->bytes_as_string_view().equals_ignoring_ascii_case("theme-color"sv) && content.has_value()) {
  48. auto* page = document().page();
  49. if (!page)
  50. return;
  51. auto context = CSS::Parser::ParsingContext { document() };
  52. // 2. For each element in candidate elements:
  53. // 1. If element has a media attribute and the value of element's media attribute does not match the environment, then continue.
  54. auto media = attribute(AttributeNames::media);
  55. if (media.has_value()) {
  56. auto query = parse_media_query(context, media.value());
  57. if (!query->evaluate(document().window()))
  58. return;
  59. }
  60. // 2. Let value be the result of stripping leading and trailing ASCII whitespace from the value of element's content attribute.
  61. auto value = content->bytes_as_string_view().trim(Infra::ASCII_WHITESPACE);
  62. // 3. Let color be the result of parsing value.
  63. auto css_value = parse_css_value(context, value, CSS::PropertyID::Color);
  64. if (css_value.is_null() || !css_value->is_color())
  65. return;
  66. auto color = css_value->as_color().color();
  67. // 4. If color is not failure, then return color.
  68. page->client().page_did_change_theme_color(color);
  69. return;
  70. }
  71. // https://html.spec.whatwg.org/multipage/semantics.html#pragma-directives
  72. // When a meta element is inserted into the document, if its http-equiv attribute is present and represents one of
  73. // the above states, then the user agent must run the algorithm appropriate for that state, as described in the
  74. // following list:
  75. auto http_equiv = http_equiv_state();
  76. if (http_equiv.has_value()) {
  77. switch (http_equiv.value()) {
  78. case HttpEquivAttributeState::Refresh: {
  79. // https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh
  80. // 1. If the meta element has no content attribute, or if that attribute's value is the empty string, then return.
  81. // 2. Let input be the value of the element's content attribute.
  82. if (!has_attribute(AttributeNames::content))
  83. break;
  84. auto input = deprecated_attribute(AttributeNames::content);
  85. if (input.is_empty())
  86. break;
  87. // 3. Run the shared declarative refresh steps with the meta element's node document, input, and the meta element.
  88. document().shared_declarative_refresh_steps(input, this);
  89. break;
  90. }
  91. default:
  92. dbgln("FIXME: Implement '{}' http-equiv state", deprecated_attribute(AttributeNames::http_equiv));
  93. break;
  94. }
  95. }
  96. }
  97. }