HTMLPictureElement.cpp 3.1 KB

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