CSSStyleSheet.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSStyleSheet.h>
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. #include <LibWeb/CSS/StyleSheetList.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/ExceptionOr.h>
  11. namespace Web::CSS {
  12. CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules, Optional<AK::URL> location)
  13. : m_rules(CSSRuleList::create(move(rules)))
  14. {
  15. if (location.has_value())
  16. set_location(location->to_string());
  17. for (auto& rule : *m_rules)
  18. rule.set_parent_style_sheet(this);
  19. }
  20. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
  21. DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  22. {
  23. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  24. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  25. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  26. auto parsed_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule);
  27. // 4. If parsed rule is a syntax error, return parsed rule.
  28. if (!parsed_rule)
  29. return DOM::SyntaxError::create("Unable to parse CSS rule.");
  30. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  31. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  32. auto parsed_rule_nonnull = parsed_rule.release_nonnull();
  33. auto result = m_rules->insert_a_css_rule(parsed_rule_nonnull, index);
  34. if (!result.is_exception()) {
  35. // NOTE: The spec doesn't say where to set the parent style sheet, so we'll do it here.
  36. parsed_rule_nonnull->set_parent_style_sheet(this);
  37. if (m_style_sheet_list) {
  38. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  39. m_style_sheet_list->document().invalidate_style();
  40. }
  41. }
  42. return result;
  43. }
  44. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
  45. DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  46. {
  47. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  48. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  49. // 3. Remove a CSS rule in the CSS rules at index.
  50. auto result = m_rules->remove_a_css_rule(index);
  51. if (!result.is_exception()) {
  52. if (m_style_sheet_list) {
  53. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  54. m_style_sheet_list->document().invalidate_style();
  55. }
  56. }
  57. return result;
  58. }
  59. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  60. DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  61. {
  62. // The removeRule(index) method must run the same steps as deleteRule().
  63. return delete_rule(index);
  64. }
  65. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  66. {
  67. m_rules->for_each_effective_style_rule(callback);
  68. }
  69. bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
  70. {
  71. return m_rules->evaluate_media_queries(window);
  72. }
  73. void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
  74. {
  75. m_style_sheet_list = list;
  76. }
  77. }