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"
39 #include "TextResourceDecoder.h"
42 #include <wtf/Assertions.h>
43 #include <wtf/Vector.h>
47 static inline void transformTextStringToXHTMLDocumentString(String& text)
49 // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
50 text.replace('&', "&");
51 text.replace('<', "<");
52 text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
53 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
54 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
55 "<head><title/></head>\n"
57 "<pre>" + text + "</pre>\n"
62 XSLTProcessor::~XSLTProcessor()
64 // Stylesheet shouldn't outlive its root node.
65 ASSERT(!m_stylesheetRootNode || !m_stylesheet || m_stylesheet->hasOneRef());
68 PassRefPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString,
69 const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, Frame* frame)
71 RefPtr<Document> ownerDocument = sourceNode->document();
72 bool sourceIsDocument = (sourceNode == ownerDocument.get());
73 String documentSource = sourceString;
75 RefPtr<Document> result;
76 if (sourceMIMEType == "text/plain") {
77 result = Document::create(frame, sourceIsDocument ? ownerDocument->url() : KURL());
78 transformTextStringToXHTMLDocumentString(documentSource);
80 result = DOMImplementation::createDocument(sourceMIMEType, frame, sourceIsDocument ? ownerDocument->url() : KURL(), false);
82 // Before parsing, we need to save & detach the old document and get the new document
83 // in place. We have to do this only if we're rendering the result document.
85 if (FrameView* view = frame->view())
87 result->setTransformSourceDocument(frame->document());
88 frame->setDocument(result);
91 RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create(sourceMIMEType);
92 decoder->setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : TextEncoding(sourceEncoding), TextResourceDecoder::EncodingFromXMLHeader);
93 result->setDecoder(decoder.release());
95 result->setContent(documentSource);
97 return result.release();
100 static inline RefPtr<DocumentFragment> createFragmentFromSource(const String& sourceString, const String& sourceMIMEType, Document* outputDoc)
102 RefPtr<DocumentFragment> fragment = outputDoc->createDocumentFragment();
104 if (sourceMIMEType == "text/html") {
105 // As far as I can tell, there isn't a spec for how transformToFragment
106 // is supposed to work. Based on the documentation I can find, it looks
107 // like we want to start parsing the fragment in the InBody insertion
108 // mode. Unfortunately, that's an implementation detail of the parser.
109 // We achieve that effect here by passing in a fake body element as
110 // context for the fragment.
111 RefPtr<HTMLBodyElement> fakeBody = HTMLBodyElement::create(outputDoc);
112 fragment->parseHTML(sourceString, fakeBody.get());
113 } else if (sourceMIMEType == "text/plain")
114 fragment->parserAddChild(Text::create(outputDoc, sourceString));
116 bool successfulParse = fragment->parseXML(sourceString, 0);
117 if (!successfulParse)
121 // FIXME: Do we need to mess with URLs here?
126 PassRefPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode)
128 String resultMIMEType;
130 String resultEncoding;
131 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
133 return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);
136 PassRefPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc)
138 String resultMIMEType;
140 String resultEncoding;
142 // If the output document is HTML, default to HTML method.
143 if (outputDoc->isHTMLDocument())
144 resultMIMEType = "text/html";
146 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
148 return createFragmentFromSource(resultString, resultMIMEType, outputDoc);
151 void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value)
153 // FIXME: namespace support?
154 // should make a QualifiedName here but we'd have to expose the impl
155 m_parameters.set(localName, value);
158 String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const
160 // FIXME: namespace support?
161 // should make a QualifiedName here but we'd have to expose the impl
162 return m_parameters.get(localName);
165 void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName)
167 // FIXME: namespace support?
168 m_parameters.remove(localName);
171 void XSLTProcessor::reset()
173 m_stylesheet.clear();
174 m_stylesheetRootNode.clear();
175 m_parameters.clear();
178 } // namespace WebCore
180 #endif // ENABLE(XSLT)