winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / mshtml / mutation.c
blob04b1d7f30d6a35cdd3126e4cfd8b3c193aa5379f
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>
22 #include <assert.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "ole2.h"
31 #include "shlguid.h"
33 #include "mshtml_private.h"
34 #include "htmlscript.h"
35 #include "htmlevent.h"
36 #include "binding.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
42 #define IE_MAJOR_VERSION 7
43 #define IE_MINOR_VERSION 0
45 static const IID NS_ICONTENTUTILS_CID =
46 {0x762C4AE7,0xB923,0x422F,{0xB9,0x7E,0xB9,0xBF,0xC1,0xEF,0x7B,0xF0}};
48 static nsIContentUtils *content_utils;
50 static PRUnichar *handle_insert_comment(HTMLDocumentNode *doc, const PRUnichar *comment)
52 int majorv = 0, minorv = 0;
53 const PRUnichar *ptr, *end;
54 PRUnichar *buf;
55 DWORD len;
57 enum {
58 CMP_EQ,
59 CMP_LT,
60 CMP_LTE,
61 CMP_GT,
62 CMP_GTE
63 } cmpt = CMP_EQ;
65 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
67 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
68 return NULL;
70 ptr = comment+3;
71 while(isspaceW(*ptr))
72 ptr++;
74 if(ptr[0] == 'l' && ptr[1] == 't') {
75 ptr += 2;
76 if(*ptr == 'e') {
77 cmpt = CMP_LTE;
78 ptr++;
79 }else {
80 cmpt = CMP_LT;
82 }else if(ptr[0] == 'g' && ptr[1] == 't') {
83 ptr += 2;
84 if(*ptr == 'e') {
85 cmpt = CMP_GTE;
86 ptr++;
87 }else {
88 cmpt = CMP_GT;
92 if(!isspaceW(*ptr++))
93 return NULL;
94 while(isspaceW(*ptr))
95 ptr++;
97 if(ptr[0] != 'I' || ptr[1] != 'E')
98 return NULL;
100 ptr +=2;
101 if(!isspaceW(*ptr++))
102 return NULL;
103 while(isspaceW(*ptr))
104 ptr++;
106 if(!isdigitW(*ptr))
107 return NULL;
108 while(isdigitW(*ptr))
109 majorv = majorv*10 + (*ptr++ - '0');
111 if(*ptr == '.') {
112 ptr++;
113 if(!isdigitW(*ptr))
114 return NULL;
115 while(isdigitW(*ptr))
116 minorv = minorv*10 + (*ptr++ - '0');
119 while(isspaceW(*ptr))
120 ptr++;
121 if(ptr[0] != ']' || ptr[1] != '>')
122 return NULL;
123 ptr += 2;
125 len = strlenW(ptr);
126 if(len < sizeof(endifW)/sizeof(WCHAR))
127 return NULL;
129 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
130 if(memcmp(end, endifW, sizeof(endifW)))
131 return NULL;
133 switch(cmpt) {
134 case CMP_EQ:
135 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
136 break;
137 return NULL;
138 case CMP_LT:
139 if(majorv > IE_MAJOR_VERSION)
140 break;
141 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
142 break;
143 return NULL;
144 case CMP_LTE:
145 if(majorv > IE_MAJOR_VERSION)
146 break;
147 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
148 break;
149 return NULL;
150 case CMP_GT:
151 if(majorv < IE_MAJOR_VERSION)
152 break;
153 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
154 break;
155 return NULL;
156 case CMP_GTE:
157 if(majorv < IE_MAJOR_VERSION)
158 break;
159 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
160 break;
161 return NULL;
164 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
165 if(!buf)
166 return NULL;
168 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
169 buf[end-ptr] = 0;
171 return buf;
174 static nsresult run_insert_comment(HTMLDocumentNode *doc, nsISupports *comment_iface, nsISupports *arg2)
176 const PRUnichar *comment;
177 nsIDOMComment *nscomment;
178 PRUnichar *replace_html;
179 nsAString comment_str;
180 nsresult nsres;
182 nsres = nsISupports_QueryInterface(comment_iface, &IID_nsIDOMComment, (void**)&nscomment);
183 if(NS_FAILED(nsres)) {
184 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
185 return nsres;
188 nsAString_Init(&comment_str, NULL);
189 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
190 if(NS_FAILED(nsres))
191 return nsres;
193 nsAString_GetData(&comment_str, &comment);
194 replace_html = handle_insert_comment(doc, comment);
195 nsAString_Finish(&comment_str);
197 if(replace_html) {
198 HRESULT hres;
200 hres = replace_node_by_html(doc->nsdoc, (nsIDOMNode*)nscomment, replace_html);
201 heap_free(replace_html);
202 if(FAILED(hres))
203 nsres = NS_ERROR_FAILURE;
207 nsIDOMComment_Release(nscomment);
208 return nsres;
211 static nsresult run_bind_to_tree(HTMLDocumentNode *doc, nsISupports *nsiface, nsISupports *arg2)
213 nsIDOMNode *nsnode;
214 HTMLDOMNode *node;
215 nsresult nsres;
216 HRESULT hres;
218 TRACE("(%p)->(%p)\n", doc, nsiface);
220 nsres = nsISupports_QueryInterface(nsiface, &IID_nsIDOMNode, (void**)&nsnode);
221 if(NS_FAILED(nsres))
222 return nsres;
224 hres = get_node(doc, nsnode, TRUE, &node);
225 nsIDOMNode_Release(nsnode);
226 if(FAILED(hres)) {
227 ERR("Could not get node\n");
228 return nsres;
231 if(node->vtbl->bind_to_tree)
232 node->vtbl->bind_to_tree(node);
234 node_release(node);
235 return nsres;
238 /* Calls undocumented 69 cmd of CGID_Explorer */
239 static void call_explorer_69(HTMLDocumentObj *doc)
241 IOleCommandTarget *olecmd;
242 VARIANT var;
243 HRESULT hres;
245 if(!doc->client)
246 return;
248 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
249 if(FAILED(hres))
250 return;
252 VariantInit(&var);
253 hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
254 IOleCommandTarget_Release(olecmd);
255 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
256 FIXME("handle result\n");
259 static void parse_complete(HTMLDocumentObj *doc)
261 TRACE("(%p)\n", doc);
263 if(doc->usermode == EDITMODE)
264 init_editor(&doc->basedoc);
266 call_explorer_69(doc);
267 if(doc->view_sink)
268 IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
269 call_property_onchanged(&doc->basedoc.cp_container, 1005);
270 call_explorer_69(doc);
272 if(doc->webbrowser && doc->usermode != EDITMODE && !(doc->basedoc.window->load_flags & BINDING_REFRESH))
273 IDocObjectService_FireNavigateComplete2(doc->doc_object_service, &doc->basedoc.window->base.IHTMLWindow2_iface, 0);
275 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
278 static nsresult run_end_load(HTMLDocumentNode *This, nsISupports *arg1, nsISupports *arg2)
280 TRACE("(%p)\n", This);
282 if(!This->basedoc.doc_obj)
283 return NS_OK;
285 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
287 * This should be done in the worker thread that parses HTML,
288 * but we don't have such thread (Gecko parses HTML for us).
290 parse_complete(This->basedoc.doc_obj);
293 bind_event_scripts(This);
294 set_ready_state(This->basedoc.window, READYSTATE_INTERACTIVE);
295 return NS_OK;
298 static nsresult run_insert_script(HTMLDocumentNode *doc, nsISupports *script_iface, nsISupports *parser_iface)
300 nsIDOMHTMLScriptElement *nsscript;
301 HTMLScriptElement *script_elem;
302 nsIParser *nsparser = NULL;
303 script_queue_entry_t *iter;
304 HTMLInnerWindow *window;
305 nsresult nsres;
306 HRESULT hres;
308 TRACE("(%p)->(%p)\n", doc, script_iface);
310 window = doc->window;
311 if(!window)
312 return NS_OK;
314 nsres = nsISupports_QueryInterface(script_iface, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
315 if(NS_FAILED(nsres)) {
316 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
317 return nsres;
320 if(parser_iface) {
321 nsres = nsISupports_QueryInterface(parser_iface, &IID_nsIParser, (void**)&nsparser);
322 if(NS_FAILED(nsres)) {
323 ERR("Could not get nsIParser iface: %08x\n", nsres);
324 nsparser = NULL;
328 hres = script_elem_from_nsscript(doc, nsscript, &script_elem);
329 nsIDOMHTMLScriptElement_Release(nsscript);
330 if(FAILED(hres))
331 return NS_ERROR_FAILURE;
333 if(nsparser) {
334 nsIParser_BeginEvaluatingParserInsertedScript(nsparser);
335 window->parser_callback_cnt++;
338 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
340 doc_insert_script(window, script_elem);
342 while(!list_empty(&window->script_queue)) {
343 iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
344 list_remove(&iter->entry);
345 if(!iter->script->parsed)
346 doc_insert_script(window, iter->script);
347 IHTMLScriptElement_Release(&iter->script->IHTMLScriptElement_iface);
348 heap_free(iter);
351 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
353 if(nsparser) {
354 window->parser_callback_cnt--;
355 nsIParser_EndEvaluatingParserInsertedScript(nsparser);
356 nsIParser_Release(nsparser);
359 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
361 return NS_OK;
364 typedef struct nsRunnable nsRunnable;
366 typedef nsresult (*runnable_proc_t)(HTMLDocumentNode*,nsISupports*,nsISupports*);
368 struct nsRunnable {
369 nsIRunnable nsIRunnable_iface;
371 LONG ref;
373 runnable_proc_t proc;
375 HTMLDocumentNode *doc;
376 nsISupports *arg1;
377 nsISupports *arg2;
380 static inline nsRunnable *impl_from_nsIRunnable(nsIRunnable *iface)
382 return CONTAINING_RECORD(iface, nsRunnable, nsIRunnable_iface);
385 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
386 nsIIDRef riid, void **result)
388 nsRunnable *This = impl_from_nsIRunnable(iface);
390 if(IsEqualGUID(riid, &IID_nsISupports)) {
391 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
392 *result = &This->nsIRunnable_iface;
393 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
394 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
395 *result = &This->nsIRunnable_iface;
396 }else {
397 *result = NULL;
398 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
399 return NS_NOINTERFACE;
402 nsISupports_AddRef((nsISupports*)*result);
403 return NS_OK;
406 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
408 nsRunnable *This = impl_from_nsIRunnable(iface);
409 LONG ref = InterlockedIncrement(&This->ref);
411 TRACE("(%p) ref=%d\n", This, ref);
413 return ref;
416 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
418 nsRunnable *This = impl_from_nsIRunnable(iface);
419 LONG ref = InterlockedDecrement(&This->ref);
421 TRACE("(%p) ref=%d\n", This, ref);
423 if(!ref) {
424 htmldoc_release(&This->doc->basedoc);
425 if(This->arg1)
426 nsISupports_Release(This->arg1);
427 if(This->arg2)
428 nsISupports_Release(This->arg2);
429 heap_free(This);
432 return ref;
435 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
437 nsRunnable *This = impl_from_nsIRunnable(iface);
439 return This->proc(This->doc, This->arg1, This->arg2);
442 static const nsIRunnableVtbl nsRunnableVtbl = {
443 nsRunnable_QueryInterface,
444 nsRunnable_AddRef,
445 nsRunnable_Release,
446 nsRunnable_Run
449 static void add_script_runner(HTMLDocumentNode *This, runnable_proc_t proc, nsISupports *arg1, nsISupports *arg2)
451 nsRunnable *runnable;
453 runnable = heap_alloc_zero(sizeof(*runnable));
454 if(!runnable)
455 return;
457 runnable->nsIRunnable_iface.lpVtbl = &nsRunnableVtbl;
458 runnable->ref = 1;
460 htmldoc_addref(&This->basedoc);
461 runnable->doc = This;
462 runnable->proc = proc;
464 if(arg1)
465 nsISupports_AddRef(arg1);
466 runnable->arg1 = arg1;
468 if(arg2)
469 nsISupports_AddRef(arg2);
470 runnable->arg2 = arg2;
472 nsIContentUtils_AddScriptRunner(content_utils, &runnable->nsIRunnable_iface);
474 nsIRunnable_Release(&runnable->nsIRunnable_iface);
477 static inline HTMLDocumentNode *impl_from_nsIDocumentObserver(nsIDocumentObserver *iface)
479 return CONTAINING_RECORD(iface, HTMLDocumentNode, nsIDocumentObserver_iface);
482 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
483 nsIIDRef riid, void **result)
485 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
487 if(IsEqualGUID(&IID_nsISupports, riid)) {
488 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
489 *result = &This->nsIDocumentObserver_iface;
490 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
491 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
492 *result = &This->nsIDocumentObserver_iface;
493 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
494 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
495 *result = &This->nsIDocumentObserver_iface;
496 }else {
497 *result = NULL;
498 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
499 return NS_NOINTERFACE;
502 htmldoc_addref(&This->basedoc);
503 return NS_OK;
506 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
508 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
509 return htmldoc_addref(&This->basedoc);
512 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
514 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
515 return htmldoc_release(&This->basedoc);
518 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
519 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
523 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
524 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
528 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
529 nsIContent *aContent, LONG aNameSpaceID, nsIAtom *aAttribute, LONG aModType)
533 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
534 nsIContent *aContent, LONG aNameSpaceID, nsIAtom *aAttribute, LONG aModType)
538 static void NSAPI nsDocumentObserver_AttributeSetToCurrentValue(nsIDocumentObserver *iface, nsIDocument *aDocument,
539 void *aElement, LONG aNameSpaceID, nsIAtom *aAttribute)
543 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
544 nsIContent *aContainer, nsIContent *aFirstNewContent, LONG aNewIndexInContainer)
548 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
549 nsIContent *aContainer, nsIContent *aChild, LONG aIndexInContainer)
553 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
554 nsIContent *aContainer, nsIContent *aChild, LONG aIndexInContainer,
555 nsIContent *aProviousSibling)
559 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
563 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
567 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
568 nsUpdateType aUpdateType)
572 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
573 nsUpdateType aUpdateType)
577 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
581 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
583 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
585 TRACE("(%p)\n", This);
587 if(This->skip_mutation_notif)
588 return;
590 This->content_ready = TRUE;
591 add_script_runner(This, run_end_load, NULL, NULL);
594 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
595 nsIContent *aContent, EventStates aStateMask)
599 static void NSAPI nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
600 EventStates aStateMask)
604 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
605 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
609 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
610 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
614 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
615 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, cpp_bool aApplicable)
619 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
620 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
624 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
625 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
629 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
630 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
634 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
635 nsIContent *aContent)
637 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
638 nsIDOMHTMLIFrameElement *nsiframe;
639 nsIDOMHTMLFrameElement *nsframe;
640 nsIDOMHTMLScriptElement *nsscript;
641 nsIDOMHTMLElement *nselem;
642 nsIDOMComment *nscomment;
643 nsresult nsres;
645 TRACE("(%p)->(%p %p)\n", This, aDocument, aContent);
647 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLElement, (void**)&nselem);
648 if(NS_SUCCEEDED(nsres)) {
649 check_event_attr(This, nselem);
650 nsIDOMHTMLElement_Release(nselem);
653 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
654 if(NS_SUCCEEDED(nsres)) {
655 TRACE("comment node\n");
657 add_script_runner(This, run_insert_comment, (nsISupports*)nscomment, NULL);
658 nsIDOMComment_Release(nscomment);
659 return;
662 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
663 if(NS_SUCCEEDED(nsres)) {
664 TRACE("iframe node\n");
666 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsiframe, NULL);
667 nsIDOMHTMLIFrameElement_Release(nsiframe);
668 return;
671 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLFrameElement, (void**)&nsframe);
672 if(NS_SUCCEEDED(nsres)) {
673 TRACE("frame node\n");
675 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsframe, NULL);
676 nsIDOMHTMLFrameElement_Release(nsframe);
677 return;
680 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
681 if(NS_SUCCEEDED(nsres)) {
682 HTMLScriptElement *script_elem;
683 HRESULT hres;
685 TRACE("script element\n");
687 hres = script_elem_from_nsscript(This, nsscript, &script_elem);
688 nsIDOMHTMLScriptElement_Release(nsscript);
689 if(FAILED(hres))
690 return;
692 if(script_elem->parse_on_bind)
693 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, NULL);
695 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
699 static void NSAPI nsDocumentObserver_AttemptToExecuteScript(nsIDocumentObserver *iface, nsIContent *aContent,
700 nsIParser *aParser, cpp_bool *aBlock)
702 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
703 nsIDOMHTMLScriptElement *nsscript;
704 nsresult nsres;
706 TRACE("(%p)->(%p %p %p)\n", This, aContent, aParser, aBlock);
708 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
709 if(NS_SUCCEEDED(nsres)) {
710 TRACE("script node\n");
712 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, (nsISupports*)aParser);
713 nsIDOMHTMLScriptElement_Release(nsscript);
717 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
718 nsDocumentObserver_QueryInterface,
719 nsDocumentObserver_AddRef,
720 nsDocumentObserver_Release,
721 nsDocumentObserver_CharacterDataWillChange,
722 nsDocumentObserver_CharacterDataChanged,
723 nsDocumentObserver_AttributeWillChange,
724 nsDocumentObserver_AttributeChanged,
725 nsDocumentObserver_AttributeSetToCurrentValue,
726 nsDocumentObserver_ContentAppended,
727 nsDocumentObserver_ContentInserted,
728 nsDocumentObserver_ContentRemoved,
729 nsDocumentObserver_NodeWillBeDestroyed,
730 nsDocumentObserver_ParentChainChanged,
731 nsDocumentObserver_BeginUpdate,
732 nsDocumentObserver_EndUpdate,
733 nsDocumentObserver_BeginLoad,
734 nsDocumentObserver_EndLoad,
735 nsDocumentObserver_ContentStatesChanged,
736 nsDocumentObserver_DocumentStatesChanged,
737 nsDocumentObserver_StyleSheetAdded,
738 nsDocumentObserver_StyleSheetRemoved,
739 nsDocumentObserver_StyleSheetApplicableStateChanged,
740 nsDocumentObserver_StyleRuleChanged,
741 nsDocumentObserver_StyleRuleAdded,
742 nsDocumentObserver_StyleRuleRemoved,
743 nsDocumentObserver_BindToDocument,
744 nsDocumentObserver_AttemptToExecuteScript
747 void init_document_mutation(HTMLDocumentNode *doc)
749 nsIDocument *nsdoc;
750 nsresult nsres;
752 doc->nsIDocumentObserver_iface.lpVtbl = &nsDocumentObserverVtbl;
754 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
755 if(NS_FAILED(nsres)) {
756 ERR("Could not get nsIDocument: %08x\n", nsres);
757 return;
760 nsIContentUtils_AddDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
761 nsIDocument_Release(nsdoc);
764 void release_document_mutation(HTMLDocumentNode *doc)
766 nsIDocument *nsdoc;
767 nsresult nsres;
769 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
770 if(NS_FAILED(nsres)) {
771 ERR("Could not get nsIDocument: %08x\n", nsres);
772 return;
775 nsIContentUtils_RemoveDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
776 nsIDocument_Release(nsdoc);
779 JSContext *get_context_from_document(nsIDOMHTMLDocument *nsdoc)
781 nsIDocument *doc;
782 JSContext *ctx;
783 nsresult nsres;
785 nsres = nsIDOMHTMLDocument_QueryInterface(nsdoc, &IID_nsIDocument, (void**)&doc);
786 assert(nsres == NS_OK);
788 ctx = nsIContentUtils_GetContextFromDocument(content_utils, doc);
789 nsIDocument_Release(doc);
791 TRACE("ret %p\n", ctx);
792 return ctx;
795 void init_mutation(nsIComponentManager *component_manager)
797 nsIFactory *factory;
798 nsresult nsres;
800 if(!component_manager) {
801 if(content_utils) {
802 nsIContentUtils_Release(content_utils);
803 content_utils = NULL;
805 return;
808 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_ICONTENTUTILS_CID,
809 &IID_nsIFactory, (void**)&factory);
810 if(NS_FAILED(nsres)) {
811 ERR("Could not create nsIContentUtils service: %08x\n", nsres);
812 return;
815 nsres = nsIFactory_CreateInstance(factory, NULL, &IID_nsIContentUtils, (void**)&content_utils);
816 nsIFactory_Release(factory);
817 if(NS_FAILED(nsres))
818 ERR("Could not create nsIContentUtils instance: %08x\n", nsres);