HTMLImageElement.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <LibHTML/CSS/StyleResolver.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/HTMLImageElement.h>
  4. #include <LibHTML/Layout/LayoutImage.h>
  5. HTMLImageElement::HTMLImageElement(Document& document, const String& tag_name)
  6. : HTMLElement(document, tag_name)
  7. {
  8. }
  9. HTMLImageElement::~HTMLImageElement()
  10. {
  11. }
  12. void HTMLImageElement::parse_attribute(const String& name, const String& value)
  13. {
  14. if (name == "src")
  15. load_image(value);
  16. }
  17. void HTMLImageElement::load_image(const String& src)
  18. {
  19. URL src_url = document().complete_url(src);
  20. if (src_url.protocol() == "file") {
  21. m_bitmap = GraphicsBitmap::load_from_file(src_url.path());
  22. } else {
  23. // FIXME: Implement! This whole thing should be at a different layer though..
  24. ASSERT_NOT_REACHED();
  25. }
  26. }
  27. int HTMLImageElement::preferred_width() const
  28. {
  29. bool ok = false;
  30. int width = attribute("width").to_int(ok);
  31. if (ok)
  32. return width;
  33. if (m_bitmap)
  34. return m_bitmap->width();
  35. return 0;
  36. }
  37. int HTMLImageElement::preferred_height() const
  38. {
  39. bool ok = false;
  40. int height = attribute("height").to_int(ok);
  41. if (ok)
  42. return height;
  43. if (m_bitmap)
  44. return m_bitmap->height();
  45. return 0;
  46. }
  47. RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_style) const
  48. {
  49. auto style = resolver.resolve_style(*this, parent_style);
  50. auto display_property = style->property(CSS::PropertyID::Display);
  51. String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
  52. if (display == "none")
  53. return nullptr;
  54. return adopt(*new LayoutImage(*this, move(style)));
  55. }
  56. const GraphicsBitmap* HTMLImageElement::bitmap() const
  57. {
  58. return m_bitmap;
  59. }