2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43 void set_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
45 nsIDOMNSDocument *nsdoc;
48 nsres = nsIDOMHTMLDocument_QueryInterface(nshtmldoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
49 if(NS_FAILED(nsres)) {
50 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
54 nsIDOMNSDocument_WineAddObserver(nsdoc, NSDOCOBS(nscontainer));
55 nsIDOMNSDocument_Release(nsdoc);
58 void remove_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
60 nsIDOMNSDocument *nsdoc;
63 nsres = nsIDOMHTMLDocument_QueryInterface(nshtmldoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
64 if(NS_FAILED(nsres)) {
65 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
69 nsIDOMNSDocument_WineRemoveObserver(nsdoc, NSDOCOBS(nscontainer));
70 nsIDOMNSDocument_Release(nsdoc);
73 #define IE_MAJOR_VERSION 7
74 #define IE_MINOR_VERSION 0
76 static BOOL handle_insert_comment(HTMLDocument *doc, const PRUnichar *comment)
79 int majorv = 0, minorv = 0;
80 const PRUnichar *ptr, *end;
93 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
95 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
102 if(ptr[0] == 'l' && ptr[1] == 't') {
110 }else if(ptr[0] == 'g' && ptr[1] == 't') {
120 if(!isspaceW(*ptr++))
122 while(isspaceW(*ptr))
125 if(ptr[0] != 'I' || ptr[1] != 'E')
129 if(!isspaceW(*ptr++))
131 while(isspaceW(*ptr))
136 while(isdigitW(*ptr))
137 majorv = majorv*10 + (*ptr++ - '0');
143 while(isdigitW(*ptr))
144 minorv = minorv*10 + (*ptr++ - '0');
147 while(isspaceW(*ptr))
149 if(ptr[0] != ']' || ptr[1] != '>')
154 if(len < sizeof(endifW)/sizeof(WCHAR))
157 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
158 if(memcmp(end, endifW, sizeof(endifW)))
163 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
167 if(majorv > IE_MAJOR_VERSION)
169 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
173 if(majorv > IE_MAJOR_VERSION)
175 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
179 if(majorv < IE_MAJOR_VERSION)
181 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
185 if(majorv < IE_MAJOR_VERSION)
187 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
192 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
196 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
198 nsAString_Init(&nsstr, buf);
201 /* FIXME: Find better way to insert HTML to document. */
202 nsres = nsIDOMHTMLDocument_Write(doc->nsdoc, &nsstr);
203 nsAString_Finish(&nsstr);
204 if(NS_FAILED(nsres)) {
205 ERR("Write failed: %08x\n", nsres);
212 static void add_script_runner(NSContainer *This)
214 nsIDOMNSDocument *nsdoc;
217 nsres = nsIDOMHTMLDocument_QueryInterface(This->doc->basedoc.nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
218 if(NS_FAILED(nsres)) {
219 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
223 nsIDOMNSDocument_WineAddScriptRunner(nsdoc, NSRUNNABLE(This));
224 nsIDOMNSDocument_Release(nsdoc);
227 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(NSContainer, Runnable, iface)
229 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
230 nsIIDRef riid, nsQIResult result)
232 NSContainer *This = NSRUNNABLE_THIS(iface);
234 if(IsEqualGUID(riid, &IID_nsISupports)) {
235 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
236 *result = NSRUNNABLE(This);
237 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
238 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
239 *result = NSRUNNABLE(This);
242 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
243 return NS_NOINTERFACE;
246 nsISupports_AddRef((nsISupports*)*result);
250 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
252 NSContainer *This = NSRUNNABLE_THIS(iface);
253 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
256 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
258 NSContainer *This = NSRUNNABLE_THIS(iface);
259 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
262 static void pop_mutation_queue(NSContainer *nscontainer)
264 mutation_queue_t *tmp = nscontainer->mutation_queue;
269 nscontainer->mutation_queue = tmp->next;
271 nscontainer->mutation_queue_tail = NULL;
273 nsISupports_Release(tmp->nsiface);
277 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
279 NSContainer *This = NSRUNNABLE_THIS(iface);
282 TRACE("(%p)\n", This);
284 while(This->mutation_queue) {
285 switch(This->mutation_queue->type) {
286 case MUTATION_COMMENT: {
287 nsIDOMComment *nscomment;
288 nsAString comment_str;
289 BOOL remove_comment = FALSE;
291 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMComment, (void**)&nscomment);
292 if(NS_FAILED(nsres)) {
293 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
297 nsAString_Init(&comment_str, NULL);
298 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
299 if(NS_SUCCEEDED(nsres)) {
300 const PRUnichar *comment;
302 nsAString_GetData(&comment_str, &comment);
303 remove_comment = handle_insert_comment(&This->doc->basedoc, comment);
306 nsAString_Finish(&comment_str);
309 nsIDOMNode *nsparent, *tmp;
312 static const PRUnichar remove_comment_magicW[] =
313 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
315 nsAString_Init(&magic_str, remove_comment_magicW);
316 nsres = nsIDOMComment_SetData(nscomment, &magic_str);
317 nsAString_Finish(&magic_str);
319 ERR("SetData failed: %08x\n", nsres);
321 nsIDOMComment_GetParentNode(nscomment, &nsparent);
323 nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
324 nsIDOMNode_Release(nsparent);
325 nsIDOMNode_Release(tmp);
329 nsIDOMComment_Release(nscomment);
333 case MUTATION_SCRIPT: {
334 nsIDOMHTMLScriptElement *nsscript;
336 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMHTMLScriptElement,
338 if(NS_FAILED(nsres)) {
339 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
343 doc_insert_script(This->doc->basedoc.window, nsscript);
344 nsIDOMHTMLScriptElement_Release(nsscript);
349 ERR("invalid type %d\n", This->mutation_queue->type);
352 pop_mutation_queue(This);
358 #undef NSRUNNABLE_THIS
360 static const nsIRunnableVtbl nsRunnableVtbl = {
361 nsRunnable_QueryInterface,
367 #define NSDOCOBS_THIS(iface) DEFINE_THIS(NSContainer, DocumentObserver, iface)
369 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
370 nsIIDRef riid, nsQIResult result)
372 NSContainer *This = NSDOCOBS_THIS(iface);
374 if(IsEqualGUID(&IID_nsISupports, riid)) {
375 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
376 *result = NSDOCOBS(This);
377 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
378 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
379 *result = NSDOCOBS(This);
380 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
381 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
382 *result = NSDOCOBS(This);
385 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
386 return NS_NOINTERFACE;
389 nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
393 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
395 NSContainer *This = NSDOCOBS_THIS(iface);
396 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
399 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
401 NSContainer *This = NSDOCOBS_THIS(iface);
402 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
405 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
406 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
410 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
411 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
415 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
416 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
420 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
421 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType, PRUint32 aStateMask)
425 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
426 nsIContent *aContainer, PRInt32 aNewIndexInContainer)
430 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
431 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
435 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
436 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
440 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
444 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
448 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
449 nsUpdateType aUpdateType)
453 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
454 nsUpdateType aUpdateType)
458 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
462 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
464 NSContainer *This = NSDOCOBS_THIS(iface);
469 task = heap_alloc(sizeof(task_t));
471 task->doc = &This->doc->basedoc;
472 task->task_id = TASK_PARSECOMPLETE;
476 * This should be done in the worker thread that parses HTML,
477 * but we don't have such thread (Gecko parses HTML for us).
482 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
483 nsIContent *aContent1, nsIContent *aContent2, PRInt32 aStateMask)
487 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
488 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
492 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
493 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
497 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
498 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
502 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
503 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
507 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
508 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
512 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
513 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
517 static void push_mutation_queue(NSContainer *nscontainer, DWORD type, nsISupports *nsiface)
519 mutation_queue_t *elem;
521 elem = heap_alloc(sizeof(mutation_queue_t));
527 elem->nsiface = nsiface;
528 nsISupports_AddRef(nsiface);
530 if(nscontainer->mutation_queue_tail)
531 nscontainer->mutation_queue_tail = nscontainer->mutation_queue_tail->next = elem;
533 nscontainer->mutation_queue = nscontainer->mutation_queue_tail = elem;
536 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
537 nsIContent *aContent)
539 NSContainer *This = NSDOCOBS_THIS(iface);
540 nsIDOMComment *nscomment;
541 nsIDOMElement *nselem;
544 TRACE("(%p)\n", This);
546 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
547 if(NS_SUCCEEDED(nsres)) {
548 check_event_attr(This->doc->basedoc.doc_node, nselem);
549 nsIDOMElement_Release(nselem);
552 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
553 if(NS_SUCCEEDED(nsres)) {
554 TRACE("comment node\n");
556 push_mutation_queue(This, MUTATION_COMMENT, (nsISupports*)nscomment);
557 nsIDOMComment_Release(nscomment);
558 add_script_runner(This);
562 static void NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
563 PRBool aHaveNotified)
565 NSContainer *This = NSDOCOBS_THIS(iface);
566 nsIDOMHTMLScriptElement *nsscript;
569 TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
571 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
572 if(NS_SUCCEEDED(nsres)) {
573 push_mutation_queue(This, MUTATION_SCRIPT, (nsISupports*)nsscript);
574 nsIDOMHTMLScriptElement_Release(nsscript);
575 add_script_runner(This);
579 #undef NSMUTATIONOBS_THIS
581 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
582 nsDocumentObserver_QueryInterface,
583 nsDocumentObserver_AddRef,
584 nsDocumentObserver_Release,
585 nsDocumentObserver_CharacterDataWillChange,
586 nsDocumentObserver_CharacterDataChanged,
587 nsDocumentObserver_AttributeWillChange,
588 nsDocumentObserver_AttributeChanged,
589 nsDocumentObserver_ContentAppended,
590 nsDocumentObserver_ContentInserted,
591 nsDocumentObserver_ContentRemoved,
592 nsDocumentObserver_NodeWillBeDestroyed,
593 nsDocumentObserver_ParentChainChanged,
594 nsDocumentObserver_BeginUpdate,
595 nsDocumentObserver_EndUpdate,
596 nsDocumentObserver_BeginLoad,
597 nsDocumentObserver_EndLoad,
598 nsDocumentObserver_ContentStatesChanged,
599 nsDocumentObserver_StyleSheetAdded,
600 nsDocumentObserver_StyleSheetRemoved,
601 nsDocumentObserver_StyleSheetApplicableStateChanged,
602 nsDocumentObserver_StyleRuleChanged,
603 nsDocumentObserver_StyleRuleAdded,
604 nsDocumentObserver_StyleRuleRemoved,
605 nsDocumentObserver_BindToDocument,
606 nsDocumentObserver_DoneAddingChildren
609 void init_mutation(NSContainer *nscontainer)
611 nscontainer->lpDocumentObserverVtbl = &nsDocumentObserverVtbl;
612 nscontainer->lpRunnableVtbl = &nsRunnableVtbl;