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"
35 #include <libxml/uri.h>
36 #include <libxslt/xsltutils.h>
40 XSLStyleSheet::XSLStyleSheet(XSLImportRule* parentRule, const String& originalURL, const KURL& finalURL)
41 : m_ownerNode(nullptr)
42 , m_originalURL(originalURL)
43 , m_finalURL(finalURL)
46 // Child sheets get marked as processed when the libxslt engine has finally
50 , m_stylesheetDocTaken(false)
51 , m_compilationFailed(false)
52 , m_parentStyleSheet(parentRule ? parentRule->parentStyleSheet() : 0)
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)
61 , m_embedded(embedded)
62 , m_processed(true) // The root sheet starts off processed.
64 , m_stylesheetDocTaken(false)
65 , m_compilationFailed(false)
66 , m_parentStyleSheet(nullptr)
70 XSLStyleSheet::~XSLStyleSheet()
72 if (!m_stylesheetDocTaken)
73 xmlFreeDoc(m_stylesheetDoc);
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);
82 bool XSLStyleSheet::isLoading() const
84 for (unsigned i = 0; i < m_children.size(); ++i) {
85 if (m_children.at(i)->isLoading())
91 void XSLStyleSheet::checkLoaded()
95 if (XSLStyleSheet* styleSheet = parentStyleSheet())
96 styleSheet->checkLoaded();
98 ownerNode()->sheetLoaded();
101 xmlDocPtr XSLStyleSheet::document()
103 if (m_embedded && ownerDocument() && ownerDocument()->transformSource())
104 return (xmlDocPtr)ownerDocument()->transformSource()->platformSource();
105 return m_stylesheetDoc;
108 void XSLStyleSheet::clearDocuments()
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();
118 ResourceFetcher* XSLStyleSheet::fetcher()
120 if (Document* document = ownerDocument())
121 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 if (LocalFrame* frame = ownerDocument()->frame())
134 console = &frame->console();
136 XMLDocumentParserScope scope(fetcher(), XSLTProcessor::genericErrorFunc, XSLTProcessor::parseErrorFunc, console);
137 XMLParserInput input(source);
139 xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(input.data(), input.size());
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
150 xmlDictFree(ctxt->dict);
151 ctxt->dict = m_parentStyleSheet->m_stylesheetDoc->dict;
152 xmlDictReference(ctxt->dict);
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);
159 xmlFreeParserCtxt(ctxt);
161 return m_stylesheetDoc;
164 void XSLStyleSheet::loadChildSheets()
169 xmlNodePtr stylesheetRoot = document()->children;
171 // Top level children may include other things such as DTD nodes, we ignore
173 while (stylesheetRoot && stylesheetRoot->type != XML_ELEMENT_NODE)
174 stylesheetRoot = stylesheetRoot->next;
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()));
182 stylesheetRoot = idNode->parent;
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.
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;
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));
207 // Now handle includes.
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));
219 void XSLStyleSheet::loadChildSheet(const String& href)
221 OwnPtrWillBeRawPtr<XSLImportRule> childRule = XSLImportRule::create(this, href);
222 XSLImportRule* c = childRule.get();
223 m_children.append(childRule.release());
227 xsltStylesheetPtr XSLStyleSheet::compileStyleSheet()
229 // FIXME: Hook up error reporting for the stylesheet compilation process.
231 return xsltLoadStylesheetPI(document());
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)
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);
243 m_stylesheetDocTaken = true;
245 m_compilationFailed = true;
249 void XSLStyleSheet::setParentStyleSheet(XSLStyleSheet* parent)
251 m_parentStyleSheet = parent;
254 Document* XSLStyleSheet::ownerDocument()
256 for (XSLStyleSheet* styleSheet = this; styleSheet; styleSheet = styleSheet->parentStyleSheet()) {
257 Node* node = styleSheet->ownerNode();
259 return &node->document();
264 xmlDocPtr XSLStyleSheet::locateStylesheetSubResource(xmlDocPtr parentDoc, const xmlChar* uri)
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();
273 if (child->processed())
274 continue; // libxslt has been given this sheet already.
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);
287 child->markAsProcessed();
288 return child->document();
292 xmlDocPtr result = import->styleSheet()->locateStylesheetSubResource(parentDoc, uri);
300 void XSLStyleSheet::markAsProcessed()
302 ASSERT(!m_processed);
303 ASSERT(!m_stylesheetDocTaken);
305 m_stylesheetDocTaken = true;
308 void XSLStyleSheet::trace(Visitor* visitor)
310 visitor->trace(m_ownerNode);
311 visitor->trace(m_children);
312 visitor->trace(m_parentStyleSheet);
313 StyleSheet::trace(visitor);