Upstream version 9.38.198.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 #include <libxml/uri.h>
36 #include <libxslt/xsltutils.h>
37
38 namespace blink {
39
40 XSLStyleSheet::XSLStyleSheet(XSLImportRule* parentRule, const String& originalURL, const KURL& finalURL)
41     : m_ownerNode(nullptr)
42     , m_originalURL(originalURL)
43     , m_finalURL(finalURL)
44     , m_isDisabled(false)
45     , m_embedded(false)
46     // Child sheets get marked as processed when the libxslt engine has finally
47     // seen them.
48     , m_processed(false)
49     , m_stylesheetDoc(0)
50     , m_stylesheetDocTaken(false)
51     , m_compilationFailed(false)
52     , m_parentStyleSheet(parentRule ? parentRule->parentStyleSheet() : 0)
53 {
54 }
55
56 XSLStyleSheet::XSLStyleSheet(Node* parentNode, const String& originalURL, const KURL& finalURL,  bool embedded)
57     : m_ownerNode(parentNode)
58     , m_originalURL(originalURL)
59     , m_finalURL(finalURL)
60     , m_isDisabled(false)
61     , m_embedded(embedded)
62     , m_processed(true) // The root sheet starts off processed.
63     , m_stylesheetDoc(0)
64     , m_stylesheetDocTaken(false)
65     , m_compilationFailed(false)
66     , m_parentStyleSheet(nullptr)
67 {
68 }
69
70 XSLStyleSheet::~XSLStyleSheet()
71 {
72     if (!m_stylesheetDocTaken)
73         xmlFreeDoc(m_stylesheetDoc);
74 #if !ENABLE(OILPAN)
75     for (unsigned i = 0; i < m_children.size(); ++i) {
76         ASSERT(m_children.at(i)->parentStyleSheet() == this);
77         m_children.at(i)->setParentStyleSheet(0);
78     }
79 #endif
80 }
81
82 bool XSLStyleSheet::isLoading() const
83 {
84     for (unsigned i = 0; i < m_children.size(); ++i) {
85         if (m_children.at(i)->isLoading())
86             return true;
87     }
88     return false;
89 }
90
91 void XSLStyleSheet::checkLoaded()
92 {
93     if (isLoading())
94         return;
95     if (XSLStyleSheet* styleSheet = parentStyleSheet())
96         styleSheet->checkLoaded();
97     if (ownerNode())
98         ownerNode()->sheetLoaded();
99 }
100
101 xmlDocPtr XSLStyleSheet::document()
102 {
103     if (m_embedded && ownerDocument() && ownerDocument()->transformSource())
104         return (xmlDocPtr)ownerDocument()->transformSource()->platformSource();
105     return m_stylesheetDoc;
106 }
107
108 void XSLStyleSheet::clearDocuments()
109 {
110     m_stylesheetDoc = 0;
111     for (unsigned i = 0; i < m_children.size(); ++i) {
112         XSLImportRule* import = m_children.at(i).get();
113         if (import->styleSheet())
114             import->styleSheet()->clearDocuments();
115     }
116 }
117
118 ResourceFetcher* XSLStyleSheet::fetcher()
119 {
120     if (Document* document = ownerDocument())
121         return document->fetcher();
122     return 0;
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     if (LocalFrame* frame = ownerDocument()->frame())
134         console = &frame->console();
135
136     XMLDocumentParserScope scope(fetcher(), XSLTProcessor::genericErrorFunc, XSLTProcessor::parseErrorFunc, console);
137     XMLParserInput input(source);
138
139     xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(input.data(), input.size());
140     if (!ctxt)
141         return 0;
142
143     if (m_parentStyleSheet) {
144         // The XSL transform may leave the newly-transformed document
145         // with references to the symbol dictionaries of the style sheet
146         // and any of its children. XML document disposal can corrupt memory
147         // if a document uses more than one symbol dictionary, so we
148         // ensure that all child stylesheets use the same dictionaries as their
149         // parents.
150         xmlDictFree(ctxt->dict);
151         ctxt->dict = m_parentStyleSheet->m_stylesheetDoc->dict;
152         xmlDictReference(ctxt->dict);
153     }
154
155     m_stylesheetDoc = xmlCtxtReadMemory(ctxt, input.data(), input.size(),
156         finalURL().string().utf8().data(), input.encoding(),
157         XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_NOWARNING | XML_PARSE_NOCDATA);
158
159     xmlFreeParserCtxt(ctxt);
160     loadChildSheets();
161     return m_stylesheetDoc;
162 }
163
164 void XSLStyleSheet::loadChildSheets()
165 {
166     if (!document())
167         return;
168
169     xmlNodePtr stylesheetRoot = document()->children;
170
171     // Top level children may include other things such as DTD nodes, we ignore
172     // 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
178         // element, so that we can walk the 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
185         // pretty minor edge case, so we'll deal with it later.
186     }
187
188     if (stylesheetRoot) {
189         // Walk the children of the root element and look for import/include
190         // elements. 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             }
204             curr = curr->next;
205         }
206
207         // Now handle includes.
208         while (curr) {
209             if (curr->type == XML_ELEMENT_NODE && IS_XSLT_ELEM(curr) && IS_XSLT_NAME(curr, "include")) {
210                 xmlChar* uriRef = xsltGetNsProp(curr, (const xmlChar*)"href", XSLT_NAMESPACE);
211                 loadChildSheet(String::fromUTF8((const char*)uriRef));
212                 xmlFree(uriRef);
213             }
214             curr = curr->next;
215         }
216     }
217 }
218
219 void XSLStyleSheet::loadChildSheet(const String& href)
220 {
221     OwnPtrWillBeRawPtr<XSLImportRule> childRule = XSLImportRule::create(this, href);
222     XSLImportRule* c = childRule.get();
223     m_children.append(childRule.release());
224     c->loadSheet();
225 }
226
227 xsltStylesheetPtr XSLStyleSheet::compileStyleSheet()
228 {
229     // FIXME: Hook up error reporting for the stylesheet compilation process.
230     if (m_embedded)
231         return xsltLoadStylesheetPI(document());
232
233     // Certain libxslt versions are corrupting the xmlDoc on compilation
234     // failures - hence attempting to recompile after a failure is unsafe.
235     if (m_compilationFailed)
236         return 0;
237
238     // xsltParseStylesheetDoc makes the document part of the stylesheet
239     // so we have to release our pointer to it.
240     ASSERT(!m_stylesheetDocTaken);
241     xsltStylesheetPtr result = xsltParseStylesheetDoc(m_stylesheetDoc);
242     if (result)
243         m_stylesheetDocTaken = true;
244     else
245         m_compilationFailed = true;
246     return result;
247 }
248
249 void XSLStyleSheet::setParentStyleSheet(XSLStyleSheet* parent)
250 {
251     m_parentStyleSheet = parent;
252 }
253
254 Document* XSLStyleSheet::ownerDocument()
255 {
256     for (XSLStyleSheet* styleSheet = this; styleSheet; styleSheet = styleSheet->parentStyleSheet()) {
257         Node* node = styleSheet->ownerNode();
258         if (node)
259             return &node->document();
260     }
261     return 0;
262 }
263
264 xmlDocPtr XSLStyleSheet::locateStylesheetSubResource(xmlDocPtr parentDoc, const xmlChar* uri)
265 {
266     bool matchedParent = (parentDoc == document());
267     for (unsigned i = 0; i < m_children.size(); ++i) {
268         XSLImportRule* import = m_children.at(i).get();
269         XSLStyleSheet* child = import->styleSheet();
270         if (!child)
271             continue;
272         if (matchedParent) {
273             if (child->processed())
274                 continue; // libxslt has been given this sheet already.
275
276             // Check the URI of the child stylesheet against the doc URI.
277             // In order to ensure that libxml canonicalized both URLs, we get
278             // the original href string from the import rule and canonicalize it
279             // using libxml before comparing it with the URI argument.
280             CString importHref = import->href().utf8();
281             xmlChar* base = xmlNodeGetBase(parentDoc, (xmlNodePtr)parentDoc);
282             xmlChar* childURI = xmlBuildURI((const xmlChar*)importHref.data(), base);
283             bool equalURIs = xmlStrEqual(uri, childURI);
284             xmlFree(base);
285             xmlFree(childURI);
286             if (equalURIs) {
287                 child->markAsProcessed();
288                 return child->document();
289             }
290             continue;
291         }
292         xmlDocPtr result = import->styleSheet()->locateStylesheetSubResource(parentDoc, uri);
293         if (result)
294             return result;
295     }
296
297     return 0;
298 }
299
300 void XSLStyleSheet::markAsProcessed()
301 {
302     ASSERT(!m_processed);
303     ASSERT(!m_stylesheetDocTaken);
304     m_processed = true;
305     m_stylesheetDocTaken = true;
306 }
307
308 void XSLStyleSheet::trace(Visitor* visitor)
309 {
310     visitor->trace(m_ownerNode);
311     visitor->trace(m_children);
312     visitor->trace(m_parentStyleSheet);
313     StyleSheet::trace(visitor);
314 }
315
316 } // namespace blink