HTMLMetaElement.cpp 4.5 KB

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