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/. */
7 * A fake content node class so that the image frame for
9 * in CSS can have an nsIImageLoadingContent but use an
10 * imgIRequest that's already been loaded from the style system.
13 #include "nsContentCreatorFunctions.h"
14 #include "nsXMLElement.h"
15 #include "nsImageLoadingContent.h"
16 #include "imgIRequest.h"
17 #include "mozilla/BasicEvents.h"
18 #include "mozilla/EventDispatcher.h"
19 #include "mozilla/EventStates.h"
21 using namespace mozilla
;
23 class nsGenConImageContent MOZ_FINAL
: public nsXMLElement
,
24 public nsImageLoadingContent
27 explicit nsGenConImageContent(already_AddRefed
<mozilla::dom::NodeInfo
>& aNodeInfo
)
28 : nsXMLElement(aNodeInfo
)
30 // nsImageLoadingContent starts out broken, so we start out
31 // suppressed to match it.
32 AddStatesSilently(NS_EVENT_STATE_SUPPRESSED
);
35 nsresult
Init(imgRequestProxy
* aImageRequest
)
37 // No need to notify, since we have no frame.
38 return UseAsPrimaryRequest(aImageRequest
, false);
41 // nsIContent overrides
42 virtual nsresult
BindToTree(nsIDocument
* aDocument
, nsIContent
* aParent
,
43 nsIContent
* aBindingParent
,
44 bool aCompileEventHandlers
);
45 virtual void UnbindFromTree(bool aDeep
, bool aNullParent
);
46 virtual EventStates
IntrinsicState() const;
48 virtual nsresult
PreHandleEvent(EventChainPreVisitor
& aVisitor
)
50 MOZ_ASSERT(IsInNativeAnonymousSubtree());
51 if (aVisitor
.mEvent
->message
== NS_LOAD
||
52 aVisitor
.mEvent
->message
== NS_LOAD_ERROR
) {
53 // Don't propagate the events to the parent.
56 return nsXMLElement::PreHandleEvent(aVisitor
);
60 virtual ~nsGenConImageContent();
63 NS_DECL_ISUPPORTS_INHERITED
66 NS_IMPL_ISUPPORTS_INHERITED(nsGenConImageContent
,
68 nsIImageLoadingContent
,
69 imgINotificationObserver
,
73 NS_NewGenConImageContent(nsIContent
** aResult
, already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
,
74 imgRequestProxy
* aImageRequest
)
76 NS_PRECONDITION(aImageRequest
, "Must have request!");
77 nsGenConImageContent
*it
= new nsGenConImageContent(aNodeInfo
);
79 return NS_ERROR_OUT_OF_MEMORY
;
80 NS_ADDREF(*aResult
= it
);
81 nsresult rv
= it
->Init(aImageRequest
);
87 nsGenConImageContent::~nsGenConImageContent()
89 DestroyImageLoadingContent();
93 nsGenConImageContent::BindToTree(nsIDocument
* aDocument
, nsIContent
* aParent
,
94 nsIContent
* aBindingParent
,
95 bool aCompileEventHandlers
)
98 rv
= nsXMLElement::BindToTree(aDocument
, aParent
, aBindingParent
,
99 aCompileEventHandlers
);
100 NS_ENSURE_SUCCESS(rv
, rv
);
102 nsImageLoadingContent::BindToTree(aDocument
, aParent
, aBindingParent
,
103 aCompileEventHandlers
);
108 nsGenConImageContent::UnbindFromTree(bool aDeep
, bool aNullParent
)
110 nsImageLoadingContent::UnbindFromTree(aDeep
, aNullParent
);
111 nsXMLElement::UnbindFromTree(aDeep
, aNullParent
);
115 nsGenConImageContent::IntrinsicState() const
117 EventStates state
= nsXMLElement::IntrinsicState();
119 EventStates imageState
= nsImageLoadingContent::ImageState();
120 if (imageState
.HasAtLeastOneOfStates(NS_EVENT_STATE_BROKEN
| NS_EVENT_STATE_USERDISABLED
)) {
121 // We should never be in an error state; if the image fails to load, we
122 // just go to the suppressed state.
123 imageState
|= NS_EVENT_STATE_SUPPRESSED
;
124 imageState
&= ~NS_EVENT_STATE_BROKEN
;
126 imageState
&= ~NS_EVENT_STATE_LOADING
;
127 return state
| imageState
;