📄 xsltprocessor.cpp
字号:
return parameterArray;}static void freeXsltParamArray(const char** params){ const char** temp = params; if (!params) return; while (*temp) { free((void*)*(temp++)); // strdup returns malloc'd blocks, so we have to use free() here free((void*)*(temp++)); } fastFree(params);}PassRefPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString, const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, Frame* frame){ RefPtr<Document> ownerDocument = sourceNode->document(); bool sourceIsDocument = (sourceNode == ownerDocument.get()); String documentSource = sourceString; RefPtr<Document> result; if (sourceMIMEType == "text/plain") { result = ownerDocument->implementation()->createDocument(frame); transformTextStringToXHTMLDocumentString(documentSource); } else result = ownerDocument->implementation()->createDocument(sourceMIMEType, frame, false); // Before parsing, we need to save & detach the old document and get the new document // in place. We have to do this only if we're rendering the result document. if (frame) { if (FrameView* view = frame->view()) view->clear(); result->setTransformSourceDocument(frame->document()); frame->setDocument(result); } if (sourceIsDocument) result->setURL(ownerDocument->url()); result->open(); RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create(sourceMIMEType); decoder->setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : TextEncoding(sourceEncoding), TextResourceDecoder::EncodingFromXMLHeader); result->setDecoder(decoder.release()); result->write(documentSource); result->finishParsing(); result->close(); return result.release();}static inline RefPtr<DocumentFragment> createFragmentFromSource(const String& sourceString, const String& sourceMIMEType, Document* outputDoc){ RefPtr<DocumentFragment> fragment = new DocumentFragment(outputDoc); if (sourceMIMEType == "text/html") parseHTMLDocumentFragment(sourceString, fragment.get()); else if (sourceMIMEType == "text/plain") fragment->addChild(new Text(outputDoc, sourceString)); else { bool successfulParse = parseXMLDocumentFragment(sourceString, fragment.get(), outputDoc->documentElement()); if (!successfulParse) return 0; } // FIXME: Do we need to mess with URLs here? return fragment;}static xsltStylesheetPtr xsltStylesheetPointer(RefPtr<XSLStyleSheet>& cachedStylesheet, Node* stylesheetRootNode){ if (!cachedStylesheet && stylesheetRootNode) { cachedStylesheet = XSLStyleSheet::create(stylesheetRootNode->parent() ? stylesheetRootNode->parent() : stylesheetRootNode, stylesheetRootNode->document()->url().string()); cachedStylesheet->parseString(createMarkup(stylesheetRootNode)); } if (!cachedStylesheet || !cachedStylesheet->document()) return 0; return cachedStylesheet->compileStyleSheet();}static inline xmlDocPtr xmlDocPtrFromNode(Node* sourceNode, bool& shouldDelete){ RefPtr<Document> ownerDocument = sourceNode->document(); bool sourceIsDocument = (sourceNode == ownerDocument.get()); xmlDocPtr sourceDoc = 0; if (sourceIsDocument) sourceDoc = (xmlDocPtr)ownerDocument->transformSource(); if (!sourceDoc) { sourceDoc = (xmlDocPtr)xmlDocPtrForString(ownerDocument->docLoader(), createMarkup(sourceNode), sourceIsDocument ? ownerDocument->url().string() : String()); shouldDelete = (sourceDoc != 0); } return sourceDoc;}static inline String resultMIMEType(xmlDocPtr resultDoc, xsltStylesheetPtr sheet){ // There are three types of output we need to be able to deal with: // HTML (create an HTML document), XML (create an XML document), // and text (wrap in a <pre> and create an XML document). const xmlChar* resultType = 0; XSLT_GET_IMPORT_PTR(resultType, sheet, method); if (resultType == 0 && resultDoc->type == XML_HTML_DOCUMENT_NODE) resultType = (const xmlChar*)"html"; if (xmlStrEqual(resultType, (const xmlChar*)"html")) return "text/html"; else if (xmlStrEqual(resultType, (const xmlChar*)"text")) return "text/plain"; return "application/xml";}bool XSLTProcessor::transformToString(Node* sourceNode, String& mimeType, String& resultString, String& resultEncoding){ RefPtr<Document> ownerDocument = sourceNode->document(); setXSLTLoadCallBack(docLoaderFunc, this, ownerDocument->docLoader()); xsltStylesheetPtr sheet = xsltStylesheetPointer(m_stylesheet, m_stylesheetRootNode.get()); if (!sheet) { setXSLTLoadCallBack(0, 0, 0); return false; } m_stylesheet->clearDocuments(); xmlChar* origMethod = sheet->method; if (!origMethod && mimeType == "text/html") sheet->method = (xmlChar*)"html"; bool success = false; bool shouldFreeSourceDoc = false; if (xmlDocPtr sourceDoc = xmlDocPtrFromNode(sourceNode, shouldFreeSourceDoc)) { // The XML declaration would prevent parsing the result as a fragment, and it's not needed even for documents, // as the result of this function is always immediately parsed. sheet->omitXmlDeclaration = true; xsltTransformContextPtr transformContext = xsltNewTransformContext(sheet, sourceDoc); registerXSLTExtensions(transformContext); // <http://bugs.webkit.org/show_bug.cgi?id=16077>: XSLT processor <xsl:sort> algorithm only compares by code point xsltSetCtxtSortFunc(transformContext, xsltUnicodeSortFunction); // This is a workaround for a bug in libxslt. // The bug has been fixed in version 1.1.13, so once we ship that this can be removed. if (transformContext->globalVars == NULL) transformContext->globalVars = xmlHashCreate(20); const char** params = xsltParamArrayFromParameterMap(m_parameters); xsltQuoteUserParams(transformContext, params); xmlDocPtr resultDoc = xsltApplyStylesheetUser(sheet, sourceDoc, 0, 0, 0, transformContext); xsltFreeTransformContext(transformContext); freeXsltParamArray(params); if (shouldFreeSourceDoc) xmlFreeDoc(sourceDoc); if (success = saveResultToString(resultDoc, sheet, resultString)) { mimeType = resultMIMEType(resultDoc, sheet); resultEncoding = (char*)resultDoc->encoding; } xmlFreeDoc(resultDoc); } sheet->method = origMethod; setXSLTLoadCallBack(0, 0, 0); xsltFreeStylesheet(sheet); m_stylesheet = 0; return success;}PassRefPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode){ String resultMIMEType; String resultString; String resultEncoding; if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding)) return 0; return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);}PassRefPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc){ String resultMIMEType; String resultString; String resultEncoding; // If the output document is HTML, default to HTML method. if (outputDoc->isHTMLDocument()) resultMIMEType = "text/html"; if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding)) return 0; return createFragmentFromSource(resultString, resultMIMEType, outputDoc);}void XSLTProcessor::setParameter(const String& /*namespaceURI*/, const String& localName, const String& value){ // FIXME: namespace support? // should make a QualifiedName here but we'd have to expose the impl m_parameters.set(localName, value);}String XSLTProcessor::getParameter(const String& /*namespaceURI*/, const String& localName) const{ // FIXME: namespace support? // should make a QualifiedName here but we'd have to expose the impl return m_parameters.get(localName);}void XSLTProcessor::removeParameter(const String& /*namespaceURI*/, const String& localName){ // FIXME: namespace support? m_parameters.remove(localName);}} // namespace WebCore#endif // ENABLE(XSLT)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -