tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsHtml5DocumentBuilder.cpp (3493B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 sw=2 et tw=78: */
      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 
      7 #include "nsHtml5DocumentBuilder.h"
      8 
      9 #include "mozilla/dom/ScriptLoader.h"
     10 #include "mozilla/dom/LinkStyle.h"
     11 #include "nsNameSpaceManager.h"
     12 #include "nsNetUtil.h"
     13 
     14 using mozilla::dom::LinkStyle;
     15 
     16 NS_IMPL_CYCLE_COLLECTION_INHERITED(nsHtml5DocumentBuilder, nsContentSink,
     17                                   mOwnedElements)
     18 
     19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsHtml5DocumentBuilder)
     20 NS_INTERFACE_MAP_END_INHERITING(nsContentSink)
     21 
     22 NS_IMPL_ADDREF_INHERITED(nsHtml5DocumentBuilder, nsContentSink)
     23 NS_IMPL_RELEASE_INHERITED(nsHtml5DocumentBuilder, nsContentSink)
     24 
     25 nsHtml5DocumentBuilder::nsHtml5DocumentBuilder(bool aRunsToCompletion)
     26    : mBroken(NS_OK), mFlushState(eHtml5FlushState::eNotFlushing) {
     27  mRunsToCompletion = aRunsToCompletion;
     28 }
     29 
     30 nsresult nsHtml5DocumentBuilder::Init(mozilla::dom::Document* aDoc,
     31                                      nsIURI* aURI, nsISupports* aContainer,
     32                                      nsIChannel* aChannel) {
     33  return nsContentSink::Init(aDoc, aURI, aContainer, aChannel);
     34 }
     35 
     36 nsHtml5DocumentBuilder::~nsHtml5DocumentBuilder() = default;
     37 
     38 nsresult nsHtml5DocumentBuilder::MarkAsBroken(nsresult aReason) {
     39  mBroken = aReason;
     40  return aReason;
     41 }
     42 
     43 void nsHtml5DocumentBuilder::UpdateStyleSheet(nsIContent* aElement) {
     44  auto* linkStyle = LinkStyle::FromNode(*aElement);
     45  if (!linkStyle) {
     46    MOZ_ASSERT(nsNameSpaceManager::GetInstance()->mSVGDisabled,
     47               "Node didn't QI to style, but SVG wasn't disabled.");
     48    return;
     49  }
     50 
     51  auto updateOrError = linkStyle->EnableUpdatesAndUpdateStyleSheet(
     52      mRunsToCompletion ? nullptr : this);
     53 
     54  if (updateOrError.isOk() && updateOrError.unwrap().ShouldBlock() &&
     55      !mRunsToCompletion) {
     56    ++mPendingSheetCount;
     57    if (mScriptLoader) {
     58      mScriptLoader->AddParserBlockingScriptExecutionBlocker();
     59    }
     60  }
     61 }
     62 
     63 void nsHtml5DocumentBuilder::SetDocumentMode(nsHtml5DocumentMode m) {
     64  nsCompatibility mode = eCompatibility_NavQuirks;
     65  const char* errMsgId = nullptr;
     66 
     67  switch (m) {
     68    case STANDARDS_MODE:
     69      mode = eCompatibility_FullStandards;
     70      break;
     71    case ALMOST_STANDARDS_MODE:
     72      mode = eCompatibility_AlmostStandards;
     73      errMsgId = "errAlmostStandardsDoctypeVerbose";
     74      break;
     75    case QUIRKS_MODE:
     76      mode = eCompatibility_NavQuirks;
     77      errMsgId = "errQuirkyDoctypeVerbose";
     78      break;
     79  }
     80  mDocument->SetCompatibilityMode(mode);
     81 
     82  if (errMsgId && !mDocument->IsLoadedAsData()) {
     83    nsCOMPtr<nsIURI> docURI = mDocument->GetDocumentURI();
     84    nsCOMPtr<nsIPrincipal> principal = mDocument->GetPrincipal();
     85    if (principal->GetIsNullPrincipal() && !docURI->SchemeIs("data") &&
     86        !mozilla::net::SchemeIsHttpOrHttps(docURI)) {
     87      // Don't normally warn for null principals. It may well be internal
     88      // documents for which the warning is not applicable.
     89      return;
     90    }
     91 
     92    nsContentUtils::ReportToConsole(
     93        nsIScriptError::warningFlag, "HTML_PARSER__DOCTYPE"_ns, mDocument,
     94        nsContentUtils::eHTMLPARSER_PROPERTIES, errMsgId);
     95  }
     96 }
     97 
     98 // nsContentSink overrides
     99 
    100 void nsHtml5DocumentBuilder::UpdateChildCounts() {
    101  // No-op
    102 }
    103 
    104 nsresult nsHtml5DocumentBuilder::FlushTags() { return NS_OK; }