summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xslt/txUnknownHandler.cpp
blob: 2771d9069fe16032b3ba33f4d9594dcbceff872a (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
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
199
200
201
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */

#include "txUnknownHandler.h"

#include "mozilla/Move.h"
#include "txExecutionState.h"
#include "txStringUtils.h"
#include "txStylesheet.h"
#include "nsGkAtoms.h"

using mozilla::Move;

txUnknownHandler::txUnknownHandler(txExecutionState* aEs)
    : mEs(aEs),
      mFlushed(false)
{
    MOZ_COUNT_CTOR_INHERITED(txUnknownHandler, txBufferingHandler);
}

txUnknownHandler::~txUnknownHandler()
{
    MOZ_COUNT_DTOR_INHERITED(txUnknownHandler, txBufferingHandler);
}

nsresult
txUnknownHandler::attribute(nsIAtom* aPrefix, nsIAtom* aLocalName,
                            nsIAtom* aLowercaseLocalName, int32_t aNsID,
                            const nsString& aValue)
{
    return mFlushed ?
           mEs->mResultHandler->attribute(aPrefix, aLocalName,
                                          aLowercaseLocalName, aNsID, aValue) :
           txBufferingHandler::attribute(aPrefix, aLocalName,
                                         aLowercaseLocalName, aNsID, aValue);
}

nsresult
txUnknownHandler::attribute(nsIAtom* aPrefix, const nsSubstring& aLocalName,
                            const int32_t aNsID, const nsString& aValue)
{
    return mFlushed ?
           mEs->mResultHandler->attribute(aPrefix, aLocalName, aNsID, aValue) :
           txBufferingHandler::attribute(aPrefix, aLocalName, aNsID, aValue);
}

nsresult
txUnknownHandler::characters(const nsSubstring& aData, bool aDOE)
{
    return mFlushed ?
           mEs->mResultHandler->characters(aData, aDOE) :
           txBufferingHandler::characters(aData, aDOE);
}

nsresult
txUnknownHandler::comment(const nsString& aData)
{
    return mFlushed ?
           mEs->mResultHandler->comment(aData) :
           txBufferingHandler::comment(aData);
}

nsresult
txUnknownHandler::endDocument(nsresult aResult)
{
    if (!mFlushed) {
        if (NS_FAILED(aResult)) {
            return NS_OK;
        }

        // This is an unusual case, no output method has been set and we
        // didn't create a document element. Switching to XML output mode
        // anyway.

        // Make sure that mEs->mResultHandler == this is true, otherwise we'll
        // leak mEs->mResultHandler in createHandlerAndFlush.
        NS_ASSERTION(mEs->mResultHandler == this,
                     "We're leaking mEs->mResultHandler.");

        nsresult rv = createHandlerAndFlush(false, EmptyString(),
                                            kNameSpaceID_None);
        NS_ENSURE_SUCCESS(rv, rv);
    }

    return mEs->mResultHandler->endDocument(aResult);
}

nsresult
txUnknownHandler::endElement()
{
    return mFlushed ?
           mEs->mResultHandler->endElement() :
           txBufferingHandler::endElement();
}

nsresult
txUnknownHandler::processingInstruction(const nsString& aTarget,
                                        const nsString& aData)
{
    return mFlushed ?
           mEs->mResultHandler->processingInstruction(aTarget, aData) :
           txBufferingHandler::processingInstruction(aTarget, aData);
}

nsresult
txUnknownHandler::startDocument()
{
    return mFlushed ?
           mEs->mResultHandler->startDocument() :
           txBufferingHandler::startDocument();
}

nsresult
txUnknownHandler::startElement(nsIAtom* aPrefix, nsIAtom* aLocalName,
                               nsIAtom* aLowercaseLocalName, int32_t aNsID)
{
    if (!mFlushed) {
        // Make sure that mEs->mResultHandler == this is true, otherwise we'll
        // leak mEs->mResultHandler in createHandlerAndFlush.
        NS_ASSERTION(mEs->mResultHandler == this,
                     "We're leaking mEs->mResultHandler.");

        nsCOMPtr<nsIAtom> owner;
        if (!aLowercaseLocalName) {
            owner = TX_ToLowerCaseAtom(aLocalName);
            NS_ENSURE_TRUE(owner, NS_ERROR_OUT_OF_MEMORY);

            aLowercaseLocalName = owner;
        }

        bool htmlRoot = aNsID == kNameSpaceID_None && !aPrefix &&
                          aLowercaseLocalName == nsGkAtoms::html;

        // Use aLocalName and not aLowercaseLocalName in case the output
        // handler cares about case. For eHTMLOutput the handler will hardcode
        // to 'html' anyway.
        nsresult rv = createHandlerAndFlush(htmlRoot,
                                            nsDependentAtomString(aLocalName),
                                            aNsID);
        NS_ENSURE_SUCCESS(rv, rv);
    }

    return mEs->mResultHandler->startElement(aPrefix, aLocalName,
                                             aLowercaseLocalName, aNsID);
}

nsresult
txUnknownHandler::startElement(nsIAtom* aPrefix, const nsSubstring& aLocalName,
                               const int32_t aNsID)
{
    if (!mFlushed) {
        // Make sure that mEs->mResultHandler == this is true, otherwise we'll
        // leak mEs->mResultHandler in createHandlerAndFlush.
        NS_ASSERTION(mEs->mResultHandler == this,
                     "We're leaking mEs->mResultHandler.");

        bool htmlRoot = aNsID == kNameSpaceID_None && !aPrefix &&
                          aLocalName.Equals(NS_LITERAL_STRING("html"),
                                            txCaseInsensitiveStringComparator());
        nsresult rv = createHandlerAndFlush(htmlRoot, aLocalName, aNsID);
        NS_ENSURE_SUCCESS(rv, rv);
    }

    return mEs->mResultHandler->startElement(aPrefix, aLocalName, aNsID);
}

nsresult txUnknownHandler::createHandlerAndFlush(bool aHTMLRoot,
                                                 const nsSubstring& aName,
                                                 const int32_t aNsID)
{
    NS_ENSURE_TRUE(mBuffer, NS_ERROR_NOT_INITIALIZED);

    txOutputFormat format;
    format.merge(*(mEs->mStylesheet->getOutputFormat()));
    if (format.mMethod == eMethodNotSet) {
        format.mMethod = aHTMLRoot ? eHTMLOutput : eXMLOutput;
    }

    nsAutoPtr<txAXMLEventHandler> handler;
    nsresult rv = mEs->mOutputHandlerFactory->createHandlerWith(&format, aName,
                                                                aNsID,
                                                                getter_Transfers(handler));
    NS_ENSURE_SUCCESS(rv, rv);

    mEs->mOutputHandler = handler;
    mEs->mResultHandler = handler.forget();
    // Let the executionstate delete us. We need to stay alive because we might
    // need to forward hooks to mEs->mResultHandler if someone is currently
    // flushing a buffer to mEs->mResultHandler.
    mEs->mObsoleteHandler = this;

    mFlushed = true;

    // Let go of out buffer as soon as we're done flushing it, we're not going
    // to need it anymore from this point on (all hooks get forwarded to
    // mEs->mResultHandler.
    nsAutoPtr<txResultBuffer> buffer(Move(mBuffer));
    return buffer->flushToHandler(mEs->mResultHandler);
}