Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set sw=2 ts=2 et tw=78: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * This file is near-OBSOLETE. It is used for about:blank only and for the |
michael@0 | 9 | * HTML element factory. |
michael@0 | 10 | * Don't bother adding new stuff in this file. |
michael@0 | 11 | */ |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/ArrayUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "nsContentSink.h" |
michael@0 | 16 | #include "nsCOMPtr.h" |
michael@0 | 17 | #include "nsReadableUtils.h" |
michael@0 | 18 | #include "nsUnicharUtils.h" |
michael@0 | 19 | #include "nsIHTMLContentSink.h" |
michael@0 | 20 | #include "nsIInterfaceRequestor.h" |
michael@0 | 21 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 22 | #include "nsScriptLoader.h" |
michael@0 | 23 | #include "nsIURI.h" |
michael@0 | 24 | #include "nsNetUtil.h" |
michael@0 | 25 | #include "nsIContentViewer.h" |
michael@0 | 26 | #include "nsIMarkupDocumentViewer.h" |
michael@0 | 27 | #include "nsINodeInfo.h" |
michael@0 | 28 | #include "nsToken.h" |
michael@0 | 29 | #include "nsIAppShell.h" |
michael@0 | 30 | #include "nsCRT.h" |
michael@0 | 31 | #include "prtime.h" |
michael@0 | 32 | #include "prlog.h" |
michael@0 | 33 | #include "nsNodeUtils.h" |
michael@0 | 34 | #include "nsIContent.h" |
michael@0 | 35 | #include "mozilla/dom/Element.h" |
michael@0 | 36 | #include "mozilla/Preferences.h" |
michael@0 | 37 | |
michael@0 | 38 | #include "nsGenericHTMLElement.h" |
michael@0 | 39 | |
michael@0 | 40 | #include "nsIDOMDocument.h" |
michael@0 | 41 | #include "nsIDOMDocumentType.h" |
michael@0 | 42 | #include "nsIScriptElement.h" |
michael@0 | 43 | |
michael@0 | 44 | #include "nsIComponentManager.h" |
michael@0 | 45 | #include "nsIServiceManager.h" |
michael@0 | 46 | |
michael@0 | 47 | #include "nsGkAtoms.h" |
michael@0 | 48 | #include "nsContentUtils.h" |
michael@0 | 49 | #include "nsIChannel.h" |
michael@0 | 50 | #include "nsIHttpChannel.h" |
michael@0 | 51 | #include "nsIDocShell.h" |
michael@0 | 52 | #include "nsIDocument.h" |
michael@0 | 53 | #include "nsStubDocumentObserver.h" |
michael@0 | 54 | #include "nsIHTMLDocument.h" |
michael@0 | 55 | #include "nsIDOMHTMLMapElement.h" |
michael@0 | 56 | #include "nsICookieService.h" |
michael@0 | 57 | #include "nsTArray.h" |
michael@0 | 58 | #include "nsIScriptSecurityManager.h" |
michael@0 | 59 | #include "nsIPrincipal.h" |
michael@0 | 60 | #include "nsTextFragment.h" |
michael@0 | 61 | #include "nsIScriptGlobalObject.h" |
michael@0 | 62 | #include "nsNameSpaceManager.h" |
michael@0 | 63 | |
michael@0 | 64 | #include "nsIParserService.h" |
michael@0 | 65 | |
michael@0 | 66 | #include "nsIStyleSheetLinkingElement.h" |
michael@0 | 67 | #include "nsITimer.h" |
michael@0 | 68 | #include "nsError.h" |
michael@0 | 69 | #include "nsContentPolicyUtils.h" |
michael@0 | 70 | #include "nsIScriptContext.h" |
michael@0 | 71 | #include "nsStyleLinkElement.h" |
michael@0 | 72 | |
michael@0 | 73 | #include "nsWeakReference.h" // nsHTMLElementFactory supports weak references |
michael@0 | 74 | #include "nsIPrompt.h" |
michael@0 | 75 | #include "nsLayoutCID.h" |
michael@0 | 76 | #include "nsIDocShellTreeItem.h" |
michael@0 | 77 | |
michael@0 | 78 | #include "nsEscape.h" |
michael@0 | 79 | #include "nsNodeInfoManager.h" |
michael@0 | 80 | #include "nsContentCreatorFunctions.h" |
michael@0 | 81 | #include "mozAutoDocUpdate.h" |
michael@0 | 82 | #include "nsTextNode.h" |
michael@0 | 83 | |
michael@0 | 84 | using namespace mozilla; |
michael@0 | 85 | using namespace mozilla::dom; |
michael@0 | 86 | |
michael@0 | 87 | //---------------------------------------------------------------------- |
michael@0 | 88 | |
michael@0 | 89 | typedef nsGenericHTMLElement* |
michael@0 | 90 | (*contentCreatorCallback)(already_AddRefed<nsINodeInfo>&&, |
michael@0 | 91 | FromParser aFromParser); |
michael@0 | 92 | |
michael@0 | 93 | nsGenericHTMLElement* |
michael@0 | 94 | NS_NewHTMLNOTUSEDElement(already_AddRefed<nsINodeInfo>&& aNodeInfo, |
michael@0 | 95 | FromParser aFromParser) |
michael@0 | 96 | { |
michael@0 | 97 | NS_NOTREACHED("The element ctor should never be called"); |
michael@0 | 98 | return nullptr; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | #define HTML_TAG(_tag, _classname) NS_NewHTML##_classname##Element, |
michael@0 | 102 | #define HTML_HTMLELEMENT_TAG(_tag) NS_NewHTMLElement, |
michael@0 | 103 | #define HTML_OTHER(_tag) NS_NewHTMLNOTUSEDElement, |
michael@0 | 104 | static const contentCreatorCallback sContentCreatorCallbacks[] = { |
michael@0 | 105 | NS_NewHTMLUnknownElement, |
michael@0 | 106 | #include "nsHTMLTagList.h" |
michael@0 | 107 | #undef HTML_TAG |
michael@0 | 108 | #undef HTML_HTMLELEMENT_TAG |
michael@0 | 109 | #undef HTML_OTHER |
michael@0 | 110 | NS_NewHTMLUnknownElement |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | class SinkContext; |
michael@0 | 114 | class HTMLContentSink; |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * This class is near-OBSOLETE. It is used for about:blank only. |
michael@0 | 118 | * Don't bother adding new stuff in this file. |
michael@0 | 119 | */ |
michael@0 | 120 | class HTMLContentSink : public nsContentSink, |
michael@0 | 121 | public nsIHTMLContentSink |
michael@0 | 122 | { |
michael@0 | 123 | public: |
michael@0 | 124 | friend class SinkContext; |
michael@0 | 125 | |
michael@0 | 126 | HTMLContentSink(); |
michael@0 | 127 | virtual ~HTMLContentSink(); |
michael@0 | 128 | |
michael@0 | 129 | NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW |
michael@0 | 130 | |
michael@0 | 131 | nsresult Init(nsIDocument* aDoc, nsIURI* aURI, nsISupports* aContainer, |
michael@0 | 132 | nsIChannel* aChannel); |
michael@0 | 133 | |
michael@0 | 134 | // nsISupports |
michael@0 | 135 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 136 | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLContentSink, nsContentSink) |
michael@0 | 137 | |
michael@0 | 138 | // nsIContentSink |
michael@0 | 139 | NS_IMETHOD WillParse(void); |
michael@0 | 140 | NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode); |
michael@0 | 141 | NS_IMETHOD DidBuildModel(bool aTerminated); |
michael@0 | 142 | NS_IMETHOD WillInterrupt(void); |
michael@0 | 143 | NS_IMETHOD WillResume(void); |
michael@0 | 144 | NS_IMETHOD SetParser(nsParserBase* aParser); |
michael@0 | 145 | virtual void FlushPendingNotifications(mozFlushType aType); |
michael@0 | 146 | NS_IMETHOD SetDocumentCharset(nsACString& aCharset); |
michael@0 | 147 | virtual nsISupports *GetTarget(); |
michael@0 | 148 | virtual bool IsScriptExecuting(); |
michael@0 | 149 | |
michael@0 | 150 | // nsIHTMLContentSink |
michael@0 | 151 | NS_IMETHOD OpenContainer(ElementType aNodeType); |
michael@0 | 152 | NS_IMETHOD CloseContainer(ElementType aTag); |
michael@0 | 153 | |
michael@0 | 154 | protected: |
michael@0 | 155 | nsCOMPtr<nsIHTMLDocument> mHTMLDocument; |
michael@0 | 156 | |
michael@0 | 157 | // The maximum length of a text run |
michael@0 | 158 | int32_t mMaxTextRun; |
michael@0 | 159 | |
michael@0 | 160 | nsRefPtr<nsGenericHTMLElement> mRoot; |
michael@0 | 161 | nsRefPtr<nsGenericHTMLElement> mBody; |
michael@0 | 162 | nsRefPtr<nsGenericHTMLElement> mHead; |
michael@0 | 163 | |
michael@0 | 164 | nsAutoTArray<SinkContext*, 8> mContextStack; |
michael@0 | 165 | SinkContext* mCurrentContext; |
michael@0 | 166 | SinkContext* mHeadContext; |
michael@0 | 167 | |
michael@0 | 168 | // Boolean indicating whether we've seen a <head> tag that might have had |
michael@0 | 169 | // attributes once already. |
michael@0 | 170 | bool mHaveSeenHead; |
michael@0 | 171 | |
michael@0 | 172 | // Boolean indicating whether we've notified insertion of our root content |
michael@0 | 173 | // yet. We want to make sure to only do this once. |
michael@0 | 174 | bool mNotifiedRootInsertion; |
michael@0 | 175 | |
michael@0 | 176 | uint8_t mScriptEnabled : 1; |
michael@0 | 177 | uint8_t mFramesEnabled : 1; |
michael@0 | 178 | uint8_t unused : 6; // bits available if someone needs one |
michael@0 | 179 | |
michael@0 | 180 | nsINodeInfo* mNodeInfoCache[NS_HTML_TAG_MAX + 1]; |
michael@0 | 181 | |
michael@0 | 182 | nsresult FlushTags(); |
michael@0 | 183 | |
michael@0 | 184 | // Routines for tags that require special handling |
michael@0 | 185 | nsresult CloseHTML(); |
michael@0 | 186 | nsresult OpenBody(); |
michael@0 | 187 | nsresult CloseBody(); |
michael@0 | 188 | |
michael@0 | 189 | void CloseHeadContext(); |
michael@0 | 190 | |
michael@0 | 191 | // nsContentSink overrides |
michael@0 | 192 | void UpdateChildCounts(); |
michael@0 | 193 | |
michael@0 | 194 | void NotifyInsert(nsIContent* aContent, |
michael@0 | 195 | nsIContent* aChildContent, |
michael@0 | 196 | int32_t aIndexInContainer); |
michael@0 | 197 | void NotifyRootInsertion(); |
michael@0 | 198 | }; |
michael@0 | 199 | |
michael@0 | 200 | class SinkContext |
michael@0 | 201 | { |
michael@0 | 202 | public: |
michael@0 | 203 | SinkContext(HTMLContentSink* aSink); |
michael@0 | 204 | ~SinkContext(); |
michael@0 | 205 | |
michael@0 | 206 | nsresult Begin(nsHTMLTag aNodeType, nsGenericHTMLElement* aRoot, |
michael@0 | 207 | uint32_t aNumFlushed, int32_t aInsertionPoint); |
michael@0 | 208 | nsresult OpenBody(); |
michael@0 | 209 | nsresult CloseBody(); |
michael@0 | 210 | nsresult End(); |
michael@0 | 211 | |
michael@0 | 212 | nsresult GrowStack(); |
michael@0 | 213 | nsresult FlushTags(); |
michael@0 | 214 | |
michael@0 | 215 | bool IsCurrentContainer(nsHTMLTag mType); |
michael@0 | 216 | |
michael@0 | 217 | void DidAddContent(nsIContent* aContent); |
michael@0 | 218 | void UpdateChildCounts(); |
michael@0 | 219 | |
michael@0 | 220 | private: |
michael@0 | 221 | // Function to check whether we've notified for the current content. |
michael@0 | 222 | // What this actually does is check whether we've notified for all |
michael@0 | 223 | // of the parent's kids. |
michael@0 | 224 | bool HaveNotifiedForCurrentContent() const; |
michael@0 | 225 | |
michael@0 | 226 | public: |
michael@0 | 227 | HTMLContentSink* mSink; |
michael@0 | 228 | int32_t mNotifyLevel; |
michael@0 | 229 | |
michael@0 | 230 | struct Node { |
michael@0 | 231 | nsHTMLTag mType; |
michael@0 | 232 | nsGenericHTMLElement* mContent; |
michael@0 | 233 | uint32_t mNumFlushed; |
michael@0 | 234 | int32_t mInsertionPoint; |
michael@0 | 235 | |
michael@0 | 236 | nsIContent *Add(nsIContent *child); |
michael@0 | 237 | }; |
michael@0 | 238 | |
michael@0 | 239 | Node* mStack; |
michael@0 | 240 | int32_t mStackSize; |
michael@0 | 241 | int32_t mStackPos; |
michael@0 | 242 | }; |
michael@0 | 243 | |
michael@0 | 244 | nsresult |
michael@0 | 245 | NS_NewHTMLElement(Element** aResult, already_AddRefed<nsINodeInfo>&& aNodeInfo, |
michael@0 | 246 | FromParser aFromParser) |
michael@0 | 247 | { |
michael@0 | 248 | *aResult = nullptr; |
michael@0 | 249 | |
michael@0 | 250 | nsCOMPtr<nsINodeInfo> nodeInfo = aNodeInfo; |
michael@0 | 251 | |
michael@0 | 252 | nsIParserService* parserService = nsContentUtils::GetParserService(); |
michael@0 | 253 | if (!parserService) |
michael@0 | 254 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 255 | |
michael@0 | 256 | nsIAtom *name = nodeInfo->NameAtom(); |
michael@0 | 257 | |
michael@0 | 258 | NS_ASSERTION(nodeInfo->NamespaceEquals(kNameSpaceID_XHTML), |
michael@0 | 259 | "Trying to HTML elements that don't have the XHTML namespace"); |
michael@0 | 260 | |
michael@0 | 261 | // Per the Custom Element specification, unknown tags that are valid custom |
michael@0 | 262 | // element names should be HTMLElement instead of HTMLUnknownElement. |
michael@0 | 263 | int32_t tag = parserService->HTMLCaseSensitiveAtomTagToId(name); |
michael@0 | 264 | if (tag == eHTMLTag_userdefined && |
michael@0 | 265 | nsContentUtils::IsCustomElementName(name)) { |
michael@0 | 266 | nsIDocument* doc = nodeInfo->GetDocument(); |
michael@0 | 267 | |
michael@0 | 268 | NS_IF_ADDREF(*aResult = NS_NewHTMLElement(nodeInfo.forget(), aFromParser)); |
michael@0 | 269 | if (!*aResult) { |
michael@0 | 270 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // Element may be unresolved at this point. |
michael@0 | 274 | doc->RegisterUnresolvedElement(*aResult); |
michael@0 | 275 | |
michael@0 | 276 | // Try to enqueue a created callback. The custom element data will be set |
michael@0 | 277 | // and created callback will be enqueued if the custom element type |
michael@0 | 278 | // has already been registered. |
michael@0 | 279 | doc->EnqueueLifecycleCallback(nsIDocument::eCreated, *aResult); |
michael@0 | 280 | |
michael@0 | 281 | return NS_OK; |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | *aResult = CreateHTMLElement(tag, |
michael@0 | 285 | nodeInfo.forget(), aFromParser).take(); |
michael@0 | 286 | return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | already_AddRefed<nsGenericHTMLElement> |
michael@0 | 290 | CreateHTMLElement(uint32_t aNodeType, |
michael@0 | 291 | already_AddRefed<nsINodeInfo>&& aNodeInfo, |
michael@0 | 292 | FromParser aFromParser) |
michael@0 | 293 | { |
michael@0 | 294 | NS_ASSERTION(aNodeType <= NS_HTML_TAG_MAX || |
michael@0 | 295 | aNodeType == eHTMLTag_userdefined, |
michael@0 | 296 | "aNodeType is out of bounds"); |
michael@0 | 297 | |
michael@0 | 298 | contentCreatorCallback cb = sContentCreatorCallbacks[aNodeType]; |
michael@0 | 299 | |
michael@0 | 300 | NS_ASSERTION(cb != NS_NewHTMLNOTUSEDElement, |
michael@0 | 301 | "Don't know how to construct tag element!"); |
michael@0 | 302 | |
michael@0 | 303 | nsRefPtr<nsGenericHTMLElement> result = cb(Move(aNodeInfo), aFromParser); |
michael@0 | 304 | |
michael@0 | 305 | return result.forget(); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | //---------------------------------------------------------------------- |
michael@0 | 309 | |
michael@0 | 310 | SinkContext::SinkContext(HTMLContentSink* aSink) |
michael@0 | 311 | : mSink(aSink), |
michael@0 | 312 | mNotifyLevel(0), |
michael@0 | 313 | mStack(nullptr), |
michael@0 | 314 | mStackSize(0), |
michael@0 | 315 | mStackPos(0) |
michael@0 | 316 | { |
michael@0 | 317 | MOZ_COUNT_CTOR(SinkContext); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | SinkContext::~SinkContext() |
michael@0 | 321 | { |
michael@0 | 322 | MOZ_COUNT_DTOR(SinkContext); |
michael@0 | 323 | |
michael@0 | 324 | if (mStack) { |
michael@0 | 325 | for (int32_t i = 0; i < mStackPos; i++) { |
michael@0 | 326 | NS_RELEASE(mStack[i].mContent); |
michael@0 | 327 | } |
michael@0 | 328 | delete [] mStack; |
michael@0 | 329 | } |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | nsresult |
michael@0 | 333 | SinkContext::Begin(nsHTMLTag aNodeType, |
michael@0 | 334 | nsGenericHTMLElement* aRoot, |
michael@0 | 335 | uint32_t aNumFlushed, |
michael@0 | 336 | int32_t aInsertionPoint) |
michael@0 | 337 | { |
michael@0 | 338 | if (mStackSize < 1) { |
michael@0 | 339 | nsresult rv = GrowStack(); |
michael@0 | 340 | if (NS_FAILED(rv)) { |
michael@0 | 341 | return rv; |
michael@0 | 342 | } |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | mStack[0].mType = aNodeType; |
michael@0 | 346 | mStack[0].mContent = aRoot; |
michael@0 | 347 | mStack[0].mNumFlushed = aNumFlushed; |
michael@0 | 348 | mStack[0].mInsertionPoint = aInsertionPoint; |
michael@0 | 349 | NS_ADDREF(aRoot); |
michael@0 | 350 | mStackPos = 1; |
michael@0 | 351 | |
michael@0 | 352 | return NS_OK; |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | bool |
michael@0 | 356 | SinkContext::IsCurrentContainer(nsHTMLTag aTag) |
michael@0 | 357 | { |
michael@0 | 358 | if (aTag == mStack[mStackPos - 1].mType) { |
michael@0 | 359 | return true; |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | return false; |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | void |
michael@0 | 366 | SinkContext::DidAddContent(nsIContent* aContent) |
michael@0 | 367 | { |
michael@0 | 368 | if ((mStackPos == 2) && (mSink->mBody == mStack[1].mContent)) { |
michael@0 | 369 | // We just finished adding something to the body |
michael@0 | 370 | mNotifyLevel = 0; |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | // If we just added content to a node for which |
michael@0 | 374 | // an insertion happen, we need to do an immediate |
michael@0 | 375 | // notification for that insertion. |
michael@0 | 376 | if (0 < mStackPos && |
michael@0 | 377 | mStack[mStackPos - 1].mInsertionPoint != -1 && |
michael@0 | 378 | mStack[mStackPos - 1].mNumFlushed < |
michael@0 | 379 | mStack[mStackPos - 1].mContent->GetChildCount()) { |
michael@0 | 380 | nsIContent* parent = mStack[mStackPos - 1].mContent; |
michael@0 | 381 | int32_t childIndex = mStack[mStackPos - 1].mInsertionPoint - 1; |
michael@0 | 382 | NS_ASSERTION(parent->GetChildAt(childIndex) == aContent, |
michael@0 | 383 | "Flushing the wrong child."); |
michael@0 | 384 | mSink->NotifyInsert(parent, aContent, childIndex); |
michael@0 | 385 | mStack[mStackPos - 1].mNumFlushed = parent->GetChildCount(); |
michael@0 | 386 | } else if (mSink->IsTimeToNotify()) { |
michael@0 | 387 | FlushTags(); |
michael@0 | 388 | } |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | nsresult |
michael@0 | 392 | SinkContext::OpenBody() |
michael@0 | 393 | { |
michael@0 | 394 | if (mStackPos <= 0) { |
michael@0 | 395 | NS_ERROR("container w/o parent"); |
michael@0 | 396 | |
michael@0 | 397 | return NS_ERROR_FAILURE; |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | nsresult rv; |
michael@0 | 401 | if (mStackPos + 1 > mStackSize) { |
michael@0 | 402 | rv = GrowStack(); |
michael@0 | 403 | if (NS_FAILED(rv)) { |
michael@0 | 404 | return rv; |
michael@0 | 405 | } |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | nsCOMPtr<nsINodeInfo> nodeInfo = |
michael@0 | 409 | mSink->mNodeInfoManager->GetNodeInfo(nsGkAtoms::body, nullptr, |
michael@0 | 410 | kNameSpaceID_XHTML, |
michael@0 | 411 | nsIDOMNode::ELEMENT_NODE); |
michael@0 | 412 | NS_ENSURE_TRUE(nodeInfo, NS_ERROR_UNEXPECTED); |
michael@0 | 413 | |
michael@0 | 414 | // Make the content object |
michael@0 | 415 | nsRefPtr<nsGenericHTMLElement> body = |
michael@0 | 416 | NS_NewHTMLBodyElement(nodeInfo.forget(), FROM_PARSER_NETWORK); |
michael@0 | 417 | if (!body) { |
michael@0 | 418 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | mStack[mStackPos].mType = eHTMLTag_body; |
michael@0 | 422 | body.forget(&mStack[mStackPos].mContent); |
michael@0 | 423 | mStack[mStackPos].mNumFlushed = 0; |
michael@0 | 424 | mStack[mStackPos].mInsertionPoint = -1; |
michael@0 | 425 | ++mStackPos; |
michael@0 | 426 | mStack[mStackPos - 2].Add(mStack[mStackPos - 1].mContent); |
michael@0 | 427 | |
michael@0 | 428 | return NS_OK; |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | bool |
michael@0 | 432 | SinkContext::HaveNotifiedForCurrentContent() const |
michael@0 | 433 | { |
michael@0 | 434 | if (0 < mStackPos) { |
michael@0 | 435 | nsIContent* parent = mStack[mStackPos - 1].mContent; |
michael@0 | 436 | return mStack[mStackPos-1].mNumFlushed == parent->GetChildCount(); |
michael@0 | 437 | } |
michael@0 | 438 | |
michael@0 | 439 | return true; |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | nsIContent * |
michael@0 | 443 | SinkContext::Node::Add(nsIContent *child) |
michael@0 | 444 | { |
michael@0 | 445 | NS_ASSERTION(mContent, "No parent to insert/append into!"); |
michael@0 | 446 | if (mInsertionPoint != -1) { |
michael@0 | 447 | NS_ASSERTION(mNumFlushed == mContent->GetChildCount(), |
michael@0 | 448 | "Inserting multiple children without flushing."); |
michael@0 | 449 | mContent->InsertChildAt(child, mInsertionPoint++, false); |
michael@0 | 450 | } else { |
michael@0 | 451 | mContent->AppendChildTo(child, false); |
michael@0 | 452 | } |
michael@0 | 453 | return child; |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | nsresult |
michael@0 | 457 | SinkContext::CloseBody() |
michael@0 | 458 | { |
michael@0 | 459 | NS_ASSERTION(mStackPos > 0, |
michael@0 | 460 | "stack out of bounds. wrong context probably!"); |
michael@0 | 461 | |
michael@0 | 462 | if (mStackPos <= 0) { |
michael@0 | 463 | return NS_OK; // Fix crash - Ref. bug 45975 or 45007 |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | --mStackPos; |
michael@0 | 467 | NS_ASSERTION(mStack[mStackPos].mType == eHTMLTag_body, |
michael@0 | 468 | "Tag mismatch. Closing tag on wrong context or something?"); |
michael@0 | 469 | |
michael@0 | 470 | nsGenericHTMLElement* content = mStack[mStackPos].mContent; |
michael@0 | 471 | |
michael@0 | 472 | content->Compact(); |
michael@0 | 473 | |
michael@0 | 474 | // If we're in a state where we do append notifications as |
michael@0 | 475 | // we go up the tree, and we're at the level where the next |
michael@0 | 476 | // notification needs to be done, do the notification. |
michael@0 | 477 | if (mNotifyLevel >= mStackPos) { |
michael@0 | 478 | // Check to see if new content has been added after our last |
michael@0 | 479 | // notification |
michael@0 | 480 | |
michael@0 | 481 | if (mStack[mStackPos].mNumFlushed < content->GetChildCount()) { |
michael@0 | 482 | mSink->NotifyAppend(content, mStack[mStackPos].mNumFlushed); |
michael@0 | 483 | mStack[mStackPos].mNumFlushed = content->GetChildCount(); |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | // Indicate that notification has now happened at this level |
michael@0 | 487 | mNotifyLevel = mStackPos - 1; |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | DidAddContent(content); |
michael@0 | 491 | NS_IF_RELEASE(content); |
michael@0 | 492 | |
michael@0 | 493 | return NS_OK; |
michael@0 | 494 | } |
michael@0 | 495 | |
michael@0 | 496 | nsresult |
michael@0 | 497 | SinkContext::End() |
michael@0 | 498 | { |
michael@0 | 499 | for (int32_t i = 0; i < mStackPos; i++) { |
michael@0 | 500 | NS_RELEASE(mStack[i].mContent); |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | mStackPos = 0; |
michael@0 | 504 | |
michael@0 | 505 | return NS_OK; |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | nsresult |
michael@0 | 509 | SinkContext::GrowStack() |
michael@0 | 510 | { |
michael@0 | 511 | int32_t newSize = mStackSize * 2; |
michael@0 | 512 | if (newSize == 0) { |
michael@0 | 513 | newSize = 32; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | Node* stack = new Node[newSize]; |
michael@0 | 517 | |
michael@0 | 518 | if (mStackPos != 0) { |
michael@0 | 519 | memcpy(stack, mStack, sizeof(Node) * mStackPos); |
michael@0 | 520 | delete [] mStack; |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | mStack = stack; |
michael@0 | 524 | mStackSize = newSize; |
michael@0 | 525 | |
michael@0 | 526 | return NS_OK; |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | /** |
michael@0 | 530 | * NOTE!! Forked into nsXMLContentSink. Please keep in sync. |
michael@0 | 531 | * |
michael@0 | 532 | * Flush all elements that have been seen so far such that |
michael@0 | 533 | * they are visible in the tree. Specifically, make sure |
michael@0 | 534 | * that they are all added to their respective parents. |
michael@0 | 535 | * Also, do notification at the top for all content that |
michael@0 | 536 | * has been newly added so that the frame tree is complete. |
michael@0 | 537 | */ |
michael@0 | 538 | nsresult |
michael@0 | 539 | SinkContext::FlushTags() |
michael@0 | 540 | { |
michael@0 | 541 | mSink->mDeferredFlushTags = false; |
michael@0 | 542 | bool oldBeganUpdate = mSink->mBeganUpdate; |
michael@0 | 543 | uint32_t oldUpdates = mSink->mUpdatesInNotification; |
michael@0 | 544 | |
michael@0 | 545 | ++(mSink->mInNotification); |
michael@0 | 546 | mSink->mUpdatesInNotification = 0; |
michael@0 | 547 | { |
michael@0 | 548 | // Scope so we call EndUpdate before we decrease mInNotification |
michael@0 | 549 | mozAutoDocUpdate updateBatch(mSink->mDocument, UPDATE_CONTENT_MODEL, |
michael@0 | 550 | true); |
michael@0 | 551 | mSink->mBeganUpdate = true; |
michael@0 | 552 | |
michael@0 | 553 | // Start from the base of the stack (growing downward) and do |
michael@0 | 554 | // a notification from the node that is closest to the root of |
michael@0 | 555 | // tree for any content that has been added. |
michael@0 | 556 | |
michael@0 | 557 | // Note that we can start at stackPos == 0 here, because it's the caller's |
michael@0 | 558 | // responsibility to handle flushing interactions between contexts (see |
michael@0 | 559 | // HTMLContentSink::BeginContext). |
michael@0 | 560 | int32_t stackPos = 0; |
michael@0 | 561 | bool flushed = false; |
michael@0 | 562 | uint32_t childCount; |
michael@0 | 563 | nsGenericHTMLElement* content; |
michael@0 | 564 | |
michael@0 | 565 | while (stackPos < mStackPos) { |
michael@0 | 566 | content = mStack[stackPos].mContent; |
michael@0 | 567 | childCount = content->GetChildCount(); |
michael@0 | 568 | |
michael@0 | 569 | if (!flushed && (mStack[stackPos].mNumFlushed < childCount)) { |
michael@0 | 570 | if (mStack[stackPos].mInsertionPoint != -1) { |
michael@0 | 571 | // We might have popped the child off our stack already |
michael@0 | 572 | // but not notified on it yet, which is why we have to get it |
michael@0 | 573 | // directly from its parent node. |
michael@0 | 574 | |
michael@0 | 575 | int32_t childIndex = mStack[stackPos].mInsertionPoint - 1; |
michael@0 | 576 | nsIContent* child = content->GetChildAt(childIndex); |
michael@0 | 577 | // Child not on stack anymore; can't assert it's correct |
michael@0 | 578 | NS_ASSERTION(!(mStackPos > (stackPos + 1)) || |
michael@0 | 579 | (child == mStack[stackPos + 1].mContent), |
michael@0 | 580 | "Flushing the wrong child."); |
michael@0 | 581 | mSink->NotifyInsert(content, child, childIndex); |
michael@0 | 582 | } else { |
michael@0 | 583 | mSink->NotifyAppend(content, mStack[stackPos].mNumFlushed); |
michael@0 | 584 | } |
michael@0 | 585 | |
michael@0 | 586 | flushed = true; |
michael@0 | 587 | } |
michael@0 | 588 | |
michael@0 | 589 | mStack[stackPos].mNumFlushed = childCount; |
michael@0 | 590 | stackPos++; |
michael@0 | 591 | } |
michael@0 | 592 | mNotifyLevel = mStackPos - 1; |
michael@0 | 593 | } |
michael@0 | 594 | --(mSink->mInNotification); |
michael@0 | 595 | |
michael@0 | 596 | if (mSink->mUpdatesInNotification > 1) { |
michael@0 | 597 | UpdateChildCounts(); |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | mSink->mUpdatesInNotification = oldUpdates; |
michael@0 | 601 | mSink->mBeganUpdate = oldBeganUpdate; |
michael@0 | 602 | |
michael@0 | 603 | return NS_OK; |
michael@0 | 604 | } |
michael@0 | 605 | |
michael@0 | 606 | /** |
michael@0 | 607 | * NOTE!! Forked into nsXMLContentSink. Please keep in sync. |
michael@0 | 608 | */ |
michael@0 | 609 | void |
michael@0 | 610 | SinkContext::UpdateChildCounts() |
michael@0 | 611 | { |
michael@0 | 612 | // Start from the top of the stack (growing upwards) and see if any |
michael@0 | 613 | // new content has been appended. If so, we recognize that reflows |
michael@0 | 614 | // have been generated for it and we should make sure that no |
michael@0 | 615 | // further reflows occur. Note that we have to include stackPos == 0 |
michael@0 | 616 | // to properly notify on kids of <html>. |
michael@0 | 617 | int32_t stackPos = mStackPos - 1; |
michael@0 | 618 | while (stackPos >= 0) { |
michael@0 | 619 | Node & node = mStack[stackPos]; |
michael@0 | 620 | node.mNumFlushed = node.mContent->GetChildCount(); |
michael@0 | 621 | |
michael@0 | 622 | stackPos--; |
michael@0 | 623 | } |
michael@0 | 624 | |
michael@0 | 625 | mNotifyLevel = mStackPos - 1; |
michael@0 | 626 | } |
michael@0 | 627 | |
michael@0 | 628 | nsresult |
michael@0 | 629 | NS_NewHTMLContentSink(nsIHTMLContentSink** aResult, |
michael@0 | 630 | nsIDocument* aDoc, |
michael@0 | 631 | nsIURI* aURI, |
michael@0 | 632 | nsISupports* aContainer, |
michael@0 | 633 | nsIChannel* aChannel) |
michael@0 | 634 | { |
michael@0 | 635 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 636 | |
michael@0 | 637 | nsRefPtr<HTMLContentSink> it = new HTMLContentSink(); |
michael@0 | 638 | |
michael@0 | 639 | nsresult rv = it->Init(aDoc, aURI, aContainer, aChannel); |
michael@0 | 640 | |
michael@0 | 641 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 642 | |
michael@0 | 643 | *aResult = it; |
michael@0 | 644 | NS_ADDREF(*aResult); |
michael@0 | 645 | |
michael@0 | 646 | return NS_OK; |
michael@0 | 647 | } |
michael@0 | 648 | |
michael@0 | 649 | HTMLContentSink::HTMLContentSink() |
michael@0 | 650 | { |
michael@0 | 651 | // Note: operator new zeros our memory |
michael@0 | 652 | } |
michael@0 | 653 | |
michael@0 | 654 | HTMLContentSink::~HTMLContentSink() |
michael@0 | 655 | { |
michael@0 | 656 | if (mNotificationTimer) { |
michael@0 | 657 | mNotificationTimer->Cancel(); |
michael@0 | 658 | } |
michael@0 | 659 | |
michael@0 | 660 | int32_t numContexts = mContextStack.Length(); |
michael@0 | 661 | |
michael@0 | 662 | if (mCurrentContext == mHeadContext && numContexts > 0) { |
michael@0 | 663 | // Pop off the second html context if it's not done earlier |
michael@0 | 664 | mContextStack.RemoveElementAt(--numContexts); |
michael@0 | 665 | } |
michael@0 | 666 | |
michael@0 | 667 | int32_t i; |
michael@0 | 668 | for (i = 0; i < numContexts; i++) { |
michael@0 | 669 | SinkContext* sc = mContextStack.ElementAt(i); |
michael@0 | 670 | if (sc) { |
michael@0 | 671 | sc->End(); |
michael@0 | 672 | if (sc == mCurrentContext) { |
michael@0 | 673 | mCurrentContext = nullptr; |
michael@0 | 674 | } |
michael@0 | 675 | |
michael@0 | 676 | delete sc; |
michael@0 | 677 | } |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | if (mCurrentContext == mHeadContext) { |
michael@0 | 681 | mCurrentContext = nullptr; |
michael@0 | 682 | } |
michael@0 | 683 | |
michael@0 | 684 | delete mCurrentContext; |
michael@0 | 685 | |
michael@0 | 686 | delete mHeadContext; |
michael@0 | 687 | |
michael@0 | 688 | for (i = 0; uint32_t(i) < ArrayLength(mNodeInfoCache); ++i) { |
michael@0 | 689 | NS_IF_RELEASE(mNodeInfoCache[i]); |
michael@0 | 690 | } |
michael@0 | 691 | } |
michael@0 | 692 | |
michael@0 | 693 | NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLContentSink) |
michael@0 | 694 | |
michael@0 | 695 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLContentSink, nsContentSink) |
michael@0 | 696 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mHTMLDocument) |
michael@0 | 697 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot) |
michael@0 | 698 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mBody) |
michael@0 | 699 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mHead) |
michael@0 | 700 | for (uint32_t i = 0; i < ArrayLength(tmp->mNodeInfoCache); ++i) { |
michael@0 | 701 | NS_IF_RELEASE(tmp->mNodeInfoCache[i]); |
michael@0 | 702 | } |
michael@0 | 703 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 704 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLContentSink, |
michael@0 | 705 | nsContentSink) |
michael@0 | 706 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHTMLDocument) |
michael@0 | 707 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot) |
michael@0 | 708 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBody) |
michael@0 | 709 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHead) |
michael@0 | 710 | for (uint32_t i = 0; i < ArrayLength(tmp->mNodeInfoCache); ++i) { |
michael@0 | 711 | NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mNodeInfoCache[i]"); |
michael@0 | 712 | cb.NoteXPCOMChild(tmp->mNodeInfoCache[i]); |
michael@0 | 713 | } |
michael@0 | 714 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 715 | |
michael@0 | 716 | NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(HTMLContentSink) |
michael@0 | 717 | NS_INTERFACE_TABLE_BEGIN |
michael@0 | 718 | NS_INTERFACE_TABLE_ENTRY(HTMLContentSink, nsIContentSink) |
michael@0 | 719 | NS_INTERFACE_TABLE_ENTRY(HTMLContentSink, nsIHTMLContentSink) |
michael@0 | 720 | NS_INTERFACE_TABLE_END |
michael@0 | 721 | NS_INTERFACE_TABLE_TAIL_INHERITING(nsContentSink) |
michael@0 | 722 | |
michael@0 | 723 | NS_IMPL_ADDREF_INHERITED(HTMLContentSink, nsContentSink) |
michael@0 | 724 | NS_IMPL_RELEASE_INHERITED(HTMLContentSink, nsContentSink) |
michael@0 | 725 | |
michael@0 | 726 | static bool |
michael@0 | 727 | IsScriptEnabled(nsIDocument *aDoc, nsIDocShell *aContainer) |
michael@0 | 728 | { |
michael@0 | 729 | NS_ENSURE_TRUE(aDoc && aContainer, true); |
michael@0 | 730 | |
michael@0 | 731 | nsCOMPtr<nsIScriptGlobalObject> globalObject = |
michael@0 | 732 | do_QueryInterface(aDoc->GetInnerWindow()); |
michael@0 | 733 | |
michael@0 | 734 | // Getting context is tricky if the document hasn't had its |
michael@0 | 735 | // GlobalObject set yet |
michael@0 | 736 | if (!globalObject) { |
michael@0 | 737 | globalObject = aContainer->GetScriptGlobalObject(); |
michael@0 | 738 | } |
michael@0 | 739 | |
michael@0 | 740 | NS_ENSURE_TRUE(globalObject && globalObject->GetGlobalJSObject(), true); |
michael@0 | 741 | return nsContentUtils::GetSecurityManager()-> |
michael@0 | 742 | ScriptAllowed(globalObject->GetGlobalJSObject()); |
michael@0 | 743 | } |
michael@0 | 744 | |
michael@0 | 745 | nsresult |
michael@0 | 746 | HTMLContentSink::Init(nsIDocument* aDoc, |
michael@0 | 747 | nsIURI* aURI, |
michael@0 | 748 | nsISupports* aContainer, |
michael@0 | 749 | nsIChannel* aChannel) |
michael@0 | 750 | { |
michael@0 | 751 | NS_ENSURE_TRUE(aContainer, NS_ERROR_NULL_POINTER); |
michael@0 | 752 | |
michael@0 | 753 | nsresult rv = nsContentSink::Init(aDoc, aURI, aContainer, aChannel); |
michael@0 | 754 | if (NS_FAILED(rv)) { |
michael@0 | 755 | return rv; |
michael@0 | 756 | } |
michael@0 | 757 | |
michael@0 | 758 | aDoc->AddObserver(this); |
michael@0 | 759 | mIsDocumentObserver = true; |
michael@0 | 760 | mHTMLDocument = do_QueryInterface(aDoc); |
michael@0 | 761 | |
michael@0 | 762 | NS_ASSERTION(mDocShell, "oops no docshell!"); |
michael@0 | 763 | |
michael@0 | 764 | // Find out if subframes are enabled |
michael@0 | 765 | if (mDocShell) { |
michael@0 | 766 | bool subFramesEnabled = true; |
michael@0 | 767 | mDocShell->GetAllowSubframes(&subFramesEnabled); |
michael@0 | 768 | if (subFramesEnabled) { |
michael@0 | 769 | mFramesEnabled = true; |
michael@0 | 770 | } |
michael@0 | 771 | } |
michael@0 | 772 | |
michael@0 | 773 | // Find out if scripts are enabled, if not, show <noscript> content |
michael@0 | 774 | if (IsScriptEnabled(aDoc, mDocShell)) { |
michael@0 | 775 | mScriptEnabled = true; |
michael@0 | 776 | } |
michael@0 | 777 | |
michael@0 | 778 | |
michael@0 | 779 | // Changed from 8192 to greatly improve page loading performance on |
michael@0 | 780 | // large pages. See bugzilla bug 77540. |
michael@0 | 781 | mMaxTextRun = Preferences::GetInt("content.maxtextrun", 8191); |
michael@0 | 782 | |
michael@0 | 783 | nsCOMPtr<nsINodeInfo> nodeInfo; |
michael@0 | 784 | nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::html, nullptr, |
michael@0 | 785 | kNameSpaceID_XHTML, |
michael@0 | 786 | nsIDOMNode::ELEMENT_NODE); |
michael@0 | 787 | |
michael@0 | 788 | // Make root part |
michael@0 | 789 | mRoot = NS_NewHTMLHtmlElement(nodeInfo.forget()); |
michael@0 | 790 | if (!mRoot) { |
michael@0 | 791 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 792 | } |
michael@0 | 793 | |
michael@0 | 794 | NS_ASSERTION(mDocument->GetChildCount() == 0, |
michael@0 | 795 | "Document should have no kids here!"); |
michael@0 | 796 | rv = mDocument->AppendChildTo(mRoot, false); |
michael@0 | 797 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 798 | |
michael@0 | 799 | // Make head part |
michael@0 | 800 | nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::head, |
michael@0 | 801 | nullptr, kNameSpaceID_XHTML, |
michael@0 | 802 | nsIDOMNode::ELEMENT_NODE); |
michael@0 | 803 | |
michael@0 | 804 | mHead = NS_NewHTMLHeadElement(nodeInfo.forget()); |
michael@0 | 805 | if (NS_FAILED(rv)) { |
michael@0 | 806 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 807 | } |
michael@0 | 808 | |
michael@0 | 809 | mRoot->AppendChildTo(mHead, false); |
michael@0 | 810 | |
michael@0 | 811 | mCurrentContext = new SinkContext(this); |
michael@0 | 812 | mCurrentContext->Begin(eHTMLTag_html, mRoot, 0, -1); |
michael@0 | 813 | mContextStack.AppendElement(mCurrentContext); |
michael@0 | 814 | |
michael@0 | 815 | return NS_OK; |
michael@0 | 816 | } |
michael@0 | 817 | |
michael@0 | 818 | NS_IMETHODIMP |
michael@0 | 819 | HTMLContentSink::WillParse(void) |
michael@0 | 820 | { |
michael@0 | 821 | return WillParseImpl(); |
michael@0 | 822 | } |
michael@0 | 823 | |
michael@0 | 824 | NS_IMETHODIMP |
michael@0 | 825 | HTMLContentSink::WillBuildModel(nsDTDMode aDTDMode) |
michael@0 | 826 | { |
michael@0 | 827 | WillBuildModelImpl(); |
michael@0 | 828 | |
michael@0 | 829 | if (mHTMLDocument) { |
michael@0 | 830 | nsCompatibility mode = eCompatibility_NavQuirks; |
michael@0 | 831 | switch (aDTDMode) { |
michael@0 | 832 | case eDTDMode_full_standards: |
michael@0 | 833 | mode = eCompatibility_FullStandards; |
michael@0 | 834 | break; |
michael@0 | 835 | case eDTDMode_almost_standards: |
michael@0 | 836 | mode = eCompatibility_AlmostStandards; |
michael@0 | 837 | break; |
michael@0 | 838 | default: |
michael@0 | 839 | break; |
michael@0 | 840 | } |
michael@0 | 841 | mHTMLDocument->SetCompatibilityMode(mode); |
michael@0 | 842 | } |
michael@0 | 843 | |
michael@0 | 844 | // Notify document that the load is beginning |
michael@0 | 845 | mDocument->BeginLoad(); |
michael@0 | 846 | |
michael@0 | 847 | return NS_OK; |
michael@0 | 848 | } |
michael@0 | 849 | |
michael@0 | 850 | NS_IMETHODIMP |
michael@0 | 851 | HTMLContentSink::DidBuildModel(bool aTerminated) |
michael@0 | 852 | { |
michael@0 | 853 | DidBuildModelImpl(aTerminated); |
michael@0 | 854 | |
michael@0 | 855 | // Reflow the last batch of content |
michael@0 | 856 | if (mBody) { |
michael@0 | 857 | mCurrentContext->FlushTags(); |
michael@0 | 858 | } else if (!mLayoutStarted) { |
michael@0 | 859 | // We never saw the body, and layout never got started. Force |
michael@0 | 860 | // layout *now*, to get an initial reflow. |
michael@0 | 861 | // NOTE: only force the layout if we are NOT destroying the |
michael@0 | 862 | // docshell. If we are destroying it, then starting layout will |
michael@0 | 863 | // likely cause us to crash, or at best waste a lot of time as we |
michael@0 | 864 | // are just going to tear it down anyway. |
michael@0 | 865 | bool bDestroying = true; |
michael@0 | 866 | if (mDocShell) { |
michael@0 | 867 | mDocShell->IsBeingDestroyed(&bDestroying); |
michael@0 | 868 | } |
michael@0 | 869 | |
michael@0 | 870 | if (!bDestroying) { |
michael@0 | 871 | StartLayout(false); |
michael@0 | 872 | } |
michael@0 | 873 | } |
michael@0 | 874 | |
michael@0 | 875 | ScrollToRef(); |
michael@0 | 876 | |
michael@0 | 877 | // Make sure we no longer respond to document mutations. We've flushed all |
michael@0 | 878 | // our notifications out, so there's no need to do anything else here. |
michael@0 | 879 | |
michael@0 | 880 | // XXXbz I wonder whether we could End() our contexts here too, or something, |
michael@0 | 881 | // just to make sure we no longer notify... Or is the mIsDocumentObserver |
michael@0 | 882 | // thing sufficient? |
michael@0 | 883 | mDocument->RemoveObserver(this); |
michael@0 | 884 | mIsDocumentObserver = false; |
michael@0 | 885 | |
michael@0 | 886 | mDocument->EndLoad(); |
michael@0 | 887 | |
michael@0 | 888 | DropParserAndPerfHint(); |
michael@0 | 889 | |
michael@0 | 890 | return NS_OK; |
michael@0 | 891 | } |
michael@0 | 892 | |
michael@0 | 893 | NS_IMETHODIMP |
michael@0 | 894 | HTMLContentSink::SetParser(nsParserBase* aParser) |
michael@0 | 895 | { |
michael@0 | 896 | NS_PRECONDITION(aParser, "Should have a parser here!"); |
michael@0 | 897 | mParser = aParser; |
michael@0 | 898 | return NS_OK; |
michael@0 | 899 | } |
michael@0 | 900 | |
michael@0 | 901 | nsresult |
michael@0 | 902 | HTMLContentSink::CloseHTML() |
michael@0 | 903 | { |
michael@0 | 904 | if (mHeadContext) { |
michael@0 | 905 | if (mCurrentContext == mHeadContext) { |
michael@0 | 906 | uint32_t numContexts = mContextStack.Length(); |
michael@0 | 907 | |
michael@0 | 908 | // Pop off the second html context if it's not done earlier |
michael@0 | 909 | mCurrentContext = mContextStack.ElementAt(--numContexts); |
michael@0 | 910 | mContextStack.RemoveElementAt(numContexts); |
michael@0 | 911 | } |
michael@0 | 912 | |
michael@0 | 913 | mHeadContext->End(); |
michael@0 | 914 | |
michael@0 | 915 | delete mHeadContext; |
michael@0 | 916 | mHeadContext = nullptr; |
michael@0 | 917 | } |
michael@0 | 918 | |
michael@0 | 919 | return NS_OK; |
michael@0 | 920 | } |
michael@0 | 921 | |
michael@0 | 922 | nsresult |
michael@0 | 923 | HTMLContentSink::OpenBody() |
michael@0 | 924 | { |
michael@0 | 925 | CloseHeadContext(); // do this just in case if the HEAD was left open! |
michael@0 | 926 | |
michael@0 | 927 | // if we already have a body we're done |
michael@0 | 928 | if (mBody) { |
michael@0 | 929 | return NS_OK; |
michael@0 | 930 | } |
michael@0 | 931 | |
michael@0 | 932 | nsresult rv = mCurrentContext->OpenBody(); |
michael@0 | 933 | |
michael@0 | 934 | if (NS_FAILED(rv)) { |
michael@0 | 935 | return rv; |
michael@0 | 936 | } |
michael@0 | 937 | |
michael@0 | 938 | mBody = mCurrentContext->mStack[mCurrentContext->mStackPos - 1].mContent; |
michael@0 | 939 | |
michael@0 | 940 | if (mCurrentContext->mStackPos > 1) { |
michael@0 | 941 | int32_t parentIndex = mCurrentContext->mStackPos - 2; |
michael@0 | 942 | nsGenericHTMLElement *parent = mCurrentContext->mStack[parentIndex].mContent; |
michael@0 | 943 | int32_t numFlushed = mCurrentContext->mStack[parentIndex].mNumFlushed; |
michael@0 | 944 | int32_t childCount = parent->GetChildCount(); |
michael@0 | 945 | NS_ASSERTION(numFlushed < childCount, "Already notified on the body?"); |
michael@0 | 946 | |
michael@0 | 947 | int32_t insertionPoint = |
michael@0 | 948 | mCurrentContext->mStack[parentIndex].mInsertionPoint; |
michael@0 | 949 | |
michael@0 | 950 | // XXX: I have yet to see a case where numFlushed is non-zero and |
michael@0 | 951 | // insertionPoint is not -1, but this code will try to handle |
michael@0 | 952 | // those cases too. |
michael@0 | 953 | |
michael@0 | 954 | uint32_t oldUpdates = mUpdatesInNotification; |
michael@0 | 955 | mUpdatesInNotification = 0; |
michael@0 | 956 | if (insertionPoint != -1) { |
michael@0 | 957 | NotifyInsert(parent, mBody, insertionPoint - 1); |
michael@0 | 958 | } else { |
michael@0 | 959 | NotifyAppend(parent, numFlushed); |
michael@0 | 960 | } |
michael@0 | 961 | mCurrentContext->mStack[parentIndex].mNumFlushed = childCount; |
michael@0 | 962 | if (mUpdatesInNotification > 1) { |
michael@0 | 963 | UpdateChildCounts(); |
michael@0 | 964 | } |
michael@0 | 965 | mUpdatesInNotification = oldUpdates; |
michael@0 | 966 | } |
michael@0 | 967 | |
michael@0 | 968 | StartLayout(false); |
michael@0 | 969 | |
michael@0 | 970 | return NS_OK; |
michael@0 | 971 | } |
michael@0 | 972 | |
michael@0 | 973 | nsresult |
michael@0 | 974 | HTMLContentSink::CloseBody() |
michael@0 | 975 | { |
michael@0 | 976 | // Flush out anything that's left |
michael@0 | 977 | mCurrentContext->FlushTags(); |
michael@0 | 978 | mCurrentContext->CloseBody(); |
michael@0 | 979 | |
michael@0 | 980 | return NS_OK; |
michael@0 | 981 | } |
michael@0 | 982 | |
michael@0 | 983 | NS_IMETHODIMP |
michael@0 | 984 | HTMLContentSink::OpenContainer(ElementType aElementType) |
michael@0 | 985 | { |
michael@0 | 986 | nsresult rv = NS_OK; |
michael@0 | 987 | |
michael@0 | 988 | switch (aElementType) { |
michael@0 | 989 | case eBody: |
michael@0 | 990 | rv = OpenBody(); |
michael@0 | 991 | break; |
michael@0 | 992 | case eHTML: |
michael@0 | 993 | if (mRoot) { |
michael@0 | 994 | // If we've already hit this code once, then we're done |
michael@0 | 995 | if (!mNotifiedRootInsertion) { |
michael@0 | 996 | NotifyRootInsertion(); |
michael@0 | 997 | } |
michael@0 | 998 | ProcessOfflineManifest(mRoot); |
michael@0 | 999 | } |
michael@0 | 1000 | break; |
michael@0 | 1001 | } |
michael@0 | 1002 | |
michael@0 | 1003 | return rv; |
michael@0 | 1004 | } |
michael@0 | 1005 | |
michael@0 | 1006 | NS_IMETHODIMP |
michael@0 | 1007 | HTMLContentSink::CloseContainer(const ElementType aTag) |
michael@0 | 1008 | { |
michael@0 | 1009 | nsresult rv = NS_OK; |
michael@0 | 1010 | |
michael@0 | 1011 | switch (aTag) { |
michael@0 | 1012 | case eBody: |
michael@0 | 1013 | rv = CloseBody(); |
michael@0 | 1014 | break; |
michael@0 | 1015 | case eHTML: |
michael@0 | 1016 | rv = CloseHTML(); |
michael@0 | 1017 | break; |
michael@0 | 1018 | } |
michael@0 | 1019 | |
michael@0 | 1020 | return rv; |
michael@0 | 1021 | } |
michael@0 | 1022 | |
michael@0 | 1023 | NS_IMETHODIMP |
michael@0 | 1024 | HTMLContentSink::WillInterrupt() |
michael@0 | 1025 | { |
michael@0 | 1026 | return WillInterruptImpl(); |
michael@0 | 1027 | } |
michael@0 | 1028 | |
michael@0 | 1029 | NS_IMETHODIMP |
michael@0 | 1030 | HTMLContentSink::WillResume() |
michael@0 | 1031 | { |
michael@0 | 1032 | return WillResumeImpl(); |
michael@0 | 1033 | } |
michael@0 | 1034 | |
michael@0 | 1035 | void |
michael@0 | 1036 | HTMLContentSink::CloseHeadContext() |
michael@0 | 1037 | { |
michael@0 | 1038 | if (mCurrentContext) { |
michael@0 | 1039 | if (!mCurrentContext->IsCurrentContainer(eHTMLTag_head)) |
michael@0 | 1040 | return; |
michael@0 | 1041 | |
michael@0 | 1042 | mCurrentContext->FlushTags(); |
michael@0 | 1043 | } |
michael@0 | 1044 | |
michael@0 | 1045 | if (!mContextStack.IsEmpty()) |
michael@0 | 1046 | { |
michael@0 | 1047 | uint32_t n = mContextStack.Length() - 1; |
michael@0 | 1048 | mCurrentContext = mContextStack.ElementAt(n); |
michael@0 | 1049 | mContextStack.RemoveElementAt(n); |
michael@0 | 1050 | } |
michael@0 | 1051 | } |
michael@0 | 1052 | |
michael@0 | 1053 | void |
michael@0 | 1054 | HTMLContentSink::NotifyInsert(nsIContent* aContent, |
michael@0 | 1055 | nsIContent* aChildContent, |
michael@0 | 1056 | int32_t aIndexInContainer) |
michael@0 | 1057 | { |
michael@0 | 1058 | if (aContent && aContent->GetCurrentDoc() != mDocument) { |
michael@0 | 1059 | // aContent is not actually in our document anymore.... Just bail out of |
michael@0 | 1060 | // here; notifying on our document for this insert would be wrong. |
michael@0 | 1061 | return; |
michael@0 | 1062 | } |
michael@0 | 1063 | |
michael@0 | 1064 | mInNotification++; |
michael@0 | 1065 | |
michael@0 | 1066 | { |
michael@0 | 1067 | // Scope so we call EndUpdate before we decrease mInNotification |
michael@0 | 1068 | MOZ_AUTO_DOC_UPDATE(mDocument, UPDATE_CONTENT_MODEL, !mBeganUpdate); |
michael@0 | 1069 | nsNodeUtils::ContentInserted(NODE_FROM(aContent, mDocument), |
michael@0 | 1070 | aChildContent, aIndexInContainer); |
michael@0 | 1071 | mLastNotificationTime = PR_Now(); |
michael@0 | 1072 | } |
michael@0 | 1073 | |
michael@0 | 1074 | mInNotification--; |
michael@0 | 1075 | } |
michael@0 | 1076 | |
michael@0 | 1077 | void |
michael@0 | 1078 | HTMLContentSink::NotifyRootInsertion() |
michael@0 | 1079 | { |
michael@0 | 1080 | NS_PRECONDITION(!mNotifiedRootInsertion, "Double-notifying on root?"); |
michael@0 | 1081 | NS_ASSERTION(!mLayoutStarted, |
michael@0 | 1082 | "How did we start layout without notifying on root?"); |
michael@0 | 1083 | // Now make sure to notify that we have now inserted our root. If |
michael@0 | 1084 | // there has been no initial reflow yet it'll be a no-op, but if |
michael@0 | 1085 | // there has been one we need this to get its frames constructed. |
michael@0 | 1086 | // Note that if mNotifiedRootInsertion is true we don't notify here, |
michael@0 | 1087 | // since that just means there are multiple <html> tags in the |
michael@0 | 1088 | // document; in those cases we just want to put all the attrs on one |
michael@0 | 1089 | // tag. |
michael@0 | 1090 | mNotifiedRootInsertion = true; |
michael@0 | 1091 | int32_t index = mDocument->IndexOf(mRoot); |
michael@0 | 1092 | NS_ASSERTION(index != -1, "mRoot not child of document?"); |
michael@0 | 1093 | NotifyInsert(nullptr, mRoot, index); |
michael@0 | 1094 | |
michael@0 | 1095 | // Now update the notification information in all our |
michael@0 | 1096 | // contexts, since we just inserted the root and notified on |
michael@0 | 1097 | // our whole tree |
michael@0 | 1098 | UpdateChildCounts(); |
michael@0 | 1099 | } |
michael@0 | 1100 | |
michael@0 | 1101 | void |
michael@0 | 1102 | HTMLContentSink::UpdateChildCounts() |
michael@0 | 1103 | { |
michael@0 | 1104 | uint32_t numContexts = mContextStack.Length(); |
michael@0 | 1105 | for (uint32_t i = 0; i < numContexts; i++) { |
michael@0 | 1106 | SinkContext* sc = mContextStack.ElementAt(i); |
michael@0 | 1107 | |
michael@0 | 1108 | sc->UpdateChildCounts(); |
michael@0 | 1109 | } |
michael@0 | 1110 | |
michael@0 | 1111 | mCurrentContext->UpdateChildCounts(); |
michael@0 | 1112 | } |
michael@0 | 1113 | |
michael@0 | 1114 | void |
michael@0 | 1115 | HTMLContentSink::FlushPendingNotifications(mozFlushType aType) |
michael@0 | 1116 | { |
michael@0 | 1117 | // Only flush tags if we're not doing the notification ourselves |
michael@0 | 1118 | // (since we aren't reentrant) |
michael@0 | 1119 | if (!mInNotification) { |
michael@0 | 1120 | // Only flush if we're still a document observer (so that our child counts |
michael@0 | 1121 | // should be correct). |
michael@0 | 1122 | if (mIsDocumentObserver) { |
michael@0 | 1123 | if (aType >= Flush_ContentAndNotify) { |
michael@0 | 1124 | FlushTags(); |
michael@0 | 1125 | } |
michael@0 | 1126 | } |
michael@0 | 1127 | if (aType >= Flush_InterruptibleLayout) { |
michael@0 | 1128 | // Make sure that layout has started so that the reflow flush |
michael@0 | 1129 | // will actually happen. |
michael@0 | 1130 | StartLayout(true); |
michael@0 | 1131 | } |
michael@0 | 1132 | } |
michael@0 | 1133 | } |
michael@0 | 1134 | |
michael@0 | 1135 | nsresult |
michael@0 | 1136 | HTMLContentSink::FlushTags() |
michael@0 | 1137 | { |
michael@0 | 1138 | if (!mNotifiedRootInsertion) { |
michael@0 | 1139 | NotifyRootInsertion(); |
michael@0 | 1140 | return NS_OK; |
michael@0 | 1141 | } |
michael@0 | 1142 | |
michael@0 | 1143 | return mCurrentContext ? mCurrentContext->FlushTags() : NS_OK; |
michael@0 | 1144 | } |
michael@0 | 1145 | |
michael@0 | 1146 | NS_IMETHODIMP |
michael@0 | 1147 | HTMLContentSink::SetDocumentCharset(nsACString& aCharset) |
michael@0 | 1148 | { |
michael@0 | 1149 | MOZ_ASSUME_UNREACHABLE("<meta charset> case doesn't occur with about:blank"); |
michael@0 | 1150 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 1151 | } |
michael@0 | 1152 | |
michael@0 | 1153 | nsISupports * |
michael@0 | 1154 | HTMLContentSink::GetTarget() |
michael@0 | 1155 | { |
michael@0 | 1156 | return mDocument; |
michael@0 | 1157 | } |
michael@0 | 1158 | |
michael@0 | 1159 | bool |
michael@0 | 1160 | HTMLContentSink::IsScriptExecuting() |
michael@0 | 1161 | { |
michael@0 | 1162 | return IsScriptExecutingImpl(); |
michael@0 | 1163 | } |