summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xslt/txMozillaXMLOutput.h
blob: c77ecb6aae88778ffd5063fbeddc317c2411765a (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 TRANSFRMX_MOZILLA_XML_OUTPUT_H
#define TRANSFRMX_MOZILLA_XML_OUTPUT_H

#include "txXMLEventHandler.h"
#include "nsIScriptLoaderObserver.h"
#include "txOutputFormat.h"
#include "nsTArray.h"
#include "nsCOMPtr.h"
#include "nsICSSLoaderObserver.h"
#include "txStack.h"
#include "mozilla/Attributes.h"

class nsIContent;
class nsAtom;
class nsITransformObserver;
class nsNodeInfoManager;
class nsINode;

namespace mozilla::dom {
class Document;
class DocumentFragment;
class Element;
}  // namespace mozilla::dom

class txTransformNotifier final : public nsIScriptLoaderObserver,
                                  public nsICSSLoaderObserver {
 public:
  explicit txTransformNotifier(mozilla::dom::Document* aSourceDocument);

  NS_DECL_ISUPPORTS
  NS_DECL_NSISCRIPTLOADEROBSERVER

  // nsICSSLoaderObserver
  NS_IMETHOD StyleSheetLoaded(mozilla::StyleSheet* aSheet, bool aWasDeferred,
                              nsresult aStatus) override;

  void Init(nsITransformObserver* aObserver);
  void AddScriptElement(nsIScriptElement* aElement);
  void AddPendingStylesheet();
  void OnTransformEnd(nsresult aResult = NS_OK);
  void OnTransformStart();
  nsresult SetOutputDocument(mozilla::dom::Document* aDocument);

 private:
  ~txTransformNotifier();
  void SignalTransformEnd(nsresult aResult = NS_OK);

  nsCOMPtr<mozilla::dom::Document> mSourceDocument;
  nsCOMPtr<mozilla::dom::Document> mDocument;
  nsCOMPtr<nsITransformObserver> mObserver;
  nsTArray<nsCOMPtr<nsIScriptElement>> mScriptElements;
  uint32_t mPendingStylesheetCount;
  bool mInTransform;
};

class txMozillaXMLOutput : public txAOutputXMLEventHandler {
 public:
  txMozillaXMLOutput(mozilla::dom::Document* aSourceDocument,
                     txOutputFormat* aFormat, nsITransformObserver* aObserver);
  txMozillaXMLOutput(txOutputFormat* aFormat,
                     mozilla::dom::DocumentFragment* aFragment, bool aNoFixup);
  ~txMozillaXMLOutput();

  TX_DECL_TXAXMLEVENTHANDLER
  TX_DECL_TXAOUTPUTXMLEVENTHANDLER

  nsresult closePrevious(bool aFlushText);

  nsresult createResultDocument(const nsAString& aName, int32_t aNsID,
                                mozilla::dom::Document* aSourceDocument,
                                bool aLoadedAsData);

 private:
  nsresult createTxWrapper();
  nsresult startHTMLElement(nsIContent* aElement, bool aXHTML);
  void endHTMLElement(nsIContent* aElement);
  nsresult createHTMLElement(nsAtom* aName, mozilla::dom::Element** aResult);

  nsresult attributeInternal(nsAtom* aPrefix, nsAtom* aLocalName, int32_t aNsID,
                             const nsString& aValue);
  nsresult startElementInternal(nsAtom* aPrefix, nsAtom* aLocalName,
                                int32_t aNsID);

  RefPtr<mozilla::dom::Document> mDocument;
  nsCOMPtr<nsINode> mCurrentNode;  // This is updated once an element is
                                   // 'closed' (i.e. once we're done
                                   // adding attributes to it).
                                   // until then the opened element is
                                   // kept in mOpenedElement
  nsCOMPtr<mozilla::dom::Element> mOpenedElement;
  RefPtr<nsNodeInfoManager> mNodeInfoManager;

  nsTArray<nsCOMPtr<nsINode>> mCurrentNodeStack;

  nsCOMPtr<nsIContent> mNonAddedNode;

  RefPtr<txTransformNotifier> mNotifier;

  uint32_t mTreeDepth, mBadChildLevel;

  txStack mTableStateStack;
  enum TableState {
    NORMAL,      // An element needing no special treatment
    TABLE,       // A HTML table element
    ADDED_TBODY  // An inserted tbody not coming from the stylesheet
  };
  TableState mTableState;

  nsAutoString mText;

  txOutputFormat mOutputFormat;

  bool mCreatingNewDocument;

  bool mOpenedElementIsHTML;

  // Set to true when we know there's a root content in our document.
  bool mRootContentCreated;

  bool mNoFixup;

  enum txAction { eCloseElement = 1, eFlushText = 2 };
};

#endif