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 "CString.h"
30 #include "Console.h"
31 #include "DOMImplementation.h"
32 #include "DOMWindow.h"
33 #include "DocLoader.h"
34 #include "DocumentFragment.h"
35 #include "Frame.h"
36 #include "FrameLoader.h"
37 #include "FrameView.h"
38 #include "HTMLDocument.h"
39 #include "HTMLTokenizer.h" // for parseHTMLDocumentFragment
40 #include "Page.h"
41 #include "ResourceError.h"
42 #include "ResourceHandle.h"
43 #include "ResourceRequest.h"
44 #include "ResourceResponse.h"
45 #include "Text.h"
46 #include "TextResourceDecoder.h"
47 #include "XMLTokenizer.h"
48 #include "XSLTExtensions.h"
49 #include "XSLTUnicodeSort.h"
50 #include "loader.h"
51 #include "markup.h"
52 #include <libxslt/imports.h>
53 #include <libxslt/variables.h>
54 #include <libxslt/xsltutils.h>
55 #include <wtf/Assertions.h>
56 #include <wtf/Platform.h>
57 #include <wtf/Vector.h>
58
59 #if PLATFORM(MAC)
60 #include "SoftLinking.h"
61
62 SOFT_LINK_LIBRARY(libxslt);
63 SOFT_LINK(libxslt, xsltFreeStylesheet, void, (xsltStylesheetPtr sheet), (sheet))
64 SOFT_LINK(libxslt, xsltFreeTransformContext, void, (xsltTransformContextPtr ctxt), (ctxt))
65 SOFT_LINK(libxslt, xsltNewTransformContext, xsltTransformContextPtr, (xsltStylesheetPtr style, xmlDocPtr doc), (style, doc))
66 SOFT_LINK(libxslt, xsltApplyStylesheetUser, xmlDocPtr, (xsltStylesheetPtr style, xmlDocPtr doc, const char** params, const char* output, FILE* profile, xsltTransformContextPtr userCtxt), (style, doc, params, output, profile, userCtxt))
67 SOFT_LINK(libxslt, xsltQuoteUserParams, int, (xsltTransformContextPtr ctxt, const char** params), (ctxt, params))
68 SOFT_LINK(libxslt, xsltSetCtxtSortFunc, void, (xsltTransformContextPtr ctxt, xsltSortFunc handler), (ctxt, handler))
69 SOFT_LINK(libxslt, xsltSetLoaderFunc, void, (xsltDocLoaderFunc f), (f))
70 SOFT_LINK(libxslt, xsltSaveResultTo, int, (xmlOutputBufferPtr buf, xmlDocPtr result, xsltStylesheetPtr style), (buf, result, style))
71 SOFT_LINK(libxslt, xsltNextImport, xsltStylesheetPtr, (xsltStylesheetPtr style), (style))
72 #endif
73
74 namespace WebCore {
75
genericErrorFunc(void *,const char *,...)76 void XSLTProcessor::genericErrorFunc(void*, const char*, ...)
77 {
78 // It would be nice to do something with this error message.
79 }
80
parseErrorFunc(void * userData,xmlError * error)81 void XSLTProcessor::parseErrorFunc(void* userData, xmlError* error)
82 {
83 Console* console = static_cast<Console*>(userData);
84 if (!console)
85 return;
86
87 MessageLevel level;
88 switch (error->level) {
89 case XML_ERR_NONE:
90 level = TipMessageLevel;
91 break;
92 case XML_ERR_WARNING:
93 level = WarningMessageLevel;
94 break;
95 case XML_ERR_ERROR:
96 case XML_ERR_FATAL:
97 default:
98 level = ErrorMessageLevel;
99 break;
100 }
101
102 console->addMessage(XMLMessageSource, LogMessageType, level, error->message, error->line, error->file);
103 }
104
105 // FIXME: There seems to be no way to control the ctxt pointer for loading here, thus we have globals.
106 static XSLTProcessor* globalProcessor = 0;
107 static DocLoader* globalDocLoader = 0;
docLoaderFunc(const xmlChar * uri,xmlDictPtr,int options,void * ctxt,xsltLoadType type)108 static xmlDocPtr docLoaderFunc(const xmlChar* uri,
109 xmlDictPtr,
110 int options,
111 void* ctxt,
112 xsltLoadType type)
113 {
114 if (!globalProcessor)
115 return 0;
116
117 switch (type) {
118 case XSLT_LOAD_DOCUMENT: {
119 xsltTransformContextPtr context = (xsltTransformContextPtr)ctxt;
120 xmlChar* base = xmlNodeGetBase(context->document->doc, context->node);
121 KURL url(KURL(reinterpret_cast<const char*>(base)), reinterpret_cast<const char*>(uri));
122 xmlFree(base);
123 ResourceError error;
124 ResourceResponse response;
125
126 Vector<char> data;
127
128 bool requestAllowed = globalDocLoader->frame() && globalDocLoader->doc()->securityOrigin()->canRequest(url);
129 if (requestAllowed) {
130 globalDocLoader->frame()->loader()->loadResourceSynchronously(url, AllowStoredCredentials, error, response, data);
131 requestAllowed = globalDocLoader->doc()->securityOrigin()->canRequest(response.url());
132 }
133 if (!requestAllowed) {
134 data.clear();
135 globalDocLoader->printAccessDeniedMessage(url);
136 }
137
138 Console* console = 0;
139 if (Frame* frame = globalProcessor->xslStylesheet()->ownerDocument()->frame())
140 console = frame->domWindow()->console();
141 xmlSetStructuredErrorFunc(console, XSLTProcessor::parseErrorFunc);
142 xmlSetGenericErrorFunc(console, XSLTProcessor::genericErrorFunc);
143
144 // We don't specify an encoding here. Neither Gecko nor WinIE respects
145 // the encoding specified in the HTTP headers.
146 xmlDocPtr doc = xmlReadMemory(data.data(), data.size(), (const char*)uri, 0, options);
147
148 xmlSetStructuredErrorFunc(0, 0);
149 xmlSetGenericErrorFunc(0, 0);
150
151 return doc;
152 }
153 case XSLT_LOAD_STYLESHEET:
154 return globalProcessor->xslStylesheet()->locateStylesheetSubResource(((xsltStylesheetPtr)ctxt)->doc, uri);
155 default:
156 break;
157 }
158
159 return 0;
160 }
161
setXSLTLoadCallBack(xsltDocLoaderFunc func,XSLTProcessor * processor,DocLoader * loader)162 static inline void setXSLTLoadCallBack(xsltDocLoaderFunc func, XSLTProcessor* processor, DocLoader* loader)
163 {
164 xsltSetLoaderFunc(func);
165 globalProcessor = processor;
166 globalDocLoader = loader;
167 }
168
writeToVector(void * context,const char * buffer,int len)169 static int writeToVector(void* context, const char* buffer, int len)
170 {
171 Vector<UChar>& resultOutput = *static_cast<Vector<UChar>*>(context);
172 String decodedChunk = String::fromUTF8(buffer, len);
173 resultOutput.append(decodedChunk.characters(), decodedChunk.length());
174 return len;
175 }
176
saveResultToString(xmlDocPtr resultDoc,xsltStylesheetPtr sheet,String & resultString)177 static bool saveResultToString(xmlDocPtr resultDoc, xsltStylesheetPtr sheet, String& resultString)
178 {
179 xmlOutputBufferPtr outputBuf = xmlAllocOutputBuffer(0);
180 if (!outputBuf)
181 return false;
182
183 Vector<UChar> resultVector;
184 outputBuf->context = &resultVector;
185 outputBuf->writecallback = writeToVector;
186
187 int retval = xsltSaveResultTo(outputBuf, resultDoc, sheet);
188 xmlOutputBufferClose(outputBuf);
189 if (retval < 0)
190 return false;
191
192 // Workaround for <http://bugzilla.gnome.org/show_bug.cgi?id=495668>: libxslt appends an extra line feed to the result.
193 if (resultVector.size() > 0 && resultVector[resultVector.size() - 1] == '\n')
194 resultVector.removeLast();
195
196 resultString = String::adopt(resultVector);
197
198 return true;
199 }
200
transformTextStringToXHTMLDocumentString(String & text)201 static inline void transformTextStringToXHTMLDocumentString(String& text)
202 {
203 // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
204 text.replace('&', "&");
205 text.replace('<', "<");
206 text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
207 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
208 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
209 "<head><title/></head>\n"
210 "<body>\n"
211 "<pre>" + text + "</pre>\n"
212 "</body>\n"
213 "</html>\n";
214 }
215
xsltParamArrayFromParameterMap(XSLTProcessor::ParameterMap & parameters)216 static const char** xsltParamArrayFromParameterMap(XSLTProcessor::ParameterMap& parameters)
217 {
218 if (parameters.isEmpty())
219 return 0;
220
221 const char** parameterArray = (const char**)fastMalloc(((parameters.size() * 2) + 1) * sizeof(char*));
222
223 XSLTProcessor::ParameterMap::iterator end = parameters.end();
224 unsigned index = 0;
225 for (XSLTProcessor::ParameterMap::iterator it = parameters.begin(); it != end; ++it) {
226 parameterArray[index++] = strdup(it->first.utf8().data());
227 parameterArray[index++] = strdup(it->second.utf8().data());
228 }
229 parameterArray[index] = 0;
230
231 return parameterArray;
232 }
233
freeXsltParamArray(const char ** params)234 static void freeXsltParamArray(const char** params)
235 {
236 const char** temp = params;
237 if (!params)
238 return;
239
240 while (*temp) {
241 free((void*)*(temp++)); // strdup returns malloc'd blocks, so we have to use free() here
242 free((void*)*(temp++));
243 }
244 fastFree(params);
245 }
246
247
createDocumentFromSource(const String & sourceString,const String & sourceEncoding,const String & sourceMIMEType,Node * sourceNode,Frame * frame)248 PassRefPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString,
249 const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, Frame* frame)
250 {
251 RefPtr<Document> ownerDocument = sourceNode->document();
252 bool sourceIsDocument = (sourceNode == ownerDocument.get());
253 String documentSource = sourceString;
254
255 RefPtr<Document> result;
256 if (sourceMIMEType == "text/plain") {
257 result = ownerDocument->implementation()->createDocument(frame);
258 transformTextStringToXHTMLDocumentString(documentSource);
259 } else
260 result = ownerDocument->implementation()->createDocument(sourceMIMEType, frame, false);
261
262 // Before parsing, we need to save & detach the old document and get the new document
263 // in place. We have to do this only if we're rendering the result document.
264 if (frame) {
265 if (FrameView* view = frame->view())
266 view->clear();
267 result->setTransformSourceDocument(frame->document());
268 frame->setDocument(result);
269 }
270
271 if (sourceIsDocument)
272 result->setURL(ownerDocument->url());
273 result->open();
274
275 RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create(sourceMIMEType);
276 decoder->setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : TextEncoding(sourceEncoding), TextResourceDecoder::EncodingFromXMLHeader);
277 result->setDecoder(decoder.release());
278
279 result->write(documentSource);
280 result->finishParsing();
281 result->close();
282
283 return result.release();
284 }
285
createFragmentFromSource(const String & sourceString,const String & sourceMIMEType,Document * outputDoc)286 static inline RefPtr<DocumentFragment> createFragmentFromSource(const String& sourceString, const String& sourceMIMEType, Document* outputDoc)
287 {
288 RefPtr<DocumentFragment> fragment = new DocumentFragment(outputDoc);
289
290 if (sourceMIMEType == "text/html")
291 parseHTMLDocumentFragment(sourceString, fragment.get());
292 else if (sourceMIMEType == "text/plain")
293 fragment->addChild(new Text(outputDoc, sourceString));
294 else {
295 bool successfulParse = parseXMLDocumentFragment(sourceString, fragment.get(), outputDoc->documentElement());
296 if (!successfulParse)
297 return 0;
298 }
299
300 // FIXME: Do we need to mess with URLs here?
301
302 return fragment;
303 }
304
xsltStylesheetPointer(RefPtr<XSLStyleSheet> & cachedStylesheet,Node * stylesheetRootNode)305 static xsltStylesheetPtr xsltStylesheetPointer(RefPtr<XSLStyleSheet>& cachedStylesheet, Node* stylesheetRootNode)
306 {
307 if (!cachedStylesheet && stylesheetRootNode) {
308 cachedStylesheet = XSLStyleSheet::create(stylesheetRootNode->parent() ? stylesheetRootNode->parent() : stylesheetRootNode,
309 stylesheetRootNode->document()->url().string());
310 cachedStylesheet->parseString(createMarkup(stylesheetRootNode));
311 }
312
313 if (!cachedStylesheet || !cachedStylesheet->document())
314 return 0;
315
316 return cachedStylesheet->compileStyleSheet();
317 }
318
xmlDocPtrFromNode(Node * sourceNode,bool & shouldDelete)319 static inline xmlDocPtr xmlDocPtrFromNode(Node* sourceNode, bool& shouldDelete)
320 {
321 RefPtr<Document> ownerDocument = sourceNode->document();
322 bool sourceIsDocument = (sourceNode == ownerDocument.get());
323
324 xmlDocPtr sourceDoc = 0;
325 if (sourceIsDocument)
326 sourceDoc = (xmlDocPtr)ownerDocument->transformSource();
327 if (!sourceDoc) {
328 sourceDoc = (xmlDocPtr)xmlDocPtrForString(ownerDocument->docLoader(), createMarkup(sourceNode),
329 sourceIsDocument ? ownerDocument->url().string() : String());
330 shouldDelete = (sourceDoc != 0);
331 }
332 return sourceDoc;
333 }
334
resultMIMEType(xmlDocPtr resultDoc,xsltStylesheetPtr sheet)335 static inline String resultMIMEType(xmlDocPtr resultDoc, xsltStylesheetPtr sheet)
336 {
337 // There are three types of output we need to be able to deal with:
338 // HTML (create an HTML document), XML (create an XML document),
339 // and text (wrap in a <pre> and create an XML document).
340
341 const xmlChar* resultType = 0;
342 XSLT_GET_IMPORT_PTR(resultType, sheet, method);
343 if (resultType == 0 && resultDoc->type == XML_HTML_DOCUMENT_NODE)
344 resultType = (const xmlChar*)"html";
345
346 if (xmlStrEqual(resultType, (const xmlChar*)"html"))
347 return "text/html";
348 else if (xmlStrEqual(resultType, (const xmlChar*)"text"))
349 return "text/plain";
350
351 return "application/xml";
352 }
353
transformToString(Node * sourceNode,String & mimeType,String & resultString,String & resultEncoding)354 bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String& resultString, String& resultEncoding)
355 {
356 RefPtr<Document> ownerDocument = sourceNode->document();
357
358 setXSLTLoadCallBack(docLoaderFunc, this, ownerDocument->docLoader());
359 xsltStylesheetPtr sheet = xsltStylesheetPointer(m_stylesheet, m_stylesheetRootNode.get());
360 if (!sheet) {
361 setXSLTLoadCallBack(0, 0, 0);
362 return false;
363 }
364 m_stylesheet->clearDocuments();
365
366 xmlChar* origMethod = sheet->method;
367 if (!origMethod && mimeType == "text/html")
368 sheet->method = (xmlChar*)"html";
369
370 bool success = false;
371 bool shouldFreeSourceDoc = false;
372 if (xmlDocPtr sourceDoc = xmlDocPtrFromNode(sourceNode, shouldFreeSourceDoc)) {
373 // The XML declaration would prevent parsing the result as a fragment, and it's not needed even for documents,
374 // as the result of this function is always immediately parsed.
375 sheet->omitXmlDeclaration = true;
376
377 xsltTransformContextPtr transformContext = xsltNewTransformContext(sheet, sourceDoc);
378 registerXSLTExtensions(transformContext);
379
380 // <http://bugs.webkit.org/show_bug.cgi?id=16077>: XSLT processor <xsl:sort> algorithm only compares by code point
381 xsltSetCtxtSortFunc(transformContext, xsltUnicodeSortFunction);
382
383 // This is a workaround for a bug in libxslt.
384 // The bug has been fixed in version 1.1.13, so once we ship that this can be removed.
385 if (transformContext->globalVars == NULL)
386 transformContext->globalVars = xmlHashCreate(20);
387
388 const char** params = xsltParamArrayFromParameterMap(m_parameters);
389 xsltQuoteUserParams(transformContext, params);
390 xmlDocPtr resultDoc = xsltApplyStylesheetUser(sheet, sourceDoc, 0, 0, 0, transformContext);
391
392 xsltFreeTransformContext(transformContext);
393 freeXsltParamArray(params);
394
395 if (shouldFreeSourceDoc)
396 xmlFreeDoc(sourceDoc);
397
398 if (success = saveResultToString(resultDoc, sheet, resultString)) {
399 mimeType = resultMIMEType(resultDoc, sheet);
400 resultEncoding = (char*)resultDoc->encoding;
401 }
402 xmlFreeDoc(resultDoc);
403 }
404
405 sheet->method = origMethod;
406 setXSLTLoadCallBack(0, 0, 0);
407 xsltFreeStylesheet(sheet);
408 m_stylesheet = 0;
409
410 return success;
411 }
412
transformToDocument(Node * sourceNode)413 PassRefPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode)
414 {
415 String resultMIMEType;
416 String resultString;
417 String resultEncoding;
418 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
419 return 0;
420 return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);
421 }
422
transformToFragment(Node * sourceNode,Document * outputDoc)423 PassRefPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc)
424 {
425 String resultMIMEType;
426 String resultString;
427 String resultEncoding;
428
429 // If the output document is HTML, default to HTML method.
430 if (outputDoc->isHTMLDocument())
431 resultMIMEType = "text/html";
432
433 if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
434 return 0;
435 return createFragmentFromSource(resultString, resultMIMEType, outputDoc);
436 }
437
setParameter(const String &,const String & localName,const String & value)438 void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value)
439 {
440 // FIXME: namespace support?
441 // should make a QualifiedName here but we'd have to expose the impl
442 m_parameters.set(localName, value);
443 }
444
getParameter(const String &,const String & localName) const445 String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const
446 {
447 // FIXME: namespace support?
448 // should make a QualifiedName here but we'd have to expose the impl
449 return m_parameters.get(localName);
450 }
451
removeParameter(const String &,const String & localName)452 void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName)
453 {
454 // FIXME: namespace support?
455 m_parameters.remove(localName);
456 }
457
458 } // namespace WebCore
459
460 #endif // ENABLE(XSLT)
461