1/*
2 * This file is part of the XSL implementation.
3 *
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple, Inc. All rights reserved.
5 * Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@webkit.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#include "config.h"
24#include "core/xml/XSLTProcessor.h"
25
26#include "core/dom/DOMImplementation.h"
27#include "core/dom/DocumentEncodingData.h"
28#include "core/dom/DocumentFragment.h"
29#include "core/editing/markup.h"
30#include "core/frame/LocalDOMWindow.h"
31#include "core/frame/FrameView.h"
32#include "core/frame/LocalFrame.h"
33#include "core/frame/csp/ContentSecurityPolicy.h"
34#include "platform/weborigin/SecurityOrigin.h"
35#include "wtf/Assertions.h"
36
37namespace blink {
38
39static inline void transformTextStringToXHTMLDocumentString(String& text)
40{
41    // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
42    text.replaceWithLiteral('&', "&amp;");
43    text.replaceWithLiteral('<', "&lt;");
44    text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
45        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
46        "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
47        "<head><title/></head>\n"
48        "<body>\n"
49        "<pre>" + text + "</pre>\n"
50        "</body>\n"
51        "</html>\n";
52}
53
54XSLTProcessor::~XSLTProcessor()
55{
56#if !ENABLE(OILPAN)
57    // Stylesheet shouldn't outlive its root node.
58    ASSERT(!m_stylesheetRootNode || !m_stylesheet || m_stylesheet->hasOneRef());
59#endif
60}
61
62PassRefPtrWillBeRawPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString,
63    const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, LocalFrame* frame)
64{
65    RefPtrWillBeRawPtr<Document> ownerDocument(sourceNode->document());
66    bool sourceIsDocument = (sourceNode == ownerDocument.get());
67    String documentSource = sourceString;
68
69    RefPtrWillBeRawPtr<Document> result = nullptr;
70    DocumentInit init(sourceIsDocument ? ownerDocument->url() : KURL(), frame);
71
72    bool forceXHTML = sourceMIMEType == "text/plain";
73    if (forceXHTML)
74        transformTextStringToXHTMLDocumentString(documentSource);
75
76    if (frame) {
77        RefPtrWillBeRawPtr<Document> oldDocument = frame->document();
78        result = frame->domWindow()->installNewDocument(sourceMIMEType, init, forceXHTML);
79
80        // Before parsing, we need to save & detach the old document and get the new document
81        // in place. We have to do this only if we're rendering the result document.
82        if (FrameView* view = frame->view())
83            view->clear();
84
85        if (oldDocument) {
86            result->setTransformSourceDocument(oldDocument.get());
87            result->updateSecurityOrigin(oldDocument->securityOrigin());
88            result->setCookieURL(oldDocument->cookieURL());
89            result->initContentSecurityPolicy();
90        }
91    } else {
92        result = LocalDOMWindow::createDocument(sourceMIMEType, init, forceXHTML);
93    }
94
95    DocumentEncodingData data;
96    data.setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : WTF::TextEncoding(sourceEncoding));
97    result->setEncodingData(data);
98    result->setContent(documentSource);
99
100    return result.release();
101}
102
103PassRefPtrWillBeRawPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode)
104{
105    if (!sourceNode)
106        return nullptr;
107
108    String resultMIMEType;
109    String resultString;
110    String resultEncoding;
111    if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
112        return nullptr;
113    return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);
114}
115
116PassRefPtrWillBeRawPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc)
117{
118    if (!sourceNode || !outputDoc)
119        return nullptr;
120
121    String resultMIMEType;
122    String resultString;
123    String resultEncoding;
124
125    // If the output document is HTML, default to HTML method.
126    if (outputDoc->isHTMLDocument())
127        resultMIMEType = "text/html";
128
129    if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
130        return nullptr;
131    return createFragmentForTransformToFragment(resultString, resultMIMEType, *outputDoc);
132}
133
134void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value)
135{
136    // FIXME: namespace support?
137    // should make a QualifiedName here but we'd have to expose the impl
138    m_parameters.set(localName, value);
139}
140
141String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const
142{
143    // FIXME: namespace support?
144    // should make a QualifiedName here but we'd have to expose the impl
145    return m_parameters.get(localName);
146}
147
148void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName)
149{
150    // FIXME: namespace support?
151    m_parameters.remove(localName);
152}
153
154void XSLTProcessor::reset()
155{
156    m_stylesheet.clear();
157    m_stylesheetRootNode.clear();
158    m_parameters.clear();
159}
160
161void XSLTProcessor::trace(Visitor* visitor)
162{
163    visitor->trace(m_stylesheet);
164    visitor->trace(m_stylesheetRootNode);
165    visitor->trace(m_document);
166}
167
168} // namespace blink
169