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.
27 #include "XSLTProcessor.h"
29 #include "DOMImplementation.h"
30 #include "CachedResourceLoader.h"
31 #include "DocumentFragment.h"
33 #include "FrameLoader.h"
34 #include "FrameView.h"
35 #include "HTMLBodyElement.h"
36 #include "HTMLDocument.h"
38 #include "SecurityOrigin.h"
40 #include "TextResourceDecoder.h"
43 #include <wtf/Assertions.h>
44 #include <wtf/Vector.h>
48 static inline void transformTextStringToXHTMLDocumentString(String& text)
50 // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
51 text.replace('&', "&");
52 text.replace('<', "<");
53 text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
54 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
55 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
56 "<head><title/></head>\n"
58 "<pre>" + text + "</pre>\n"
63 XSLTProcessor::~XSLTProcessor()
65 // Stylesheet shouldn't outlive its root node.
66 ASSERT(!m_stylesheetRootNode || !m_stylesheet || m_stylesheet->hasOneRef());
69 PassRefPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString,
70 const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, Frame* frame)
72 RefPtr<Document> ownerDocument = sourceNode->document();
73 bool sourceIsDocument = (sourceNode == ownerDocument.get());
74 String documentSource = sourceString;
76 RefPtr<Document> result;
77 if (sourceMIMEType == "text/plain") {
78 result = Document::create(frame, sourceIsDocument ? ownerDocument->url() : KURL());
79 transformTextStringToXHTMLDocumentString(documentSource);
81 result = DOMImplementation::createDocument(sourceMIMEType, frame, sourceIsDocument ? ownerDocument->url() : KURL(), false);
83 // Before parsing, we need to save & detach the old document and get the new document
84 // in place. We have to do this only if we're rendering the result document.
86 if (FrameView* view = frame->view())
89 if (Document* oldDocument = frame->document()) {
90 result->setTransformSourceDocument(oldDocument);
91 result->setSecurityOrigin(oldDocument->securityOrigin());
92 result->setCookieURL(oldDocument->cookieURL());
95 frame->setDocument(result);
98 RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create(sourceMIMEType);
99 decoder->setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : TextEncoding(sourceEncoding), TextResourceDecoder::EncodingFromXMLHeader);
100 result->setDecoder(decoder.release());
102 result->setContent(documentSource);
104 return result.release();
107 static inline RefPtr<DocumentFragment> createFragmentFromSource(const String& sourceString, const String& sourceMIMEType, Document* outputDoc)
109 RefPtr<DocumentFragment> fragment = outputDoc->createDocumentFragment();
111 if (sourceMIMEType == "text/html") {
112 // As far as I can tell, there isn't a spec for how transformToFragment
113 // is supposed to work. Based on the documentation I can find, it looks
114 // like we want to start parsing the fragment in the InBody insertion
115 // mode. Unfortunately, that's an implementation detail of the parser.
116 // We achieve that effect here by passing in a fake body element as
117 // context for the fragment.
118 RefPtr<HTMLBodyElement> fakeBody = HTMLBodyElement::create(outputDoc);
119 fragment->parseHTML(sourceString, fakeBody.get());
120 } else if (sourceMIMEType == "text/plain")
121 fragment->parserAddChild(Text::create(outputDoc, sourceString));
123 bool successfulParse = fragment->parseXML(sourceString, 0);
124 if (!successfulParse)
128 // FIXME: Do we need to mess with URLs here?
133 PassRefPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode)
135 String resultMIMEType;
137 String resultEncoding;
138 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
140 return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);
143 PassRefPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc)
145 String resultMIMEType;
147 String resultEncoding;
149 // If the output document is HTML, default to HTML method.
150 if (outputDoc->isHTMLDocument())
151 resultMIMEType = "text/html";
153 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
155 return createFragmentFromSource(resultString, resultMIMEType, outputDoc);
158 void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value)
160 // FIXME: namespace support?
161 // should make a QualifiedName here but we'd have to expose the impl
162 m_parameters.set(localName, value);
165 String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const
167 // FIXME: namespace support?
168 // should make a QualifiedName here but we'd have to expose the impl
169 return m_parameters.get(localName);
172 void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName)
174 // FIXME: namespace support?
175 m_parameters.remove(localName);
178 void XSLTProcessor::reset()
180 m_stylesheet.clear();
181 m_stylesheetRootNode.clear();
182 m_parameters.clear();
185 } // namespace WebCore
187 #endif // ENABLE(XSLT)