HTMLPictureElement.cpp (3316B)
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 9 #include "mozilla/dom/HTMLImageElement.h" 10 #include "mozilla/dom/HTMLPictureElementBinding.h" 11 #include "mozilla/dom/HTMLSourceElement.h" 12 13 // Expand NS_IMPL_NS_NEW_HTML_ELEMENT(Picture) to add pref check. 14 nsGenericHTMLElement* NS_NewHTMLPictureElement( 15 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, 16 mozilla::dom::FromParser aFromParser) { 17 RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo); 18 auto* nim = nodeInfo->NodeInfoManager(); 19 return new (nim) mozilla::dom::HTMLPictureElement(nodeInfo.forget()); 20 } 21 22 namespace mozilla::dom { 23 24 HTMLPictureElement::HTMLPictureElement( 25 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) 26 : nsGenericHTMLElement(std::move(aNodeInfo)) {} 27 28 HTMLPictureElement::~HTMLPictureElement() = default; 29 30 NS_IMPL_ELEMENT_CLONE(HTMLPictureElement) 31 32 void HTMLPictureElement::RemoveChildNode( 33 nsIContent* aKid, bool aNotify, const BatchRemovalState* aState, 34 nsINode* aNewParent, MutationEffectOnScript aMutationEffectOnScript) { 35 MOZ_ASSERT(aKid); 36 37 if (auto* img = HTMLImageElement::FromNode(aKid)) { 38 img->PictureSourceRemoved(aNotify); 39 } else if (auto* source = HTMLSourceElement::FromNode(aKid)) { 40 // Find all img siblings after this <source> to notify them of its demise 41 nsCOMPtr<nsIContent> nextSibling = source->GetNextSibling(); 42 if (nextSibling && nextSibling->GetParentNode() == this) { 43 do { 44 if (auto* img = HTMLImageElement::FromNode(nextSibling)) { 45 img->PictureSourceRemoved(aNotify, source); 46 } 47 } while ((nextSibling = nextSibling->GetNextSibling())); 48 } 49 } 50 51 nsGenericHTMLElement::RemoveChildNode(aKid, aNotify, aState, aNewParent, 52 aMutationEffectOnScript); 53 } 54 55 void HTMLPictureElement::InsertChildBefore( 56 nsIContent* aKid, nsIContent* aBeforeThis, bool aNotify, ErrorResult& aRv, 57 nsINode* aOldParent, MutationEffectOnScript aMutationEffectOnScript) { 58 nsGenericHTMLElement::InsertChildBefore(aKid, aBeforeThis, aNotify, aRv, 59 aOldParent, aMutationEffectOnScript); 60 if (aRv.Failed() || !aKid) { 61 return; 62 } 63 64 if (auto* img = HTMLImageElement::FromNode(aKid)) { 65 img->PictureSourceAdded(aNotify); 66 } else if (auto* source = HTMLSourceElement::FromNode(aKid)) { 67 // Find all img siblings after this <source> to notify them of its insertion 68 nsCOMPtr<nsIContent> nextSibling = source->GetNextSibling(); 69 if (nextSibling && nextSibling->GetParentNode() == this) { 70 do { 71 if (auto* img = HTMLImageElement::FromNode(nextSibling)) { 72 img->PictureSourceAdded(aNotify, source); 73 } 74 } while ((nextSibling = nextSibling->GetNextSibling())); 75 } 76 } 77 } 78 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