Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / xml / XSLStyleSheetLibxslt.cpp
1 /*
2  * This file is part of the XSL implementation.
3  *
4  * Copyright (C) 2004, 2005, 2006, 2008, 2012 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include "core/xml/XSLStyleSheet.h"
24
25 #include "core/dom/Document.h"
26 #include "core/dom/Node.h"
27 #include "core/dom/TransformSource.h"
28 #include "core/frame/FrameHost.h"
29 #include "core/frame/LocalFrame.h"
30 #include "core/xml/XSLImportRule.h"
31 #include "core/xml/XSLTProcessor.h"
32 #include "core/xml/parser/XMLDocumentParserScope.h"
33 #include "core/xml/parser/XMLParserInput.h"
34 #include "wtf/text/CString.h"
35
36 #include <libxml/uri.h>
37 #include <libxslt/xsltutils.h>
38
39 namespace WebCore {
40
41 XSLStyleSheet::XSLStyleSheet(XSLImportRule* parentRule, const String& originalURL, const KURL& finalURL)
42     : m_ownerNode(nullptr)
43     , m_originalURL(originalURL)
44     , m_finalURL(finalURL)
45     , m_isDisabled(false)
46     , m_embedded(false)
47     , m_processed(false) // Child sheets get marked as processed when the libxslt engine has finally seen them.
48     , m_stylesheetDoc(0)
49     , m_stylesheetDocTaken(false)
50     , m_compilationFailed(false)
51     , m_parentStyleSheet(parentRule ? parentRule->parentStyleSheet() : 0)
52 {
53 }
54
55 XSLStyleSheet::XSLStyleSheet(Node* parentNode, const String& originalURL, const KURL& finalURL,  bool embedded)
56     : m_ownerNode(parentNode)
57     , m_originalURL(originalURL)
58     , m_finalURL(finalURL)
59     , m_isDisabled(false)
60     , m_embedded(embedded)
61     , m_processed(true) // The root sheet starts off processed.
62     , m_stylesheetDoc(0)
63     , m_stylesheetDocTaken(false)
64     , m_compilationFailed(false)
65     , m_parentStyleSheet(nullptr)
66 {
67 }
68
69 XSLStyleSheet::~XSLStyleSheet()
70 {
71     if (!m_stylesheetDocTaken)
72         xmlFreeDoc(m_stylesheetDoc);
73 #if !ENABLE(OILPAN)
74     for (unsigned i = 0; i < m_children.size(); ++i) {
75         ASSERT(m_children.at(i)->parentStyleSheet() == this);
76         m_children.at(i)->setParentStyleSheet(0);
77     }
78 #endif
79 }
80
81 bool XSLStyleSheet::isLoading() const
82 {
83     for (unsigned i = 0; i < m_children.size(); ++i) {
84         if (m_children.at(i)->isLoading())
85             return true;
86     }
87     return false;
88 }
89
90 void XSLStyleSheet::checkLoaded()
91 {
92     if (isLoading())
93         return;
94     if (XSLStyleSheet* styleSheet = parentStyleSheet())
95         styleSheet->checkLoaded();
96     if (ownerNode())
97         ownerNode()->sheetLoaded();
98 }
99
100 xmlDocPtr XSLStyleSheet::document()
101 {
102     if (m_embedded && ownerDocument() && ownerDocument()->transformSource())
103         return (xmlDocPtr)ownerDocument()->transformSource()->platformSource();
104     return m_stylesheetDoc;
105 }
106
107 void XSLStyleSheet::clearDocuments()
108 {
109     m_stylesheetDoc = 0;
110     for (unsigned i = 0; i < m_children.size(); ++i) {
111         XSLImportRule* import = m_children.at(i).get();
112         if (import->styleSheet())
113             import->styleSheet()->clearDocuments();
114     }
115 }
116
117 ResourceFetcher* XSLStyleSheet::fetcher()
118 {
119     Document* document = ownerDocument();
120     if (!document)
121         return 0;
122     return document->fetcher();
123 }
124
125 bool XSLStyleSheet::parseString(const String& source)
126 {
127     // Parse in a single chunk into an xmlDocPtr
128     if (!m_stylesheetDocTaken)
129         xmlFreeDoc(m_stylesheetDoc);
130     m_stylesheetDocTaken = false;
131
132     FrameConsole* console = 0;
133     LocalFrame* frame = ownerDocument()->frame();
134     if (frame)
135         console = &frame->console();
136
137     XMLDocumentParserScope scope(fetcher(), XSLTProcessor::genericErrorFunc, XSLTProcessor::parseErrorFunc, console);
138     XMLParserInput input(source);
139
140     xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(input.data(), input.size());
141     if (!ctxt)
142         return 0;
143
144     if (m_parentStyleSheet) {
145         // The XSL transform may leave the newly-transformed document
146         // with references to the symbol dictionaries of the style sheet
147         // and any of its children. XML document disposal can corrupt memory
148         // if a document uses more than one symbol dictionary, so we
149         // ensure that all child stylesheets use the same dictionaries as their
150         // parents.
151         xmlDictFree(ctxt->dict);
152         ctxt->dict = m_parentStyleSheet->m_stylesheetDoc->dict;
153         xmlDictReference(ctxt->dict);
154     }
155
156     m_stylesheetDoc = xmlCtxtReadMemory(ctxt, input.data(), input.size(),
157         finalURL().string().utf8().data(), input.encoding(),
158         XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_NOWARNING | XML_PARSE_NOCDATA);
159
160     xmlFreeParserCtxt(ctxt);
161     loadChildSheets();
162     return m_stylesheetDoc;
163 }
164
165 void XSLStyleSheet::loadChildSheets()
166 {
167     if (!document())
168         return;
169
170     xmlNodePtr stylesheetRoot = document()->children;
171
172     // Top level children may include other things such as DTD nodes, we ignore those.
173     while (stylesheetRoot && stylesheetRoot->type != XML_ELEMENT_NODE)
174         stylesheetRoot = stylesheetRoot->next;
175
176     if (m_embedded) {
177         // We have to locate (by ID) the appropriate embedded stylesheet element, so that we can walk the
178         // import/include list.
179         xmlAttrPtr idNode = xmlGetID(document(), (const xmlChar*)(finalURL().string().utf8().data()));
180         if (!idNode)
181             return;
182         stylesheetRoot = idNode->parent;
183     } else {
184         // FIXME: Need to handle an external URI with a # in it.  This is a pretty minor edge case, so we'll deal
185         // with it later.
186     }
187
188     if (stylesheetRoot) {
189         // Walk the children of the root element and look for import/include elements.
190         // Imports must occur first.
191         xmlNodePtr curr = stylesheetRoot->children;
192         while (curr) {
193             if (curr->type != XML_ELEMENT_NODE) {
194                 curr = curr->next;
195                 continue;
196             }
197             if (IS_XSLT_ELEM(curr) && IS_XSLT_NAME(curr, "import")) {
198                 xmlChar* uriRef = xsltGetNsProp(curr, (const xmlChar*)"href", XSLT_NAMESPACE);
199                 loadChildSheet(String::fromUTF8((const char*)uriRef));
200                 xmlFree(uriRef);
201             } else
202                 break;
203             curr = curr->next;
204         }
205
206         // Now handle includes.
207         while (curr) {
208             if (curr->type == XML_ELEMENT_NODE && IS_XSLT_ELEM(curr) && IS_XSLT_NAME(curr, "include")) {
209                 xmlChar* uriRef = xsltGetNsProp(curr, (const xmlChar*)"href", XSLT_NAMESPACE);
210                 loadChildSheet(String::fromUTF8((const char*)uriRef));
211                 xmlFree(uriRef);
212             }
213             curr = curr->next;
214         }
215     }
216 }
217
218 void XSLStyleSheet::loadChildSheet(const String& href)
219 {
220     OwnPtrWillBeRawPtr<XSLImportRule> childRule = XSLImportRule::create(this, href);
221     XSLImportRule* c = childRule.get();
222     m_children.append(childRule.release());
223     c->loadSheet();
224 }
225
226 xsltStylesheetPtr XSLStyleSheet::compileStyleSheet()
227 {
228     // FIXME: Hook up error reporting for the stylesheet compilation process.
229     if (m_embedded)
230         return xsltLoadStylesheetPI(document());
231
232     // Certain libxslt versions are corrupting the xmlDoc on compilation failures -
233     // hence attempting to recompile after a failure is unsafe.
234     if (m_compilationFailed)
235         return 0;
236
237     // xsltParseStylesheetDoc makes the document part of the stylesheet
238     // so we have to release our pointer to it.
239     ASSERT(!m_stylesheetDocTaken);
240     xsltStylesheetPtr result = xsltParseStylesheetDoc(m_stylesheetDoc);
241     if (result)
242         m_stylesheetDocTaken = true;
243     else
244         m_compilationFailed = true;
245     return result;
246 }
247
248 void XSLStyleSheet::setParentStyleSheet(XSLStyleSheet* parent)
249 {
250     m_parentStyleSheet = parent;
251 }
252
253 Document* XSLStyleSheet::ownerDocument()
254 {
255     for (XSLStyleSheet* styleSheet = this; styleSheet; styleSheet = styleSheet->parentStyleSheet()) {
256         Node* node = styleSheet->ownerNode();
257         if (node)
258             return &node->document();
259     }
260     return 0;
261 }
262
263 xmlDocPtr XSLStyleSheet::locateStylesheetSubResource(xmlDocPtr parentDoc, const xmlChar* uri)
264 {
265     bool matchedParent = (parentDoc == document());
266     for (unsigned i = 0; i < m_children.size(); ++i) {
267         XSLImportRule* import = m_children.at(i).get();
268         XSLStyleSheet* child = import->styleSheet();
269         if (!child)
270             continue;
271         if (matchedParent) {
272             if (child->processed())
273                 continue; // libxslt has been given this sheet already.
274
275             // Check the URI of the child stylesheet against the doc URI.
276             // In order to ensure that libxml canonicalized both URLs, we get the original href
277             // string from the import rule and canonicalize it using libxml before comparing it
278             // with the URI argument.
279             CString importHref = import->href().utf8();
280             xmlChar* base = xmlNodeGetBase(parentDoc, (xmlNodePtr)parentDoc);
281             xmlChar* childURI = xmlBuildURI((const xmlChar*)importHref.data(), base);
282             bool equalURIs = xmlStrEqual(uri, childURI);
283             xmlFree(base);
284             xmlFree(childURI);
285             if (equalURIs) {
286                 child->markAsProcessed();
287                 return child->document();
288             }
289             continue;
290         }
291         xmlDocPtr result = import->styleSheet()->locateStylesheetSubResource(parentDoc, uri);
292         if (result)
293             return result;
294     }
295
296     return 0;
297 }
298
299 void XSLStyleSheet::markAsProcessed()
300 {
301     ASSERT(!m_processed);
302     ASSERT(!m_stylesheetDocTaken);
303     m_processed = true;
304     m_stylesheetDocTaken = true;
305 }
306
307 void XSLStyleSheet::trace(Visitor* visitor)
308 {
309     visitor->trace(m_ownerNode);
310     visitor->trace(m_children);
311     visitor->trace(m_parentStyleSheet);
312     StyleSheet::trace(visitor);
313 }
314
315 } // namespace WebCore