nsHTMLDocument.h 13 KB

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