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