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
19 #define WIN32_NO_STATUS
21 #define COM_NO_WINDOWS_H
31 //#include "winuser.h"
36 #include "mshtml_private.h"
37 #include "htmlevent.h"
39 #include <wine/debug.h>
41 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
50 #define IE_MAJOR_VERSION 7
51 #define IE_MINOR_VERSION 0
53 static BOOL
handle_insert_comment(HTMLDocumentNode
*doc
, const PRUnichar
*comment
)
56 int majorv
= 0, minorv
= 0;
57 const PRUnichar
*ptr
, *end
;
70 static const PRUnichar endifW
[] = {'<','!','[','e','n','d','i','f',']'};
72 if(comment
[0] != '[' || comment
[1] != 'i' || comment
[2] != 'f')
79 if(ptr
[0] == 'l' && ptr
[1] == 't') {
87 }else if(ptr
[0] == 'g' && ptr
[1] == 't') {
102 if(ptr
[0] != 'I' || ptr
[1] != 'E')
106 if(!isspaceW(*ptr
++))
108 while(isspaceW(*ptr
))
113 while(isdigitW(*ptr
))
114 majorv
= majorv
*10 + (*ptr
++ - '0');
120 while(isdigitW(*ptr
))
121 minorv
= minorv
*10 + (*ptr
++ - '0');
124 while(isspaceW(*ptr
))
126 if(ptr
[0] != ']' || ptr
[1] != '>')
131 if(len
< sizeof(endifW
)/sizeof(WCHAR
))
134 end
= ptr
+ len
-sizeof(endifW
)/sizeof(WCHAR
);
135 if(memcmp(end
, endifW
, sizeof(endifW
)))
140 if(majorv
== IE_MAJOR_VERSION
&& minorv
== IE_MINOR_VERSION
)
144 if(majorv
> IE_MAJOR_VERSION
)
146 if(majorv
== IE_MAJOR_VERSION
&& minorv
> IE_MINOR_VERSION
)
150 if(majorv
> IE_MAJOR_VERSION
)
152 if(majorv
== IE_MAJOR_VERSION
&& minorv
>= IE_MINOR_VERSION
)
156 if(majorv
< IE_MAJOR_VERSION
)
158 if(majorv
== IE_MAJOR_VERSION
&& minorv
< IE_MINOR_VERSION
)
162 if(majorv
< IE_MAJOR_VERSION
)
164 if(majorv
== IE_MAJOR_VERSION
&& minorv
<= IE_MINOR_VERSION
)
169 buf
= heap_alloc((end
-ptr
+1)*sizeof(WCHAR
));
173 memcpy(buf
, ptr
, (end
-ptr
)*sizeof(WCHAR
));
175 nsAString_InitDepend(&nsstr
, buf
);
177 /* FIXME: Find better way to insert HTML to document. */
178 nsres
= nsIDOMHTMLDocument_Write(doc
->nsdoc
, &nsstr
);
179 nsAString_Finish(&nsstr
);
181 if(NS_FAILED(nsres
)) {
182 ERR("Write failed: %08x\n", nsres
);
189 static void add_script_runner(HTMLDocumentNode
*This
)
191 nsIDOMNSDocument
*nsdoc
;
194 nsres
= nsIDOMHTMLDocument_QueryInterface(This
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
195 if(NS_FAILED(nsres
)) {
196 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
200 nsIDOMNSDocument_WineAddScriptRunner(nsdoc
, NSRUNNABLE(This
));
201 nsIDOMNSDocument_Release(nsdoc
);
204 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IRunnable, iface)
206 static nsresult NSAPI
nsRunnable_QueryInterface(nsIRunnable
*iface
,
207 nsIIDRef riid
, nsQIResult result
)
209 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
211 if(IsEqualGUID(riid
, &IID_nsISupports
)) {
212 TRACE("(%p)->(IID_nsISupports %p)\n", This
, result
);
213 *result
= NSRUNNABLE(This
);
214 }else if(IsEqualGUID(riid
, &IID_nsIRunnable
)) {
215 TRACE("(%p)->(IID_nsIRunnable %p)\n", This
, result
);
216 *result
= NSRUNNABLE(This
);
219 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
220 return NS_NOINTERFACE
;
223 nsISupports_AddRef((nsISupports
*)*result
);
227 static nsrefcnt NSAPI
nsRunnable_AddRef(nsIRunnable
*iface
)
229 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
230 return htmldoc_addref(&This
->basedoc
);
233 static nsrefcnt NSAPI
nsRunnable_Release(nsIRunnable
*iface
)
235 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
236 return htmldoc_release(&This
->basedoc
);
239 static void push_mutation_queue(HTMLDocumentNode
*doc
, DWORD type
, nsISupports
*nsiface
)
241 mutation_queue_t
*elem
;
243 elem
= heap_alloc(sizeof(mutation_queue_t
));
249 elem
->nsiface
= nsiface
;
251 nsISupports_AddRef(nsiface
);
253 if(doc
->mutation_queue_tail
) {
254 doc
->mutation_queue_tail
= doc
->mutation_queue_tail
->next
= elem
;
256 doc
->mutation_queue
= doc
->mutation_queue_tail
= elem
;
257 add_script_runner(doc
);
261 static void pop_mutation_queue(HTMLDocumentNode
*doc
)
263 mutation_queue_t
*tmp
= doc
->mutation_queue
;
268 doc
->mutation_queue
= tmp
->next
;
270 doc
->mutation_queue_tail
= NULL
;
273 nsISupports_Release(tmp
->nsiface
);
277 static void bind_to_tree(HTMLDocumentNode
*doc
, nsISupports
*nsiface
)
283 nsres
= nsISupports_QueryInterface(nsiface
, &IID_nsIDOMNode
, (void**)&nsnode
);
287 node
= get_node(doc
, nsnode
, TRUE
);
288 nsIDOMNode_Release(nsnode
);
290 ERR("Could not get node\n");
294 if(node
->vtbl
->bind_to_tree
)
295 node
->vtbl
->bind_to_tree(node
);
298 /* Calls undocumented 69 cmd of CGID_Explorer */
299 static void call_explorer_69(HTMLDocumentObj
*doc
)
301 IOleCommandTarget
*olecmd
;
308 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
313 hres
= IOleCommandTarget_Exec(olecmd
, &CGID_Explorer
, 69, 0, NULL
, &var
);
314 IOleCommandTarget_Release(olecmd
);
315 if(SUCCEEDED(hres
) && V_VT(&var
) != VT_NULL
)
316 FIXME("handle result\n");
319 static void parse_complete_proc(task_t
*task
)
321 HTMLDocumentObj
*doc
= ((docobj_task_t
*)task
)->doc
;
323 TRACE("(%p)\n", doc
);
325 if(doc
->usermode
== EDITMODE
)
326 init_editor(&doc
->basedoc
);
328 call_explorer_69(doc
);
330 IAdviseSink_OnViewChange(doc
->view_sink
, DVASPECT_CONTENT
, -1);
331 call_property_onchanged(&doc
->basedoc
.cp_propnotif
, 1005);
332 call_explorer_69(doc
);
334 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
336 set_ready_state(doc
->basedoc
.window
, READYSTATE_INTERACTIVE
);
339 static void handle_end_load(HTMLDocumentNode
*This
)
345 if(!This
->basedoc
.doc_obj
)
348 if(This
!= This
->basedoc
.doc_obj
->basedoc
.doc_node
) {
349 set_ready_state(This
->basedoc
.window
, READYSTATE_INTERACTIVE
);
353 task
= heap_alloc(sizeof(docobj_task_t
));
357 task
->doc
= This
->basedoc
.doc_obj
;
360 * This should be done in the worker thread that parses HTML,
361 * but we don't have such thread (Gecko parses HTML for us).
363 push_task(&task
->header
, &parse_complete_proc
, This
->basedoc
.doc_obj
->basedoc
.task_magic
);
366 static nsresult NSAPI
nsRunnable_Run(nsIRunnable
*iface
)
368 HTMLDocumentNode
*This
= NSRUNNABLE_THIS(iface
);
371 TRACE("(%p)\n", This
);
373 while(This
->mutation_queue
) {
374 switch(This
->mutation_queue
->type
) {
375 case MUTATION_BINDTOTREE
:
376 bind_to_tree(This
, This
->mutation_queue
->nsiface
);
379 case MUTATION_COMMENT
: {
380 nsIDOMComment
*nscomment
;
381 nsAString comment_str
;
382 BOOL remove_comment
= FALSE
;
384 nsres
= nsISupports_QueryInterface(This
->mutation_queue
->nsiface
, &IID_nsIDOMComment
, (void**)&nscomment
);
385 if(NS_FAILED(nsres
)) {
386 ERR("Could not get nsIDOMComment iface:%08x\n", nsres
);
390 nsAString_Init(&comment_str
, NULL
);
391 nsres
= nsIDOMComment_GetData(nscomment
, &comment_str
);
392 if(NS_SUCCEEDED(nsres
)) {
393 const PRUnichar
*comment
;
395 nsAString_GetData(&comment_str
, &comment
);
396 remove_comment
= handle_insert_comment(This
, comment
);
399 nsAString_Finish(&comment_str
);
402 nsIDOMNode
*nsparent
, *tmp
;
405 static const PRUnichar remove_comment_magicW
[] =
406 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
408 nsAString_InitDepend(&magic_str
, remove_comment_magicW
);
409 nsres
= nsIDOMComment_SetData(nscomment
, &magic_str
);
410 nsAString_Finish(&magic_str
);
412 ERR("SetData failed: %08x\n", nsres
);
414 nsIDOMComment_GetParentNode(nscomment
, &nsparent
);
416 nsIDOMNode_RemoveChild(nsparent
, (nsIDOMNode
*)nscomment
, &tmp
);
417 nsIDOMNode_Release(nsparent
);
418 nsIDOMNode_Release(tmp
);
422 nsIDOMComment_Release(nscomment
);
426 case MUTATION_ENDLOAD
:
427 handle_end_load(This
);
430 case MUTATION_SCRIPT
: {
431 nsIDOMHTMLScriptElement
*nsscript
;
433 nsres
= nsISupports_QueryInterface(This
->mutation_queue
->nsiface
, &IID_nsIDOMHTMLScriptElement
,
435 if(NS_FAILED(nsres
)) {
436 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres
);
440 doc_insert_script(This
->basedoc
.window
, nsscript
);
441 nsIDOMHTMLScriptElement_Release(nsscript
);
446 ERR("invalid type %d\n", This
->mutation_queue
->type
);
449 pop_mutation_queue(This
);
455 #undef NSRUNNABLE_THIS
457 static const nsIRunnableVtbl nsRunnableVtbl
= {
458 nsRunnable_QueryInterface
,
464 #define NSDOCOBS_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IDocumentObserver, iface)
466 static nsresult NSAPI
nsDocumentObserver_QueryInterface(nsIDocumentObserver
*iface
,
467 nsIIDRef riid
, nsQIResult result
)
469 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
471 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
472 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
473 *result
= NSDOCOBS(This
);
474 }else if(IsEqualGUID(&IID_nsIMutationObserver
, riid
)) {
475 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This
, result
);
476 *result
= NSDOCOBS(This
);
477 }else if(IsEqualGUID(&IID_nsIDocumentObserver
, riid
)) {
478 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This
, result
);
479 *result
= NSDOCOBS(This
);
482 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
483 return NS_NOINTERFACE
;
486 htmldoc_addref(&This
->basedoc
);
490 static nsrefcnt NSAPI
nsDocumentObserver_AddRef(nsIDocumentObserver
*iface
)
492 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
493 return htmldoc_addref(&This
->basedoc
);
496 static nsrefcnt NSAPI
nsDocumentObserver_Release(nsIDocumentObserver
*iface
)
498 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
499 return htmldoc_release(&This
->basedoc
);
502 static void NSAPI
nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver
*iface
,
503 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
507 static void NSAPI
nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver
*iface
,
508 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
512 static void NSAPI
nsDocumentObserver_AttributeWillChange(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
513 nsIContent
*aContent
, PRInt32 aNameSpaceID
, nsIAtom
*aAttribute
, PRInt32 aModType
)
517 static void NSAPI
nsDocumentObserver_AttributeChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
518 nsIContent
*aContent
, PRInt32 aNameSpaceID
, nsIAtom
*aAttribute
, PRInt32 aModType
, PRUint32 aStateMask
)
522 static void NSAPI
nsDocumentObserver_ContentAppended(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
523 nsIContent
*aContainer
, PRInt32 aNewIndexInContainer
)
527 static void NSAPI
nsDocumentObserver_ContentInserted(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
528 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
)
532 static void NSAPI
nsDocumentObserver_ContentRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
533 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
)
537 static void NSAPI
nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver
*iface
, const nsINode
*aNode
)
541 static void NSAPI
nsDocumentObserver_ParentChainChanged(nsIDocumentObserver
*iface
, nsIContent
*aContent
)
545 static void NSAPI
nsDocumentObserver_BeginUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
546 nsUpdateType aUpdateType
)
550 static void NSAPI
nsDocumentObserver_EndUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
551 nsUpdateType aUpdateType
)
555 static void NSAPI
nsDocumentObserver_BeginLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
559 static void NSAPI
nsDocumentObserver_EndLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
561 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
565 This
->content_ready
= TRUE
;
566 push_mutation_queue(This
, MUTATION_ENDLOAD
, NULL
);
569 static void NSAPI
nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
570 nsIContent
*aContent1
, nsIContent
*aContent2
, PRInt32 aStateMask
)
574 static void NSAPI
nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
575 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
579 static void NSAPI
nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
580 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
584 static void NSAPI
nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver
*iface
,
585 nsIDocument
*aDocument
, nsIStyleSheet
*aStyleSheet
, PRBool aApplicable
)
589 static void NSAPI
nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
590 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aOldStyleRule
, nsIStyleSheet
*aNewStyleRule
)
594 static void NSAPI
nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
595 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
599 static void NSAPI
nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
600 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
604 static void NSAPI
nsDocumentObserver_BindToDocument(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
605 nsIContent
*aContent
)
607 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
608 nsIDOMHTMLIFrameElement
*nsiframe
;
609 nsIDOMHTMLFrameElement
*nsframe
;
610 nsIDOMComment
*nscomment
;
611 nsIDOMElement
*nselem
;
614 TRACE("(%p)\n", This
);
616 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMElement
, (void**)&nselem
);
617 if(NS_SUCCEEDED(nsres
)) {
618 check_event_attr(This
, nselem
);
619 nsIDOMElement_Release(nselem
);
622 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMComment
, (void**)&nscomment
);
623 if(NS_SUCCEEDED(nsres
)) {
624 TRACE("comment node\n");
626 push_mutation_queue(This
, MUTATION_COMMENT
, (nsISupports
*)nscomment
);
627 nsIDOMComment_Release(nscomment
);
630 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLIFrameElement
, (void**)&nsiframe
);
631 if(NS_SUCCEEDED(nsres
)) {
632 TRACE("iframe node\n");
634 push_mutation_queue(This
, MUTATION_BINDTOTREE
, (nsISupports
*)nsiframe
);
635 nsIDOMHTMLIFrameElement_Release(nsiframe
);
638 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLFrameElement
, (void**)&nsframe
);
639 if(NS_SUCCEEDED(nsres
)) {
640 TRACE("frame node\n");
642 push_mutation_queue(This
, MUTATION_BINDTOTREE
, (nsISupports
*)nsframe
);
643 nsIDOMHTMLFrameElement_Release(nsframe
);
647 static void NSAPI
nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver
*iface
, nsIContent
*aContent
,
648 PRBool aHaveNotified
)
650 HTMLDocumentNode
*This
= NSDOCOBS_THIS(iface
);
651 nsIDOMHTMLScriptElement
*nsscript
;
654 TRACE("(%p)->(%p %x)\n", This
, aContent
, aHaveNotified
);
656 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
657 if(NS_SUCCEEDED(nsres
)) {
658 TRACE("script node\n");
660 push_mutation_queue(This
, MUTATION_SCRIPT
, (nsISupports
*)nsscript
);
661 nsIDOMHTMLScriptElement_Release(nsscript
);
665 #undef NSMUTATIONOBS_THIS
667 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl
= {
668 nsDocumentObserver_QueryInterface
,
669 nsDocumentObserver_AddRef
,
670 nsDocumentObserver_Release
,
671 nsDocumentObserver_CharacterDataWillChange
,
672 nsDocumentObserver_CharacterDataChanged
,
673 nsDocumentObserver_AttributeWillChange
,
674 nsDocumentObserver_AttributeChanged
,
675 nsDocumentObserver_ContentAppended
,
676 nsDocumentObserver_ContentInserted
,
677 nsDocumentObserver_ContentRemoved
,
678 nsDocumentObserver_NodeWillBeDestroyed
,
679 nsDocumentObserver_ParentChainChanged
,
680 nsDocumentObserver_BeginUpdate
,
681 nsDocumentObserver_EndUpdate
,
682 nsDocumentObserver_BeginLoad
,
683 nsDocumentObserver_EndLoad
,
684 nsDocumentObserver_ContentStatesChanged
,
685 nsDocumentObserver_StyleSheetAdded
,
686 nsDocumentObserver_StyleSheetRemoved
,
687 nsDocumentObserver_StyleSheetApplicableStateChanged
,
688 nsDocumentObserver_StyleRuleChanged
,
689 nsDocumentObserver_StyleRuleAdded
,
690 nsDocumentObserver_StyleRuleRemoved
,
691 nsDocumentObserver_BindToDocument
,
692 nsDocumentObserver_DoneAddingChildren
695 void init_mutation(HTMLDocumentNode
*doc
)
697 nsIDOMNSDocument
*nsdoc
;
700 doc
->lpIDocumentObserverVtbl
= &nsDocumentObserverVtbl
;
701 doc
->lpIRunnableVtbl
= &nsRunnableVtbl
;
703 nsres
= nsIDOMHTMLDocument_QueryInterface(doc
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
704 if(NS_FAILED(nsres
)) {
705 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
709 nsIDOMNSDocument_WineAddObserver(nsdoc
, NSDOCOBS(doc
));
710 nsIDOMNSDocument_Release(nsdoc
);
713 void release_mutation(HTMLDocumentNode
*doc
)
715 nsIDOMNSDocument
*nsdoc
;
718 nsres
= nsIDOMHTMLDocument_QueryInterface(doc
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
719 if(NS_FAILED(nsres
)) {
720 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
724 nsIDOMNSDocument_WineRemoveObserver(nsdoc
, NSDOCOBS(doc
));
725 nsIDOMNSDocument_Release(nsdoc
);