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
);
39 #define IE_MAJOR_VERSION 7
40 #define IE_MINOR_VERSION 0
42 static BOOL
handle_insert_comment(HTMLDocumentNode
*doc
, const PRUnichar
*comment
)
45 int majorv
= 0, minorv
= 0;
46 const PRUnichar
*ptr
, *end
;
59 static const PRUnichar endifW
[] = {'<','!','[','e','n','d','i','f',']'};
61 if(comment
[0] != '[' || comment
[1] != 'i' || comment
[2] != 'f')
68 if(ptr
[0] == 'l' && ptr
[1] == 't') {
76 }else if(ptr
[0] == 'g' && ptr
[1] == 't') {
91 if(ptr
[0] != 'I' || ptr
[1] != 'E')
102 while(isdigitW(*ptr
))
103 majorv
= majorv
*10 + (*ptr
++ - '0');
109 while(isdigitW(*ptr
))
110 minorv
= minorv
*10 + (*ptr
++ - '0');
113 while(isspaceW(*ptr
))
115 if(ptr
[0] != ']' || ptr
[1] != '>')
120 if(len
< sizeof(endifW
)/sizeof(WCHAR
))
123 end
= ptr
+ len
-sizeof(endifW
)/sizeof(WCHAR
);
124 if(memcmp(end
, endifW
, sizeof(endifW
)))
129 if(majorv
== IE_MAJOR_VERSION
&& minorv
== IE_MINOR_VERSION
)
133 if(majorv
> IE_MAJOR_VERSION
)
135 if(majorv
== IE_MAJOR_VERSION
&& minorv
> IE_MINOR_VERSION
)
139 if(majorv
> IE_MAJOR_VERSION
)
141 if(majorv
== IE_MAJOR_VERSION
&& minorv
>= IE_MINOR_VERSION
)
145 if(majorv
< IE_MAJOR_VERSION
)
147 if(majorv
== IE_MAJOR_VERSION
&& minorv
< IE_MINOR_VERSION
)
151 if(majorv
< IE_MAJOR_VERSION
)
153 if(majorv
== IE_MAJOR_VERSION
&& minorv
<= IE_MINOR_VERSION
)
158 buf
= heap_alloc((end
-ptr
+1)*sizeof(WCHAR
));
162 memcpy(buf
, ptr
, (end
-ptr
)*sizeof(WCHAR
));
164 nsAString_InitDepend(&nsstr
, buf
);
166 /* FIXME: Find better way to insert HTML to document. */
167 nsres
= nsIDOMHTMLDocument_Write(doc
->nsdoc
, &nsstr
);
168 nsAString_Finish(&nsstr
);
170 if(NS_FAILED(nsres
)) {
171 ERR("Write failed: %08x\n", nsres
);
178 static nsresult
run_insert_comment(HTMLDocumentNode
*doc
, nsISupports
*comment_iface
, nsISupports
*arg2
)
180 const PRUnichar
*comment
;
181 nsIDOMComment
*nscomment
;
182 nsAString comment_str
;
186 nsres
= nsISupports_QueryInterface(comment_iface
, &IID_nsIDOMComment
, (void**)&nscomment
);
187 if(NS_FAILED(nsres
)) {
188 ERR("Could not get nsIDOMComment iface:%08x\n", nsres
);
192 nsAString_Init(&comment_str
, NULL
);
193 nsres
= nsIDOMComment_GetData(nscomment
, &comment_str
);
197 nsAString_GetData(&comment_str
, &comment
);
198 remove_comment
= handle_insert_comment(doc
, comment
);
199 nsAString_Finish(&comment_str
);
202 nsIDOMNode
*nsparent
, *tmp
;
205 static const PRUnichar remove_comment_magicW
[] =
206 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
208 nsAString_InitDepend(&magic_str
, remove_comment_magicW
);
209 nsres
= nsIDOMComment_SetData(nscomment
, &magic_str
);
210 nsAString_Finish(&magic_str
);
212 ERR("SetData failed: %08x\n", nsres
);
214 nsIDOMComment_GetParentNode(nscomment
, &nsparent
);
216 nsIDOMNode_RemoveChild(nsparent
, (nsIDOMNode
*)nscomment
, &tmp
);
217 nsIDOMNode_Release(nsparent
);
218 nsIDOMNode_Release(tmp
);
222 nsIDOMComment_Release(nscomment
);
226 static nsresult
run_bind_to_tree(HTMLDocumentNode
*doc
, nsISupports
*nsiface
, nsISupports
*arg2
)
233 TRACE("(%p)->(%p)\n", doc
, nsiface
);
235 nsres
= nsISupports_QueryInterface(nsiface
, &IID_nsIDOMNode
, (void**)&nsnode
);
239 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
240 nsIDOMNode_Release(nsnode
);
242 ERR("Could not get node\n");
246 if(node
->vtbl
->bind_to_tree
)
247 node
->vtbl
->bind_to_tree(node
);
252 /* Calls undocumented 69 cmd of CGID_Explorer */
253 static void call_explorer_69(HTMLDocumentObj
*doc
)
255 IOleCommandTarget
*olecmd
;
262 hres
= IOleClientSite_QueryInterface(doc
->client
, &IID_IOleCommandTarget
, (void**)&olecmd
);
267 hres
= IOleCommandTarget_Exec(olecmd
, &CGID_Explorer
, 69, 0, NULL
, &var
);
268 IOleCommandTarget_Release(olecmd
);
269 if(SUCCEEDED(hres
) && V_VT(&var
) != VT_NULL
)
270 FIXME("handle result\n");
273 static void parse_complete(HTMLDocumentObj
*doc
)
275 TRACE("(%p)\n", doc
);
277 if(doc
->usermode
== EDITMODE
)
278 init_editor(&doc
->basedoc
);
280 call_explorer_69(doc
);
282 IAdviseSink_OnViewChange(doc
->view_sink
, DVASPECT_CONTENT
, -1);
283 call_property_onchanged(&doc
->basedoc
.cp_propnotif
, 1005);
284 call_explorer_69(doc
);
286 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
289 static nsresult
run_end_load(HTMLDocumentNode
*This
, nsISupports
*arg1
, nsISupports
*arg2
)
291 TRACE("(%p)\n", This
);
293 if(!This
->basedoc
.doc_obj
)
296 if(This
== This
->basedoc
.doc_obj
->basedoc
.doc_node
) {
298 * This should be done in the worker thread that parses HTML,
299 * but we don't have such thread (Gecko parses HTML for us).
301 parse_complete(This
->basedoc
.doc_obj
);
304 set_ready_state(This
->basedoc
.window
, READYSTATE_INTERACTIVE
);
308 static nsresult
run_insert_script(HTMLDocumentNode
*doc
, nsISupports
*script_iface
, nsISupports
*arg
)
310 nsIDOMHTMLScriptElement
*nsscript
;
313 TRACE("(%p)->(%p)\n", doc
, script_iface
);
315 nsres
= nsISupports_QueryInterface(script_iface
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
316 if(NS_FAILED(nsres
)) {
317 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres
);
321 doc_insert_script(doc
->basedoc
.window
, nsscript
);
322 nsIDOMHTMLScriptElement_Release(nsscript
);
326 typedef struct nsRunnable nsRunnable
;
328 typedef nsresult (*runnable_proc_t
)(HTMLDocumentNode
*,nsISupports
*,nsISupports
*);
331 nsIRunnable nsIRunnable_iface
;
335 runnable_proc_t proc
;
337 HTMLDocumentNode
*doc
;
342 static inline nsRunnable
*impl_from_nsIRunnable(nsIRunnable
*iface
)
344 return CONTAINING_RECORD(iface
, nsRunnable
, nsIRunnable_iface
);
347 static nsresult NSAPI
nsRunnable_QueryInterface(nsIRunnable
*iface
,
348 nsIIDRef riid
, void **result
)
350 nsRunnable
*This
= impl_from_nsIRunnable(iface
);
352 if(IsEqualGUID(riid
, &IID_nsISupports
)) {
353 TRACE("(%p)->(IID_nsISupports %p)\n", This
, result
);
354 *result
= &This
->nsIRunnable_iface
;
355 }else if(IsEqualGUID(riid
, &IID_nsIRunnable
)) {
356 TRACE("(%p)->(IID_nsIRunnable %p)\n", This
, result
);
357 *result
= &This
->nsIRunnable_iface
;
360 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
361 return NS_NOINTERFACE
;
364 nsISupports_AddRef((nsISupports
*)*result
);
368 static nsrefcnt NSAPI
nsRunnable_AddRef(nsIRunnable
*iface
)
370 nsRunnable
*This
= impl_from_nsIRunnable(iface
);
371 LONG ref
= InterlockedIncrement(&This
->ref
);
373 TRACE("(%p) ref=%d\n", This
, ref
);
378 static nsrefcnt NSAPI
nsRunnable_Release(nsIRunnable
*iface
)
380 nsRunnable
*This
= impl_from_nsIRunnable(iface
);
381 LONG ref
= InterlockedDecrement(&This
->ref
);
383 TRACE("(%p) ref=%d\n", This
, ref
);
386 htmldoc_release(&This
->doc
->basedoc
);
388 nsISupports_Release(This
->arg1
);
390 nsISupports_Release(This
->arg2
);
397 static nsresult NSAPI
nsRunnable_Run(nsIRunnable
*iface
)
399 nsRunnable
*This
= impl_from_nsIRunnable(iface
);
401 return This
->proc(This
->doc
, This
->arg1
, This
->arg2
);
404 static const nsIRunnableVtbl nsRunnableVtbl
= {
405 nsRunnable_QueryInterface
,
411 static void add_script_runner(HTMLDocumentNode
*This
, runnable_proc_t proc
, nsISupports
*arg1
, nsISupports
*arg2
)
413 nsIDOMNSDocument
*nsdoc
;
414 nsRunnable
*runnable
;
417 runnable
= heap_alloc_zero(sizeof(*runnable
));
421 runnable
->nsIRunnable_iface
.lpVtbl
= &nsRunnableVtbl
;
424 htmldoc_addref(&This
->basedoc
);
425 runnable
->doc
= This
;
426 runnable
->proc
= proc
;
429 nsISupports_AddRef(arg1
);
430 runnable
->arg1
= arg1
;
433 nsISupports_AddRef(arg2
);
434 runnable
->arg2
= arg2
;
436 nsres
= nsIDOMHTMLDocument_QueryInterface(This
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
437 if(NS_SUCCEEDED(nsres
)) {
438 nsIDOMNSDocument_WineAddScriptRunner(nsdoc
, &runnable
->nsIRunnable_iface
);
439 nsIDOMNSDocument_Release(nsdoc
);
441 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
444 nsIRunnable_Release(&runnable
->nsIRunnable_iface
);
447 static inline HTMLDocumentNode
*impl_from_nsIDocumentObserver(nsIDocumentObserver
*iface
)
449 return CONTAINING_RECORD(iface
, HTMLDocumentNode
, nsIDocumentObserver_iface
);
452 static nsresult NSAPI
nsDocumentObserver_QueryInterface(nsIDocumentObserver
*iface
,
453 nsIIDRef riid
, void **result
)
455 HTMLDocumentNode
*This
= impl_from_nsIDocumentObserver(iface
);
457 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
458 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
459 *result
= &This
->nsIDocumentObserver_iface
;
460 }else if(IsEqualGUID(&IID_nsIMutationObserver
, riid
)) {
461 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This
, result
);
462 *result
= &This
->nsIDocumentObserver_iface
;
463 }else if(IsEqualGUID(&IID_nsIDocumentObserver
, riid
)) {
464 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This
, result
);
465 *result
= &This
->nsIDocumentObserver_iface
;
468 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
469 return NS_NOINTERFACE
;
472 htmldoc_addref(&This
->basedoc
);
476 static nsrefcnt NSAPI
nsDocumentObserver_AddRef(nsIDocumentObserver
*iface
)
478 HTMLDocumentNode
*This
= impl_from_nsIDocumentObserver(iface
);
479 return htmldoc_addref(&This
->basedoc
);
482 static nsrefcnt NSAPI
nsDocumentObserver_Release(nsIDocumentObserver
*iface
)
484 HTMLDocumentNode
*This
= impl_from_nsIDocumentObserver(iface
);
485 return htmldoc_release(&This
->basedoc
);
488 static void NSAPI
nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver
*iface
,
489 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
493 static void NSAPI
nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver
*iface
,
494 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
498 static void NSAPI
nsDocumentObserver_AttributeWillChange(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
499 nsIContent
*aContent
, PRInt32 aNameSpaceID
, nsIAtom
*aAttribute
, PRInt32 aModType
)
503 static void NSAPI
nsDocumentObserver_AttributeChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
504 nsIContent
*aContent
, PRInt32 aNameSpaceID
, nsIAtom
*aAttribute
, PRInt32 aModType
)
508 static void NSAPI
nsDocumentObserver_ContentAppended(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
509 nsIContent
*aContainer
, nsIContent
*aFirstNewContent
, PRInt32 aNewIndexInContainer
)
513 static void NSAPI
nsDocumentObserver_ContentInserted(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
514 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
)
518 static void NSAPI
nsDocumentObserver_ContentRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
519 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
,
520 nsIContent
*aProviousSibling
)
524 static void NSAPI
nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver
*iface
, const nsINode
*aNode
)
528 static void NSAPI
nsDocumentObserver_ParentChainChanged(nsIDocumentObserver
*iface
, nsIContent
*aContent
)
532 static void NSAPI
nsDocumentObserver_BeginUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
533 nsUpdateType aUpdateType
)
537 static void NSAPI
nsDocumentObserver_EndUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
538 nsUpdateType aUpdateType
)
542 static void NSAPI
nsDocumentObserver_BeginLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
546 static void NSAPI
nsDocumentObserver_EndLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
548 HTMLDocumentNode
*This
= impl_from_nsIDocumentObserver(iface
);
552 if(This
->skip_mutation_notif
)
555 This
->content_ready
= TRUE
;
556 add_script_runner(This
, run_end_load
, NULL
, NULL
);
559 static void NSAPI
nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
560 nsIContent
*aContent1
, nsIContent
*aContent2
, PRInt32 aStateMask
)
564 static void NSAPI
nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
569 static void NSAPI
nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
570 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
574 static void NSAPI
nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
575 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
579 static void NSAPI
nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver
*iface
,
580 nsIDocument
*aDocument
, nsIStyleSheet
*aStyleSheet
, PRBool aApplicable
)
584 static void NSAPI
nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
585 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aOldStyleRule
, nsIStyleSheet
*aNewStyleRule
)
589 static void NSAPI
nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
590 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
594 static void NSAPI
nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
595 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
599 static void NSAPI
nsDocumentObserver_BindToDocument(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
600 nsIContent
*aContent
)
602 HTMLDocumentNode
*This
= impl_from_nsIDocumentObserver(iface
);
603 nsIDOMHTMLIFrameElement
*nsiframe
;
604 nsIDOMHTMLFrameElement
*nsframe
;
605 nsIDOMComment
*nscomment
;
606 nsIDOMElement
*nselem
;
609 TRACE("(%p)\n", This
);
611 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMElement
, (void**)&nselem
);
612 if(NS_SUCCEEDED(nsres
)) {
613 check_event_attr(This
, nselem
);
614 nsIDOMElement_Release(nselem
);
617 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMComment
, (void**)&nscomment
);
618 if(NS_SUCCEEDED(nsres
)) {
619 TRACE("comment node\n");
621 add_script_runner(This
, run_insert_comment
, (nsISupports
*)nscomment
, NULL
);
622 nsIDOMComment_Release(nscomment
);
625 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLIFrameElement
, (void**)&nsiframe
);
626 if(NS_SUCCEEDED(nsres
)) {
627 TRACE("iframe node\n");
629 add_script_runner(This
, run_bind_to_tree
, (nsISupports
*)nsiframe
, NULL
);
630 nsIDOMHTMLIFrameElement_Release(nsiframe
);
633 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLFrameElement
, (void**)&nsframe
);
634 if(NS_SUCCEEDED(nsres
)) {
635 TRACE("frame node\n");
637 add_script_runner(This
, run_bind_to_tree
, (nsISupports
*)nsframe
, NULL
);
638 nsIDOMHTMLFrameElement_Release(nsframe
);
642 static void NSAPI
nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver
*iface
, nsIContent
*aContent
,
643 PRBool aHaveNotified
)
645 HTMLDocumentNode
*This
= impl_from_nsIDocumentObserver(iface
);
646 nsIDOMHTMLScriptElement
*nsscript
;
649 TRACE("(%p)->(%p %x)\n", This
, aContent
, aHaveNotified
);
651 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
652 if(NS_SUCCEEDED(nsres
)) {
653 TRACE("script node\n");
655 add_script_runner(This
, run_insert_script
, (nsISupports
*)nsscript
, NULL
);
656 nsIDOMHTMLScriptElement_Release(nsscript
);
660 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl
= {
661 nsDocumentObserver_QueryInterface
,
662 nsDocumentObserver_AddRef
,
663 nsDocumentObserver_Release
,
664 nsDocumentObserver_CharacterDataWillChange
,
665 nsDocumentObserver_CharacterDataChanged
,
666 nsDocumentObserver_AttributeWillChange
,
667 nsDocumentObserver_AttributeChanged
,
668 nsDocumentObserver_ContentAppended
,
669 nsDocumentObserver_ContentInserted
,
670 nsDocumentObserver_ContentRemoved
,
671 nsDocumentObserver_NodeWillBeDestroyed
,
672 nsDocumentObserver_ParentChainChanged
,
673 nsDocumentObserver_BeginUpdate
,
674 nsDocumentObserver_EndUpdate
,
675 nsDocumentObserver_BeginLoad
,
676 nsDocumentObserver_EndLoad
,
677 nsDocumentObserver_ContentStatesChanged
,
678 nsDocumentObserver_DocumentStatesChanged
,
679 nsDocumentObserver_StyleSheetAdded
,
680 nsDocumentObserver_StyleSheetRemoved
,
681 nsDocumentObserver_StyleSheetApplicableStateChanged
,
682 nsDocumentObserver_StyleRuleChanged
,
683 nsDocumentObserver_StyleRuleAdded
,
684 nsDocumentObserver_StyleRuleRemoved
,
685 nsDocumentObserver_BindToDocument
,
686 nsDocumentObserver_DoneAddingChildren
689 void init_mutation(HTMLDocumentNode
*doc
)
691 nsIDOMNSDocument
*nsdoc
;
694 doc
->nsIDocumentObserver_iface
.lpVtbl
= &nsDocumentObserverVtbl
;
696 nsres
= nsIDOMHTMLDocument_QueryInterface(doc
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
697 if(NS_FAILED(nsres
)) {
698 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
702 nsIDOMNSDocument_WineAddObserver(nsdoc
, &doc
->nsIDocumentObserver_iface
);
703 nsIDOMNSDocument_Release(nsdoc
);
706 void release_mutation(HTMLDocumentNode
*doc
)
708 nsIDOMNSDocument
*nsdoc
;
711 nsres
= nsIDOMHTMLDocument_QueryInterface(doc
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
712 if(NS_FAILED(nsres
)) {
713 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
717 nsIDOMNSDocument_WineRemoveObserver(nsdoc
, &doc
->nsIDocumentObserver_iface
);
718 nsIDOMNSDocument_Release(nsdoc
);