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 #include "wtf/Vector.h"
37
38 namespace WebCore {
39
transformTextStringToXHTMLDocumentString(String & text)40 static inline void transformTextStringToXHTMLDocumentString(String& text)
41 {
42 // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
43 text.replaceWithLiteral('&', "&");
44 text.replaceWithLiteral('<', "<");
45 text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
46 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
47 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
48 "<head><title/></head>\n"
49 "<body>\n"
50 "<pre>" + text + "</pre>\n"
51 "</body>\n"
52 "</html>\n";
53 }
54
~XSLTProcessor()55 XSLTProcessor::~XSLTProcessor()
56 {
57 #if !ENABLE(OILPAN)
58 // Stylesheet shouldn't outlive its root node.
59 ASSERT(!m_stylesheetRootNode || !m_stylesheet || m_stylesheet->hasOneRef());
60 #endif
61 }
62
createDocumentFromSource(const String & sourceString,const String & sourceEncoding,const String & sourceMIMEType,Node * sourceNode,LocalFrame * frame)63 PassRefPtrWillBeRawPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString,
64 const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, LocalFrame* frame)
65 {
66 RefPtrWillBeRawPtr<Document> ownerDocument(sourceNode->document());
67 bool sourceIsDocument = (sourceNode == ownerDocument.get());
68 String documentSource = sourceString;
69
70 RefPtrWillBeRawPtr<Document> result = nullptr;
71 DocumentInit init(sourceIsDocument ? ownerDocument->url() : KURL(), frame);
72
73 bool forceXHTML = sourceMIMEType == "text/plain";
74 if (forceXHTML)
75 transformTextStringToXHTMLDocumentString(documentSource);
76
77 if (frame) {
78 RefPtrWillBeRawPtr<Document> oldDocument = frame->document();
79 result = frame->domWindow()->installNewDocument(sourceMIMEType, init, forceXHTML);
80
81 // Before parsing, we need to save & detach the old document and get the new document
82 // in place. We have to do this only if we're rendering the result document.
83 if (FrameView* view = frame->view())
84 view->clear();
85
86 if (oldDocument) {
87 result->setTransformSourceDocument(oldDocument.get());
88 result->updateSecurityOrigin(oldDocument->securityOrigin());
89 result->setCookieURL(oldDocument->cookieURL());
90 result->contentSecurityPolicy()->copyStateFrom(oldDocument->contentSecurityPolicy());
91 }
92 } else {
93 result = LocalDOMWindow::createDocument(sourceMIMEType, init, forceXHTML);
94 }
95
96 DocumentEncodingData data;
97 data.setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : WTF::TextEncoding(sourceEncoding));
98 result->setEncodingData(data);
99 result->setContent(documentSource);
100
101 return result.release();
102 }
103
transformToDocument(Node * sourceNode)104 PassRefPtrWillBeRawPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode)
105 {
106 if (!sourceNode)
107 return nullptr;
108
109 String resultMIMEType;
110 String resultString;
111 String resultEncoding;
112 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
113 return nullptr;
114 return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);
115 }
116
transformToFragment(Node * sourceNode,Document * outputDoc)117 PassRefPtrWillBeRawPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc)
118 {
119 if (!sourceNode || !outputDoc)
120 return nullptr;
121
122 String resultMIMEType;
123 String resultString;
124 String resultEncoding;
125
126 // If the output document is HTML, default to HTML method.
127 if (outputDoc->isHTMLDocument())
128 resultMIMEType = "text/html";
129
130 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
131 return nullptr;
132 return createFragmentForTransformToFragment(resultString, resultMIMEType, *outputDoc);
133 }
134
setParameter(const String &,const String & localName,const String & value)135 void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value)
136 {
137 // FIXME: namespace support?
138 // should make a QualifiedName here but we'd have to expose the impl
139 m_parameters.set(localName, value);
140 }
141
getParameter(const String &,const String & localName) const142 String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const
143 {
144 // FIXME: namespace support?
145 // should make a QualifiedName here but we'd have to expose the impl
146 return m_parameters.get(localName);
147 }
148
removeParameter(const String &,const String & localName)149 void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName)
150 {
151 // FIXME: namespace support?
152 m_parameters.remove(localName);
153 }
154
reset()155 void XSLTProcessor::reset()
156 {
157 m_stylesheet.clear();
158 m_stylesheetRootNode.clear();
159 m_parameters.clear();
160 }
161
trace(Visitor * visitor)162 void XSLTProcessor::trace(Visitor* visitor)
163 {
164 visitor->trace(m_stylesheet);
165 visitor->trace(m_stylesheetRootNode);
166 }
167
168 } // namespace WebCore
169