HTMLLinkElement.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/ByteBuffer.h>
  8. #include <AK/URL.h>
  9. #include <LibWeb/CSS/Parser/Parser.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/HTMLLinkElement.h>
  12. #include <LibWeb/Loader/ResourceLoader.h>
  13. namespace Web::HTML {
  14. HTMLLinkElement::HTMLLinkElement(DOM::Document& document, QualifiedName qualified_name)
  15. : HTMLElement(document, move(qualified_name))
  16. , m_css_loader(*this)
  17. {
  18. m_css_loader.on_load = [&] {
  19. document.update_style();
  20. };
  21. }
  22. HTMLLinkElement::~HTMLLinkElement()
  23. {
  24. }
  25. void HTMLLinkElement::inserted()
  26. {
  27. HTMLElement::inserted();
  28. if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) {
  29. m_css_loader.load_from_url(document().parse_url(href()));
  30. if (auto sheet = m_css_loader.style_sheet())
  31. document().style_sheets().add_sheet(sheet.release_nonnull());
  32. }
  33. }
  34. void HTMLLinkElement::parse_attribute(const FlyString& name, const String& value)
  35. {
  36. if (name == HTML::AttributeNames::rel) {
  37. m_relationship = 0;
  38. auto parts = value.split_view(' ');
  39. for (auto& part : parts) {
  40. if (part == "stylesheet")
  41. m_relationship |= Relationship::Stylesheet;
  42. else if (part == "alternate")
  43. m_relationship |= Relationship::Alternate;
  44. }
  45. }
  46. }
  47. }