2 * This file is part of the XSL implementation.
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple, Inc. All rights reserved.
5 * Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@webkit.org>
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.
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.
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.
24 #include "core/xml/XSLTProcessor.h"
26 #include "core/dom/DOMImplementation.h"
27 #include "core/dom/DocumentEncodingData.h"
28 #include "core/dom/DocumentFragment.h"
29 #include "core/editing/serializers/Serialization.h"
30 #include "core/frame/FrameView.h"
31 #include "core/frame/LocalDOMWindow.h"
32 #include "core/frame/LocalFrame.h"
33 #include "core/frame/csp/ContentSecurityPolicy.h"
34 #include "core/loader/FrameLoaderClient.h"
35 #include "core/xml/DocumentXSLT.h"
36 #include "platform/weborigin/SecurityOrigin.h"
37 #include "wtf/Assertions.h"
41 static inline void transformTextStringToXHTMLDocumentString(String
& text
)
43 // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
44 text
.replaceWithLiteral('&', "&");
45 text
.replaceWithLiteral('<', "<");
46 text
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
47 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
48 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
49 "<head><title/></head>\n"
51 "<pre>" + text
+ "</pre>\n"
56 XSLTProcessor::~XSLTProcessor()
59 // Stylesheet shouldn't outlive its root node.
60 ASSERT(!m_stylesheetRootNode
|| !m_stylesheet
|| m_stylesheet
->hasOneRef());
64 PassRefPtrWillBeRawPtr
<Document
> XSLTProcessor::createDocumentFromSource(const String
& sourceString
,
65 const String
& sourceEncoding
, const String
& sourceMIMEType
, Node
* sourceNode
, LocalFrame
* frame
)
67 RefPtrWillBeRawPtr
<Document
> ownerDocument(sourceNode
->document());
68 bool sourceIsDocument
= (sourceNode
== ownerDocument
.get());
69 String documentSource
= sourceString
;
71 RefPtrWillBeRawPtr
<Document
> result
= nullptr;
72 DocumentInit
init(sourceIsDocument
? ownerDocument
->url() : KURL(), frame
);
74 bool forceXHTML
= sourceMIMEType
== "text/plain";
76 transformTextStringToXHTMLDocumentString(documentSource
);
79 RefPtrWillBeRawPtr
<Document
> oldDocument
= frame
->document();
80 // Before parsing, we need to save & detach the old document and get the new document
81 // in place. Document::detach() tears down the FrameView, so remember whether or not
83 bool hasView
= frame
->view();
84 oldDocument
->detach();
85 // Re-create the FrameView if needed.
87 frame
->loader().client()->transitionToCommittedForNewPage();
88 result
= frame
->localDOMWindow()->installNewDocument(sourceMIMEType
, init
, forceXHTML
);
91 DocumentXSLT::from(*result
).setTransformSourceDocument(oldDocument
.get());
92 result
->updateSecurityOrigin(oldDocument
->securityOrigin());
93 result
->setCookieURL(oldDocument
->cookieURL());
95 RefPtrWillBeRawPtr
<ContentSecurityPolicy
> csp
= ContentSecurityPolicy::create();
96 csp
->copyStateFrom(oldDocument
->contentSecurityPolicy());
97 result
->initContentSecurityPolicy(csp
);
100 result
= LocalDOMWindow::createDocument(sourceMIMEType
, init
, forceXHTML
);
103 DocumentEncodingData data
;
104 data
.setEncoding(sourceEncoding
.isEmpty() ? UTF8Encoding() : WTF::TextEncoding(sourceEncoding
));
105 result
->setEncodingData(data
);
106 result
->setContent(documentSource
);
108 return result
.release();
111 PassRefPtrWillBeRawPtr
<Document
> XSLTProcessor::transformToDocument(Node
* sourceNode
)
113 String resultMIMEType
;
115 String resultEncoding
;
116 if (!transformToString(sourceNode
, resultMIMEType
, resultString
, resultEncoding
))
118 return createDocumentFromSource(resultString
, resultEncoding
, resultMIMEType
, sourceNode
, 0);
121 PassRefPtrWillBeRawPtr
<DocumentFragment
> XSLTProcessor::transformToFragment(Node
* sourceNode
, Document
* outputDoc
)
123 String resultMIMEType
;
125 String resultEncoding
;
127 // If the output document is HTML, default to HTML method.
128 if (outputDoc
->isHTMLDocument())
129 resultMIMEType
= "text/html";
131 if (!transformToString(sourceNode
, resultMIMEType
, resultString
, resultEncoding
))
133 return createFragmentForTransformToFragment(resultString
, resultMIMEType
, *outputDoc
);
136 void XSLTProcessor::setParameter(const String
& /*namespaceURI*/, const String
& localName
, const String
& value
)
138 // FIXME: namespace support?
139 // should make a QualifiedName here but we'd have to expose the impl
140 m_parameters
.set(localName
, value
);
143 String
XSLTProcessor::getParameter(const String
& /*namespaceURI*/, const String
& localName
) const
145 // FIXME: namespace support?
146 // should make a QualifiedName here but we'd have to expose the impl
147 return m_parameters
.get(localName
);
150 void XSLTProcessor::removeParameter(const String
& /*namespaceURI*/, const String
& localName
)
152 // FIXME: namespace support?
153 m_parameters
.remove(localName
);
156 void XSLTProcessor::reset()
158 m_stylesheet
.clear();
159 m_stylesheetRootNode
.clear();
160 m_parameters
.clear();
163 DEFINE_TRACE(XSLTProcessor
)
165 visitor
->trace(m_stylesheet
);
166 visitor
->trace(m_stylesheetRootNode
);
167 visitor
->trace(m_document
);