HTMLStyleElement.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/HTML/HTMLStyleElement.h>
  11. namespace Web::HTML {
  12. HTMLStyleElement::HTMLStyleElement(DOM::Document& document, QualifiedName qualified_name)
  13. : HTMLElement(document, move(qualified_name))
  14. , m_css_loader(*this)
  15. {
  16. m_css_loader.on_load = [&] {
  17. document.update_style();
  18. };
  19. }
  20. HTMLStyleElement::~HTMLStyleElement()
  21. {
  22. }
  23. void HTMLStyleElement::children_changed()
  24. {
  25. StringBuilder builder;
  26. for_each_child([&](auto& child) {
  27. if (is<DOM::Text>(child))
  28. builder.append(verify_cast<DOM::Text>(child).text_content());
  29. });
  30. m_css_loader.load_from_text(builder.to_string());
  31. if (auto sheet = m_css_loader.style_sheet())
  32. document().style_sheets().add_sheet(sheet.release_nonnull());
  33. HTMLElement::children_changed();
  34. }
  35. void HTMLStyleElement::removed_from(Node* old_parent)
  36. {
  37. if (m_css_loader.style_sheet()) {
  38. // FIXME: Remove the sheet from the document
  39. }
  40. return HTMLElement::removed_from(old_parent);
  41. }
  42. }