Line data Source code
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___
8 :
9 : #include "mozilla/Attributes.h"
10 : #include "nsDocument.h"
11 : #include "nsIHTMLDocument.h"
12 : #include "nsIDOMHTMLDocument.h"
13 : #include "nsIDOMHTMLCollection.h"
14 : #include "nsIScriptElement.h"
15 : #include "nsTArray.h"
16 :
17 : #include "PLDHashTable.h"
18 : #include "nsIHttpChannel.h"
19 : #include "nsHTMLStyleSheet.h"
20 :
21 : #include "nsICommandManager.h"
22 : #include "mozilla/dom/HTMLSharedElement.h"
23 : #include "mozilla/dom/BindingDeclarations.h"
24 :
25 : class nsIEditor;
26 : class nsIURI;
27 : class nsIDocShell;
28 : class nsICachingChannel;
29 : class nsIWyciwygChannel;
30 : class nsILoadGroup;
31 :
32 : namespace mozilla {
33 : namespace dom {
34 : class HTMLAllCollection;
35 : } // namespace dom
36 : } // namespace mozilla
37 :
38 : class nsHTMLDocument : public nsDocument,
39 : public nsIHTMLDocument,
40 : public nsIDOMHTMLDocument
41 : {
42 : public:
43 : using nsDocument::SetDocumentURI;
44 : using nsDocument::GetPlugins;
45 :
46 : nsHTMLDocument();
47 : virtual nsresult Init() override;
48 :
49 : NS_DECL_ISUPPORTS_INHERITED
50 164 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHTMLDocument, nsDocument)
51 :
52 : // nsIDocument
53 : virtual void Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) override;
54 : virtual void ResetToURI(nsIURI* aURI, nsILoadGroup* aLoadGroup,
55 : nsIPrincipal* aPrincipal) override;
56 :
57 : virtual nsresult StartDocumentLoad(const char* aCommand,
58 : nsIChannel* aChannel,
59 : nsILoadGroup* aLoadGroup,
60 : nsISupports* aContainer,
61 : nsIStreamListener **aDocListener,
62 : bool aReset = true,
63 : nsIContentSink* aSink = nullptr) override;
64 : virtual void StopDocumentLoad() override;
65 :
66 : virtual void BeginLoad() override;
67 : virtual void EndLoad() override;
68 :
69 : // nsIHTMLDocument
70 : virtual void SetCompatibilityMode(nsCompatibility aMode) override;
71 :
72 0 : virtual bool IsWriting() override
73 : {
74 0 : return mWriteLevel != uint32_t(0);
75 : }
76 :
77 : virtual nsContentList* GetForms() override;
78 :
79 : virtual nsContentList* GetFormControls() override;
80 :
81 : // nsIDOMDocument interface
82 : using nsDocument::CreateElement;
83 : using nsDocument::CreateElementNS;
84 59 : NS_FORWARD_NSIDOMDOCUMENT(nsDocument::)
85 :
86 : // And explicitly import the things from nsDocument that we just shadowed
87 : using nsDocument::GetImplementation;
88 : using nsDocument::GetTitle;
89 : using nsDocument::SetTitle;
90 : using nsDocument::GetLastStyleSheetSet;
91 : using nsDocument::MozSetImageElement;
92 :
93 : // nsIDOMNode interface
94 0 : NS_FORWARD_NSIDOMNODE_TO_NSINODE
95 :
96 : // nsIDOMHTMLDocument interface
97 : NS_DECL_NSIDOMHTMLDOCUMENT
98 :
99 : mozilla::dom::HTMLAllCollection* All();
100 :
101 : nsISupports* ResolveName(const nsAString& aName, nsWrapperCache **aCache);
102 :
103 : virtual void AddedForm() override;
104 : virtual void RemovedForm() override;
105 : virtual int32_t GetNumFormsSynchronous() override;
106 : virtual void TearingDownEditor(nsIEditor *aEditor) override;
107 0 : virtual void SetIsXHTML(bool aXHTML) override
108 : {
109 0 : mType = (aXHTML ? eXHTML : eHTML);
110 0 : }
111 0 : virtual void SetDocWriteDisabled(bool aDisabled) override
112 : {
113 0 : mDisableDocWrite = aDisabled;
114 0 : }
115 :
116 : nsresult ChangeContentEditableCount(nsIContent *aElement, int32_t aChange) override;
117 : void DeferredContentEditableCountChange(nsIContent *aElement);
118 :
119 82 : virtual EditingState GetEditingState() override
120 : {
121 82 : return mEditingState;
122 : }
123 :
124 0 : virtual void DisableCookieAccess() override
125 : {
126 0 : mDisableCookieAccess = true;
127 0 : }
128 :
129 : class nsAutoEditingState {
130 : public:
131 0 : nsAutoEditingState(nsHTMLDocument* aDoc, EditingState aState)
132 0 : : mDoc(aDoc), mSavedState(aDoc->mEditingState)
133 : {
134 0 : aDoc->mEditingState = aState;
135 0 : }
136 0 : ~nsAutoEditingState() {
137 0 : mDoc->mEditingState = mSavedState;
138 0 : }
139 : private:
140 : nsHTMLDocument* mDoc;
141 : EditingState mSavedState;
142 : };
143 : friend class nsAutoEditingState;
144 :
145 : void EndUpdate(nsUpdateType aUpdateType) override;
146 :
147 : virtual void SetMayStartLayout(bool aMayStartLayout) override;
148 :
149 : virtual nsresult SetEditingState(EditingState aState) override;
150 :
151 : virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
152 : bool aPreallocateChildren) const override;
153 :
154 : virtual void RemovedFromDocShell() override;
155 :
156 19 : virtual mozilla::dom::Element *GetElementById(const nsAString& aElementId) override
157 : {
158 19 : return nsDocument::GetElementById(aElementId);
159 : }
160 :
161 : virtual void DocAddSizeOfExcludingThis(nsWindowSizes* aWindowSizes) const override;
162 : // DocAddSizeOfIncludingThis is inherited from nsIDocument.
163 :
164 : virtual bool WillIgnoreCharsetOverride() override;
165 :
166 : // WebIDL API
167 : virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
168 : override;
169 : void SetDomain(const nsAString& aDomain, mozilla::ErrorResult& rv);
170 : bool IsRegistrableDomainSuffixOfOrEqualTo(const nsAString& aHostSuffixString,
171 : const nsACString& aOrigHost);
172 : void GetCookie(nsAString& aCookie, mozilla::ErrorResult& rv);
173 : void SetCookie(const nsAString& aCookie, mozilla::ErrorResult& rv);
174 : void NamedGetter(JSContext* cx, const nsAString& aName, bool& aFound,
175 : JS::MutableHandle<JSObject*> aRetval,
176 : mozilla::ErrorResult& rv);
177 : void GetSupportedNames(nsTArray<nsString>& aNames);
178 : nsGenericHTMLElement *GetBody();
179 : void SetBody(nsGenericHTMLElement* aBody, mozilla::ErrorResult& rv);
180 0 : mozilla::dom::HTMLSharedElement *GetHead() {
181 0 : return static_cast<mozilla::dom::HTMLSharedElement*>(GetHeadElement());
182 : }
183 : nsIHTMLCollection* Images();
184 : nsIHTMLCollection* Embeds();
185 : nsIHTMLCollection* Plugins();
186 : nsIHTMLCollection* Links();
187 0 : nsIHTMLCollection* Forms()
188 : {
189 0 : return nsHTMLDocument::GetForms();
190 : }
191 : nsIHTMLCollection* Scripts();
192 0 : already_AddRefed<nsContentList> GetElementsByName(const nsAString & aName)
193 : {
194 : return NS_GetFuncStringNodeList(this, MatchNameAttribute, nullptr,
195 0 : UseExistingNameString, aName);
196 : }
197 : already_AddRefed<nsIDocument> Open(JSContext* cx,
198 : const nsAString& aType,
199 : const nsAString& aReplace,
200 : mozilla::ErrorResult& rv);
201 : already_AddRefed<nsPIDOMWindowOuter>
202 : Open(JSContext* cx,
203 : const nsAString& aURL,
204 : const nsAString& aName,
205 : const nsAString& aFeatures,
206 : bool aReplace,
207 : mozilla::ErrorResult& rv);
208 : void Close(mozilla::ErrorResult& rv);
209 : void Write(JSContext* cx, const mozilla::dom::Sequence<nsString>& aText,
210 : mozilla::ErrorResult& rv);
211 : void Writeln(JSContext* cx, const mozilla::dom::Sequence<nsString>& aText,
212 : mozilla::ErrorResult& rv);
213 0 : void GetDesignMode(nsAString& aDesignMode,
214 : nsIPrincipal& aSubjectPrincipal)
215 : {
216 0 : GetDesignMode(aDesignMode);
217 0 : }
218 : void SetDesignMode(const nsAString& aDesignMode,
219 : nsIPrincipal& aSubjectPrincipal,
220 : mozilla::ErrorResult& rv);
221 : void SetDesignMode(const nsAString& aDesignMode,
222 : const mozilla::Maybe<nsIPrincipal*>& aSubjectPrincipal,
223 : mozilla::ErrorResult& rv);
224 : bool ExecCommand(const nsAString& aCommandID, bool aDoShowUI,
225 : const nsAString& aValue,
226 : nsIPrincipal& aSubjectPrincipal,
227 : mozilla::ErrorResult& rv);
228 : bool QueryCommandEnabled(const nsAString& aCommandID,
229 : nsIPrincipal& aSubjectPrincipal,
230 : mozilla::ErrorResult& rv);
231 : bool QueryCommandIndeterm(const nsAString& aCommandID,
232 : mozilla::ErrorResult& rv);
233 : bool QueryCommandState(const nsAString& aCommandID, mozilla::ErrorResult& rv);
234 : bool QueryCommandSupported(const nsAString& aCommandID,
235 : mozilla::dom::CallerType aCallerType);
236 : void QueryCommandValue(const nsAString& aCommandID, nsAString& aValue,
237 : mozilla::ErrorResult& rv);
238 : // The XPCOM Get/SetFgColor work OK for us, since they never throw.
239 : // The XPCOM Get/SetLinkColor work OK for us, since they never throw.
240 : // The XPCOM Get/SetVLinkColor work OK for us, since they never throw.
241 : // The XPCOM Get/SetALinkColor work OK for us, since they never throw.
242 : // The XPCOM Get/SetBgColor work OK for us, since they never throw.
243 : nsIHTMLCollection* Anchors();
244 : nsIHTMLCollection* Applets();
245 : void Clear() const
246 : {
247 : // Deprecated
248 : }
249 : mozilla::dom::Selection* GetSelection(mozilla::ErrorResult& aRv);
250 : // The XPCOM CaptureEvents works fine for us.
251 : // The XPCOM ReleaseEvents works fine for us.
252 : // We're picking up GetLocation from Document
253 1 : already_AddRefed<mozilla::dom::Location> GetLocation() const
254 : {
255 1 : return nsIDocument::GetLocation();
256 : }
257 :
258 0 : virtual nsHTMLDocument* AsHTMLDocument() override { return this; }
259 :
260 : protected:
261 : ~nsHTMLDocument();
262 :
263 : nsresult GetBodySize(int32_t* aWidth,
264 : int32_t* aHeight);
265 :
266 : nsIContent *MatchId(nsIContent *aContent, const nsAString& aId);
267 :
268 : static bool MatchLinks(mozilla::dom::Element* aElement, int32_t aNamespaceID,
269 : nsIAtom* aAtom, void* aData);
270 : static bool MatchAnchors(mozilla::dom::Element* aElement, int32_t aNamespaceID,
271 : nsIAtom* aAtom, void* aData);
272 : static bool MatchNameAttribute(mozilla::dom::Element* aElement,
273 : int32_t aNamespaceID,
274 : nsIAtom* aAtom, void* aData);
275 : static void* UseExistingNameString(nsINode* aRootNode, const nsString* aName);
276 :
277 : static void DocumentWriteTerminationFunc(nsISupports *aRef);
278 :
279 : already_AddRefed<nsIURI> GetDomainURI();
280 : already_AddRefed<nsIURI> CreateInheritingURIForHost(const nsACString& aHostString);
281 : already_AddRefed<nsIURI> RegistrableDomainSuffixOfInternal(const nsAString& aHostSuffixString,
282 : nsIURI* aOrigHost);
283 :
284 :
285 : nsresult WriteCommon(JSContext *cx, const nsAString& aText,
286 : bool aNewlineTerminate);
287 : // A version of WriteCommon used by WebIDL bindings
288 : void WriteCommon(JSContext *cx,
289 : const mozilla::dom::Sequence<nsString>& aText,
290 : bool aNewlineTerminate,
291 : mozilla::ErrorResult& rv);
292 :
293 : nsresult CreateAndAddWyciwygChannel(void);
294 : nsresult RemoveWyciwygChannel(void);
295 :
296 : // This should *ONLY* be used in GetCookie/SetCookie.
297 : already_AddRefed<nsIChannel> CreateDummyChannelForCookies(nsIURI* aCodebaseURI);
298 :
299 : /**
300 : * Like IsEditingOn(), but will flush as needed first.
301 : */
302 : bool IsEditingOnAfterFlush();
303 :
304 : void *GenerateParserKey(void);
305 :
306 : RefPtr<nsContentList> mImages;
307 : RefPtr<nsContentList> mApplets;
308 : RefPtr<nsContentList> mEmbeds;
309 : RefPtr<nsContentList> mLinks;
310 : RefPtr<nsContentList> mAnchors;
311 : RefPtr<nsContentList> mScripts;
312 : RefPtr<nsContentList> mForms;
313 : RefPtr<nsContentList> mFormControls;
314 :
315 : RefPtr<mozilla::dom::HTMLAllCollection> mAll;
316 :
317 : /** # of forms in the document, synchronously set */
318 : int32_t mNumForms;
319 :
320 : static uint32_t gWyciwygSessionCnt;
321 :
322 : static void TryHintCharset(nsIContentViewer* aContentViewer,
323 : int32_t& aCharsetSource,
324 : NotNull<const Encoding*>& aEncoding);
325 : void TryUserForcedCharset(nsIContentViewer* aCv,
326 : nsIDocShell* aDocShell,
327 : int32_t& aCharsetSource,
328 : NotNull<const Encoding*>& aEncoding);
329 : static void TryCacheCharset(nsICachingChannel* aCachingChannel,
330 : int32_t& aCharsetSource,
331 : NotNull<const Encoding*>& aEncoding);
332 : void TryParentCharset(nsIDocShell* aDocShell,
333 : int32_t& charsetSource,
334 : NotNull<const Encoding*>& aEncoding);
335 : void TryTLD(int32_t& aCharsetSource, NotNull<const Encoding*>& aCharset);
336 : static void TryFallback(int32_t& aCharsetSource,
337 : NotNull<const Encoding*>& aEncoding);
338 :
339 : // Override so we can munge the charset on our wyciwyg channel as needed.
340 : virtual void
341 : SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding) override;
342 :
343 : // Tracks if we are currently processing any document.write calls (either
344 : // implicit or explicit). Note that if a write call writes out something which
345 : // would block the parser, then mWriteLevel will be incorrect until the parser
346 : // finishes processing that script.
347 : uint32_t mWriteLevel;
348 :
349 : // Load flags of the document's channel
350 : uint32_t mLoadFlags;
351 :
352 : bool mTooDeepWriteRecursion;
353 :
354 : bool mDisableDocWrite;
355 :
356 : bool mWarnedWidthHeight;
357 :
358 : nsCOMPtr<nsIWyciwygChannel> mWyciwygChannel;
359 :
360 : /* Midas implementation */
361 : nsresult GetMidasCommandManager(nsICommandManager** aCommandManager);
362 :
363 : nsCOMPtr<nsICommandManager> mMidasCommandManager;
364 :
365 : nsresult TurnEditingOff();
366 : nsresult EditingStateChanged();
367 : void MaybeEditingStateChanged();
368 :
369 : uint32_t mContentEditableCount;
370 : EditingState mEditingState;
371 :
372 : // When false, the .cookies property is completely disabled
373 : bool mDisableCookieAccess;
374 :
375 : /**
376 : * Temporary flag that is set in EndUpdate() to ignore
377 : * MaybeEditingStateChanged() script runners from a nested scope.
378 : */
379 : bool mPendingMaybeEditingStateChanged;
380 : };
381 :
382 : #define NS_HTML_DOCUMENT_INTERFACE_TABLE_BEGIN(_class) \
383 : NS_DOCUMENT_INTERFACE_TABLE_BEGIN(_class) \
384 : NS_INTERFACE_TABLE_ENTRY(_class, nsIHTMLDocument) \
385 : NS_INTERFACE_TABLE_ENTRY(_class, nsIDOMHTMLDocument)
386 :
387 : #endif /* nsHTMLDocument_h___ */
|