XSLStyleSheetLibxslt.cpp 11 KB

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