1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "mozilla/dom/HTMLPictureElement.h"
8 #include "mozilla/dom/HTMLPictureElementBinding.h"
9 #include "mozilla/dom/HTMLImageElement.h"
10
11 // Expand NS_IMPL_NS_NEW_HTML_ELEMENT(Picture) to add pref check.
NS_NewHTMLPictureElement(already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo,mozilla::dom::FromParser aFromParser)12 nsGenericHTMLElement* NS_NewHTMLPictureElement(
13 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
14 mozilla::dom::FromParser aFromParser) {
15 RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo);
16 auto* nim = nodeInfo->NodeInfoManager();
17 return new (nim) mozilla::dom::HTMLPictureElement(nodeInfo.forget());
18 }
19
20 namespace mozilla::dom {
21
HTMLPictureElement(already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo)22 HTMLPictureElement::HTMLPictureElement(
23 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
24 : nsGenericHTMLElement(std::move(aNodeInfo)) {}
25
26 HTMLPictureElement::~HTMLPictureElement() = default;
27
NS_IMPL_ELEMENT_CLONE(HTMLPictureElement)28 NS_IMPL_ELEMENT_CLONE(HTMLPictureElement)
29
30 void HTMLPictureElement::RemoveChildNode(nsIContent* aKid, bool aNotify) {
31 if (aKid && aKid->IsHTMLElement(nsGkAtoms::img)) {
32 HTMLImageElement* img = HTMLImageElement::FromNode(aKid);
33 if (img) {
34 img->PictureSourceRemoved(aKid->AsContent());
35 }
36 } else if (aKid && aKid->IsHTMLElement(nsGkAtoms::source)) {
37 // Find all img siblings after this <source> to notify them of its demise
38 nsCOMPtr<nsIContent> nextSibling = aKid->GetNextSibling();
39 if (nextSibling && nextSibling->GetParentNode() == this) {
40 do {
41 HTMLImageElement* img = HTMLImageElement::FromNode(nextSibling);
42 if (img) {
43 img->PictureSourceRemoved(aKid->AsContent());
44 }
45 } while ((nextSibling = nextSibling->GetNextSibling()));
46 }
47 }
48
49 nsGenericHTMLElement::RemoveChildNode(aKid, aNotify);
50 }
51
InsertChildBefore(nsIContent * aKid,nsIContent * aBeforeThis,bool aNotify,ErrorResult & aRv)52 void HTMLPictureElement::InsertChildBefore(nsIContent* aKid,
53 nsIContent* aBeforeThis,
54 bool aNotify, ErrorResult& aRv) {
55 nsGenericHTMLElement::InsertChildBefore(aKid, aBeforeThis, aNotify, aRv);
56 if (aRv.Failed() || !aKid) {
57 return;
58 }
59
60 if (aKid->IsHTMLElement(nsGkAtoms::img)) {
61 HTMLImageElement* img = HTMLImageElement::FromNode(aKid);
62 if (img) {
63 img->PictureSourceAdded(aKid->AsContent());
64 }
65 } else if (aKid->IsHTMLElement(nsGkAtoms::source)) {
66 // Find all img siblings after this <source> to notify them of its insertion
67 nsCOMPtr<nsIContent> nextSibling = aKid->GetNextSibling();
68 if (nextSibling && nextSibling->GetParentNode() == this) {
69 do {
70 HTMLImageElement* img = HTMLImageElement::FromNode(nextSibling);
71 if (img) {
72 img->PictureSourceAdded(aKid->AsContent());
73 }
74 } while ((nextSibling = nextSibling->GetNextSibling()));
75 }
76 }
77 }
78
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)79 JSObject* HTMLPictureElement::WrapNode(JSContext* aCx,
80 JS::Handle<JSObject*> aGivenProto) {
81 return HTMLPictureElement_Binding::Wrap(aCx, this, aGivenProto);
82 }
83
84 } // namespace mozilla::dom
85