user32: Disable assert() for the mingw build since mingw gets confused trying to...
[wine.git] / dlls / mshtml / mutation.c
blobab9649ca6c1191e3aeb97ca94260aa3838aa663f
1 /*
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 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 enum {
39 MUTATION_COMMENT,
40 MUTATION_SCRIPT
43 void set_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
45 nsIDOMNSDocument *nsdoc;
46 nsresult nsres;
48 nsres = nsIDOMHTMLDocument_QueryInterface(nshtmldoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
49 if(NS_FAILED(nsres)) {
50 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
51 return;
54 nsIDOMNSDocument_WineAddObserver(nsdoc, NSDOCOBS(nscontainer));
55 nsIDOMNSDocument_Release(nsdoc);
58 void remove_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
60 nsIDOMNSDocument *nsdoc;
61 nsresult nsres;
63 nsres = nsIDOMHTMLDocument_QueryInterface(nshtmldoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
64 if(NS_FAILED(nsres)) {
65 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
66 return;
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)
78 DWORD len;
79 int majorv = 0, minorv = 0;
80 const PRUnichar *ptr, *end;
81 nsAString nsstr;
82 PRUnichar *buf;
83 nsresult nsres;
85 enum {
86 CMP_EQ,
87 CMP_LT,
88 CMP_LTE,
89 CMP_GT,
90 CMP_GTE
91 } cmpt = CMP_EQ;
93 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
95 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
96 return FALSE;
98 ptr = comment+3;
99 while(isspaceW(*ptr))
100 ptr++;
102 if(ptr[0] == 'l' && ptr[1] == 't') {
103 ptr += 2;
104 if(*ptr == 'e') {
105 cmpt = CMP_LTE;
106 ptr++;
107 }else {
108 cmpt = CMP_LT;
110 }else if(ptr[0] == 'g' && ptr[1] == 't') {
111 ptr += 2;
112 if(*ptr == 'e') {
113 cmpt = CMP_GTE;
114 ptr++;
115 }else {
116 cmpt = CMP_GT;
120 if(!isspaceW(*ptr++))
121 return FALSE;
122 while(isspaceW(*ptr))
123 ptr++;
125 if(ptr[0] != 'I' || ptr[1] != 'E')
126 return FALSE;
128 ptr +=2;
129 if(!isspaceW(*ptr++))
130 return FALSE;
131 while(isspaceW(*ptr))
132 ptr++;
134 if(!isdigitW(*ptr))
135 return FALSE;
136 while(isdigitW(*ptr))
137 majorv = majorv*10 + (*ptr++ - '0');
139 if(*ptr == '.') {
140 ptr++;
141 if(!isdigitW(*ptr))
142 return FALSE;
143 while(isdigitW(*ptr))
144 minorv = minorv*10 + (*ptr++ - '0');
147 while(isspaceW(*ptr))
148 ptr++;
149 if(ptr[0] != ']' || ptr[1] != '>')
150 return FALSE;
151 ptr += 2;
153 len = strlenW(ptr);
154 if(len < sizeof(endifW)/sizeof(WCHAR))
155 return FALSE;
157 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
158 if(memcmp(end, endifW, sizeof(endifW)))
159 return FALSE;
161 switch(cmpt) {
162 case CMP_EQ:
163 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
164 break;
165 return FALSE;
166 case CMP_LT:
167 if(majorv > IE_MAJOR_VERSION)
168 break;
169 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
170 break;
171 return FALSE;
172 case CMP_LTE:
173 if(majorv > IE_MAJOR_VERSION)
174 break;
175 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
176 break;
177 return FALSE;
178 case CMP_GT:
179 if(majorv < IE_MAJOR_VERSION)
180 break;
181 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
182 break;
183 return FALSE;
184 case CMP_GTE:
185 if(majorv < IE_MAJOR_VERSION)
186 break;
187 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
188 break;
189 return FALSE;
192 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
193 if(!buf)
194 return FALSE;
196 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
197 buf[end-ptr] = 0;
198 nsAString_Init(&nsstr, buf);
199 heap_free(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);
206 return FALSE;
209 return TRUE;
212 static void add_script_runner(NSContainer *This)
214 nsIDOMNSDocument *nsdoc;
215 nsresult nsres;
217 nsres = nsIDOMHTMLDocument_QueryInterface(This->doc->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
218 if(NS_FAILED(nsres)) {
219 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
220 return;
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);
240 }else {
241 *result = NULL;
242 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
243 return NS_NOINTERFACE;
246 nsISupports_AddRef((nsISupports*)*result);
247 return NS_OK;
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;
266 if(!tmp)
267 return;
269 nscontainer->mutation_queue = tmp->next;
270 if(!tmp->next)
271 nscontainer->mutation_queue_tail = NULL;
273 nsISupports_Release(tmp->nsiface);
274 heap_free(tmp);
277 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
279 NSContainer *This = NSRUNNABLE_THIS(iface);
280 nsresult nsres;
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);
294 return NS_OK;
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, comment);
306 nsAString_Finish(&comment_str);
308 if(remove_comment) {
309 nsIDOMNode *nsparent, *tmp;
310 nsAString magic_str;
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);
318 if(NS_FAILED(nsres))
319 ERR("SetData failed: %08x\n", nsres);
321 nsIDOMComment_GetParentNode(nscomment, &nsparent);
322 if(nsparent) {
323 nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
324 nsIDOMNode_Release(nsparent);
325 nsIDOMNode_Release(tmp);
329 nsIDOMComment_Release(nscomment);
330 break;
333 case MUTATION_SCRIPT: {
334 nsIDOMHTMLScriptElement *nsscript;
336 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMHTMLScriptElement,
337 (void**)&nsscript);
338 if(NS_FAILED(nsres)) {
339 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
340 break;
343 doc_insert_script(This->doc, nsscript);
344 nsIDOMHTMLScriptElement_Release(nsscript);
345 break;
348 default:
349 ERR("invalid type %d\n", This->mutation_queue->type);
352 pop_mutation_queue(This);
355 return S_OK;
358 #undef NSRUNNABLE_THIS
360 static const nsIRunnableVtbl nsRunnableVtbl = {
361 nsRunnable_QueryInterface,
362 nsRunnable_AddRef,
363 nsRunnable_Release,
364 nsRunnable_Run
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);
383 }else {
384 *result = NULL;
385 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
386 return NS_NOINTERFACE;
389 nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
390 return NS_OK;
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_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
416 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType, PRUint32 aStateMask)
420 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
421 nsIContent *aContainer, PRInt32 aNewIndexInContainer)
425 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
426 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
430 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
431 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
435 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
439 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
443 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
444 nsUpdateType aUpdateType)
448 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
449 nsUpdateType aUpdateType)
453 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
457 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
461 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
462 nsIContent *aContent1, nsIContent *aContent2, PRInt32 aStateMask)
466 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
467 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
471 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
472 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
476 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
477 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
481 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
482 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
486 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
487 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
491 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
492 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
496 static void push_mutation_queue(NSContainer *nscontainer, DWORD type, nsISupports *nsiface)
498 mutation_queue_t *elem;
500 elem = heap_alloc(sizeof(mutation_queue_t));
501 if(!elem)
502 return;
504 elem->next = NULL;
505 elem->type = type;
506 elem->nsiface = nsiface;
507 nsISupports_AddRef(nsiface);
509 if(nscontainer->mutation_queue_tail)
510 nscontainer->mutation_queue_tail = nscontainer->mutation_queue_tail->next = elem;
511 else
512 nscontainer->mutation_queue = nscontainer->mutation_queue_tail = elem;
515 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
516 nsIContent *aContent)
518 NSContainer *This = NSDOCOBS_THIS(iface);
519 nsIDOMComment *nscomment;
520 nsIDOMElement *nselem;
521 nsresult nsres;
523 TRACE("(%p)\n", This);
525 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
526 if(NS_SUCCEEDED(nsres)) {
527 check_event_attr(This->doc, nselem);
528 nsIDOMElement_Release(nselem);
531 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
532 if(NS_SUCCEEDED(nsres)) {
533 TRACE("comment node\n");
535 push_mutation_queue(This, MUTATION_COMMENT, (nsISupports*)nscomment);
536 nsIDOMComment_Release(nscomment);
537 add_script_runner(This);
541 static void NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
542 PRBool aHaveNotified)
544 NSContainer *This = NSDOCOBS_THIS(iface);
545 nsIDOMHTMLScriptElement *nsscript;
546 nsresult nsres;
548 TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
550 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
551 if(NS_SUCCEEDED(nsres)) {
552 push_mutation_queue(This, MUTATION_SCRIPT, (nsISupports*)nsscript);
553 nsIDOMHTMLScriptElement_Release(nsscript);
554 add_script_runner(This);
558 #undef NSMUTATIONOBS_THIS
560 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
561 nsDocumentObserver_QueryInterface,
562 nsDocumentObserver_AddRef,
563 nsDocumentObserver_Release,
564 nsDocumentObserver_CharacterDataWillChange,
565 nsDocumentObserver_CharacterDataChanged,
566 nsDocumentObserver_AttributeChanged,
567 nsDocumentObserver_ContentAppended,
568 nsDocumentObserver_ContentInserted,
569 nsDocumentObserver_ContentRemoved,
570 nsDocumentObserver_NodeWillBeDestroyed,
571 nsDocumentObserver_ParentChainChanged,
572 nsDocumentObserver_BeginUpdate,
573 nsDocumentObserver_EndUpdate,
574 nsDocumentObserver_BeginLoad,
575 nsDocumentObserver_EndLoad,
576 nsDocumentObserver_ContentStatesChanged,
577 nsDocumentObserver_StyleSheetAdded,
578 nsDocumentObserver_StyleSheetRemoved,
579 nsDocumentObserver_StyleSheetApplicableStateChanged,
580 nsDocumentObserver_StyleRuleChanged,
581 nsDocumentObserver_StyleRuleAdded,
582 nsDocumentObserver_StyleRuleRemoved,
583 nsDocumentObserver_BindToDocument,
584 nsDocumentObserver_DoneAddingChildren
587 void init_mutation(NSContainer *nscontainer)
589 nscontainer->lpDocumentObserverVtbl = &nsDocumentObserverVtbl;
590 nscontainer->lpRunnableVtbl = &nsRunnableVtbl;