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