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