HTMLObjectElement.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibWeb/CSS/StyleComputer.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Event.h>
  10. #include <LibWeb/HTML/HTMLObjectElement.h>
  11. #include <LibWeb/Layout/ImageBox.h>
  12. #include <LibWeb/Loader/ResourceLoader.h>
  13. namespace Web::HTML {
  14. HTMLObjectElement::HTMLObjectElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : FormAssociatedElement(document, move(qualified_name))
  16. , m_image_loader(*this)
  17. {
  18. m_image_loader.on_load = [this] {
  19. m_should_show_fallback_content = false;
  20. set_needs_style_update(true);
  21. this->document().set_needs_layout();
  22. // FIXME: This should be done by the HTML^Wdocument parser.
  23. dispatch_event(DOM::Event::create(HTML::EventNames::load));
  24. };
  25. m_image_loader.on_fail = [this] {
  26. m_should_show_fallback_content = true;
  27. set_needs_style_update(true);
  28. this->document().set_needs_layout();
  29. // FIXME: This should be done by the HTML^Wdocument parser.
  30. dispatch_event(DOM::Event::create(HTML::EventNames::load));
  31. };
  32. }
  33. HTMLObjectElement::~HTMLObjectElement() = default;
  34. void HTMLObjectElement::parse_attribute(const FlyString& name, const String& value)
  35. {
  36. HTMLElement::parse_attribute(name, value);
  37. if (name == HTML::AttributeNames::data)
  38. m_image_loader.load(document().parse_url(value));
  39. }
  40. RefPtr<Layout::Node> HTMLObjectElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  41. {
  42. if (m_should_show_fallback_content)
  43. return HTMLElement::create_layout_node(move(style));
  44. if (m_image_loader.has_image())
  45. return adopt_ref(*new Layout::ImageBox(document(), *this, move(style), m_image_loader));
  46. return nullptr;
  47. }
  48. }