HTMLObjectElement.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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()
  34. {
  35. }
  36. void HTMLObjectElement::parse_attribute(const FlyString& name, const String& value)
  37. {
  38. HTMLElement::parse_attribute(name, value);
  39. if (name == HTML::AttributeNames::data)
  40. m_image_loader.load(document().parse_url(value));
  41. }
  42. RefPtr<Layout::Node> HTMLObjectElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  43. {
  44. if (m_should_show_fallback_content)
  45. return HTMLElement::create_layout_node(move(style));
  46. if (m_image_loader.has_image())
  47. return adopt_ref(*new Layout::ImageBox(document(), *this, move(style), m_image_loader));
  48. return nullptr;
  49. }
  50. }