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