Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / xml / XSLTProcessorLibxslt.cpp
index 197b223..a2ee294 100644 (file)
 #include "config.h"
 #include "core/xml/XSLTProcessor.h"
 
-#include <libxslt/imports.h>
-#include <libxslt/security.h>
-#include <libxslt/variables.h>
-#include <libxslt/xsltutils.h>
-#include "FetchInitiatorTypeNames.h"
+#include "core/FetchInitiatorTypeNames.h"
 #include "core/dom/Document.h"
 #include "core/dom/TransformSource.h"
 #include "core/editing/markup.h"
 #include "core/fetch/Resource.h"
 #include "core/fetch/ResourceFetcher.h"
+#include "core/frame/FrameConsole.h"
 #include "core/frame/FrameHost.h"
 #include "core/frame/LocalFrame.h"
-#include "core/frame/PageConsole.h"
+#include "core/inspector/ConsoleMessage.h"
 #include "core/xml/XSLStyleSheet.h"
 #include "core/xml/XSLTExtensions.h"
 #include "core/xml/XSLTUnicodeSort.h"
 #include "platform/network/ResourceResponse.h"
 #include "platform/weborigin/SecurityOrigin.h"
 #include "wtf/Assertions.h"
-#include "wtf/Vector.h"
 #include "wtf/text/CString.h"
 #include "wtf/text/StringBuffer.h"
 #include "wtf/unicode/UTF8.h"
+#include <libxslt/imports.h>
+#include <libxslt/security.h>
+#include <libxslt/variables.h>
+#include <libxslt/xsltutils.h>
 
-namespace WebCore {
+namespace blink {
 
 void XSLTProcessor::genericErrorFunc(void*, const char*, ...)
 {
@@ -60,7 +60,7 @@ void XSLTProcessor::genericErrorFunc(void*, const char*, ...)
 
 void XSLTProcessor::parseErrorFunc(void* userData, xmlError* error)
 {
-    PageConsole* console = static_cast<PageConsole*>(userData);
+    FrameConsole* console = static_cast<FrameConsole*>(userData);
     if (!console)
         return;
 
@@ -79,17 +79,15 @@ void XSLTProcessor::parseErrorFunc(void* userData, xmlError* error)
         break;
     }
 
-    console->addMessage(XMLMessageSource, level, error->message, error->file, error->line);
+    console->addMessage(ConsoleMessage::create(XMLMessageSource, level, error->message, error->file, error->line));
 }
 
 // FIXME: There seems to be no way to control the ctxt pointer for loading here, thus we have globals.
 static XSLTProcessor* globalProcessor = 0;
 static ResourceFetcher* globalResourceFetcher = 0;
-static xmlDocPtr docLoaderFunc(const xmlChar* uri,
-                               xmlDictPtr,
-                               int options,
-                               void* ctxt,
-                               xsltLoadType type)
+
+static xmlDocPtr docLoaderFunc(
+    const xmlChar* uri, xmlDictPtr, int options, void* ctxt, xsltLoadType type)
 {
     if (!globalProcessor)
         return 0;
@@ -108,10 +106,10 @@ static xmlDocPtr docLoaderFunc(const xmlChar* uri,
         if (!resource || !globalProcessor)
             return 0;
 
-        PageConsole* console = 0;
+        FrameConsole* console = 0;
         LocalFrame* frame = globalProcessor->xslStylesheet()->ownerDocument()->frame();
-        if (frame && frame->host())
-            console = &frame->host()->console();
+        if (frame)
+            console = &frame->console();
         xmlSetStructuredErrorFunc(console, XSLTProcessor::parseErrorFunc);
         xmlSetGenericErrorFunc(console, XSLTProcessor::genericErrorFunc);
 
@@ -179,7 +177,8 @@ static bool saveResultToString(xmlDocPtr resultDoc, xsltStylesheetPtr sheet, Str
     if (retval < 0)
         return false;
 
-    // Workaround for <http://bugzilla.gnome.org/show_bug.cgi?id=495668>: libxslt appends an extra line feed to the result.
+    // Workaround for <http://bugzilla.gnome.org/show_bug.cgi?id=495668>:
+    // libxslt appends an extra line feed to the result.
     if (resultBuilder.length() > 0 && resultBuilder[resultBuilder.length() - 1] == '\n')
         resultBuilder.resize(resultBuilder.length() - 1);
 
@@ -193,7 +192,7 @@ static const char** xsltParamArrayFromParameterMap(XSLTProcessor::ParameterMap&
     if (parameters.isEmpty())
         return 0;
 
-    const char** parameterArray = (const char**)fastMalloc(((parameters.size() * 2) + 1) * sizeof(char*));
+    const char** parameterArray = static_cast<const char**>(fastMalloc(((parameters.size() * 2) + 1) * sizeof(char*)));
 
     XSLTProcessor::ParameterMap::iterator end = parameters.end();
     unsigned index = 0;
@@ -213,21 +212,25 @@ static void freeXsltParamArray(const char** params)
         return;
 
     while (*temp) {
-        fastFree((void*)*(temp++));
-        fastFree((void*)*(temp++));
+        fastFree(const_cast<char*>(*(temp++)));
+        fastFree(const_cast<char*>(*(temp++)));
     }
     fastFree(params);
 }
 
-static xsltStylesheetPtr xsltStylesheetPointer(RefPtrWillBeMember<XSLStyleSheet>& cachedStylesheet, Node* stylesheetRootNode)
+static xsltStylesheetPtr xsltStylesheetPointer(Document* document, RefPtrWillBeMember<XSLStyleSheet>& cachedStylesheet, Node* stylesheetRootNode)
 {
     if (!cachedStylesheet && stylesheetRootNode) {
-        cachedStylesheet = XSLStyleSheet::createForXSLTProcessor(stylesheetRootNode->parentNode() ? stylesheetRootNode->parentNode() : stylesheetRootNode,
+        // When using importStylesheet, we will use the given document as the imported stylesheet's owner.
+        cachedStylesheet = XSLStyleSheet::createForXSLTProcessor(
+            stylesheetRootNode->parentNode() ? &stylesheetRootNode->parentNode()->document() : document,
+            stylesheetRootNode,
             stylesheetRootNode->document().url().string(),
             stylesheetRootNode->document().url()); // FIXME: Should we use baseURL here?
 
-        // According to Mozilla documentation, the node must be a Document node, an xsl:stylesheet or xsl:transform element.
-        // But we just use text content regardless of node type.
+        // According to Mozilla documentation, the node must be a Document node,
+        // an xsl:stylesheet or xsl:transform element. But we just use text
+        // content regardless of node type.
         cachedStylesheet->parseString(createMarkup(stylesheetRootNode));
     }
 
@@ -239,7 +242,7 @@ static xsltStylesheetPtr xsltStylesheetPointer(RefPtrWillBeMember<XSLStyleSheet>
 
 static inline xmlDocPtr xmlDocPtrFromNode(Node* sourceNode, bool& shouldDelete)
 {
-    RefPtr<Document> ownerDocument(sourceNode->document());
+    RefPtrWillBeRawPtr<Document> ownerDocument(sourceNode->document());
     bool sourceIsDocument = (sourceNode == ownerDocument.get());
 
     xmlDocPtr sourceDoc = 0;
@@ -274,10 +277,10 @@ static inline String resultMIMEType(xmlDocPtr resultDoc, xsltStylesheetPtr sheet
 
 bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String& resultString, String& resultEncoding)
 {
-    RefPtr<Document> ownerDocument(sourceNode->document());
+    RefPtrWillBeRawPtr<Document> ownerDocument(sourceNode->document());
 
     setXSLTLoadCallBack(docLoaderFunc, this, ownerDocument->fetcher());
-    xsltStylesheetPtr sheet = xsltStylesheetPointer(m_stylesheet, m_stylesheetRootNode.get());
+    xsltStylesheetPtr sheet = xsltStylesheetPointer(m_document.get(), m_stylesheet, m_stylesheetRootNode.get());
     if (!sheet) {
         setXSLTLoadCallBack(0, 0, 0);
         m_stylesheet = nullptr;
@@ -292,8 +295,9 @@ bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String
     bool success = false;
     bool shouldFreeSourceDoc = false;
     if (xmlDocPtr sourceDoc = xmlDocPtrFromNode(sourceNode, shouldFreeSourceDoc)) {
-        // The XML declaration would prevent parsing the result as a fragment, and it's not needed even for documents,
-        // as the result of this function is always immediately parsed.
+        // The XML declaration would prevent parsing the result as a fragment,
+        // and it's not needed even for documents, as the result of this
+        // function is always immediately parsed.
         sheet->omitXmlDeclaration = true;
 
         xsltTransformContextPtr transformContext = xsltNewTransformContext(sheet, sourceDoc);
@@ -310,13 +314,15 @@ bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String
         if (0 != xsltSetCtxtSecurityPrefs(securityPrefs, transformContext))
             CRASH();
 
-        // <http://bugs.webkit.org/show_bug.cgi?id=16077>: XSLT processor <xsl:sort> algorithm only compares by code point.
+        // <http://bugs.webkit.org/show_bug.cgi?id=16077>: XSLT processor
+        // <xsl:sort> algorithm only compares by code point.
         xsltSetCtxtSortFunc(transformContext, xsltUnicodeSortFunction);
 
         // This is a workaround for a bug in libxslt.
-        // The bug has been fixed in version 1.1.13, so once we ship that this can be removed.
+        // The bug has been fixed in version 1.1.13, so once we ship that this
+        // can be removed.
         if (!transformContext->globalVars)
-           transformContext->globalVars = xmlHashCreate(20);
+            transformContext->globalVars = xmlHashCreate(20);
 
         const char** params = xsltParamArrayFromParameterMap(m_parameters);
         xsltQuoteUserParams(transformContext, params);
@@ -329,7 +335,8 @@ bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String
         if (shouldFreeSourceDoc)
             xmlFreeDoc(sourceDoc);
 
-        if ((success = saveResultToString(resultDoc, sheet, resultString))) {
+        success = saveResultToString(resultDoc, sheet, resultString);
+        if (success) {
             mimeType = resultMIMEType(resultDoc, sheet);
             resultEncoding = (char*)resultDoc->encoding;
         }
@@ -344,4 +351,4 @@ bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String
     return success;
 }
 
-} // namespace WebCore
+} // namespace blink