DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (27a812186ff4)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sw=2 et tw=78: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsHtml5DocumentBuilder_h
#define nsHtml5DocumentBuilder_h

#include "nsHtml5PendingNotification.h"
#include "nsContentSink.h"
#include "nsHtml5DocumentMode.h"
#include "nsIDocument.h"

typedef nsIContent* nsIContentPtr;

enum eHtml5FlushState {
  eNotFlushing = 0,  // not flushing
  eInFlush = 1,      // the Flush() method is on the call stack
  eInDocUpdate = 2,  // inside an update batch on the document
  eNotifying = 3     // flushing pending append notifications
};

class nsHtml5DocumentBuilder : public nsContentSink
{
public:
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHtml5DocumentBuilder,
                                           nsContentSink)

  NS_DECL_ISUPPORTS_INHERITED

  inline void HoldElement(nsIContent* aContent)
  {
    mOwnedElements.AppendElement(aContent);
  }

  inline bool HaveNotified(nsIContent* aNode)
  {
    NS_PRECONDITION(aNode, "HaveNotified called with null argument.");
    const nsHtml5PendingNotification* start = mPendingNotifications.Elements();
    const nsHtml5PendingNotification* end = start + mPendingNotifications.Length();
    for (;;) {
      nsIContent* parent = aNode->GetParent();
      if (!parent) {
        return true;
      }
      for (nsHtml5PendingNotification* iter = (nsHtml5PendingNotification*)start; iter < end; ++iter) {
        if (iter->Contains(parent)) {
          return iter->HaveNotifiedIndex(parent->IndexOf(aNode));
        }
      }
      aNode = parent;
    }
  }

  void PostPendingAppendNotification(nsIContent* aParent, nsIContent* aChild)
  {
    bool newParent = true;
    const nsIContentPtr* first = mElementsSeenInThisAppendBatch.Elements();
    const nsIContentPtr* last = first + mElementsSeenInThisAppendBatch.Length() - 1;
    for (const nsIContentPtr* iter = last; iter >= first; --iter) {
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
      sAppendBatchSlotsExamined++;
#endif
      if (*iter == aParent) {
        newParent = false;
        break;
      }
    }
    if (aChild->IsElement()) {
      mElementsSeenInThisAppendBatch.AppendElement(aChild);
    }
    mElementsSeenInThisAppendBatch.AppendElement(aParent);
    if (newParent) {
      mPendingNotifications.AppendElement(aParent);
    }
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
    sAppendBatchExaminations++;
#endif
  }

  void FlushPendingAppendNotifications()
  {
    NS_PRECONDITION(mFlushState == eInDocUpdate, "Notifications flushed outside update");
    mFlushState = eNotifying;
    const nsHtml5PendingNotification* start = mPendingNotifications.Elements();
    const nsHtml5PendingNotification* end = start + mPendingNotifications.Length();
    for (nsHtml5PendingNotification* iter = (nsHtml5PendingNotification*)start; iter < end; ++iter) {
      iter->Fire();
    }
    mPendingNotifications.Clear();
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
    if (mElementsSeenInThisAppendBatch.Length() > sAppendBatchMaxSize) {
      sAppendBatchMaxSize = mElementsSeenInThisAppendBatch.Length();
    }
#endif
    mElementsSeenInThisAppendBatch.Clear();
    NS_ASSERTION(mFlushState == eNotifying, "mFlushState out of sync");
    mFlushState = eInDocUpdate;
  }

  nsresult Init(nsIDocument* aDoc, nsIURI* aURI,
                nsISupports* aContainer, nsIChannel* aChannel);

  // Getters and setters for fields from nsContentSink
  nsIDocument* GetDocument()
  {
    return mDocument;
  }

  nsNodeInfoManager* GetNodeInfoManager()
  {
    return mNodeInfoManager;
  }

  /**
   * Marks this parser as broken and tells the stream parser (if any) to
   * terminate.
   *
   * @return aReason for convenience
   */
  virtual nsresult MarkAsBroken(nsresult aReason);

  /**
   * Checks if this parser is broken. Returns a non-NS_OK (i.e. non-0)
   * value if broken.
   */
  inline nsresult IsBroken()
  {
    return mBroken;
  }

  inline void BeginDocUpdate()
  {
    NS_PRECONDITION(mFlushState == eInFlush, "Tried to double-open update.");
    NS_PRECONDITION(mParser, "Started update without parser.");
    mFlushState = eInDocUpdate;
    mDocument->BeginUpdate(UPDATE_CONTENT_MODEL);
  }

  inline void EndDocUpdate()
  {
    NS_PRECONDITION(mFlushState != eNotifying, "mFlushState out of sync");
    if (mFlushState == eInDocUpdate) {
      FlushPendingAppendNotifications();
      mFlushState = eInFlush;
      mDocument->EndUpdate(UPDATE_CONTENT_MODEL);
    }
  }

  void SetDocumentCharsetAndSource(nsACString& aCharset, int32_t aCharsetSource);

  /**
   * Sets up style sheet load / parse
   */
  void UpdateStyleSheet(nsIContent* aElement);

  void SetDocumentMode(nsHtml5DocumentMode m);

  void SetNodeInfoManager(nsNodeInfoManager* aManager)
  {
    mNodeInfoManager = aManager;
  }

  // nsContentSink methods
  virtual void UpdateChildCounts();
  virtual nsresult FlushTags();

protected:
  inline void SetAppendBatchCapacity(uint32_t aCapacity)
  {
    mElementsSeenInThisAppendBatch.SetCapacity(aCapacity);
  }

  nsHtml5DocumentBuilder(bool aRunsToCompletion);
  virtual ~nsHtml5DocumentBuilder();

private:
  nsTArray<nsHtml5PendingNotification> mPendingNotifications;
  nsTArray<nsIContentPtr>              mElementsSeenInThisAppendBatch;
protected:
  nsTArray<nsCOMPtr<nsIContent> >      mOwnedElements;
  /**
   * Non-NS_OK if this parser should refuse to process any more input.
   * For example, the parser needs to be marked as broken if it drops some
   * input due to a memory allocation failure. In such a case, the whole
   * parser needs to be marked as broken, because some input has been lost
   * and parsing more input could lead to a DOM where pieces of HTML source
   * that weren't supposed to become scripts become scripts.
   *
   * Since NS_OK is actually 0, zeroing operator new takes care of
   * initializing this.
   */
  nsresult                             mBroken;
  eHtml5FlushState                     mFlushState;
};

#endif // nsHtml5DocumentBuilder_h