2019-10-08 20:37:15 +03:00
|
|
|
#include <LibDraw/PNGLoader.h>
|
2019-10-06 00:20:35 +03:00
|
|
|
#include <LibHTML/CSS/StyleResolver.h>
|
2019-10-06 00:41:14 +03:00
|
|
|
#include <LibHTML/DOM/Document.h>
|
2019-10-05 23:07:45 +03:00
|
|
|
#include <LibHTML/DOM/HTMLImageElement.h>
|
2019-10-06 00:20:35 +03:00
|
|
|
#include <LibHTML/Layout/LayoutImage.h>
|
2019-10-08 20:37:15 +03:00
|
|
|
#include <LibHTML/ResourceLoader.h>
|
2019-10-05 23:07:45 +03:00
|
|
|
|
|
|
|
HTMLImageElement::HTMLImageElement(Document& document, const String& tag_name)
|
|
|
|
: HTMLElement(document, tag_name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLImageElement::~HTMLImageElement()
|
|
|
|
{
|
|
|
|
}
|
2019-10-06 00:20:35 +03:00
|
|
|
|
2019-10-06 15:03:51 +03:00
|
|
|
void HTMLImageElement::parse_attribute(const String& name, const String& value)
|
|
|
|
{
|
|
|
|
if (name == "src")
|
|
|
|
load_image(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLImageElement::load_image(const String& src)
|
|
|
|
{
|
|
|
|
URL src_url = document().complete_url(src);
|
2019-10-19 10:43:05 +03:00
|
|
|
ResourceLoader::the().load(src_url, [this, weak_element = make_weak_ptr()](auto data) {
|
|
|
|
if (!weak_element) {
|
|
|
|
dbg() << "HTMLImageElement: Load completed after element destroyed.";
|
|
|
|
return;
|
|
|
|
}
|
2019-10-08 20:37:15 +03:00
|
|
|
if (data.is_null()) {
|
|
|
|
dbg() << "HTMLImageElement: Failed to load " << this->src();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:30:42 +03:00
|
|
|
m_encoded_data = data;
|
|
|
|
m_image_decoder = ImageDecoder::create(m_encoded_data.data(), m_encoded_data.size());
|
2019-10-14 18:56:19 +03:00
|
|
|
document().update_layout();
|
2019-10-08 20:37:15 +03:00
|
|
|
});
|
2019-10-06 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
2019-10-06 15:15:30 +03:00
|
|
|
int HTMLImageElement::preferred_width() const
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
int width = attribute("width").to_int(ok);
|
|
|
|
if (ok)
|
|
|
|
return width;
|
|
|
|
|
2019-10-20 11:39:59 +03:00
|
|
|
if (m_image_decoder)
|
|
|
|
return m_image_decoder->width();
|
2019-10-06 15:15:30 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTMLImageElement::preferred_height() const
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
int height = attribute("height").to_int(ok);
|
|
|
|
if (ok)
|
|
|
|
return height;
|
|
|
|
|
2019-10-20 11:39:59 +03:00
|
|
|
if (m_image_decoder)
|
|
|
|
return m_image_decoder->height();
|
2019-10-06 15:15:30 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-15 16:06:16 +03:00
|
|
|
RefPtr<LayoutNode> HTMLImageElement::create_layout_node(const StyleProperties* parent_style) const
|
2019-10-06 00:20:35 +03:00
|
|
|
{
|
2019-10-15 16:06:16 +03:00
|
|
|
auto style = document().style_resolver().resolve_style(*this, parent_style);
|
|
|
|
auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
|
2019-10-06 00:20:35 +03:00
|
|
|
if (display == "none")
|
|
|
|
return nullptr;
|
|
|
|
return adopt(*new LayoutImage(*this, move(style)));
|
|
|
|
}
|
2019-10-06 00:41:14 +03:00
|
|
|
|
|
|
|
const GraphicsBitmap* HTMLImageElement::bitmap() const
|
|
|
|
{
|
2019-10-20 11:39:59 +03:00
|
|
|
if (!m_image_decoder)
|
2019-10-15 22:53:08 +03:00
|
|
|
return nullptr;
|
2019-10-20 11:39:59 +03:00
|
|
|
return m_image_decoder->bitmap();
|
2019-10-06 00:41:14 +03:00
|
|
|
}
|