LCOV - code coverage report
Current view: top level - dom/xml - nsXMLFragmentContentSink.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 135 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 35 0.0 %
Legend: Lines: hit not hit

          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             : 
       7             : #include "nsCOMPtr.h"
       8             : #include "nsXMLContentSink.h"
       9             : #include "nsIFragmentContentSink.h"
      10             : #include "nsIXMLContentSink.h"
      11             : #include "nsContentSink.h"
      12             : #include "nsIExpatSink.h"
      13             : #include "nsIDTD.h"
      14             : #include "nsIDocument.h"
      15             : #include "nsIDOMDocumentFragment.h"
      16             : #include "nsIContent.h"
      17             : #include "nsGkAtoms.h"
      18             : #include "mozilla/dom/NodeInfo.h"
      19             : #include "nsContentCreatorFunctions.h"
      20             : #include "nsError.h"
      21             : #include "nsIConsoleService.h"
      22             : #include "nsIScriptError.h"
      23             : #include "nsTHashtable.h"
      24             : #include "nsHashKeys.h"
      25             : #include "nsTArray.h"
      26             : #include "nsCycleCollectionParticipant.h"
      27             : #include "nsIDocShell.h"
      28             : #include "mozilla/css/Loader.h"
      29             : #include "mozilla/dom/DocumentFragment.h"
      30             : #include "mozilla/dom/ProcessingInstruction.h"
      31             : #include "mozilla/dom/ScriptLoader.h"
      32             : 
      33             : using namespace mozilla::dom;
      34             : 
      35             : class nsXMLFragmentContentSink : public nsXMLContentSink,
      36             :                                  public nsIFragmentContentSink
      37             : {
      38             : public:
      39             :   nsXMLFragmentContentSink();
      40             : 
      41             :   // nsISupports
      42             :   NS_DECL_ISUPPORTS_INHERITED
      43           0 :   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED_NO_UNLINK(nsXMLFragmentContentSink,
      44             :                                                      nsXMLContentSink)
      45             : 
      46             :   // nsIExpatSink
      47             :   NS_IMETHOD HandleDoctypeDecl(const nsAString& aSubset,
      48             :                                const nsAString& aName,
      49             :                                const nsAString& aSystemId,
      50             :                                const nsAString& aPublicId,
      51             :                                nsISupports* aCatalogData) override;
      52             :   NS_IMETHOD HandleProcessingInstruction(const char16_t* aTarget,
      53             :                                          const char16_t* aData) override;
      54             :   NS_IMETHOD HandleXMLDeclaration(const char16_t* aVersion,
      55             :                                   const char16_t* aEncoding,
      56             :                                   int32_t aStandalone) override;
      57             :   NS_IMETHOD ReportError(const char16_t* aErrorText,
      58             :                          const char16_t* aSourceText,
      59             :                          nsIScriptError* aError,
      60             :                          bool* aRetval) override;
      61             : 
      62             :   // nsIContentSink
      63             :   NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode) override;
      64             :   NS_IMETHOD DidBuildModel(bool aTerminated) override;
      65             :   virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override;
      66             :   virtual nsISupports* GetTarget() override;
      67             :   NS_IMETHOD DidProcessATokenImpl();
      68             : 
      69             :   // nsIXMLContentSink
      70             : 
      71             :   // nsIFragmentContentSink
      72             :   NS_IMETHOD FinishFragmentParsing(nsIDOMDocumentFragment** aFragment) override;
      73             :   NS_IMETHOD SetTargetDocument(nsIDocument* aDocument) override;
      74             :   NS_IMETHOD WillBuildContent() override;
      75             :   NS_IMETHOD DidBuildContent() override;
      76             :   NS_IMETHOD IgnoreFirstContainer() override;
      77             :   NS_IMETHOD SetPreventScriptExecution(bool aPreventScriptExecution) override;
      78             : 
      79             : protected:
      80             :   virtual ~nsXMLFragmentContentSink();
      81             : 
      82             :   virtual bool SetDocElement(int32_t aNameSpaceID,
      83             :                                nsIAtom* aTagName,
      84             :                                nsIContent* aContent) override;
      85             :   virtual nsresult CreateElement(const char16_t** aAtts, uint32_t aAttsCount,
      86             :                                  mozilla::dom::NodeInfo* aNodeInfo, uint32_t aLineNumber,
      87             :                                  nsIContent** aResult, bool* aAppendContent,
      88             :                                  mozilla::dom::FromParser aFromParser) override;
      89             :   virtual nsresult CloseElement(nsIContent* aContent) override;
      90             : 
      91             :   virtual void MaybeStartLayout(bool aIgnorePendingSheets) override;
      92             : 
      93             :   // nsContentSink overrides
      94             :   virtual nsresult ProcessStyleLink(nsIContent* aElement,
      95             :                                     const nsAString& aHref,
      96             :                                     bool aAlternate,
      97             :                                     const nsAString& aTitle,
      98             :                                     const nsAString& aType,
      99             :                                     const nsAString& aMedia) override;
     100             :   nsresult LoadXSLStyleSheet(nsIURI* aUrl);
     101             :   void StartLayout();
     102             : 
     103             :   nsCOMPtr<nsIDocument> mTargetDocument;
     104             :   // the fragment
     105             :   nsCOMPtr<nsIContent>  mRoot;
     106             :   bool                  mParseError;
     107             : };
     108             : 
     109             : static nsresult
     110           0 : NewXMLFragmentContentSinkHelper(nsIFragmentContentSink** aResult)
     111             : {
     112           0 :   nsXMLFragmentContentSink* it = new nsXMLFragmentContentSink();
     113             : 
     114           0 :   NS_ADDREF(*aResult = it);
     115             : 
     116           0 :   return NS_OK;
     117             : }
     118             : 
     119             : nsresult
     120           0 : NS_NewXMLFragmentContentSink(nsIFragmentContentSink** aResult)
     121             : {
     122           0 :   return NewXMLFragmentContentSinkHelper(aResult);
     123             : }
     124             : 
     125           0 : nsXMLFragmentContentSink::nsXMLFragmentContentSink()
     126           0 :  : mParseError(false)
     127             : {
     128           0 :   mRunsToCompletion = true;
     129           0 : }
     130             : 
     131           0 : nsXMLFragmentContentSink::~nsXMLFragmentContentSink()
     132             : {
     133           0 : }
     134             : 
     135           0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsXMLFragmentContentSink)
     136           0 :   NS_INTERFACE_MAP_ENTRY(nsIFragmentContentSink)
     137           0 : NS_INTERFACE_MAP_END_INHERITING(nsXMLContentSink)
     138             : 
     139           0 : NS_IMPL_ADDREF_INHERITED(nsXMLFragmentContentSink, nsXMLContentSink)
     140           0 : NS_IMPL_RELEASE_INHERITED(nsXMLFragmentContentSink, nsXMLContentSink)
     141             : 
     142             : NS_IMPL_CYCLE_COLLECTION_CLASS(nsXMLFragmentContentSink)
     143             : 
     144           0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsXMLFragmentContentSink,
     145             :                                                   nsXMLContentSink)
     146           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTargetDocument)
     147           0 :   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot)
     148           0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
     149             : 
     150             : NS_IMETHODIMP
     151           0 : nsXMLFragmentContentSink::WillBuildModel(nsDTDMode aDTDMode)
     152             : {
     153           0 :   if (mRoot) {
     154           0 :     return NS_OK;
     155             :   }
     156             : 
     157           0 :   mState = eXMLContentSinkState_InDocumentElement;
     158             : 
     159           0 :   NS_ASSERTION(mTargetDocument, "Need a document!");
     160             : 
     161           0 :   mRoot = new DocumentFragment(mNodeInfoManager);
     162             : 
     163           0 :   return NS_OK;
     164             : }
     165             : 
     166             : NS_IMETHODIMP
     167           0 : nsXMLFragmentContentSink::DidBuildModel(bool aTerminated)
     168             : {
     169             :   // Drop our reference to the parser to get rid of a circular
     170             :   // reference.
     171           0 :   mParser = nullptr;
     172             : 
     173           0 :   return NS_OK;
     174             : }
     175             : 
     176             : void
     177           0 : nsXMLFragmentContentSink::SetDocumentCharset(
     178             :   NotNull<const Encoding*> aEncoding)
     179             : {
     180           0 :   NS_NOTREACHED("fragments shouldn't set charset");
     181           0 : }
     182             : 
     183             : nsISupports *
     184           0 : nsXMLFragmentContentSink::GetTarget()
     185             : {
     186           0 :   return mTargetDocument;
     187             : }
     188             : 
     189             : ////////////////////////////////////////////////////////////////////////
     190             : 
     191             : bool
     192           0 : nsXMLFragmentContentSink::SetDocElement(int32_t aNameSpaceID,
     193             :                                         nsIAtom* aTagName,
     194             :                                         nsIContent *aContent)
     195             : {
     196             :   // this is a fragment, not a document
     197           0 :   return false;
     198             : }
     199             : 
     200             : nsresult
     201           0 : nsXMLFragmentContentSink::CreateElement(const char16_t** aAtts, uint32_t aAttsCount,
     202             :                                         mozilla::dom::NodeInfo* aNodeInfo, uint32_t aLineNumber,
     203             :                                         nsIContent** aResult, bool* aAppendContent,
     204             :                                         FromParser /*aFromParser*/)
     205             : {
     206             :   // Claim to not be coming from parser, since we don't do any of the
     207             :   // fancy CloseElement stuff.
     208           0 :   nsresult rv = nsXMLContentSink::CreateElement(aAtts, aAttsCount,
     209             :                                                 aNodeInfo, aLineNumber,
     210             :                                                 aResult, aAppendContent,
     211           0 :                                                 NOT_FROM_PARSER);
     212             : 
     213             :   // When we aren't grabbing all of the content we, never open a doc
     214             :   // element, we run into trouble on the first element, so we don't append,
     215             :   // and simply push this onto the content stack.
     216           0 :   if (mContentStack.Length() == 0) {
     217           0 :     *aAppendContent = false;
     218             :   }
     219             : 
     220           0 :   return rv;
     221             : }
     222             : 
     223             : nsresult
     224           0 : nsXMLFragmentContentSink::CloseElement(nsIContent* aContent)
     225             : {
     226             :   // don't do fancy stuff in nsXMLContentSink
     227           0 :   if (mPreventScriptExecution &&
     228           0 :       (aContent->IsHTMLElement(nsGkAtoms::script) ||
     229           0 :        aContent->IsSVGElement(nsGkAtoms::script))) {
     230           0 :     nsCOMPtr<nsIScriptElement> sele = do_QueryInterface(aContent);
     231           0 :     if (sele) {
     232           0 :       sele->PreventExecution();
     233             :     } else {
     234           0 :       NS_ASSERTION(nsNameSpaceManager::GetInstance()->mSVGDisabled, "Script did QI correctly, but wasn't a disabled SVG!");
     235             :     }
     236             :   }
     237           0 :   return NS_OK;
     238             : }
     239             : 
     240             : void
     241           0 : nsXMLFragmentContentSink::MaybeStartLayout(bool aIgnorePendingSheets)
     242             : {
     243           0 :   return;
     244             : }
     245             : 
     246             : ////////////////////////////////////////////////////////////////////////
     247             : 
     248             : NS_IMETHODIMP
     249           0 : nsXMLFragmentContentSink::HandleDoctypeDecl(const nsAString & aSubset,
     250             :                                             const nsAString & aName,
     251             :                                             const nsAString & aSystemId,
     252             :                                             const nsAString & aPublicId,
     253             :                                             nsISupports* aCatalogData)
     254             : {
     255           0 :   NS_NOTREACHED("fragments shouldn't have doctype declarations");
     256             : 
     257           0 :   return NS_OK;
     258             : }
     259             : 
     260             : NS_IMETHODIMP
     261           0 : nsXMLFragmentContentSink::HandleProcessingInstruction(const char16_t *aTarget,
     262             :                                                       const char16_t *aData)
     263             : {
     264           0 :   FlushText();
     265             : 
     266           0 :   const nsDependentString target(aTarget);
     267           0 :   const nsDependentString data(aData);
     268             : 
     269             :   RefPtr<ProcessingInstruction> node =
     270           0 :     NS_NewXMLProcessingInstruction(mNodeInfoManager, target, data);
     271             : 
     272             :   // no special processing here.  that should happen when the fragment moves into the document
     273           0 :   return AddContentAsLeaf(node);
     274             : }
     275             : 
     276             : NS_IMETHODIMP
     277           0 : nsXMLFragmentContentSink::HandleXMLDeclaration(const char16_t *aVersion,
     278             :                                                const char16_t *aEncoding,
     279             :                                                int32_t aStandalone)
     280             : {
     281           0 :   NS_NOTREACHED("fragments shouldn't have XML declarations");
     282           0 :   return NS_OK;
     283             : }
     284             : 
     285             : NS_IMETHODIMP
     286           0 : nsXMLFragmentContentSink::ReportError(const char16_t* aErrorText,
     287             :                                       const char16_t* aSourceText,
     288             :                                       nsIScriptError *aError,
     289             :                                       bool *_retval)
     290             : {
     291           0 :   NS_PRECONDITION(aError && aSourceText && aErrorText, "Check arguments!!!");
     292             : 
     293             :   // The expat driver should report the error.
     294           0 :   *_retval = true;
     295             : 
     296           0 :   mParseError = true;
     297             : 
     298             : #ifdef DEBUG
     299             :   // Report the error to stderr.
     300           0 :   fprintf(stderr,
     301             :           "\n%s\n%s\n\n",
     302           0 :           NS_LossyConvertUTF16toASCII(aErrorText).get(),
     303           0 :           NS_LossyConvertUTF16toASCII(aSourceText).get());
     304             : #endif
     305             : 
     306             :   // The following code is similar to the cleanup in nsXMLContentSink::ReportError()
     307           0 :   mState = eXMLContentSinkState_InProlog;
     308             : 
     309             :   // Clear the current content
     310           0 :   nsCOMPtr<nsIDOMNode> node(do_QueryInterface(mRoot));
     311           0 :   if (node) {
     312             :     for (;;) {
     313           0 :       nsCOMPtr<nsIDOMNode> child, dummy;
     314           0 :       node->GetLastChild(getter_AddRefs(child));
     315           0 :       if (!child)
     316           0 :         break;
     317           0 :       node->RemoveChild(child, getter_AddRefs(dummy));
     318           0 :     }
     319             :   }
     320             : 
     321             :   // Clear any buffered-up text we have.  It's enough to set the length to 0.
     322             :   // The buffer itself is allocated when we're created and deleted in our
     323             :   // destructor, so don't mess with it.
     324           0 :   mTextLength = 0;
     325             : 
     326           0 :   return NS_OK;
     327             : }
     328             : 
     329             : nsresult
     330           0 : nsXMLFragmentContentSink::ProcessStyleLink(nsIContent* aElement,
     331             :                                            const nsAString& aHref,
     332             :                                            bool aAlternate,
     333             :                                            const nsAString& aTitle,
     334             :                                            const nsAString& aType,
     335             :                                            const nsAString& aMedia)
     336             : {
     337             :   // don't process until moved to document
     338           0 :   return NS_OK;
     339             : }
     340             : 
     341             : nsresult
     342           0 : nsXMLFragmentContentSink::LoadXSLStyleSheet(nsIURI* aUrl)
     343             : {
     344           0 :   NS_NOTREACHED("fragments shouldn't have XSL style sheets");
     345           0 :   return NS_ERROR_UNEXPECTED;
     346             : }
     347             : 
     348             : void
     349           0 : nsXMLFragmentContentSink::StartLayout()
     350             : {
     351           0 :   NS_NOTREACHED("fragments shouldn't layout");
     352           0 : }
     353             : 
     354             : ////////////////////////////////////////////////////////////////////////
     355             : 
     356             : NS_IMETHODIMP
     357           0 : nsXMLFragmentContentSink::FinishFragmentParsing(nsIDOMDocumentFragment** aFragment)
     358             : {
     359           0 :   *aFragment = nullptr;
     360           0 :   mTargetDocument = nullptr;
     361           0 :   mNodeInfoManager = nullptr;
     362           0 :   mScriptLoader = nullptr;
     363           0 :   mCSSLoader = nullptr;
     364           0 :   mContentStack.Clear();
     365           0 :   mDocumentURI = nullptr;
     366           0 :   mDocShell = nullptr;
     367           0 :   mDocElement = nullptr;
     368           0 :   mCurrentHead = nullptr;
     369           0 :   if (mParseError) {
     370             :     //XXX PARSE_ERR from DOM3 Load and Save would be more appropriate
     371           0 :     mRoot = nullptr;
     372           0 :     mParseError = false;
     373           0 :     return NS_ERROR_DOM_SYNTAX_ERR;
     374           0 :   } else if (mRoot) {
     375           0 :     nsresult rv = CallQueryInterface(mRoot, aFragment);
     376           0 :     mRoot = nullptr;
     377           0 :     return rv;
     378             :   } else {
     379           0 :     return NS_OK;
     380             :   }
     381             : }
     382             : 
     383             : NS_IMETHODIMP
     384           0 : nsXMLFragmentContentSink::SetTargetDocument(nsIDocument* aTargetDocument)
     385             : {
     386           0 :   NS_ENSURE_ARG_POINTER(aTargetDocument);
     387             : 
     388           0 :   mTargetDocument = aTargetDocument;
     389           0 :   mNodeInfoManager = aTargetDocument->NodeInfoManager();
     390             : 
     391           0 :   return NS_OK;
     392             : }
     393             : 
     394             : NS_IMETHODIMP
     395           0 : nsXMLFragmentContentSink::WillBuildContent()
     396             : {
     397           0 :   PushContent(mRoot);
     398             : 
     399           0 :   return NS_OK;
     400             : }
     401             : 
     402             : NS_IMETHODIMP
     403           0 : nsXMLFragmentContentSink::DidBuildContent()
     404             : {
     405             :   // Note: we need to FlushText() here because if we don't, we might not get
     406             :   // an end element to do it for us, so make sure.
     407           0 :   if (!mParseError) {
     408           0 :     FlushText();
     409             :   }
     410           0 :   PopContent();
     411             : 
     412           0 :   return NS_OK;
     413             : }
     414             : 
     415             : NS_IMETHODIMP
     416           0 : nsXMLFragmentContentSink::DidProcessATokenImpl()
     417             : {
     418           0 :   return NS_OK;
     419             : }
     420             : 
     421             : NS_IMETHODIMP
     422           0 : nsXMLFragmentContentSink::IgnoreFirstContainer()
     423             : {
     424           0 :   NS_NOTREACHED("XML isn't as broken as HTML");
     425           0 :   return NS_ERROR_FAILURE;
     426             : }
     427             : 
     428             : NS_IMETHODIMP
     429           0 : nsXMLFragmentContentSink::SetPreventScriptExecution(bool aPrevent)
     430             : {
     431           0 :   mPreventScriptExecution = aPrevent;
     432           0 :   return NS_OK;
     433             : }

Generated by: LCOV version 1.13