wow64: Add thunks for the directory object syscalls.
[wine.git] / dlls / mshtml / mutation.c
blob4352e93eba39cd8faad5604de956e49d0ad9619d
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 <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "shlguid.h"
29 #include "wininet.h"
31 #include "mshtml_private.h"
32 #include "htmlscript.h"
33 #include "htmlevent.h"
34 #include "binding.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 const compat_mode_info_t compat_mode_info[] = {
41 { 5, 7 }, /* DOCMODE_QUIRKS */
42 { 5, 5 }, /* DOCMODE_IE5 */
43 { 7, 7 }, /* DOCMODE_IE7 */
44 { 8, 8 }, /* DOCMODE_IE8 */
45 { 9, 9 }, /* DOCMODE_IE8 */
46 { 10, 10 }, /* DOCMODE_IE10 */
47 { 11, 11 } /* DOCMODE_IE11 */
50 static const IID NS_ICONTENTUTILS_CID =
51 {0x762C4AE7,0xB923,0x422F,{0xB9,0x7E,0xB9,0xBF,0xC1,0xEF,0x7B,0xF0}};
53 static nsIContentUtils *content_utils;
55 static PRUnichar *handle_insert_comment(HTMLDocumentNode *doc, const PRUnichar *comment)
57 unsigned majorv = 0, minorv = 0, compat_version;
58 const PRUnichar *ptr, *end;
59 PRUnichar *buf;
60 DWORD len;
62 enum {
63 CMP_EQ,
64 CMP_LT,
65 CMP_LTE,
66 CMP_GT,
67 CMP_GTE
68 } cmpt = CMP_EQ;
70 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
72 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
73 return NULL;
75 ptr = comment+3;
76 while(iswspace(*ptr))
77 ptr++;
79 if(ptr[0] == 'l' && ptr[1] == 't') {
80 ptr += 2;
81 if(*ptr == 'e') {
82 cmpt = CMP_LTE;
83 ptr++;
84 }else {
85 cmpt = CMP_LT;
87 }else if(ptr[0] == 'g' && ptr[1] == 't') {
88 ptr += 2;
89 if(*ptr == 'e') {
90 cmpt = CMP_GTE;
91 ptr++;
92 }else {
93 cmpt = CMP_GT;
97 if(!iswspace(*ptr++))
98 return NULL;
99 while(iswspace(*ptr))
100 ptr++;
102 if(ptr[0] != 'I' || ptr[1] != 'E')
103 return NULL;
105 ptr +=2;
106 if(!iswspace(*ptr++))
107 return NULL;
108 while(iswspace(*ptr))
109 ptr++;
111 if(!is_digit(*ptr))
112 return NULL;
113 while(is_digit(*ptr))
114 majorv = majorv*10 + (*ptr++ - '0');
116 if(*ptr == '.') {
117 ptr++;
118 if(!is_digit(*ptr))
119 return NULL;
120 while(is_digit(*ptr))
121 minorv = minorv*10 + (*ptr++ - '0');
124 while(iswspace(*ptr))
125 ptr++;
126 if(ptr[0] != ']' || ptr[1] != '>')
127 return NULL;
128 ptr += 2;
130 len = lstrlenW(ptr);
131 if(len < ARRAY_SIZE(endifW))
132 return NULL;
134 end = ptr + len - ARRAY_SIZE(endifW);
135 if(memcmp(end, endifW, sizeof(endifW)))
136 return NULL;
138 compat_version = compat_mode_info[doc->document_mode].ie_version;
140 switch(cmpt) {
141 case CMP_EQ:
142 if(compat_version == majorv && !minorv)
143 break;
144 return NULL;
145 case CMP_LT:
146 if(compat_version < majorv || (compat_version == majorv && minorv))
147 break;
148 return NULL;
149 case CMP_LTE:
150 if(compat_version <= majorv)
151 break;
152 return NULL;
153 case CMP_GT:
154 if(compat_version > majorv)
155 break;
156 return NULL;
157 case CMP_GTE:
158 if(compat_version >= majorv || (compat_version == majorv && !minorv))
159 break;
160 return NULL;
163 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
164 if(!buf)
165 return NULL;
167 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
168 buf[end-ptr] = 0;
170 return buf;
173 static nsresult run_insert_comment(HTMLDocumentNode *doc, nsISupports *comment_iface, nsISupports *arg2)
175 const PRUnichar *comment;
176 nsIDOMComment *nscomment;
177 PRUnichar *replace_html;
178 nsAString comment_str;
179 nsresult nsres;
181 nsres = nsISupports_QueryInterface(comment_iface, &IID_nsIDOMComment, (void**)&nscomment);
182 if(NS_FAILED(nsres)) {
183 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
184 return nsres;
187 nsAString_Init(&comment_str, NULL);
188 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
189 if(NS_FAILED(nsres))
190 return nsres;
192 nsAString_GetData(&comment_str, &comment);
193 replace_html = handle_insert_comment(doc, comment);
194 nsAString_Finish(&comment_str);
196 if(replace_html) {
197 HRESULT hres;
199 hres = replace_node_by_html(doc->nsdoc, (nsIDOMNode*)nscomment, replace_html);
200 heap_free(replace_html);
201 if(FAILED(hres))
202 nsres = NS_ERROR_FAILURE;
206 nsIDOMComment_Release(nscomment);
207 return nsres;
210 static nsresult run_bind_to_tree(HTMLDocumentNode *doc, nsISupports *nsiface, nsISupports *arg2)
212 nsIDOMNode *nsnode;
213 HTMLDOMNode *node;
214 nsresult nsres;
215 HRESULT hres;
217 TRACE("(%p)->(%p)\n", doc, nsiface);
219 nsres = nsISupports_QueryInterface(nsiface, &IID_nsIDOMNode, (void**)&nsnode);
220 if(NS_FAILED(nsres))
221 return nsres;
223 hres = get_node(nsnode, TRUE, &node);
224 nsIDOMNode_Release(nsnode);
225 if(FAILED(hres)) {
226 ERR("Could not get node\n");
227 return nsres;
230 if(node->vtbl->bind_to_tree)
231 node->vtbl->bind_to_tree(node);
233 node_release(node);
234 return nsres;
237 /* Calls undocumented 69 cmd of CGID_Explorer */
238 static void call_explorer_69(HTMLDocumentObj *doc)
240 IOleCommandTarget *olecmd;
241 VARIANT var;
242 HRESULT hres;
244 if(!doc->client)
245 return;
247 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
248 if(FAILED(hres))
249 return;
251 VariantInit(&var);
252 hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
253 IOleCommandTarget_Release(olecmd);
254 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
255 FIXME("handle result\n");
258 static void parse_complete(HTMLDocumentObj *doc)
260 TRACE("(%p)\n", doc);
262 if(doc->nscontainer->usermode == EDITMODE)
263 init_editor(doc->basedoc.doc_node);
265 call_explorer_69(doc);
266 if(doc->view_sink)
267 IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
268 call_property_onchanged(&doc->basedoc.cp_container, 1005);
269 call_explorer_69(doc);
271 if(doc->webbrowser && doc->nscontainer->usermode != EDITMODE && !(doc->basedoc.window->load_flags & BINDING_REFRESH))
272 IDocObjectService_FireNavigateComplete2(doc->doc_object_service, &doc->basedoc.window->base.IHTMLWindow2_iface, 0);
274 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
277 static nsresult run_end_load(HTMLDocumentNode *This, nsISupports *arg1, nsISupports *arg2)
279 TRACE("(%p)\n", This);
281 if(!This->basedoc.doc_obj)
282 return NS_OK;
284 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
286 * This should be done in the worker thread that parses HTML,
287 * but we don't have such thread (Gecko parses HTML for us).
289 parse_complete(This->basedoc.doc_obj);
292 bind_event_scripts(This);
293 set_ready_state(This->basedoc.window, READYSTATE_INTERACTIVE);
294 return NS_OK;
297 static nsresult run_insert_script(HTMLDocumentNode *doc, nsISupports *script_iface, nsISupports *parser_iface)
299 nsIDOMHTMLScriptElement *nsscript;
300 HTMLScriptElement *script_elem;
301 nsIParser *nsparser = NULL;
302 script_queue_entry_t *iter;
303 HTMLInnerWindow *window;
304 nsresult nsres;
305 HRESULT hres;
307 TRACE("(%p)->(%p)\n", doc, script_iface);
309 window = doc->window;
310 if(!window)
311 return NS_OK;
313 nsres = nsISupports_QueryInterface(script_iface, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
314 if(NS_FAILED(nsres)) {
315 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
316 return nsres;
319 if(parser_iface) {
320 nsres = nsISupports_QueryInterface(parser_iface, &IID_nsIParser, (void**)&nsparser);
321 if(NS_FAILED(nsres)) {
322 ERR("Could not get nsIParser iface: %08x\n", nsres);
323 nsparser = NULL;
327 hres = script_elem_from_nsscript(nsscript, &script_elem);
328 nsIDOMHTMLScriptElement_Release(nsscript);
329 if(FAILED(hres))
330 return NS_ERROR_FAILURE;
332 if(nsparser) {
333 nsIParser_BeginEvaluatingParserInsertedScript(nsparser);
334 window->parser_callback_cnt++;
337 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
339 doc_insert_script(window, script_elem, TRUE);
341 while(!list_empty(&window->script_queue)) {
342 iter = LIST_ENTRY(list_head(&window->script_queue), script_queue_entry_t, entry);
343 list_remove(&iter->entry);
344 if(!iter->script->parsed)
345 doc_insert_script(window, iter->script, TRUE);
346 IHTMLScriptElement_Release(&iter->script->IHTMLScriptElement_iface);
347 heap_free(iter);
350 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
352 if(nsparser) {
353 window->parser_callback_cnt--;
354 nsIParser_EndEvaluatingParserInsertedScript(nsparser);
355 nsIParser_Release(nsparser);
358 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
360 return NS_OK;
364 * We may change document mode only in early stage of document lifetime.
365 * Later attempts will not have an effect.
367 compat_mode_t lock_document_mode(HTMLDocumentNode *doc)
369 TRACE("%p: %d\n", doc, doc->document_mode);
371 doc->document_mode_locked = TRUE;
372 return doc->document_mode;
375 static void set_document_mode(HTMLDocumentNode *doc, compat_mode_t document_mode, BOOL lock)
377 compat_mode_t max_compat_mode;
379 if(doc->document_mode_locked) {
380 WARN("attempting to set document mode %d on locked document %p\n", document_mode, doc);
381 return;
384 TRACE("%p: %d\n", doc, document_mode);
386 max_compat_mode = doc->window && doc->window->base.outer_window
387 ? get_max_compat_mode(doc->window->base.outer_window->uri)
388 : COMPAT_MODE_IE11;
389 if(max_compat_mode < document_mode) {
390 WARN("Tried to set compat mode %u higher than maximal configured %u\n",
391 document_mode, max_compat_mode);
392 document_mode = max_compat_mode;
395 doc->document_mode = document_mode;
396 if(lock)
397 lock_document_mode(doc);
400 BOOL parse_compat_version(const WCHAR *version_string, compat_mode_t *r)
402 DWORD version = 0;
403 const WCHAR *p;
405 for(p = version_string; '0' <= *p && *p <= '9'; p++)
406 version = version * 10 + *p-'0';
407 if((*p && *p != ';') || p == version_string)
408 return FALSE;
410 switch(version){
411 case 5:
412 case 6:
413 *r = COMPAT_MODE_IE5;
414 break;
415 case 7:
416 *r = COMPAT_MODE_IE7;
417 break;
418 case 8:
419 *r = COMPAT_MODE_IE8;
420 break;
421 case 9:
422 *r = COMPAT_MODE_IE9;
423 break;
424 case 10:
425 *r = COMPAT_MODE_IE10;
426 break;
427 default:
428 *r = version < 5 ? COMPAT_MODE_QUIRKS : COMPAT_MODE_IE11;
430 return TRUE;
433 static BOOL parse_ua_compatible(const WCHAR *p, compat_mode_t *r)
435 static const WCHAR ie_eqW[] = {'I','E','='};
436 static const WCHAR edgeW[] = {'e','d','g','e'};
438 TRACE("%s\n", debugstr_w(p));
440 if(wcsnicmp(ie_eqW, p, ARRAY_SIZE(ie_eqW)))
441 return FALSE;
442 p += 3;
444 if(!wcsnicmp(p, edgeW, ARRAY_SIZE(edgeW))) {
445 p += ARRAY_SIZE(edgeW);
446 if(*p && *p != ';')
447 return FALSE;
448 *r = COMPAT_MODE_IE11;
449 return TRUE;
452 return parse_compat_version(p, r);
455 void process_document_response_headers(HTMLDocumentNode *doc, IBinding *binding)
457 IWinInetHttpInfo *http_info;
458 char buf[1024];
459 DWORD size;
460 HRESULT hres;
462 hres = IBinding_QueryInterface(binding, &IID_IWinInetHttpInfo, (void**)&http_info);
463 if(FAILED(hres)) {
464 TRACE("No IWinInetHttpInfo\n");
465 return;
468 size = sizeof(buf);
469 strcpy(buf, "X-UA-Compatible");
470 hres = IWinInetHttpInfo_QueryInfo(http_info, HTTP_QUERY_CUSTOM, buf, &size, NULL, NULL);
471 if(hres == S_OK && size) {
472 compat_mode_t document_mode;
473 WCHAR *header;
475 TRACE("size %u\n", size);
477 header = heap_strdupAtoW(buf);
478 if(header && parse_ua_compatible(header, &document_mode)) {
479 TRACE("setting document mode %d\n", document_mode);
480 set_document_mode(doc, document_mode, FALSE);
482 heap_free(header);
485 IWinInetHttpInfo_Release(http_info);
488 static void process_meta_element(HTMLDocumentNode *doc, nsIDOMHTMLMetaElement *meta_element)
490 nsAString http_equiv_str, content_str;
491 nsresult nsres;
493 nsAString_Init(&http_equiv_str, NULL);
494 nsAString_Init(&content_str, NULL);
495 nsres = nsIDOMHTMLMetaElement_GetHttpEquiv(meta_element, &http_equiv_str);
496 if(NS_SUCCEEDED(nsres))
497 nsres = nsIDOMHTMLMetaElement_GetContent(meta_element, &content_str);
499 if(NS_SUCCEEDED(nsres)) {
500 const PRUnichar *http_equiv, *content;
502 nsAString_GetData(&http_equiv_str, &http_equiv);
503 nsAString_GetData(&content_str, &content);
505 TRACE("%s: %s\n", debugstr_w(http_equiv), debugstr_w(content));
507 if(!wcsicmp(http_equiv, L"x-ua-compatible")) {
508 compat_mode_t document_mode;
509 if(parse_ua_compatible(content, &document_mode))
510 set_document_mode(doc, document_mode, TRUE);
511 else
512 FIXME("Unsupported document mode %s\n", debugstr_w(content));
516 nsAString_Finish(&http_equiv_str);
517 nsAString_Finish(&content_str);
520 typedef struct nsRunnable nsRunnable;
522 typedef nsresult (*runnable_proc_t)(HTMLDocumentNode*,nsISupports*,nsISupports*);
524 struct nsRunnable {
525 nsIRunnable nsIRunnable_iface;
527 LONG ref;
529 runnable_proc_t proc;
531 HTMLDocumentNode *doc;
532 nsISupports *arg1;
533 nsISupports *arg2;
536 static inline nsRunnable *impl_from_nsIRunnable(nsIRunnable *iface)
538 return CONTAINING_RECORD(iface, nsRunnable, nsIRunnable_iface);
541 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
542 nsIIDRef riid, void **result)
544 nsRunnable *This = impl_from_nsIRunnable(iface);
546 if(IsEqualGUID(riid, &IID_nsISupports)) {
547 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
548 *result = &This->nsIRunnable_iface;
549 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
550 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
551 *result = &This->nsIRunnable_iface;
552 }else {
553 *result = NULL;
554 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
555 return NS_NOINTERFACE;
558 nsISupports_AddRef((nsISupports*)*result);
559 return NS_OK;
562 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
564 nsRunnable *This = impl_from_nsIRunnable(iface);
565 LONG ref = InterlockedIncrement(&This->ref);
567 TRACE("(%p) ref=%d\n", This, ref);
569 return ref;
572 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
574 nsRunnable *This = impl_from_nsIRunnable(iface);
575 LONG ref = InterlockedDecrement(&This->ref);
577 TRACE("(%p) ref=%d\n", This, ref);
579 if(!ref) {
580 htmldoc_release(&This->doc->basedoc);
581 if(This->arg1)
582 nsISupports_Release(This->arg1);
583 if(This->arg2)
584 nsISupports_Release(This->arg2);
585 heap_free(This);
588 return ref;
591 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
593 nsRunnable *This = impl_from_nsIRunnable(iface);
595 return This->proc(This->doc, This->arg1, This->arg2);
598 static const nsIRunnableVtbl nsRunnableVtbl = {
599 nsRunnable_QueryInterface,
600 nsRunnable_AddRef,
601 nsRunnable_Release,
602 nsRunnable_Run
605 static void add_script_runner(HTMLDocumentNode *This, runnable_proc_t proc, nsISupports *arg1, nsISupports *arg2)
607 nsRunnable *runnable;
609 runnable = heap_alloc_zero(sizeof(*runnable));
610 if(!runnable)
611 return;
613 runnable->nsIRunnable_iface.lpVtbl = &nsRunnableVtbl;
614 runnable->ref = 1;
616 htmldoc_addref(&This->basedoc);
617 runnable->doc = This;
618 runnable->proc = proc;
620 if(arg1)
621 nsISupports_AddRef(arg1);
622 runnable->arg1 = arg1;
624 if(arg2)
625 nsISupports_AddRef(arg2);
626 runnable->arg2 = arg2;
628 nsIContentUtils_AddScriptRunner(content_utils, &runnable->nsIRunnable_iface);
630 nsIRunnable_Release(&runnable->nsIRunnable_iface);
633 static inline HTMLDocumentNode *impl_from_nsIDocumentObserver(nsIDocumentObserver *iface)
635 return CONTAINING_RECORD(iface, HTMLDocumentNode, nsIDocumentObserver_iface);
638 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
639 nsIIDRef riid, void **result)
641 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
643 if(IsEqualGUID(&IID_nsISupports, riid)) {
644 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
645 *result = &This->nsIDocumentObserver_iface;
646 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
647 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
648 *result = &This->nsIDocumentObserver_iface;
649 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
650 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
651 *result = &This->nsIDocumentObserver_iface;
652 }else {
653 *result = NULL;
654 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
655 return NS_NOINTERFACE;
658 htmldoc_addref(&This->basedoc);
659 return NS_OK;
662 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
664 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
665 return htmldoc_addref(&This->basedoc);
668 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
670 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
671 return htmldoc_release(&This->basedoc);
674 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
675 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
679 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
680 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
684 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
685 void *aElement, LONG aNameSpaceID, nsIAtom *aAttribute, LONG aModType, const nsAttrValue *aNewValue)
689 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
690 void *aElement, LONG aNameSpaceID, nsIAtom *aAttribute, LONG aModType, const nsAttrValue *aOldValue)
694 static void NSAPI nsDocumentObserver_NativeAnonymousChildListChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
695 nsIContent *aContent, cpp_bool aIsRemove)
699 static void NSAPI nsDocumentObserver_AttributeSetToCurrentValue(nsIDocumentObserver *iface, nsIDocument *aDocument,
700 void *aElement, LONG aNameSpaceID, nsIAtom *aAttribute)
704 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
705 nsIContent *aContainer, nsIContent *aFirstNewContent, LONG aNewIndexInContainer)
709 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
710 nsIContent *aContainer, nsIContent *aChild, LONG aIndexInContainer)
714 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
715 nsIContent *aContainer, nsIContent *aChild, LONG aIndexInContainer,
716 nsIContent *aProviousSibling)
720 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
724 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
728 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
729 nsUpdateType aUpdateType)
733 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
734 nsUpdateType aUpdateType)
738 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
742 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
744 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
746 TRACE("(%p)\n", This);
748 if(This->skip_mutation_notif)
749 return;
751 This->content_ready = TRUE;
752 add_script_runner(This, run_end_load, NULL, NULL);
755 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
756 nsIContent *aContent, EventStates aStateMask)
760 static void NSAPI nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
761 EventStates aStateMask)
765 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, mozilla_StyleSheetHandle aStyleSheet,
766 cpp_bool aDocumentSheet)
770 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, mozilla_StyleSheetHandle aStyleSheet,
771 cpp_bool aDocumentSheet)
775 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
776 mozilla_StyleSheetHandle aStyleSheet)
780 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, mozilla_StyleSheetHandle aStyleSheet)
784 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, mozilla_StyleSheetHandle aStyleSheet)
788 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, mozilla_StyleSheetHandle aStyleSheet)
792 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
793 nsIContent *aContent)
795 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
796 nsIDOMHTMLIFrameElement *nsiframe;
797 nsIDOMHTMLFrameElement *nsframe;
798 nsIDOMHTMLScriptElement *nsscript;
799 nsIDOMHTMLMetaElement *nsmeta;
800 nsIDOMElement *nselem;
801 nsIDOMComment *nscomment;
802 nsresult nsres;
804 TRACE("(%p)->(%p %p)\n", This, aDocument, aContent);
806 if(This->document_mode < COMPAT_MODE_IE10) {
807 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
808 if(NS_SUCCEEDED(nsres)) {
809 TRACE("comment node\n");
811 add_script_runner(This, run_insert_comment, (nsISupports*)nscomment, NULL);
812 nsIDOMComment_Release(nscomment);
813 return;
817 if(This->document_mode == COMPAT_MODE_QUIRKS) {
818 nsIDOMDocumentType *nsdoctype;
820 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMDocumentType, (void**)&nsdoctype);
821 if(NS_SUCCEEDED(nsres)) {
822 compat_mode_t mode = COMPAT_MODE_IE7;
824 TRACE("doctype node\n");
826 if(This->window && This->window->base.outer_window) {
827 HTMLOuterWindow *window = This->window->base.outer_window;
828 DWORD zone;
829 HRESULT hres;
831 /* Internet URL zone is treated differently and defaults to the latest supported mode. */
832 hres = IInternetSecurityManager_MapUrlToZone(get_security_manager(), window->url, &zone, 0);
833 if(SUCCEEDED(hres) && zone == URLZONE_INTERNET)
834 mode = COMPAT_MODE_IE11;
837 set_document_mode(This, mode, FALSE);
838 nsIDOMDocumentType_Release(nsdoctype);
842 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
843 if(NS_FAILED(nsres))
844 return;
846 check_event_attr(This, nselem);
847 nsIDOMElement_Release(nselem);
849 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
850 if(NS_SUCCEEDED(nsres)) {
851 TRACE("iframe node\n");
853 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsiframe, NULL);
854 nsIDOMHTMLIFrameElement_Release(nsiframe);
855 return;
858 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLFrameElement, (void**)&nsframe);
859 if(NS_SUCCEEDED(nsres)) {
860 TRACE("frame node\n");
862 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsframe, NULL);
863 nsIDOMHTMLFrameElement_Release(nsframe);
864 return;
867 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
868 if(NS_SUCCEEDED(nsres)) {
869 TRACE("script element\n");
871 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsscript, NULL);
872 nsIDOMHTMLScriptElement_Release(nsscript);
873 return;
876 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLMetaElement, (void**)&nsmeta);
877 if(NS_SUCCEEDED(nsres)) {
878 process_meta_element(This, nsmeta);
879 nsIDOMHTMLMetaElement_Release(nsmeta);
883 static void NSAPI nsDocumentObserver_AttemptToExecuteScript(nsIDocumentObserver *iface, nsIContent *aContent,
884 nsIParser *aParser, cpp_bool *aBlock)
886 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
887 nsIDOMHTMLScriptElement *nsscript;
888 nsresult nsres;
890 TRACE("(%p)->(%p %p %p)\n", This, aContent, aParser, aBlock);
892 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
893 if(NS_SUCCEEDED(nsres)) {
894 TRACE("script node\n");
896 lock_document_mode(This);
897 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, (nsISupports*)aParser);
898 nsIDOMHTMLScriptElement_Release(nsscript);
902 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
903 nsDocumentObserver_QueryInterface,
904 nsDocumentObserver_AddRef,
905 nsDocumentObserver_Release,
906 nsDocumentObserver_CharacterDataWillChange,
907 nsDocumentObserver_CharacterDataChanged,
908 nsDocumentObserver_AttributeWillChange,
909 nsDocumentObserver_AttributeChanged,
910 nsDocumentObserver_NativeAnonymousChildListChange,
911 nsDocumentObserver_AttributeSetToCurrentValue,
912 nsDocumentObserver_ContentAppended,
913 nsDocumentObserver_ContentInserted,
914 nsDocumentObserver_ContentRemoved,
915 nsDocumentObserver_NodeWillBeDestroyed,
916 nsDocumentObserver_ParentChainChanged,
917 nsDocumentObserver_BeginUpdate,
918 nsDocumentObserver_EndUpdate,
919 nsDocumentObserver_BeginLoad,
920 nsDocumentObserver_EndLoad,
921 nsDocumentObserver_ContentStatesChanged,
922 nsDocumentObserver_DocumentStatesChanged,
923 nsDocumentObserver_StyleSheetAdded,
924 nsDocumentObserver_StyleSheetRemoved,
925 nsDocumentObserver_StyleSheetApplicableStateChanged,
926 nsDocumentObserver_StyleRuleChanged,
927 nsDocumentObserver_StyleRuleAdded,
928 nsDocumentObserver_StyleRuleRemoved,
929 nsDocumentObserver_BindToDocument,
930 nsDocumentObserver_AttemptToExecuteScript
933 void init_document_mutation(HTMLDocumentNode *doc)
935 nsIDocument *nsdoc;
936 nsresult nsres;
938 doc->nsIDocumentObserver_iface.lpVtbl = &nsDocumentObserverVtbl;
940 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
941 if(NS_FAILED(nsres)) {
942 ERR("Could not get nsIDocument: %08x\n", nsres);
943 return;
946 nsIContentUtils_AddDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
947 nsIDocument_Release(nsdoc);
950 void release_document_mutation(HTMLDocumentNode *doc)
952 nsIDocument *nsdoc;
953 nsresult nsres;
955 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
956 if(NS_FAILED(nsres)) {
957 ERR("Could not get nsIDocument: %08x\n", nsres);
958 return;
961 nsIContentUtils_RemoveDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
962 nsIDocument_Release(nsdoc);
965 JSContext *get_context_from_document(nsIDOMHTMLDocument *nsdoc)
967 nsIDocument *doc;
968 JSContext *ctx;
969 nsresult nsres;
971 nsres = nsIDOMHTMLDocument_QueryInterface(nsdoc, &IID_nsIDocument, (void**)&doc);
972 assert(nsres == NS_OK);
974 ctx = nsIContentUtils_GetContextFromDocument(content_utils, doc);
975 nsIDocument_Release(doc);
977 TRACE("ret %p\n", ctx);
978 return ctx;
981 void init_mutation(nsIComponentManager *component_manager)
983 nsIFactory *factory;
984 nsresult nsres;
986 if(!component_manager) {
987 if(content_utils) {
988 nsIContentUtils_Release(content_utils);
989 content_utils = NULL;
991 return;
994 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_ICONTENTUTILS_CID,
995 &IID_nsIFactory, (void**)&factory);
996 if(NS_FAILED(nsres)) {
997 ERR("Could not create nsIContentUtils service: %08x\n", nsres);
998 return;
1001 nsres = nsIFactory_CreateInstance(factory, NULL, &IID_nsIContentUtils, (void**)&content_utils);
1002 nsIFactory_Release(factory);
1003 if(NS_FAILED(nsres))
1004 ERR("Could not create nsIContentUtils instance: %08x\n", nsres);