darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 1 | /* |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 2 | * This file is part of the XSL implementation. |
| 3 | * |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 4 | * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple, Inc. All rights reserved. |
ap | 247ccf0 | 2006-12-25 18:37:20 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@webkit.org> |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | |
mjs | d2948ef | 2007-02-26 19:29:04 +0000 | [diff] [blame] | 25 | #if ENABLE(XSLT) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 26 | |
darin | 91298e5 | 2006-06-12 01:10:17 +0000 | [diff] [blame] | 27 | #include "XSLTProcessor.h" |
| 28 | |
darin | 6f2a3ef | 2006-04-05 21:19:57 +0000 | [diff] [blame] | 29 | #include "DOMImplementation.h" |
abarth@webkit.org | 17d66c6 | 2010-09-08 10:26:02 +0000 | [diff] [blame] | 30 | #include "CachedResourceLoader.h" |
tsepez@chromium.org | 3656825 | 2011-12-23 10:40:25 +0000 | [diff] [blame] | 31 | #include "ContentSecurityPolicy.h" |
darin | 36d1136 | 2006-04-11 16:30:21 +0000 | [diff] [blame] | 32 | #include "DocumentFragment.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 33 | #include "Frame.h" |
darin | c370e7e | 2006-11-08 05:52:27 +0000 | [diff] [blame] | 34 | #include "FrameLoader.h" |
darin | 647be15 | 2006-11-05 22:02:23 +0000 | [diff] [blame] | 35 | #include "FrameView.h" |
eric@webkit.org | 7135346 | 2010-08-23 23:49:09 +0000 | [diff] [blame] | 36 | #include "HTMLBodyElement.h" |
darin | 6f2a3ef | 2006-04-05 21:19:57 +0000 | [diff] [blame] | 37 | #include "HTMLDocument.h" |
ap | 276970c | 2007-10-29 17:02:51 +0000 | [diff] [blame] | 38 | #include "Page.h" |
abarth@webkit.org | 51ad70c | 2011-11-18 00:20:41 +0000 | [diff] [blame] | 39 | #include "SecurityOrigin.h" |
andersca@apple.com | a57d555 | 2014-12-22 23:42:30 +0000 | [diff] [blame] | 40 | #include "SecurityOriginPolicy.h" |
weinig | f18aae3 | 2006-08-03 21:55:57 +0000 | [diff] [blame] | 41 | #include "Text.h" |
weinig | 54a89ffb | 2007-01-08 17:50:01 +0000 | [diff] [blame] | 42 | #include "TextResourceDecoder.h" |
cdumez@apple.com | d7e206f | 2016-01-25 01:11:35 +0000 | [diff] [blame] | 43 | #include "XMLDocument.h" |
darin | 6f2a3ef | 2006-04-05 21:19:57 +0000 | [diff] [blame] | 44 | #include "markup.h" |
abarth@webkit.org | 26dfd17 | 2010-06-22 23:43:59 +0000 | [diff] [blame] | 45 | |
weinig | f18aae3 | 2006-08-03 21:55:57 +0000 | [diff] [blame] | 46 | #include <wtf/Assertions.h> |
weinig | f18aae3 | 2006-08-03 21:55:57 +0000 | [diff] [blame] | 47 | #include <wtf/Vector.h> |
kdecker | e6b1607 | 2007-09-27 23:18:24 +0000 | [diff] [blame] | 48 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 49 | namespace WebCore { |
| 50 | |
ap | 276970c | 2007-10-29 17:02:51 +0000 | [diff] [blame] | 51 | static inline void transformTextStringToXHTMLDocumentString(String& text) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 52 | { |
| 53 | // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text. |
commit-queue@webkit.org | bf6f7fa | 2012-11-07 09:43:05 +0000 | [diff] [blame] | 54 | text.replaceWithLiteral('&', "&"); |
| 55 | text.replaceWithLiteral('<', "<"); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 56 | text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 57 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" |
| 58 | "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" |
| 59 | "<head><title/></head>\n" |
| 60 | "<body>\n" |
| 61 | "<pre>" + text + "</pre>\n" |
| 62 | "</body>\n" |
| 63 | "</html>\n"; |
| 64 | } |
| 65 | |
ap@apple.com | 1fe9089 | 2010-11-10 21:12:39 +0000 | [diff] [blame] | 66 | XSLTProcessor::~XSLTProcessor() |
| 67 | { |
| 68 | // Stylesheet shouldn't outlive its root node. |
| 69 | ASSERT(!m_stylesheetRootNode || !m_stylesheet || m_stylesheet->hasOneRef()); |
| 70 | } |
| 71 | |
akling@apple.com | f3266b9 | 2015-08-09 08:11:32 +0000 | [diff] [blame] | 72 | Ref<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString, |
ap | 276970c | 2007-10-29 17:02:51 +0000 | [diff] [blame] | 73 | const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, Frame* frame) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 74 | { |
akling@apple.com | 55cde01 | 2013-10-05 00:35:12 +0000 | [diff] [blame] | 75 | Ref<Document> ownerDocument(sourceNode->document()); |
| 76 | bool sourceIsDocument = (sourceNode == &ownerDocument.get()); |
darin | b3547a3 | 2006-09-06 04:40:44 +0000 | [diff] [blame] | 77 | String documentSource = sourceString; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 78 | |
| 79 | RefPtr<Document> result; |
mjs | 22f8a1a | 2007-04-29 07:33:46 +0000 | [diff] [blame] | 80 | if (sourceMIMEType == "text/plain") { |
cdumez@apple.com | d7e206f | 2016-01-25 01:11:35 +0000 | [diff] [blame] | 81 | result = XMLDocument::createXHTML(frame, sourceIsDocument ? ownerDocument->url() : URL()); |
mjs | 22f8a1a | 2007-04-29 07:33:46 +0000 | [diff] [blame] | 82 | transformTextStringToXHTMLDocumentString(documentSource); |
kmccullough@apple.com | 83edbdb | 2008-02-01 01:53:55 +0000 | [diff] [blame] | 83 | } else |
andersca@apple.com | 003f466 | 2014-02-17 23:53:29 +0000 | [diff] [blame] | 84 | result = DOMImplementation::createDocument(sourceMIMEType, frame, sourceIsDocument ? ownerDocument->url() : URL()); |
hausmann@webkit.org | 34a3f96 | 2009-09-28 14:36:24 +0000 | [diff] [blame] | 85 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 86 | // Before parsing, we need to save & detach the old document and get the new document |
| 87 | // in place. We have to do this only if we're rendering the result document. |
mjs | 22f8a1a | 2007-04-29 07:33:46 +0000 | [diff] [blame] | 88 | if (frame) { |
| 89 | if (FrameView* view = frame->view()) |
| 90 | view->clear(); |
commit-queue@webkit.org | 20bc083 | 2011-10-07 23:00:04 +0000 | [diff] [blame] | 91 | |
| 92 | if (Document* oldDocument = frame->document()) { |
| 93 | result->setTransformSourceDocument(oldDocument); |
abarth@webkit.org | 23ea90a | 2012-08-14 19:47:59 +0000 | [diff] [blame] | 94 | result->takeDOMWindowFrom(oldDocument); |
andersca@apple.com | a57d555 | 2014-12-22 23:42:30 +0000 | [diff] [blame] | 95 | result->setSecurityOriginPolicy(oldDocument->securityOriginPolicy()); |
commit-queue@webkit.org | 20bc083 | 2011-10-07 23:00:04 +0000 | [diff] [blame] | 96 | result->setCookieURL(oldDocument->cookieURL()); |
tsepez@chromium.org | 6181ccb | 2011-12-22 10:39:40 +0000 | [diff] [blame] | 97 | result->setFirstPartyForCookies(oldDocument->firstPartyForCookies()); |
tsepez@chromium.org | 3656825 | 2011-12-23 10:40:25 +0000 | [diff] [blame] | 98 | result->contentSecurityPolicy()->copyStateFrom(oldDocument->contentSecurityPolicy()); |
commit-queue@webkit.org | 20bc083 | 2011-10-07 23:00:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
akling@apple.com | 6fbca0f | 2014-12-17 19:39:16 +0000 | [diff] [blame] | 101 | frame->setDocument(result.copyRef()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 102 | } |
hausmann@webkit.org | 34a3f96 | 2009-09-28 14:36:24 +0000 | [diff] [blame] | 103 | |
darin@apple.com | 642f500 | 2008-06-07 22:51:37 +0000 | [diff] [blame] | 104 | RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create(sourceMIMEType); |
darin | 5b2cbe1 | 2006-10-30 00:57:20 +0000 | [diff] [blame] | 105 | decoder->setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : TextEncoding(sourceEncoding), TextResourceDecoder::EncodingFromXMLHeader); |
gyuyoung.kim@webkit.org | 2db3c5d | 2016-01-25 06:13:09 +0000 | [diff] [blame] | 106 | result->setDecoder(WTFMove(decoder)); |
hausmann@webkit.org | 34a3f96 | 2009-09-28 14:36:24 +0000 | [diff] [blame] | 107 | |
paroga@webkit.org | db65784 | 2011-01-12 01:26:04 +0000 | [diff] [blame] | 108 | result->setContent(documentSource); |
ggaren | d9f8cfd | 2007-07-10 16:25:40 +0000 | [diff] [blame] | 109 | |
akling@apple.com | f3266b9 | 2015-08-09 08:11:32 +0000 | [diff] [blame] | 110 | return result.releaseNonNull(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 111 | } |
| 112 | |
gyuyoung.kim@webkit.org | 9559e5d | 2015-09-15 06:00:54 +0000 | [diff] [blame] | 113 | RefPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 114 | { |
commit-queue@webkit.org | 5719331 | 2012-09-06 09:17:10 +0000 | [diff] [blame] | 115 | if (!sourceNode) |
gyuyoung.kim@webkit.org | 9559e5d | 2015-09-15 06:00:54 +0000 | [diff] [blame] | 116 | return nullptr; |
commit-queue@webkit.org | 5719331 | 2012-09-06 09:17:10 +0000 | [diff] [blame] | 117 | |
ap | 276970c | 2007-10-29 17:02:51 +0000 | [diff] [blame] | 118 | String resultMIMEType; |
| 119 | String resultString; |
| 120 | String resultEncoding; |
weinig@apple.com | 65ea5f3 | 2013-10-06 19:18:54 +0000 | [diff] [blame] | 121 | if (!transformToString(*sourceNode, resultMIMEType, resultString, resultEncoding)) |
gyuyoung.kim@webkit.org | 9559e5d | 2015-09-15 06:00:54 +0000 | [diff] [blame] | 122 | return nullptr; |
mjs | 22f8a1a | 2007-04-29 07:33:46 +0000 | [diff] [blame] | 123 | return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 124 | } |
| 125 | |
gyuyoung.kim@webkit.org | 9559e5d | 2015-09-15 06:00:54 +0000 | [diff] [blame] | 126 | RefPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 127 | { |
commit-queue@webkit.org | 5719331 | 2012-09-06 09:17:10 +0000 | [diff] [blame] | 128 | if (!sourceNode || !outputDoc) |
gyuyoung.kim@webkit.org | 9559e5d | 2015-09-15 06:00:54 +0000 | [diff] [blame] | 129 | return nullptr; |
commit-queue@webkit.org | 5719331 | 2012-09-06 09:17:10 +0000 | [diff] [blame] | 130 | |
ap | 276970c | 2007-10-29 17:02:51 +0000 | [diff] [blame] | 131 | String resultMIMEType; |
| 132 | String resultString; |
| 133 | String resultEncoding; |
ap | fef4798 | 2006-11-28 05:33:18 +0000 | [diff] [blame] | 134 | |
| 135 | // If the output document is HTML, default to HTML method. |
| 136 | if (outputDoc->isHTMLDocument()) |
| 137 | resultMIMEType = "text/html"; |
hausmann@webkit.org | 34a3f96 | 2009-09-28 14:36:24 +0000 | [diff] [blame] | 138 | |
weinig@apple.com | 65ea5f3 | 2013-10-06 19:18:54 +0000 | [diff] [blame] | 139 | if (!transformToString(*sourceNode, resultMIMEType, resultString, resultEncoding)) |
gyuyoung.kim@webkit.org | 9559e5d | 2015-09-15 06:00:54 +0000 | [diff] [blame] | 140 | return nullptr; |
rniwa@webkit.org | 3976f1b | 2012-05-24 21:15:17 +0000 | [diff] [blame] | 141 | return createFragmentForTransformToFragment(resultString, resultMIMEType, outputDoc); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 142 | } |
| 143 | |
darin@apple.com | 98a7ac6 | 2009-01-05 17:26:53 +0000 | [diff] [blame] | 144 | void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 145 | { |
| 146 | // FIXME: namespace support? |
| 147 | // should make a QualifiedName here but we'd have to expose the impl |
| 148 | m_parameters.set(localName, value); |
| 149 | } |
| 150 | |
darin@apple.com | 98a7ac6 | 2009-01-05 17:26:53 +0000 | [diff] [blame] | 151 | String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 152 | { |
| 153 | // FIXME: namespace support? |
| 154 | // should make a QualifiedName here but we'd have to expose the impl |
| 155 | return m_parameters.get(localName); |
| 156 | } |
| 157 | |
darin@apple.com | 98a7ac6 | 2009-01-05 17:26:53 +0000 | [diff] [blame] | 158 | void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 159 | { |
| 160 | // FIXME: namespace support? |
| 161 | m_parameters.remove(localName); |
| 162 | } |
| 163 | |
hausmann@webkit.org | 34a3f96 | 2009-09-28 14:36:24 +0000 | [diff] [blame] | 164 | void XSLTProcessor::reset() |
| 165 | { |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 166 | m_stylesheet = nullptr; |
| 167 | m_stylesheetRootNode = nullptr; |
hausmann@webkit.org | 34a3f96 | 2009-09-28 14:36:24 +0000 | [diff] [blame] | 168 | m_parameters.clear(); |
| 169 | } |
| 170 | |
weinig | f18aae3 | 2006-08-03 21:55:57 +0000 | [diff] [blame] | 171 | } // namespace WebCore |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 172 | |
mjs | d2948ef | 2007-02-26 19:29:04 +0000 | [diff] [blame] | 173 | #endif // ENABLE(XSLT) |