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 #ifndef nsHTMLDocument_h___
7 #define nsHTMLDocument_h___
9 #include "mozilla/Attributes.h"
10 #include "nsContentList.h"
11 #include "mozilla/dom/Document.h"
12 #include "nsIHTMLCollection.h"
13 #include "nsIScriptElement.h"
16 #include "PLDHashTable.h"
17 #include "nsThreadUtils.h"
18 #include "mozilla/dom/HTMLSharedElement.h"
19 #include "mozilla/dom/BindingDeclarations.h"
21 class nsCommandManager
;
24 class nsICachingChannel
;
27 namespace mozilla::dom
{
30 class WindowProxyHolder
;
31 } // namespace mozilla::dom
33 class nsHTMLDocument
: public mozilla::dom::Document
{
35 using ReferrerPolicy
= mozilla::dom::ReferrerPolicy
;
36 using Document
= mozilla::dom::Document
;
37 using Encoding
= mozilla::Encoding
;
39 using NotNull
= mozilla::NotNull
<T
>;
42 using Document::SetDocumentURI
;
45 virtual nsresult
Init(nsIPrincipal
* aPrincipal
,
46 nsIPrincipal
* aPartitionedPrincipal
) override
;
49 virtual void Reset(nsIChannel
* aChannel
, nsILoadGroup
* aLoadGroup
) override
;
50 virtual void ResetToURI(nsIURI
* aURI
, nsILoadGroup
* aLoadGroup
,
51 nsIPrincipal
* aPrincipal
,
52 nsIPrincipal
* aPartitionedPrincipal
) override
;
54 virtual nsresult
StartDocumentLoad(const char* aCommand
, nsIChannel
* aChannel
,
55 nsILoadGroup
* aLoadGroup
,
56 nsISupports
* aContainer
,
57 nsIStreamListener
** aDocListener
,
58 bool aReset
= true) override
;
61 virtual bool UseWidthDeviceWidthFallbackViewport() const override
;
64 mozilla::dom::Element
* GetUnfocusedKeyEventTarget() override
;
66 nsContentList
* GetExistingForms() const { return mForms
; }
68 bool IsPlainText() const { return mIsPlainText
; }
70 bool IsViewSource() const { return mViewSource
; }
72 // Returns whether an object was found for aName.
73 bool ResolveName(JSContext
* aCx
, const nsAString
& aName
,
74 JS::MutableHandle
<JS::Value
> aRetval
,
75 mozilla::ErrorResult
& aError
);
78 * Called when form->BindToTree() is called so that document knows
79 * immediately when a form is added
83 * Called when form->SetDocument() is called so that document knows
84 * immediately when a form is removed
88 * Called to get a better count of forms than document.forms can provide
89 * without calling FlushPendingNotifications (bug 138892).
91 // XXXbz is this still needed now that we can flush just content,
93 int32_t GetNumFormsSynchronous() const;
94 void SetIsXHTML(bool aXHTML
) { mType
= (aXHTML
? eXHTML
: eHTML
); }
96 virtual nsresult
Clone(mozilla::dom::NodeInfo
*,
97 nsINode
** aResult
) const override
;
99 using mozilla::dom::DocumentOrShadowRoot::GetElementById
;
101 virtual void DocAddSizeOfExcludingThis(
102 nsWindowSizes
& aWindowSizes
) const override
;
103 // DocAddSizeOfIncludingThis is inherited from Document.
105 virtual bool WillIgnoreCharsetOverride() override
;
108 virtual JSObject
* WrapNode(JSContext
* aCx
,
109 JS::Handle
<JSObject
*> aGivenProto
) override
;
110 bool IsRegistrableDomainSuffixOfOrEqualTo(const nsAString
& aHostSuffixString
,
111 const nsACString
& aOrigHost
);
112 void NamedGetter(JSContext
* cx
, const nsAString
& aName
, bool& aFound
,
113 JS::MutableHandle
<JSObject
*> aRetval
,
114 mozilla::ErrorResult
& rv
) {
115 JS::Rooted
<JS::Value
> v(cx
);
116 if ((aFound
= ResolveName(cx
, aName
, &v
, rv
))) {
117 SetUseCounter(mozilla::eUseCounter_custom_HTMLDocumentNamedGetterHit
);
118 aRetval
.set(v
.toObjectOrNull());
121 void GetSupportedNames(nsTArray
<nsString
>& aNames
);
122 // We're picking up GetLocation from Document
123 already_AddRefed
<mozilla::dom::Location
> GetLocation() const {
124 return Document::GetLocation();
127 static bool MatchFormControls(mozilla::dom::Element
* aElement
,
128 int32_t aNamespaceID
, nsAtom
* aAtom
,
131 void GetFormsAndFormControls(nsContentList
** aFormList
,
132 nsContentList
** aFormControlList
);
137 nsresult
GetBodySize(int32_t* aWidth
, int32_t* aHeight
);
139 nsIContent
* MatchId(nsIContent
* aContent
, const nsAString
& aId
);
141 static void DocumentWriteTerminationFunc(nsISupports
* aRef
);
143 // A helper class to keep nsContentList objects alive for a short period of
144 // time. Note, when the final Release is called on an nsContentList object, it
145 // removes itself from MutationObserver list.
146 class ContentListHolder
: public mozilla::Runnable
{
148 ContentListHolder(nsHTMLDocument
* aDocument
, nsContentList
* aFormList
,
149 nsContentList
* aFormControlList
)
150 : mozilla::Runnable("ContentListHolder"),
151 mDocument(aDocument
),
152 mFormList(aFormList
),
153 mFormControlList(aFormControlList
) {}
155 ~ContentListHolder() {
156 MOZ_ASSERT(!mDocument
->mContentListHolder
||
157 mDocument
->mContentListHolder
== this);
158 mDocument
->mContentListHolder
= nullptr;
161 RefPtr
<nsHTMLDocument
> mDocument
;
162 RefPtr
<nsContentList
> mFormList
;
163 RefPtr
<nsContentList
> mFormControlList
;
166 friend class ContentListHolder
;
167 ContentListHolder
* mContentListHolder
;
169 /** # of forms in the document, synchronously set */
172 static void TryReloadCharset(nsIDocumentViewer
* aViewer
,
173 int32_t& aCharsetSource
,
174 NotNull
<const Encoding
*>& aEncoding
);
175 void TryUserForcedCharset(nsIDocumentViewer
* aViewer
, nsIDocShell
* aDocShell
,
176 int32_t& aCharsetSource
,
177 NotNull
<const Encoding
*>& aEncoding
,
178 bool& aForceAutoDetection
);
179 void TryParentCharset(nsIDocShell
* aDocShell
, int32_t& charsetSource
,
180 NotNull
<const Encoding
*>& aEncoding
,
181 bool& aForceAutoDetection
);
183 // Load flags of the document's channel
186 bool mWarnedWidthHeight
;
189 * Set to true once we know that we are loading plain text content.
194 * Set to true once we know that we are viewing source.
199 namespace mozilla::dom
{
201 inline nsHTMLDocument
* Document::AsHTMLDocument() {
202 MOZ_ASSERT(IsHTMLOrXHTML());
203 return static_cast<nsHTMLDocument
*>(this);
206 inline const nsHTMLDocument
* Document::AsHTMLDocument() const {
207 MOZ_ASSERT(IsHTMLOrXHTML());
208 return static_cast<const nsHTMLDocument
*>(this);
211 } // namespace mozilla::dom
213 #endif /* nsHTMLDocument_h___ */