1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/xml/DocumentXSLT.h"
#include "bindings/core/v8/DOMWrapperWorld.h"
#include "bindings/core/v8/ScriptState.h"
#include "bindings/core/v8/V8AbstractEventListener.h"
#include "bindings/core/v8/V8Binding.h"
#include "core/dom/Document.h"
#include "core/dom/Node.h"
#include "core/dom/ProcessingInstruction.h"
#include "core/events/Event.h"
#include "core/events/EventListener.h"
#include "core/frame/UseCounter.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/xml/XSLStyleSheet.h"
#include "core/xml/XSLTProcessor.h"
namespace blink {
class DOMContentLoadedListener final
: public V8AbstractEventListener,
public ProcessingInstruction::DetachableEventListener {
USING_GARBAGE_COLLECTED_MIXIN(DOMContentLoadedListener);
public:
static DOMContentLoadedListener* create(ScriptState* scriptState,
ProcessingInstruction* pi) {
return new DOMContentLoadedListener(scriptState, pi);
}
bool operator==(const EventListener&) const override { return true; }
virtual void handleEvent(ScriptState* scriptState, Event* event) {
DCHECK(RuntimeEnabledFeatures::xsltEnabled());
DCHECK_EQ(event->type(), "DOMContentLoaded");
ScriptState::Scope scope(scriptState);
Document& document = *toDocument(scriptState->getExecutionContext());
DCHECK(!document.parsing());
// Processing instruction (XML documents only).
// We don't support linking to embedded CSS stylesheets,
// see <https://bugs.webkit.org/show_bug.cgi?id=49281> for discussion.
// Don't apply XSL transforms to already transformed documents.
if (DocumentXSLT::hasTransformSourceDocument(document))
return;
ProcessingInstruction* pi = DocumentXSLT::findXSLStyleSheet(document);
if (!pi || pi != m_processingInstruction || pi->isLoading())
return;
DocumentXSLT::applyXSLTransform(document, pi);
}
void detach() override { m_processingInstruction = nullptr; }
EventListener* toEventListener() override { return this; }
DEFINE_INLINE_VIRTUAL_TRACE() {
visitor->trace(m_processingInstruction);
V8AbstractEventListener::trace(visitor);
ProcessingInstruction::DetachableEventListener::trace(visitor);
}
private:
DOMContentLoadedListener(ScriptState* scriptState, ProcessingInstruction* pi)
: V8AbstractEventListener(false,
scriptState->world(),
scriptState->isolate()),
m_processingInstruction(pi) {}
virtual v8::Local<v8::Value> callListenerFunction(ScriptState*,
v8::Local<v8::Value>,
Event*) {
NOTREACHED();
return v8::Local<v8::Value>();
}
// If this event listener is attached to a ProcessingInstruction, keep a
// weak reference back to it. That ProcessingInstruction is responsible for
// detaching itself and clear out the reference.
Member<ProcessingInstruction> m_processingInstruction;
};
DocumentXSLT::DocumentXSLT(Document& document)
: Supplement<Document>(document), m_transformSourceDocument(nullptr) {}
void DocumentXSLT::applyXSLTransform(Document& document,
ProcessingInstruction* pi) {
DCHECK(!pi->isLoading());
UseCounter::count(document, UseCounter::XSLProcessingInstruction);
XSLTProcessor* processor = XSLTProcessor::create(document);
processor->setXSLStyleSheet(toXSLStyleSheet(pi->sheet()));
String resultMIMEType;
String newSource;
String resultEncoding;
document.setParsingState(Document::Parsing);
if (!processor->transformToString(&document, resultMIMEType, newSource,
resultEncoding)) {
document.setParsingState(Document::FinishedParsing);
return;
}
// FIXME: If the transform failed we should probably report an error (like
// Mozilla does).
LocalFrame* ownerFrame = document.frame();
processor->createDocumentFromSource(newSource, resultEncoding, resultMIMEType,
&document, ownerFrame);
InspectorInstrumentation::frameDocumentUpdated(ownerFrame);
document.setParsingState(Document::FinishedParsing);
}
ProcessingInstruction* DocumentXSLT::findXSLStyleSheet(Document& document) {
for (Node* node = document.firstChild(); node; node = node->nextSibling()) {
if (node->getNodeType() != Node::kProcessingInstructionNode)
continue;
ProcessingInstruction* pi = toProcessingInstruction(node);
if (pi->isXSL())
return pi;
}
return nullptr;
}
bool DocumentXSLT::processingInstructionInsertedIntoDocument(
Document& document,
ProcessingInstruction* pi) {
if (!pi->isXSL())
return false;
if (!RuntimeEnabledFeatures::xsltEnabled() || !document.frame())
return true;
ScriptState* scriptState = ScriptState::forMainWorld(document.frame());
if (!scriptState)
return false;
DOMContentLoadedListener* listener =
DOMContentLoadedListener::create(scriptState, pi);
document.addEventListener(EventTypeNames::DOMContentLoaded, listener, false);
DCHECK(!pi->eventListenerForXSLT());
pi->setEventListenerForXSLT(listener);
return true;
}
bool DocumentXSLT::processingInstructionRemovedFromDocument(
Document& document,
ProcessingInstruction* pi) {
if (!pi->isXSL())
return false;
if (!pi->eventListenerForXSLT())
return true;
DCHECK(RuntimeEnabledFeatures::xsltEnabled());
document.removeEventListener(EventTypeNames::DOMContentLoaded,
pi->eventListenerForXSLT(), false);
pi->clearEventListenerForXSLT();
return true;
}
bool DocumentXSLT::sheetLoaded(Document& document, ProcessingInstruction* pi) {
if (!pi->isXSL())
return false;
if (RuntimeEnabledFeatures::xsltEnabled() && !document.parsing() &&
!pi->isLoading() && !DocumentXSLT::hasTransformSourceDocument(document)) {
if (findXSLStyleSheet(document) == pi)
applyXSLTransform(document, pi);
}
return true;
}
const char* DocumentXSLT::supplementName() {
return "DocumentXSLT";
}
bool DocumentXSLT::hasTransformSourceDocument(Document& document) {
return static_cast<DocumentXSLT*>(
Supplement<Document>::from(document, supplementName()));
}
DocumentXSLT& DocumentXSLT::from(Document& document) {
DocumentXSLT* supplement = static_cast<DocumentXSLT*>(
Supplement<Document>::from(document, supplementName()));
if (!supplement) {
supplement = new DocumentXSLT(document);
Supplement<Document>::provideTo(document, supplementName(), supplement);
}
return *supplement;
}
DEFINE_TRACE(DocumentXSLT) {
visitor->trace(m_transformSourceDocument);
Supplement<Document>::trace(visitor);
}
} // namespace blink
|