Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: Source/core/xml/DocumentXSLT.cpp

Issue 730003002: Refactoring XSLT (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include "config.h"
haraken 2014/11/17 14:18:31 Add a copyright.
tasak 2014/11/19 08:23:17 Done.
2 #include "core/xml/DocumentXSLT.h"
3
4 #include "bindings/core/v8/DOMWrapperWorld.h"
5 #include "bindings/core/v8/ScriptState.h"
6 #include "bindings/core/v8/V8AbstractEventListener.h"
7 #include "bindings/core/v8/V8Binding.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/Node.h"
10 #include "core/dom/ProcessingInstruction.h"
11 #include "core/events/Event.h"
12 #include "core/events/EventListener.h"
13 #include "core/frame/UseCounter.h"
14 #include "core/inspector/InspectorInstrumentation.h"
15 #include "core/xml/XSLStyleSheet.h"
16 #include "core/xml/XSLTProcessor.h"
17
18 namespace blink {
19
20 namespace {
haraken 2014/11/17 14:18:31 What is this namespace for?
tasak 2014/11/19 08:23:17 Done.
21
22 class DOMContentLoadedListener : public V8AbstractEventListener {
23 public:
24 virtual bool operator==(const EventListener&)
25 {
26 return true;
27 }
28
29 virtual void handleEvent(ExecutionContext* context, Event* event)
30 {
31 ASSERT(event->type() == "DOMContentLoaded");
32 ScriptState::Scope scope(scriptState());
33
34 Document& document = *toDocument(context);
35 // Processing instruction (XML documents only).
36 // We don't support linking to embedded CSS stylesheets, see <https://bu gs.webkit.org/show_bug.cgi?id=49281> for discussion.
37 // Don't apply XSL transforms to already transformed documents -- <rdar: //problem/4132806>
haraken 2014/11/17 14:18:31 Remove the rdar part.
tasak 2014/11/19 08:23:17 Done.
38 if (DocumentXSLT::hasTransformSourceDocument(document))
39 return;
40
41 ProcessingInstruction* pi = DocumentXSLT::findXSLStyleSheet(document);
42 if (!pi || pi != m_processingInstruction.get() || pi->isLoading())
43 return;
44 DocumentXSLT::applyXSLTransform(document, pi);
45 }
46
47 static PassRefPtr<DOMContentLoadedListener> create(ScriptState* scriptState, ProcessingInstruction* pi)
48 {
49 return adoptRef(new DOMContentLoadedListener(scriptState, pi));
50 }
51
52 private:
53 DOMContentLoadedListener(ScriptState* scriptState, ProcessingInstruction* pi )
54 : V8AbstractEventListener(false, scriptState)
55 , m_processingInstruction(pi)
56 {
57 }
58
59 virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsev ent, Event*)
60 {
61 ASSERT_NOT_REACHED();
62 return v8::Local<v8::Value>();
63 }
64
65 RefPtr<ProcessingInstruction> m_processingInstruction;
haraken 2014/11/17 14:18:31 This should be RefPtrWillBePersistent. Let's confi
tasak 2014/11/19 08:23:17 Done.
66 };
67
68 }
69
70 DocumentXSLT::DocumentXSLT()
71 : m_transformSourceDocument(nullptr)
72 {
73 }
74
75 PassRefPtr<EventListener> DocumentXSLT::addDOMContentLoadedListenerForXSLT(Docum ent& document, ProcessingInstruction* pi)
76 {
77 if (!RuntimeEnabledFeatures::xsltEnabled())
78 return nullptr;
79
80 if (!document.frame())
81 return nullptr;
82
83 v8::HandleScope handleScope(v8::Isolate::GetCurrent());
84 v8::Local<v8::Context> context = toV8Context(document.frame(), DOMWrapperWor ld::mainWorld());
haraken 2014/11/17 14:18:31 Is it guaranteed that this is for the main world?
tasak 2014/11/19 08:23:17 According to offline discussion, for the main worl
85 ScriptState* scriptState = ScriptState::from(context);
haraken 2014/11/17 14:18:31 You can use ScriptState::forMainWorld(document.fra
tasak 2014/11/19 08:23:17 Done.
86
87 RefPtr<EventListener> listener = DOMContentLoadedListener::create(scriptStat e, pi);
88 document.addEventListener(EventTypeNames::DOMContentLoaded, listener, false) ;
89 return listener;
90 }
91
92 void DocumentXSLT::applyXSLTransform(Document& document, ProcessingInstruction* pi)
93 {
94 ASSERT(!pi->isLoading());
95 UseCounter::count(document, UseCounter::XSLProcessingInstruction);
96 RefPtrWillBeRawPtr<XSLTProcessor> processor = XSLTProcessor::create(document );
97 processor->setXSLStyleSheet(toXSLStyleSheet(pi->sheet()));
98 String resultMIMEType;
99 String newSource;
100 String resultEncoding;
101 document.setParsingState(Document::Parsing);
102 if (!processor->transformToString(&document, resultMIMEType, newSource, resu ltEncoding)) {
103 document.setParsingState(Document::FinishedParsing);
104 return;
105 }
106 // FIXME: If the transform failed we should probably report an error (like M ozilla does).
107 LocalFrame* ownerFrame = document.frame();
108 processor->createDocumentFromSource(newSource, resultEncoding, resultMIMETyp e, &document, ownerFrame);
109 InspectorInstrumentation::frameDocumentUpdated(ownerFrame);
110 document.setParsingState(Document::FinishedParsing);
111 }
112
113 ProcessingInstruction* DocumentXSLT::findXSLStyleSheet(Document& document)
114 {
115 for (Node* node = document.firstChild(); node; node = node->nextSibling()) {
116 if (node->nodeType() != Node::PROCESSING_INSTRUCTION_NODE)
117 continue;
118
119 ProcessingInstruction* pi = toProcessingInstruction(node);
120 if (pi->isXSL())
121 return pi;
122 }
123 return 0;
124 }
125
126 void DocumentXSLT::sheetLoaded(Document& document, ProcessingInstruction* pi)
127 {
128 if (!RuntimeEnabledFeatures::xsltEnabled())
129 return;
130
131 if (document.parsing() || pi->isLoading())
132 return;
133
134 if (DocumentXSLT::hasTransformSourceDocument(document))
135 return;
136
137 ProcessingInstruction* firstPi = findXSLStyleSheet(document);
138 ASSERT(firstPi);
139 if (firstPi != pi)
140 return;
141 applyXSLTransform(document, pi);
142 }
143
144 const char* DocumentXSLT::supplementName()
145 {
146 return "DocumentXSLT";
147 }
148
149 bool DocumentXSLT::hasTransformSourceDocument(Document& document)
150 {
151 return static_cast<DocumentXSLT*>(DocumentSupplement::from(document, supplem entName()));
152 }
153
154
155 DocumentXSLT& DocumentXSLT::from(DocumentSupplementable& document)
156 {
157 DocumentXSLT* supplement = static_cast<DocumentXSLT*>(DocumentSupplement::fr om(document, supplementName()));
158 if (!supplement) {
159 supplement = new DocumentXSLT();
160 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe Noop(supplement));
161 }
162 return *supplement;
163 }
164
165 void DocumentXSLT::trace(Visitor* visitor)
166 {
167 visitor->trace(m_transformSourceDocument);
168 DocumentSupplement::trace(visitor);
169 }
170
171 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698