StyleElementUtils.cpp 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2023, Preston Taylor <PrestonLeeTaylor@proton.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Parser/Parser.h>
  7. #include <LibWeb/CSS/StyleComputer.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/ShadowRoot.h>
  10. #include <LibWeb/DOM/StyleElementUtils.h>
  11. #include <LibWeb/Infra/Strings.h>
  12. namespace Web::DOM {
  13. static CSS::StyleSheetList& relevant_style_sheet_list_for_node(DOM::Node& node)
  14. {
  15. auto& root_node = node.root();
  16. if (is<DOM::ShadowRoot>(root_node))
  17. return static_cast<DOM::ShadowRoot&>(root_node).style_sheets();
  18. return node.document().style_sheets();
  19. }
  20. // The user agent must run the "update a style block" algorithm whenever one of the following conditions occur:
  21. // FIXME: The element is popped off the stack of open elements of an HTML parser or XML parser.
  22. //
  23. // NOTE: This is basically done by children_changed() today:
  24. // The element's children changed steps run.
  25. //
  26. // NOTE: This is basically done by inserted() and removed_from() today:
  27. // The element is not on the stack of open elements of an HTML parser or XML parser, and it becomes connected or disconnected.
  28. //
  29. // https://html.spec.whatwg.org/multipage/semantics.html#update-a-style-block
  30. void StyleElementUtils::update_a_style_block(DOM::Element& style_element, JS::GCPtr<DOM::Node> old_parent_if_removed_from)
  31. {
  32. // OPTIMIZATION: Skip parsing CSS if we're in the middle of parsing a HTML fragment.
  33. // The style block will be parsed upon insertion into a proper document.
  34. if (style_element.document().is_temporary_document_for_fragment_parsing())
  35. return;
  36. // 1. Let element be the style element.
  37. // 2. If element has an associated CSS style sheet, remove the CSS style sheet in question.
  38. if (m_associated_css_style_sheet) {
  39. // NOTE: If we're here in response to a node being removed from the tree, we need to remove the stylesheet from the style scope
  40. // of the old parent, not the style scope of the node itself, since it's too late to find it that way!
  41. if (old_parent_if_removed_from) {
  42. relevant_style_sheet_list_for_node(*old_parent_if_removed_from).remove_a_css_style_sheet(*m_associated_css_style_sheet);
  43. } else {
  44. style_element.document_or_shadow_root_style_sheets().remove_a_css_style_sheet(*m_associated_css_style_sheet);
  45. }
  46. // FIXME: This should probably be handled by StyleSheet::set_owner_node().
  47. m_associated_css_style_sheet = nullptr;
  48. }
  49. // 3. If element is not connected, then return.
  50. if (!style_element.is_connected())
  51. return;
  52. // 4. If element's type attribute is present and its value is neither the empty string nor an ASCII case-insensitive match for "text/css", then return.
  53. auto type_attribute = style_element.attribute(HTML::AttributeNames::type);
  54. if (type_attribute.has_value() && !type_attribute->is_empty() && !Infra::is_ascii_case_insensitive_match(type_attribute->bytes_as_string_view(), "text/css"sv))
  55. return;
  56. // FIXME: 5. If the Should element's inline behavior be blocked by Content Security Policy? algorithm returns "Blocked" when executed upon the style element, "style", and the style element's child text content, then return. [CSP]
  57. // FIXME: This is a bit awkward, as the spec doesn't actually tell us when to parse the CSS text,
  58. // so we just do it here and pass the parsed sheet to create_a_css_style_sheet().
  59. auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(style_element.document()), style_element.text_content().value_or(String {}));
  60. if (!sheet)
  61. return;
  62. // FIXME: This should probably be handled by StyleSheet::set_owner_node().
  63. m_associated_css_style_sheet = sheet;
  64. // 6. Create a CSS style sheet with the following properties...
  65. style_element.document_or_shadow_root_style_sheets().create_a_css_style_sheet(
  66. "text/css"_string,
  67. &style_element,
  68. style_element.attribute(HTML::AttributeNames::media).value_or({}),
  69. style_element.in_a_document_tree()
  70. ? style_element.attribute(HTML::AttributeNames::title).value_or({})
  71. : String {},
  72. false,
  73. true,
  74. {},
  75. nullptr,
  76. nullptr,
  77. *sheet);
  78. }
  79. }