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 #define IE_MAJOR_VERSION 7
59 #define IE_MINOR_VERSION 0
61 static BOOL
handle_insert_comment(HTMLDocument
*doc
, const PRUnichar
*comment
)
64 int majorv
= 0, minorv
= 0;
65 const PRUnichar
*ptr
, *end
;
78 static const PRUnichar endifW
[] = {'<','!','[','e','n','d','i','f',']'};
80 if(comment
[0] != '[' || comment
[1] != 'i' || comment
[2] != 'f')
87 if(ptr
[0] == 'l' && ptr
[1] == 't') {
95 }else if(ptr
[0] == 'g' && ptr
[1] == 't') {
105 if(!isspaceW(*ptr
++))
107 while(isspaceW(*ptr
))
110 if(ptr
[0] != 'I' || ptr
[1] != 'E')
114 if(!isspaceW(*ptr
++))
116 while(isspaceW(*ptr
))
121 while(isdigitW(*ptr
))
122 majorv
= majorv
*10 + (*ptr
++ - '0');
128 while(isdigitW(*ptr
))
129 minorv
= minorv
*10 + (*ptr
++ - '0');
132 while(isspaceW(*ptr
))
134 if(ptr
[0] != ']' || ptr
[1] != '>')
139 if(len
< sizeof(endifW
)/sizeof(WCHAR
))
142 end
= ptr
+ len
-sizeof(endifW
)/sizeof(WCHAR
);
143 if(memcmp(end
, endifW
, sizeof(endifW
)))
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
)
164 if(majorv
< IE_MAJOR_VERSION
)
166 if(majorv
== IE_MAJOR_VERSION
&& minorv
< IE_MINOR_VERSION
)
170 if(majorv
< IE_MAJOR_VERSION
)
172 if(majorv
== IE_MAJOR_VERSION
&& minorv
<= IE_MINOR_VERSION
)
177 buf
= heap_alloc((end
-ptr
+1)*sizeof(WCHAR
));
181 memcpy(buf
, ptr
, (end
-ptr
)*sizeof(WCHAR
));
183 nsAString_Init(&nsstr
, buf
);
186 /* FIXME: Find better way to insert HTML to document. */
187 nsres
= nsIDOMHTMLDocument_Write(doc
->nsdoc
, &nsstr
);
188 nsAString_Finish(&nsstr
);
189 if(NS_FAILED(nsres
)) {
190 ERR("Write failed: %08x\n", nsres
);
197 static void add_script_runner(NSContainer
*This
)
199 nsIDOMNSDocument
*nsdoc
;
202 nsres
= nsIDOMHTMLDocument_QueryInterface(This
->doc
->nsdoc
, &IID_nsIDOMNSDocument
, (void**)&nsdoc
);
203 if(NS_FAILED(nsres
)) {
204 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres
);
208 nsIDOMNSDocument_WineAddScriptRunner(nsdoc
, NSRUNNABLE(This
));
209 nsIDOMNSDocument_Release(nsdoc
);
212 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(NSContainer, Runnable, iface)
214 static nsresult NSAPI
nsRunnable_QueryInterface(nsIRunnable
*iface
,
215 nsIIDRef riid
, nsQIResult result
)
217 NSContainer
*This
= NSRUNNABLE_THIS(iface
);
219 if(IsEqualGUID(riid
, &IID_nsISupports
)) {
220 TRACE("(%p)->(IID_nsISupports %p)\n", This
, result
);
221 *result
= NSRUNNABLE(This
);
222 }else if(IsEqualGUID(riid
, &IID_nsIRunnable
)) {
223 TRACE("(%p)->(IID_nsIRunnable %p)\n", This
, result
);
224 *result
= NSRUNNABLE(This
);
227 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
228 return NS_NOINTERFACE
;
231 nsISupports_AddRef((nsISupports
*)*result
);
235 static nsrefcnt NSAPI
nsRunnable_AddRef(nsIRunnable
*iface
)
237 NSContainer
*This
= NSRUNNABLE_THIS(iface
);
238 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This
));
241 static nsrefcnt NSAPI
nsRunnable_Release(nsIRunnable
*iface
)
243 NSContainer
*This
= NSRUNNABLE_THIS(iface
);
244 return nsIWebBrowserChrome_Release(NSWBCHROME(This
));
247 static void pop_mutation_queue(NSContainer
*nscontainer
)
249 mutation_queue_t
*tmp
= nscontainer
->mutation_queue
;
254 nscontainer
->mutation_queue
= tmp
->next
;
256 nscontainer
->mutation_queue_tail
= NULL
;
258 nsISupports_Release(tmp
->nsiface
);
262 static nsresult NSAPI
nsRunnable_Run(nsIRunnable
*iface
)
264 NSContainer
*This
= NSRUNNABLE_THIS(iface
);
267 TRACE("(%p)\n", This
);
269 while(This
->mutation_queue
) {
270 switch(This
->mutation_queue
->type
) {
271 case MUTATION_COMMENT
: {
272 nsIDOMComment
*nscomment
;
273 nsAString comment_str
;
274 BOOL remove_comment
= FALSE
;
276 nsres
= nsISupports_QueryInterface(This
->mutation_queue
->nsiface
, &IID_nsIDOMComment
, (void**)&nscomment
);
277 if(NS_FAILED(nsres
)) {
278 ERR("Could not get nsIDOMComment iface:%08x\n", nsres
);
282 nsAString_Init(&comment_str
, NULL
);
283 nsres
= nsIDOMComment_GetData(nscomment
, &comment_str
);
284 if(NS_SUCCEEDED(nsres
)) {
285 const PRUnichar
*comment
;
287 nsAString_GetData(&comment_str
, &comment
);
288 remove_comment
= handle_insert_comment(This
->doc
, comment
);
291 nsAString_Finish(&comment_str
);
294 nsIDOMNode
*nsparent
, *tmp
;
297 static const PRUnichar remove_comment_magicW
[] =
298 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
300 nsAString_Init(&magic_str
, remove_comment_magicW
);
301 nsres
= nsIDOMComment_SetData(nscomment
, &magic_str
);
302 nsAString_Finish(&magic_str
);
304 ERR("SetData failed: %08x\n", nsres
);
306 nsIDOMComment_GetParentNode(nscomment
, &nsparent
);
308 nsIDOMNode_RemoveChild(nsparent
, (nsIDOMNode
*)nscomment
, &tmp
);
309 nsIDOMNode_Release(nsparent
);
310 nsIDOMNode_Release(tmp
);
314 nsIDOMComment_Release(nscomment
);
318 case MUTATION_SCRIPT
: {
319 nsIDOMHTMLScriptElement
*nsscript
;
321 nsres
= nsISupports_QueryInterface(This
->mutation_queue
->nsiface
, &IID_nsIDOMHTMLScriptElement
,
323 if(NS_FAILED(nsres
)) {
324 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres
);
328 doc_insert_script(This
->doc
, nsscript
);
329 nsIDOMHTMLScriptElement_Release(nsscript
);
334 ERR("invalid type %d\n", This
->mutation_queue
->type
);
337 pop_mutation_queue(This
);
343 #undef NSRUNNABLE_THIS
345 static const nsIRunnableVtbl nsRunnableVtbl
= {
346 nsRunnable_QueryInterface
,
352 #define NSDOCOBS_THIS(iface) DEFINE_THIS(NSContainer, DocumentObserver, iface)
354 static nsresult NSAPI
nsDocumentObserver_QueryInterface(nsIDocumentObserver
*iface
,
355 nsIIDRef riid
, nsQIResult result
)
357 NSContainer
*This
= NSDOCOBS_THIS(iface
);
359 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
360 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
361 *result
= NSWBCHROME(This
);
362 }else if(IsEqualGUID(&IID_nsIMutationObserver
, riid
)) {
363 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This
, result
);
364 *result
= NSDOCOBS(This
);
365 }else if(IsEqualGUID(&IID_nsIDocumentObserver
, riid
)) {
366 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This
, result
);
367 *result
= NSDOCOBS(This
);
370 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
371 return NS_NOINTERFACE
;
374 nsIWebBrowserChrome_AddRef(NSWBCHROME(This
));
378 static nsrefcnt NSAPI
nsDocumentObserver_AddRef(nsIDocumentObserver
*iface
)
380 NSContainer
*This
= NSDOCOBS_THIS(iface
);
381 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This
));
384 static nsrefcnt NSAPI
nsDocumentObserver_Release(nsIDocumentObserver
*iface
)
386 NSContainer
*This
= NSDOCOBS_THIS(iface
);
387 return nsIWebBrowserChrome_Release(NSWBCHROME(This
));
390 static void NSAPI
nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver
*iface
,
391 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
395 static void NSAPI
nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver
*iface
,
396 nsIDocument
*aDocument
, nsIContent
*aContent
, void /*CharacterDataChangeInfo*/ *aInfo
)
400 static void NSAPI
nsDocumentObserver_AttributeChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
401 nsIContent
*aContent
, PRInt32 aNameSpaceID
, nsIAtom
*aAttribute
, PRInt32 aModType
, PRUint32 aStateMask
)
405 static void NSAPI
nsDocumentObserver_ContentAppended(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
406 nsIContent
*aContainer
, PRInt32 aNewIndexInContainer
)
410 static void NSAPI
nsDocumentObserver_ContentInserted(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
411 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
)
415 static void NSAPI
nsDocumentObserver_ContentRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
416 nsIContent
*aContainer
, nsIContent
*aChild
, PRInt32 aIndexInContainer
)
420 static void NSAPI
nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver
*iface
, const nsINode
*aNode
)
424 static void NSAPI
nsDocumentObserver_ParentChainChanged(nsIDocumentObserver
*iface
, nsIContent
*aContent
)
428 static void NSAPI
nsDocumentObserver_BeginUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
429 nsUpdateType aUpdateType
)
433 static void NSAPI
nsDocumentObserver_EndUpdate(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
434 nsUpdateType aUpdateType
)
438 static void NSAPI
nsDocumentObserver_BeginLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
442 static void NSAPI
nsDocumentObserver_EndLoad(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
)
446 static void NSAPI
nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
447 nsIContent
*aContent1
, nsIContent
*aContent2
, PRInt32 aStateMask
)
451 static void NSAPI
nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
452 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
456 static void NSAPI
nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
457 nsIStyleSheet
*aStyleSheet
, PRBool aDocumentSheet
)
461 static void NSAPI
nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver
*iface
,
462 nsIDocument
*aDocument
, nsIStyleSheet
*aStyleSheet
, PRBool aApplicable
)
466 static void NSAPI
nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
467 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aOldStyleRule
, nsIStyleSheet
*aNewStyleRule
)
471 static void NSAPI
nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
472 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
476 static void NSAPI
nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
477 nsIStyleSheet
*aStyleSheet
, nsIStyleRule
*aStyleRule
)
481 static void push_mutation_queue(NSContainer
*nscontainer
, DWORD type
, nsISupports
*nsiface
)
483 mutation_queue_t
*elem
;
485 elem
= heap_alloc(sizeof(mutation_queue_t
));
491 elem
->nsiface
= nsiface
;
492 nsISupports_AddRef(nsiface
);
494 if(nscontainer
->mutation_queue_tail
)
495 nscontainer
->mutation_queue_tail
= nscontainer
->mutation_queue_tail
->next
= elem
;
497 nscontainer
->mutation_queue
= nscontainer
->mutation_queue_tail
= elem
;
500 static void NSAPI
nsDocumentObserver_BindToDocument(nsIDocumentObserver
*iface
, nsIDocument
*aDocument
,
501 nsIContent
*aContent
)
503 NSContainer
*This
= NSDOCOBS_THIS(iface
);
504 nsIDOMComment
*nscomment
;
505 nsIDOMElement
*nselem
;
508 TRACE("(%p)\n", This
);
510 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMElement
, (void**)&nselem
);
511 if(NS_SUCCEEDED(nsres
)) {
512 check_event_attr(This
->doc
, nselem
);
513 nsIDOMElement_Release(nselem
);
516 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMComment
, (void**)&nscomment
);
517 if(NS_SUCCEEDED(nsres
)) {
518 TRACE("comment node\n");
520 push_mutation_queue(This
, MUTATION_COMMENT
, (nsISupports
*)nscomment
);
521 nsIDOMComment_Release(nscomment
);
522 add_script_runner(This
);
526 static void NSAPI
nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver
*iface
, nsIContent
*aContent
,
527 PRBool aHaveNotified
)
529 NSContainer
*This
= NSDOCOBS_THIS(iface
);
530 nsIDOMHTMLScriptElement
*nsscript
;
533 TRACE("(%p)->(%p %x)\n", This
, aContent
, aHaveNotified
);
535 nsres
= nsISupports_QueryInterface(aContent
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
536 if(NS_SUCCEEDED(nsres
)) {
537 push_mutation_queue(This
, MUTATION_SCRIPT
, (nsISupports
*)nsscript
);
538 nsIDOMHTMLScriptElement_Release(nsscript
);
539 add_script_runner(This
);
543 #undef NSMUTATIONOBS_THIS
545 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl
= {
546 nsDocumentObserver_QueryInterface
,
547 nsDocumentObserver_AddRef
,
548 nsDocumentObserver_Release
,
549 nsDocumentObserver_CharacterDataWillChange
,
550 nsDocumentObserver_CharacterDataChanged
,
551 nsDocumentObserver_AttributeChanged
,
552 nsDocumentObserver_ContentAppended
,
553 nsDocumentObserver_ContentInserted
,
554 nsDocumentObserver_ContentRemoved
,
555 nsDocumentObserver_NodeWillBeDestroyed
,
556 nsDocumentObserver_ParentChainChanged
,
557 nsDocumentObserver_BeginUpdate
,
558 nsDocumentObserver_EndUpdate
,
559 nsDocumentObserver_BeginLoad
,
560 nsDocumentObserver_EndLoad
,
561 nsDocumentObserver_ContentStatesChanged
,
562 nsDocumentObserver_StyleSheetAdded
,
563 nsDocumentObserver_StyleSheetRemoved
,
564 nsDocumentObserver_StyleSheetApplicableStateChanged
,
565 nsDocumentObserver_StyleRuleChanged
,
566 nsDocumentObserver_StyleRuleAdded
,
567 nsDocumentObserver_StyleRuleRemoved
,
568 nsDocumentObserver_BindToDocument
,
569 nsDocumentObserver_DoneAddingChildren
572 void init_mutation(NSContainer
*nscontainer
)
574 nscontainer
->lpDocumentObserverVtbl
= &nsDocumentObserverVtbl
;
575 nscontainer
->lpRunnableVtbl
= &nsRunnableVtbl
;