2 * This file is part of the XSL implementation.
4 * Copyright (C) 2004, 2005, 2006, 2008, 2012 Apple Inc. All rights reserved.
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.
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.
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.
23 #include "core/xml/XSLStyleSheet.h"
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"
36 #include <libxml/uri.h>
37 #include <libxslt/xsltutils.h>
41 XSLStyleSheet::XSLStyleSheet(XSLImportRule* parentRule, const String& originalURL, const KURL& finalURL)
42 : m_ownerNode(nullptr)
43 , m_originalURL(originalURL)
44 , m_finalURL(finalURL)
47 , m_processed(false) // Child sheets get marked as processed when the libxslt engine has finally seen them.
49 , m_stylesheetDocTaken(false)
50 , m_compilationFailed(false)
51 , m_parentStyleSheet(parentRule ? parentRule->parentStyleSheet() : 0)
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)
60 , m_embedded(embedded)
61 , m_processed(true) // The root sheet starts off processed.
63 , m_stylesheetDocTaken(false)
64 , m_compilationFailed(false)
65 , m_parentStyleSheet(nullptr)
69 XSLStyleSheet::~XSLStyleSheet()
71 if (!m_stylesheetDocTaken)
72 xmlFreeDoc(m_stylesheetDoc);
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);
81 bool XSLStyleSheet::isLoading() const
83 for (unsigned i = 0; i < m_children.size(); ++i) {
84 if (m_children.at(i)->isLoading())
90 void XSLStyleSheet::checkLoaded()
94 if (XSLStyleSheet* styleSheet = parentStyleSheet())
95 styleSheet->checkLoaded();
97 ownerNode()->sheetLoaded();
100 xmlDocPtr XSLStyleSheet::document()
102 if (m_embedded && ownerDocument() && ownerDocument()->transformSource())
103 return (xmlDocPtr)ownerDocument()->transformSource()->platformSource();
104 return m_stylesheetDoc;
107 void XSLStyleSheet::clearDocuments()
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();
117 ResourceFetcher* XSLStyleSheet::fetcher()
119 Document* document = ownerDocument();
122 return document->fetcher();
125 bool XSLStyleSheet::parseString(const String& source)
127 // Parse in a single chunk into an xmlDocPtr
128 if (!m_stylesheetDocTaken)
129 xmlFreeDoc(m_stylesheetDoc);
130 m_stylesheetDocTaken = false;
132 FrameConsole* console = 0;
133 LocalFrame* frame = ownerDocument()->frame();
135 console = &frame->console();
137 XMLDocumentParserScope scope(fetcher(), XSLTProcessor::genericErrorFunc, XSLTProcessor::parseErrorFunc, console);
138 XMLParserInput input(source);
140 xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(input.data(), input.size());
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
151 xmlDictFree(ctxt->dict);
152 ctxt->dict = m_parentStyleSheet->m_stylesheetDoc->dict;
153 xmlDictReference(ctxt->dict);
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);
160 xmlFreeParserCtxt(ctxt);
162 return m_stylesheetDoc;
165 void XSLStyleSheet::loadChildSheets()
170 xmlNodePtr stylesheetRoot = document()->children;
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;
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()));
182 stylesheetRoot = idNode->parent;
184 // FIXME: Need to handle an external URI with a # in it. This is a pretty minor edge case, so we'll deal
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;
193 if (curr->type != XML_ELEMENT_NODE) {
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));
206 // Now handle includes.
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));
218 void XSLStyleSheet::loadChildSheet(const String& href)
220 OwnPtrWillBeRawPtr<XSLImportRule> childRule = XSLImportRule::create(this, href);
221 XSLImportRule* c = childRule.get();
222 m_children.append(childRule.release());
226 xsltStylesheetPtr XSLStyleSheet::compileStyleSheet()
228 // FIXME: Hook up error reporting for the stylesheet compilation process.
230 return xsltLoadStylesheetPI(document());
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)
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);
242 m_stylesheetDocTaken = true;
244 m_compilationFailed = true;
248 void XSLStyleSheet::setParentStyleSheet(XSLStyleSheet* parent)
250 m_parentStyleSheet = parent;
253 Document* XSLStyleSheet::ownerDocument()
255 for (XSLStyleSheet* styleSheet = this; styleSheet; styleSheet = styleSheet->parentStyleSheet()) {
256 Node* node = styleSheet->ownerNode();
258 return &node->document();
263 xmlDocPtr XSLStyleSheet::locateStylesheetSubResource(xmlDocPtr parentDoc, const xmlChar* uri)
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();
272 if (child->processed())
273 continue; // libxslt has been given this sheet already.
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);
286 child->markAsProcessed();
287 return child->document();
291 xmlDocPtr result = import->styleSheet()->locateStylesheetSubResource(parentDoc, uri);
299 void XSLStyleSheet::markAsProcessed()
301 ASSERT(!m_processed);
302 ASSERT(!m_stylesheetDocTaken);
304 m_stylesheetDocTaken = true;
307 void XSLStyleSheet::trace(Visitor* visitor)
309 visitor->trace(m_ownerNode);
310 visitor->trace(m_children);
311 visitor->trace(m_parentStyleSheet);
312 StyleSheet::trace(visitor);
315 } // namespace WebCore