This source file includes following definitions.
- transformTextStringToXHTMLDocumentString
- createDocumentFromSource
- transformToDocument
- transformToFragment
- setParameter
- getParameter
- removeParameter
- reset
- trace
#include "config.h"
#include "core/xml/XSLTProcessor.h"
#include "core/dom/DOMImplementation.h"
#include "core/dom/DocumentEncodingData.h"
#include "core/dom/DocumentFragment.h"
#include "core/editing/markup.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "wtf/Assertions.h"
#include "wtf/Vector.h"
namespace WebCore {
static inline void transformTextStringToXHTMLDocumentString(String& text)
{
text.replaceWithLiteral('&', "&");
text.replaceWithLiteral('<', "<");
text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
"<head><title/></head>\n"
"<body>\n"
"<pre>" + text + "</pre>\n"
"</body>\n"
"</html>\n";
}
XSLTProcessor::~XSLTProcessor()
{
#if !ENABLE(OILPAN)
ASSERT(!m_stylesheetRootNode || !m_stylesheet || m_stylesheet->hasOneRef());
#endif
}
PassRefPtr<Document> XSLTProcessor::createDocumentFromSource(const String& sourceString,
const String& sourceEncoding, const String& sourceMIMEType, Node* sourceNode, LocalFrame* frame)
{
RefPtr<Document> ownerDocument(sourceNode->document());
bool sourceIsDocument = (sourceNode == ownerDocument.get());
String documentSource = sourceString;
RefPtr<Document> result;
DocumentInit init(sourceIsDocument ? ownerDocument->url() : KURL(), frame);
bool forceXHTML = sourceMIMEType == "text/plain";
if (forceXHTML)
transformTextStringToXHTMLDocumentString(documentSource);
if (frame) {
RefPtr<Document> oldDocument = frame->document();
result = frame->domWindow()->installNewDocument(sourceMIMEType, init, forceXHTML);
if (FrameView* view = frame->view())
view->clear();
if (oldDocument) {
result->setTransformSourceDocument(oldDocument.get());
result->updateSecurityOrigin(oldDocument->securityOrigin());
result->setCookieURL(oldDocument->cookieURL());
result->contentSecurityPolicy()->copyStateFrom(oldDocument->contentSecurityPolicy());
}
} else {
result = DOMWindow::createDocument(sourceMIMEType, init, forceXHTML);
}
DocumentEncodingData data;
data.setEncoding(sourceEncoding.isEmpty() ? UTF8Encoding() : WTF::TextEncoding(sourceEncoding));
result->setEncodingData(data);
result->setContent(documentSource);
return result.release();
}
PassRefPtr<Document> XSLTProcessor::transformToDocument(Node* sourceNode)
{
if (!sourceNode)
return nullptr;
String resultMIMEType;
String resultString;
String resultEncoding;
if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
return nullptr;
return createDocumentFromSource(resultString, resultEncoding, resultMIMEType, sourceNode, 0);
}
PassRefPtr<DocumentFragment> XSLTProcessor::transformToFragment(Node* sourceNode, Document* outputDoc)
{
if (!sourceNode || !outputDoc)
return nullptr;
String resultMIMEType;
String resultString;
String resultEncoding;
if (outputDoc->isHTMLDocument())
resultMIMEType = "text/html";
if (!transformToString(sourceNode, resultMIMEType, resultString, resultEncoding))
return nullptr;
return createFragmentForTransformToFragment(resultString, resultMIMEType, *outputDoc);
}
void XSLTProcessor::setParameter(const String& , const String& localName, const String& value)
{
m_parameters.set(localName, value);
}
String XSLTProcessor::getParameter(const String& , const String& localName) const
{
return m_parameters.get(localName);
}
void XSLTProcessor::removeParameter(const String& , const String& localName)
{
m_parameters.remove(localName);
}
void XSLTProcessor::reset()
{
m_stylesheet.clear();
m_stylesheetRootNode.clear();
m_parameters.clear();
}
void XSLTProcessor::trace(Visitor* visitor)
{
visitor->trace(m_stylesheet);
}
}