winhlp32: Use Win32 APIs instead of strdup().
[wine] / dlls / mshtml / mutation.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 enum {
39     MUTATION_COMMENT,
40     MUTATION_SCRIPT
41 };
42
43 void set_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
44 {
45     nsIDOMNSDocument *nsdoc;
46     nsresult nsres;
47
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;
52     }
53
54     nsIDOMNSDocument_WineAddObserver(nsdoc, NSDOCOBS(nscontainer));
55     nsIDOMNSDocument_Release(nsdoc);
56 }
57
58 void remove_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
59 {
60     nsIDOMNSDocument *nsdoc;
61     nsresult nsres;
62
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;
67     }
68
69     nsIDOMNSDocument_WineRemoveObserver(nsdoc, NSDOCOBS(nscontainer));
70     nsIDOMNSDocument_Release(nsdoc);
71 }
72
73 #define IE_MAJOR_VERSION 7
74 #define IE_MINOR_VERSION 0
75
76 static BOOL handle_insert_comment(HTMLDocument *doc, const PRUnichar *comment)
77 {
78     DWORD len;
79     int majorv = 0, minorv = 0;
80     const PRUnichar *ptr, *end;
81     nsAString nsstr;
82     PRUnichar *buf;
83     nsresult nsres;
84
85     enum {
86         CMP_EQ,
87         CMP_LT,
88         CMP_LTE,
89         CMP_GT,
90         CMP_GTE
91     } cmpt = CMP_EQ;
92
93     static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
94
95     if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
96         return FALSE;
97
98     ptr = comment+3;
99     while(isspaceW(*ptr))
100         ptr++;
101
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;
109         }
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;
117         }
118     }
119
120     if(!isspaceW(*ptr++))
121         return FALSE;
122     while(isspaceW(*ptr))
123         ptr++;
124
125     if(ptr[0] != 'I' || ptr[1] != 'E')
126         return FALSE;
127
128     ptr +=2;
129     if(!isspaceW(*ptr++))
130         return FALSE;
131     while(isspaceW(*ptr))
132         ptr++;
133
134     if(!isdigitW(*ptr))
135         return FALSE;
136     while(isdigitW(*ptr))
137         majorv = majorv*10 + (*ptr++ - '0');
138
139     if(*ptr == '.') {
140         ptr++;
141         if(!isdigitW(*ptr))
142             return FALSE;
143         while(isdigitW(*ptr))
144             minorv = minorv*10 + (*ptr++ - '0');
145     }
146
147     while(isspaceW(*ptr))
148         ptr++;
149     if(ptr[0] != ']' || ptr[1] != '>')
150         return FALSE;
151     ptr += 2;
152
153     len = strlenW(ptr);
154     if(len < sizeof(endifW)/sizeof(WCHAR))
155         return FALSE;
156
157     end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
158     if(memcmp(end, endifW, sizeof(endifW)))
159         return FALSE;
160
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;
190     }
191
192     buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
193     if(!buf)
194         return FALSE;
195
196     memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
197     buf[end-ptr] = 0;
198     nsAString_Init(&nsstr, buf);
199     heap_free(buf);
200
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;
207     }
208
209     return TRUE;
210 }
211
212 static void add_script_runner(NSContainer *This)
213 {
214     nsIDOMNSDocument *nsdoc;
215     nsresult nsres;
216
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;
221     }
222
223     nsIDOMNSDocument_WineAddScriptRunner(nsdoc, NSRUNNABLE(This));
224     nsIDOMNSDocument_Release(nsdoc);
225 }
226
227 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(NSContainer, Runnable, iface)
228
229 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
230         nsIIDRef riid, nsQIResult result)
231 {
232     NSContainer *This = NSRUNNABLE_THIS(iface);
233
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;
244     }
245
246     nsISupports_AddRef((nsISupports*)*result);
247     return NS_OK;
248 }
249
250 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
251 {
252     NSContainer *This = NSRUNNABLE_THIS(iface);
253     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
254 }
255
256 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
257 {
258     NSContainer *This = NSRUNNABLE_THIS(iface);
259     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
260 }
261
262 static void pop_mutation_queue(NSContainer *nscontainer)
263 {
264     mutation_queue_t *tmp = nscontainer->mutation_queue;
265
266     if(!tmp)
267         return;
268
269     nscontainer->mutation_queue = tmp->next;
270     if(!tmp->next)
271         nscontainer->mutation_queue_tail = NULL;
272
273     nsISupports_Release(tmp->nsiface);
274     heap_free(tmp);
275 }
276
277 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
278 {
279     NSContainer *This = NSRUNNABLE_THIS(iface);
280     nsresult nsres;
281
282     TRACE("(%p)\n", This);
283
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;
290
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;
295             }
296
297             nsAString_Init(&comment_str, NULL);
298             nsres = nsIDOMComment_GetData(nscomment, &comment_str);
299             if(NS_SUCCEEDED(nsres)) {
300                 const PRUnichar *comment;
301
302                 nsAString_GetData(&comment_str, &comment);
303                 remove_comment = handle_insert_comment(This->doc, comment);
304             }
305
306             nsAString_Finish(&comment_str);
307
308             if(remove_comment) {
309                 nsIDOMNode *nsparent, *tmp;
310                 nsAString magic_str;
311
312                 static const PRUnichar remove_comment_magicW[] =
313                     {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
314
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);
320
321                 nsIDOMComment_GetParentNode(nscomment, &nsparent);
322                 if(nsparent) {
323                     nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
324                     nsIDOMNode_Release(nsparent);
325                     nsIDOMNode_Release(tmp);
326                 }
327             }
328
329             nsIDOMComment_Release(nscomment);
330             break;
331         }
332
333         case MUTATION_SCRIPT: {
334             nsIDOMHTMLScriptElement *nsscript;
335
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;
341             }
342
343             doc_insert_script(This->doc, nsscript);
344             nsIDOMHTMLScriptElement_Release(nsscript);
345             break;
346         }
347
348         default:
349             ERR("invalid type %d\n", This->mutation_queue->type);
350         }
351
352         pop_mutation_queue(This);
353     }
354
355     return S_OK;
356 }
357
358 #undef NSRUNNABLE_THIS
359
360 static const nsIRunnableVtbl nsRunnableVtbl = {
361     nsRunnable_QueryInterface,
362     nsRunnable_AddRef,
363     nsRunnable_Release,
364     nsRunnable_Run
365 };
366
367 #define NSDOCOBS_THIS(iface) DEFINE_THIS(NSContainer, DocumentObserver, iface)
368
369 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
370         nsIIDRef riid, nsQIResult result)
371 {
372     NSContainer *This = NSDOCOBS_THIS(iface);
373
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;
387     }
388
389     nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
390     return NS_OK;
391 }
392
393 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
394 {
395     NSContainer *This = NSDOCOBS_THIS(iface);
396     return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
397 }
398
399 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
400 {
401     NSContainer *This = NSDOCOBS_THIS(iface);
402     return nsIWebBrowserChrome_Release(NSWBCHROME(This));
403 }
404
405 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
406         nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
407 {
408 }
409
410 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
411         nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
412 {
413 }
414
415 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
416         nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType, PRUint32 aStateMask)
417 {
418 }
419
420 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
421         nsIContent *aContainer, PRInt32 aNewIndexInContainer)
422 {
423 }
424
425 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
426         nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
427 {
428 }
429
430 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
431         nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
432 {
433 }
434
435 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
436 {
437 }
438
439 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
440 {
441 }
442
443 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
444         nsUpdateType aUpdateType)
445 {
446 }
447
448 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
449         nsUpdateType aUpdateType)
450 {
451 }
452
453 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
454 {
455 }
456
457 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
458 {
459 }
460
461 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
462         nsIContent *aContent1, nsIContent *aContent2, PRInt32 aStateMask)
463 {
464 }
465
466 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
467         nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
468 {
469 }
470
471 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
472         nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
473 {
474 }
475
476 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
477         nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
478 {
479 }
480
481 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
482         nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
483 {
484 }
485
486 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
487         nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
488 {
489 }
490
491 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
492         nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
493 {
494 }
495
496 static void push_mutation_queue(NSContainer *nscontainer, DWORD type, nsISupports *nsiface)
497 {
498     mutation_queue_t *elem;
499
500     elem = heap_alloc(sizeof(mutation_queue_t));
501     if(!elem)
502         return;
503
504     elem->next = NULL;
505     elem->type = type;
506     elem->nsiface = nsiface;
507     nsISupports_AddRef(nsiface);
508
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;
513 }
514
515 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
516         nsIContent *aContent)
517 {
518     NSContainer *This = NSDOCOBS_THIS(iface);
519     nsIDOMComment *nscomment;
520     nsIDOMElement *nselem;
521     nsresult nsres;
522
523     TRACE("(%p)\n", This);
524
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);
529     }
530
531     nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
532     if(NS_SUCCEEDED(nsres)) {
533         TRACE("comment node\n");
534
535         push_mutation_queue(This, MUTATION_COMMENT, (nsISupports*)nscomment);
536         nsIDOMComment_Release(nscomment);
537         add_script_runner(This);
538     }
539 }
540
541 static void NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
542         PRBool aHaveNotified)
543 {
544     NSContainer *This = NSDOCOBS_THIS(iface);
545     nsIDOMHTMLScriptElement *nsscript;
546     nsresult nsres;
547
548     TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
549
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);
555     }
556 }
557
558 #undef NSMUTATIONOBS_THIS
559
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
585 };
586
587 void init_mutation(NSContainer *nscontainer)
588 {
589     nscontainer->lpDocumentObserverVtbl  = &nsDocumentObserverVtbl;
590     nscontainer->lpRunnableVtbl          = &nsRunnableVtbl;
591 }