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
32 #include "mshtml_private.h"
33 #include "htmlevent.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
46 #define IE_MAJOR_VERSION 7
47 #define IE_MINOR_VERSION 0
49 static BOOL
handle_insert_comment(HTMLDocumentNode
*doc
, const PRUnichar
*comment
)
52 int majorv
= 0, minorv
= 0;
53 const PRUnichar
*ptr
, *end
;
66 static const PRUnichar endifW
[] = {'<','!','[','e','n','d','i','f',']'};
68 if(comment
[0] != '[' || comment
[1] != 'i' || comment
[2] != 'f')
75 if(ptr
[0] == 'l' && ptr
[1] == 't') {
83 }else if(ptr
[0] == 'g' && ptr
[1] == 't') {
98 if(ptr
[0] != 'I' || ptr
[1] != 'E')
102 if(!isspaceW(*ptr
++))
104 while(isspaceW(*ptr
))
109 while(isdigitW(*ptr
))
110 majorv
= majorv
*10 + (*ptr
++ - '0');
116 while(isdigitW(*ptr
))
117 minorv
= minorv
*10 + (*ptr
++ - '0');
120 while(isspaceW(*ptr
))
122 if(ptr
[0] != ']' || ptr
[1] != '>')
127 if(len
< sizeof(endifW
)/sizeof(WCHAR
))
130 end
= ptr
+ len
-sizeof(endifW
)/sizeof(WCHAR
);
131 if(memcmp(end
, endifW
, sizeof(endifW
)))
136 if(majorv
== IE_MAJOR_VERSION
&& minorv
== IE_MINOR_VERSION
)
140 if(majorv
> IE_MAJOR_VERSION
)
142 if(majorv
== IE_MAJOR_VERSION
&& minorv
> IE_MINOR_VERSION
)
146 if(majorv
> IE_MAJOR_VERSION
)
148 if(majorv
== IE_MAJOR_VERSION
&& minorv
>= IE_MINOR_VERSION
)
152 if(majorv
< IE_MAJOR_VERSION
)
154 if(majorv
== IE_MAJOR_VERSION
&& minorv
< IE_MINOR_VERSION
)
158 if(majorv
< IE_MAJOR_VERSION
)
160 if(majorv
== IE_MAJOR_VERSION
&& minorv
<= IE_MINOR_VERSION
)
165 buf
= heap_alloc((end
-ptr
+1)*sizeof(WCHAR
));
169 memcpy(buf
, ptr
, (end
-ptr
)*sizeof(WCHAR
));
171 nsAString_InitDepend(&nsstr
, buf
);
173 /* FIXME: Find better way to insert HTML to document. */
174 nsres
= nsIDOMHTMLDocument_Write(doc
->nsdoc
, &nsstr
);
175 nsAString_Finish(&nsstr
);
177 if(NS_FAILED(nsres
)) {
178 ERR("Write failed: %08x\n", nsres
);
185 static void add_script_runner(HTMLDocumentNode
*This
)
187 nsIDOMNSDocument
*nsdoc
;
190 nsres
= nsIDOMHTMLDocument_QueryInterface(This
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
191 if(NS_FAILED(nsres
)) {
192 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
196 nsIDOMNSDocument_WineAddScriptRunner(nsdoc
, NSRUNNABLE(This
));
197 nsIDOMNSDocument_Release(nsdoc
);
200 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IRunnable, iface)
202 static nsresult NSAPI
nsRunnable_QueryInterface(nsIRunnable
*iface
,
203 nsIIDRef riid
, nsQIResult result
)
205 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
207 if(IsEqualGUID(riid
, &IID_nsISupports
)) {
208 TRACE("(%p)->(IID_nsISupports %p)\n", This
, result
);
209 *result
= NSRUNNABLE(This
);
210 }else if(IsEqualGUID(riid
, &IID_nsIRunnable
)) {
211 TRACE("(%p)->(IID_nsIRunnable %p)\n", This
, result
);
212 *result
= NSRUNNABLE(This
);
215 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
216 return NS_NOINTERFACE
;
219 nsISupports_AddRef((nsISupports
*)*result
);
223 static nsrefcnt NSAPI
nsRunnable_AddRef(nsIRunnable
*iface
)
225 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
226 return htmldoc_addref(&This
->basedoc
);
229 static nsrefcnt NSAPI
nsRunnable_Release(nsIRunnable
*iface
)
231 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
232 return htmldoc_release(&This
->basedoc
);
235 static void push_mutation_queue(HTMLDocumentNode
*doc
, DWORD type
, nsISupports
*nsiface
)
237 mutation_queue_t
*elem
;
239 elem
= heap_alloc(sizeof(mutation_queue_t
));
245 elem
->nsiface
= nsiface
;
247 nsISupports_AddRef(nsiface
);
249 if(doc
->mutation_queue_tail
) {
250 doc
->mutation_queue_tail
= doc
->mutation_queue_tail
->next
= elem
;
252 doc
->mutation_queue
= doc
->mutation_queue_tail
= elem
;
253 add_script_runner(doc
);
257 static void pop_mutation_queue(HTMLDocumentNode
*doc
)
259 mutation_queue_t
*tmp
= doc
->mutation_queue
;
264 doc
->mutation_queue
= tmp
->next
;
266 doc
->mutation_queue_tail
= NULL
;
269 nsISupports_Release(tmp
->nsiface
);
273 static void bind_to_tree(HTMLDocumentNode
*doc
, nsISupports
*nsiface
)
279 nsres
= nsISupports_QueryInterface(nsiface
, &IID_nsIDOMNode
, (void**)&nsnode
);
283 node
= get_node(doc
, nsnode
, TRUE
);
284 nsIDOMNode_Release(nsnode
);
286 ERR("Could not get node\n");
290 if(node
->vtbl
->bind_to_tree
)
291 node
->vtbl
->bind_to_tree(node
);
294 /* Calls undocumented 69 cmd of CGID_Explorer */
295 static void call_explorer_69(HTMLDocumentObj
*doc
)
297 IOleCommandTarget
*olecmd
;
304 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
309 hres
= IOleCommandTarget_Exec(olecmd
, &CGID_Explorer
, 69, 0, NULL
, &var
);
310 IOleCommandTarget_Release(olecmd
);
311 if(SUCCEEDED(hres
) && V_VT(&var
) != VT_NULL
)
312 FIXME("handle result\n");
315 static void parse_complete_proc(task_t
*task
)
317 HTMLDocumentObj
*doc
= ((docobj_task_t
*)task
)->doc
;
319 TRACE("(%p)\n", doc
);
321 if(doc
->usermode
== EDITMODE
)
322 init_editor(&doc
->basedoc
);
324 call_explorer_69(doc
);
325 call_property_onchanged(&doc
->basedoc
.cp_propnotif
, 1005);
326 call_explorer_69(doc
);
328 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
330 set_ready_state(doc
->basedoc
.window
, READYSTATE_INTERACTIVE
);
333 static void handle_end_load(HTMLDocumentNode
*This
)
339 if(This
!= This
->basedoc
.doc_obj
->basedoc
.doc_node
) {
340 set_ready_state(This
->basedoc
.window
, READYSTATE_INTERACTIVE
);
344 task
= heap_alloc(sizeof(docobj_task_t
));
348 task
->doc
= This
->basedoc
.doc_obj
;
351 * This should be done in the worker thread that parses HTML,
352 * but we don't have such thread (Gecko parses HTML for us).
354 push_task(&task
->header
, &parse_complete_proc
, This
->basedoc
.doc_obj
->basedoc
.task_magic
);
357 static nsresult NSAPI
nsRunnable_Run(nsIRunnable
*iface
)
359 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
362 TRACE("(%p)\n", This
);
364 while(This
->mutation_queue
) {
365 switch(This
->mutation_queue
->type
) {
366 case MUTATION_BINDTOTREE
:
367 bind_to_tree(This
, This
->mutation_queue
->nsiface
);
370 case MUTATION_COMMENT
: {
371 nsIDOMComment
*nscomment
;
372 nsAString comment_str
;
373 BOOL remove_comment
= FALSE
;
375 nsres
= nsISupports_QueryInterface(This
->mutation_queue
->nsiface
, &IID_nsIDOMComment
, (void**)&nscomment
);
376 if(NS_FAILED(nsres
)) {
377 ERR("Could not get nsIDOMComment iface:%08x\n", nsres
);
381 nsAString_Init(&comment_str
, NULL
);
382 nsres
= nsIDOMComment_GetData(nscomment
, &comment_str
);
383 if(NS_SUCCEEDED(nsres
)) {
384 const PRUnichar
*comment
;
386 nsAString_GetData(&comment_str
, &comment
);
387 remove_comment
= handle_insert_comment(This
, comment
);
390 nsAString_Finish(&comment_str
);
393 nsIDOMNode
*nsparent
, *tmp
;
396 static const PRUnichar remove_comment_magicW
[] =
397 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
399 nsAString_InitDepend(&magic_str
, remove_comment_magicW
);
400 nsres
= nsIDOMComment_SetData(nscomment
, &magic_str
);
401 nsAString_Finish(&magic_str
);
403 ERR("SetData failed: %08x\n", nsres
);
405 nsIDOMComment_GetParentNode(nscomment
, &nsparent
);
407 nsIDOMNode_RemoveChild(nsparent
, (nsIDOMNode
*)nscomment
, &tmp
);
408 nsIDOMNode_Release(nsparent
);
409 nsIDOMNode_Release(tmp
);
413 nsIDOMComment_Release(nscomment
);
417 case MUTATION_ENDLOAD
:
418 handle_end_load(This
);
421 case MUTATION_SCRIPT
: {
422 nsIDOMHTMLScriptElement
*nsscript
;
424 nsres
= nsISupports_QueryInterface(This
->mutation_queue
->nsiface
, &IID_nsIDOMHTMLScriptElement
,
426 if(NS_FAILED(nsres
)) {
427 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres
);
431 doc_insert_script(This
->basedoc
.window
, nsscript
);
432 nsIDOMHTMLScriptElement_Release(nsscript
);
437 ERR("invalid type %d\n", This
->mutation_queue
->type
);
440 pop_mutation_queue(This
);
446 #undef NSRUNNABLE_THIS
448 static const nsIRunnableVtbl nsRunnableVtbl
= {
449 nsRunnable_QueryInterface
,
455 #define NSDOCOBS_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IDocumentObserver, iface)
457 static nsresult NSAPI
nsDocumentObserver_QueryInterface(nsIDocumentObserver
*iface
,
458 nsIIDRef riid
, nsQIResult result
)
460 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
462 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
463 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
464 *result
= NSDOCOBS(This
);
465 }else if(IsEqualGUID(&IID_nsIMutationObserver
, riid
)) {
466 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This
, result
);
467 *result
= NSDOCOBS(This
);
468 }else if(IsEqualGUID(&IID_nsIDocumentObserver
, riid
)) {
469 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This
, result
);
470 *result
= NSDOCOBS(This
);
473 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
474 return NS_NOINTERFACE
;
477 htmldoc_addref(&This
->basedoc
);
481 static nsrefcnt NSAPI
nsDocumentObserver_AddRef(nsIDocumentObserver
*iface
)
483 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
484 return htmldoc_addref(&This
->basedoc
);
487 static nsrefcnt NSAPI
nsDocumentObserver_Release(nsIDocumentObserver
*iface
)
489 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
490 return htmldoc_release(&This
->basedoc
);
493 static void NSAPI
nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver
*iface
,
494 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
498 static void NSAPI
nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver
*iface
,
499 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
503 static void NSAPI
nsDocumentObserver_AttributeWillChange(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
504 nsIContent
*aContent
, PRInt32 aNameSpaceID
, nsIAtom
*aAttribute
, PRInt32 aModType
)
508 static void NSAPI
nsDocumentObserver_AttributeChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
509 nsIContent
*aContent
, PRInt32 aNameSpaceID
, nsIAtom
*aAttribute
, PRInt32 aModType
, PRUint32 aStateMask
)
513 static void NSAPI
nsDocumentObserver_ContentAppended(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
514 nsIContent
*aContainer
, PRInt32 aNewIndexInContainer
)
518 static void NSAPI
nsDocumentObserver_ContentInserted(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
519 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
)
523 static void NSAPI
nsDocumentObserver_ContentRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
524 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
)
528 static void NSAPI
nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver
*iface
, const nsINode
*aNode
)
532 static void NSAPI
nsDocumentObserver_ParentChainChanged(nsIDocumentObserver
*iface
, nsIContent
*aContent
)
536 static void NSAPI
nsDocumentObserver_BeginUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
537 nsUpdateType aUpdateType
)
541 static void NSAPI
nsDocumentObserver_EndUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
542 nsUpdateType aUpdateType
)
546 static void NSAPI
nsDocumentObserver_BeginLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
550 static void NSAPI
nsDocumentObserver_EndLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
552 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
556 This
->content_ready
= TRUE
;
557 push_mutation_queue(This
, MUTATION_ENDLOAD
, NULL
);
560 static void NSAPI
nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
561 nsIContent
*aContent1
, nsIContent
*aContent2
, PRInt32 aStateMask
)
565 static void NSAPI
nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
566 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
570 static void NSAPI
nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
571 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
575 static void NSAPI
nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver
*iface
,
576 nsIDocument
*aDocument
, nsIStyleSheet
*aStyleSheet
, PRBool aApplicable
)
580 static void NSAPI
nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
581 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aOldStyleRule
, nsIStyleSheet
*aNewStyleRule
)
585 static void NSAPI
nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
586 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
590 static void NSAPI
nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
591 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
595 static void NSAPI
nsDocumentObserver_BindToDocument(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
596 nsIContent
*aContent
)
598 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
599 nsIDOMHTMLIFrameElement
*nsiframe
;
600 nsIDOMHTMLFrameElement
*nsframe
;
601 nsIDOMComment
*nscomment
;
602 nsIDOMElement
*nselem
;
605 TRACE("(%p)\n", This
);
607 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMElement
, (void**)&nselem
);
608 if(NS_SUCCEEDED(nsres
)) {
609 check_event_attr(This
, nselem
);
610 nsIDOMElement_Release(nselem
);
613 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMComment
, (void**)&nscomment
);
614 if(NS_SUCCEEDED(nsres
)) {
615 TRACE("comment node\n");
617 push_mutation_queue(This
, MUTATION_COMMENT
, (nsISupports
*)nscomment
);
618 nsIDOMComment_Release(nscomment
);
621 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLIFrameElement
, (void**)&nsiframe
);
622 if(NS_SUCCEEDED(nsres
)) {
623 TRACE("iframe node\n");
625 push_mutation_queue(This
, MUTATION_BINDTOTREE
, (nsISupports
*)nsiframe
);
626 nsIDOMHTMLIFrameElement_Release(nsiframe
);
629 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLFrameElement
, (void**)&nsframe
);
630 if(NS_SUCCEEDED(nsres
)) {
631 TRACE("frame node\n");
633 push_mutation_queue(This
, MUTATION_BINDTOTREE
, (nsISupports
*)nsframe
);
634 nsIDOMHTMLFrameElement_Release(nsframe
);
638 static void NSAPI
nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver
*iface
, nsIContent
*aContent
,
639 PRBool aHaveNotified
)
641 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
642 nsIDOMHTMLScriptElement
*nsscript
;
645 TRACE("(%p)->(%p %x)\n", This
, aContent
, aHaveNotified
);
647 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
648 if(NS_SUCCEEDED(nsres
)) {
649 TRACE("script node\n");
651 push_mutation_queue(This
, MUTATION_SCRIPT
, (nsISupports
*)nsscript
);
652 nsIDOMHTMLScriptElement_Release(nsscript
);
656 #undef NSMUTATIONOBS_THIS
658 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl
= {
659 nsDocumentObserver_QueryInterface
,
660 nsDocumentObserver_AddRef
,
661 nsDocumentObserver_Release
,
662 nsDocumentObserver_CharacterDataWillChange
,
663 nsDocumentObserver_CharacterDataChanged
,
664 nsDocumentObserver_AttributeWillChange
,
665 nsDocumentObserver_AttributeChanged
,
666 nsDocumentObserver_ContentAppended
,
667 nsDocumentObserver_ContentInserted
,
668 nsDocumentObserver_ContentRemoved
,
669 nsDocumentObserver_NodeWillBeDestroyed
,
670 nsDocumentObserver_ParentChainChanged
,
671 nsDocumentObserver_BeginUpdate
,
672 nsDocumentObserver_EndUpdate
,
673 nsDocumentObserver_BeginLoad
,
674 nsDocumentObserver_EndLoad
,
675 nsDocumentObserver_ContentStatesChanged
,
676 nsDocumentObserver_StyleSheetAdded
,
677 nsDocumentObserver_StyleSheetRemoved
,
678 nsDocumentObserver_StyleSheetApplicableStateChanged
,
679 nsDocumentObserver_StyleRuleChanged
,
680 nsDocumentObserver_StyleRuleAdded
,
681 nsDocumentObserver_StyleRuleRemoved
,
682 nsDocumentObserver_BindToDocument
,
683 nsDocumentObserver_DoneAddingChildren
686 void init_mutation(HTMLDocumentNode
*doc
)
688 nsIDOMNSDocument
*nsdoc
;
691 doc
->lpIDocumentObserverVtbl
= &nsDocumentObserverVtbl
;
692 doc
->lpIRunnableVtbl
= &nsRunnableVtbl
;
694 nsres
= nsIDOMHTMLDocument_QueryInterface(doc
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
695 if(NS_FAILED(nsres
)) {
696 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
700 nsIDOMNSDocument_WineAddObserver(nsdoc
, NSDOCOBS(doc
));
701 nsIDOMNSDocument_Release(nsdoc
);
704 void release_mutation(HTMLDocumentNode
*doc
)
706 nsIDOMNSDocument
*nsdoc
;
709 nsres
= nsIDOMHTMLDocument_QueryInterface(doc
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
710 if(NS_FAILED(nsres
)) {
711 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
715 nsIDOMNSDocument_WineRemoveObserver(nsdoc
, NSDOCOBS(doc
));
716 nsIDOMNSDocument_Release(nsdoc
);