Skip to content

Commit 370befb

Browse files
committed
LibWeb: Add a JavaScript wrapper for HTMLImageElement :^)
1 parent 22de1e4 commit 370befb

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#include <AK/FlyString.h>
28+
#include <AK/Function.h>
29+
#include <LibJS/Interpreter.h>
30+
#include <LibJS/Runtime/PrimitiveString.h>
31+
#include <LibJS/Runtime/Value.h>
32+
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
33+
#include <LibWeb/DOM/HTMLImageElement.h>
34+
35+
namespace Web {
36+
namespace Bindings {
37+
38+
HTMLImageElementWrapper::HTMLImageElementWrapper(HTMLImageElement& element)
39+
: ElementWrapper(element)
40+
{
41+
}
42+
43+
HTMLImageElementWrapper::~HTMLImageElementWrapper()
44+
{
45+
}
46+
47+
HTMLImageElement& HTMLImageElementWrapper::node()
48+
{
49+
return static_cast<HTMLImageElement&>(NodeWrapper::node());
50+
}
51+
52+
const HTMLImageElement& HTMLImageElementWrapper::node() const
53+
{
54+
return static_cast<const HTMLImageElement&>(NodeWrapper::node());
55+
}
56+
57+
}
58+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#pragma once
28+
29+
#include <LibWeb/Bindings/ElementWrapper.h>
30+
31+
namespace Web {
32+
namespace Bindings {
33+
34+
class HTMLImageElementWrapper : public ElementWrapper {
35+
public:
36+
explicit HTMLImageElementWrapper(HTMLImageElement&);
37+
virtual ~HTMLImageElementWrapper() override;
38+
39+
HTMLImageElement& node();
40+
const HTMLImageElement& node() const;
41+
42+
private:
43+
virtual const char* class_name() const override { return "HTMLImageElementWrapper"; }
44+
};
45+
46+
}
47+
}

Libraries/LibWeb/Bindings/NodeWrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
#include <LibJS/Runtime/Value.h>
3030
#include <LibWeb/Bindings/DocumentWrapper.h>
3131
#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h>
32+
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
3233
#include <LibWeb/Bindings/NodeWrapper.h>
3334
#include <LibWeb/DOM/Document.h>
3435
#include <LibWeb/DOM/HTMLCanvasElement.h>
36+
#include <LibWeb/DOM/HTMLImageElement.h>
3537
#include <LibWeb/DOM/Node.h>
3638

3739
namespace Web {
@@ -43,6 +45,8 @@ NodeWrapper* wrap(JS::Heap& heap, Node& node)
4345
return static_cast<NodeWrapper*>(wrap_impl(heap, to<Document>(node)));
4446
if (is<HTMLCanvasElement>(node))
4547
return static_cast<NodeWrapper*>(wrap_impl(heap, to<HTMLCanvasElement>(node)));
48+
if (is<HTMLImageElement>(node))
49+
return static_cast<NodeWrapper*>(wrap_impl(heap, to<HTMLImageElement>(node)));
4650
if (is<Element>(node))
4751
return static_cast<NodeWrapper*>(wrap_impl(heap, to<Element>(node)));
4852
return static_cast<NodeWrapper*>(wrap_impl(heap, node));

Libraries/LibWeb/DOM/HTMLImageElement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class LayoutDocument;
3636

3737
class HTMLImageElement : public HTMLElement {
3838
public:
39+
using WrapperType = Bindings::HTMLImageElementWrapper;
40+
3941
HTMLImageElement(Document&, const FlyString& tag_name);
4042
virtual ~HTMLImageElement() override;
4143

Libraries/LibWeb/Forward.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class HTMLCanvasElement;
4040
class HTMLElement;
4141
class HTMLHeadElement;
4242
class HTMLHtmlElement;
43+
class HTMLImageElement;
4344
class HtmlView;
4445
class LayoutDocument;
4546
class LayoutNode;
@@ -62,6 +63,7 @@ class EventWrapper;
6263
class EventListenerWrapper;
6364
class EventTargetWrapper;
6465
class HTMLCanvasElementWrapper;
66+
class HTMLImageElementWrapper;
6567
class MouseEventWrapper;
6668
class NodeWrapper;
6769
class WindowObject;

Libraries/LibWeb/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LIBWEB_OBJS = \
66
Bindings/EventListenerWrapper.o \
77
Bindings/EventTargetWrapper.o \
88
Bindings/HTMLCanvasElementWrapper.o \
9+
Bindings/HTMLImageElementWrapper.o \
910
Bindings/MouseEventWrapper.o \
1011
Bindings/NavigatorObject.o \
1112
Bindings/NodeWrapper.o \

0 commit comments

Comments
 (0)