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