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
32 #include "mshtml_private.h"
33 #include "htmlevent.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 #define IE_MAJOR_VERSION 7
40 #define IE_MINOR_VERSION 0
42 static const IID NS_ICONTENTUTILS_CID =
43 {0x762C4AE7,0xB923,0x422F,{0xB9,0x7E,0xB9,0xBF,0xC1,0xEF,0x7B,0xF0}};
45 static nsIContentUtils *content_utils;
47 static PRUnichar *handle_insert_comment(HTMLDocumentNode *doc, const PRUnichar *comment)
49 int majorv = 0, minorv = 0;
50 const PRUnichar *ptr, *end;
62 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
64 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
71 if(ptr[0] == 'l' && ptr[1] == 't') {
79 }else if(ptr[0] == 'g' && ptr[1] == 't') {
94 if(ptr[0] != 'I' || ptr[1] != 'E')
100 while(isspaceW(*ptr))
105 while(isdigitW(*ptr))
106 majorv = majorv*10 + (*ptr++ - '0');
112 while(isdigitW(*ptr))
113 minorv = minorv*10 + (*ptr++ - '0');
116 while(isspaceW(*ptr))
118 if(ptr[0] != ']' || ptr[1] != '>')
123 if(len < sizeof(endifW)/sizeof(WCHAR))
126 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
127 if(memcmp(end, endifW, sizeof(endifW)))
132 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
136 if(majorv > IE_MAJOR_VERSION)
138 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
142 if(majorv > IE_MAJOR_VERSION)
144 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
148 if(majorv < IE_MAJOR_VERSION)
150 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
154 if(majorv < IE_MAJOR_VERSION)
156 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
161 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
165 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
171 static nsresult run_insert_comment(HTMLDocumentNode *doc, nsISupports *comment_iface, nsISupports *arg2)
173 const PRUnichar *comment;
174 nsIDOMComment *nscomment;
175 PRUnichar *replace_html;
176 nsAString comment_str;
179 nsres = nsISupports_QueryInterface(comment_iface, &IID_nsIDOMComment, (void**)&nscomment);
180 if(NS_FAILED(nsres)) {
181 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
185 nsAString_Init(&comment_str, NULL);
186 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
190 nsAString_GetData(&comment_str, &comment);
191 replace_html = handle_insert_comment(doc, comment);
192 nsAString_Finish(&comment_str);
197 hres = replace_node_by_html(doc->nsdoc, (nsIDOMNode*)nscomment, replace_html);
198 heap_free(replace_html);
200 nsres = NS_ERROR_FAILURE;
204 nsIDOMComment_Release(nscomment);
208 static nsresult run_bind_to_tree(HTMLDocumentNode *doc, nsISupports *nsiface, nsISupports *arg2)
215 TRACE("(%p)->(%p)\n", doc, nsiface);
217 nsres = nsISupports_QueryInterface(nsiface, &IID_nsIDOMNode, (void**)&nsnode);
221 hres = get_node(doc, nsnode, TRUE, &node);
222 nsIDOMNode_Release(nsnode);
224 ERR("Could not get node\n");
228 if(node->vtbl->bind_to_tree)
229 node->vtbl->bind_to_tree(node);
235 /* Calls undocumented 69 cmd of CGID_Explorer */
236 static void call_explorer_69(HTMLDocumentObj *doc)
238 IOleCommandTarget *olecmd;
245 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
250 hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
251 IOleCommandTarget_Release(olecmd);
252 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
253 FIXME("handle result\n");
256 static void parse_complete(HTMLDocumentObj *doc)
258 TRACE("(%p)\n", doc);
260 if(doc->usermode == EDITMODE)
261 init_editor(&doc->basedoc);
263 call_explorer_69(doc);
265 IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
266 call_property_onchanged(&doc->basedoc.cp_propnotif, 1005);
267 call_explorer_69(doc);
269 if(doc->is_webbrowser && doc->usermode != EDITMODE)
270 IDocObjectService_FireNavigateComplete2(doc->doc_object_service, &doc->basedoc.window->base.IHTMLWindow2_iface, 0);
272 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
275 static nsresult run_end_load(HTMLDocumentNode *This, nsISupports *arg1, nsISupports *arg2)
277 TRACE("(%p)\n", This);
279 if(!This->basedoc.doc_obj)
282 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
284 * This should be done in the worker thread that parses HTML,
285 * but we don't have such thread (Gecko parses HTML for us).
287 parse_complete(This->basedoc.doc_obj);
290 bind_event_scripts(This);
291 set_ready_state(This->basedoc.window, READYSTATE_INTERACTIVE);
295 static nsresult run_insert_script(HTMLDocumentNode *doc, nsISupports *script_iface, nsISupports *parser_iface)
297 nsIDOMHTMLScriptElement *nsscript;
298 HTMLScriptElement *script_elem;
299 nsIParser *nsparser = NULL;
303 TRACE("(%p)->(%p)\n", doc, script_iface);
305 nsres = nsISupports_QueryInterface(script_iface, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
306 if(NS_FAILED(nsres)) {
307 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
312 nsres = nsISupports_QueryInterface(parser_iface, &IID_nsIParser, (void**)&nsparser);
313 if(NS_FAILED(nsres)) {
314 ERR("Could not get nsIParser iface: %08x\n", nsres);
319 hres = script_elem_from_nsscript(doc, nsscript, &script_elem);
320 nsIDOMHTMLScriptElement_Release(nsscript);
322 return NS_ERROR_FAILURE;
325 nsIParser_BeginEvaluatingParserInsertedScript(nsparser);
327 doc_insert_script(doc->window, script_elem);
330 nsIParser_EndEvaluatingParserInsertedScript(nsparser);
331 nsIParser_Release(nsparser);
334 IHTMLScriptElement_Release(&script_elem->IHTMLScriptElement_iface);
338 typedef struct nsRunnable nsRunnable;
340 typedef nsresult (*runnable_proc_t)(HTMLDocumentNode*,nsISupports*,nsISupports*);
343 nsIRunnable nsIRunnable_iface;
347 runnable_proc_t proc;
349 HTMLDocumentNode *doc;
354 static inline nsRunnable *impl_from_nsIRunnable(nsIRunnable *iface)
356 return CONTAINING_RECORD(iface, nsRunnable, nsIRunnable_iface);
359 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
360 nsIIDRef riid, void **result)
362 nsRunnable *This = impl_from_nsIRunnable(iface);
364 if(IsEqualGUID(riid, &IID_nsISupports)) {
365 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
366 *result = &This->nsIRunnable_iface;
367 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
368 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
369 *result = &This->nsIRunnable_iface;
372 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
373 return NS_NOINTERFACE;
376 nsISupports_AddRef((nsISupports*)*result);
380 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
382 nsRunnable *This = impl_from_nsIRunnable(iface);
383 LONG ref = InterlockedIncrement(&This->ref);
385 TRACE("(%p) ref=%d\n", This, ref);
390 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
392 nsRunnable *This = impl_from_nsIRunnable(iface);
393 LONG ref = InterlockedDecrement(&This->ref);
395 TRACE("(%p) ref=%d\n", This, ref);
398 htmldoc_release(&This->doc->basedoc);
400 nsISupports_Release(This->arg1);
402 nsISupports_Release(This->arg2);
409 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
411 nsRunnable *This = impl_from_nsIRunnable(iface);
413 return This->proc(This->doc, This->arg1, This->arg2);
416 static const nsIRunnableVtbl nsRunnableVtbl = {
417 nsRunnable_QueryInterface,
423 static void add_script_runner(HTMLDocumentNode *This, runnable_proc_t proc, nsISupports *arg1, nsISupports *arg2)
425 nsRunnable *runnable;
427 runnable = heap_alloc_zero(sizeof(*runnable));
431 runnable->nsIRunnable_iface.lpVtbl = &nsRunnableVtbl;
434 htmldoc_addref(&This->basedoc);
435 runnable->doc = This;
436 runnable->proc = proc;
439 nsISupports_AddRef(arg1);
440 runnable->arg1 = arg1;
443 nsISupports_AddRef(arg2);
444 runnable->arg2 = arg2;
446 nsIContentUtils_AddScriptRunner(content_utils, &runnable->nsIRunnable_iface);
448 nsIRunnable_Release(&runnable->nsIRunnable_iface);
451 static inline HTMLDocumentNode *impl_from_nsIDocumentObserver(nsIDocumentObserver *iface)
453 return CONTAINING_RECORD(iface, HTMLDocumentNode, nsIDocumentObserver_iface);
456 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
457 nsIIDRef riid, void **result)
459 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
461 if(IsEqualGUID(&IID_nsISupports, riid)) {
462 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
463 *result = &This->nsIDocumentObserver_iface;
464 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
465 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
466 *result = &This->nsIDocumentObserver_iface;
467 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
468 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
469 *result = &This->nsIDocumentObserver_iface;
472 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
473 return NS_NOINTERFACE;
476 htmldoc_addref(&This->basedoc);
480 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
482 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
483 return htmldoc_addref(&This->basedoc);
486 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
488 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
489 return htmldoc_release(&This->basedoc);
492 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
493 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
497 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
498 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
502 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
503 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
507 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
508 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
512 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
513 nsIContent *aContainer, nsIContent *aFirstNewContent, PRInt32 aNewIndexInContainer)
517 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
518 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
522 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
523 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer,
524 nsIContent *aProviousSibling)
528 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
532 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
536 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
537 nsUpdateType aUpdateType)
541 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
542 nsUpdateType aUpdateType)
546 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
550 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
552 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
554 TRACE("(%p)\n", This);
556 if(This->skip_mutation_notif)
559 This->content_ready = TRUE;
560 add_script_runner(This, run_end_load, NULL, NULL);
563 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
564 nsIContent *aContent, nsEventStates *aStateMask)
568 static void NSAPI nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
569 nsEventStates *aStateMask)
573 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
574 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
578 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
579 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
583 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
584 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, cpp_bool aApplicable)
588 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
589 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
593 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
594 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
598 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
599 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
603 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
604 nsIContent *aContent)
606 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
607 nsIDOMHTMLIFrameElement *nsiframe;
608 nsIDOMHTMLFrameElement *nsframe;
609 nsIDOMComment *nscomment;
610 nsIDOMElement *nselem;
613 TRACE("(%p)\n", This);
615 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
616 if(NS_SUCCEEDED(nsres)) {
617 check_event_attr(This, nselem);
618 nsIDOMElement_Release(nselem);
621 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
622 if(NS_SUCCEEDED(nsres)) {
623 TRACE("comment node\n");
625 add_script_runner(This, run_insert_comment, (nsISupports*)nscomment, NULL);
626 nsIDOMComment_Release(nscomment);
629 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
630 if(NS_SUCCEEDED(nsres)) {
631 TRACE("iframe node\n");
633 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsiframe, NULL);
634 nsIDOMHTMLIFrameElement_Release(nsiframe);
637 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLFrameElement, (void**)&nsframe);
638 if(NS_SUCCEEDED(nsres)) {
639 TRACE("frame node\n");
641 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsframe, NULL);
642 nsIDOMHTMLFrameElement_Release(nsframe);
646 static void NSAPI nsDocumentObserver_AttemptToExecuteScript(nsIDocumentObserver *iface, nsIContent *aContent,
647 nsIParser *aParser, cpp_bool *aBlock)
649 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
650 nsIDOMHTMLScriptElement *nsscript;
653 TRACE("(%p)->(%p %p %p)\n", This, aContent, aParser, aBlock);
655 nsres = nsIContent_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
656 if(NS_SUCCEEDED(nsres)) {
657 TRACE("script node\n");
659 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, (nsISupports*)aParser);
660 nsIDOMHTMLScriptElement_Release(nsscript);
664 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
665 nsDocumentObserver_QueryInterface,
666 nsDocumentObserver_AddRef,
667 nsDocumentObserver_Release,
668 nsDocumentObserver_CharacterDataWillChange,
669 nsDocumentObserver_CharacterDataChanged,
670 nsDocumentObserver_AttributeWillChange,
671 nsDocumentObserver_AttributeChanged,
672 nsDocumentObserver_ContentAppended,
673 nsDocumentObserver_ContentInserted,
674 nsDocumentObserver_ContentRemoved,
675 nsDocumentObserver_NodeWillBeDestroyed,
676 nsDocumentObserver_ParentChainChanged,
677 nsDocumentObserver_BeginUpdate,
678 nsDocumentObserver_EndUpdate,
679 nsDocumentObserver_BeginLoad,
680 nsDocumentObserver_EndLoad,
681 nsDocumentObserver_ContentStatesChanged,
682 nsDocumentObserver_DocumentStatesChanged,
683 nsDocumentObserver_StyleSheetAdded,
684 nsDocumentObserver_StyleSheetRemoved,
685 nsDocumentObserver_StyleSheetApplicableStateChanged,
686 nsDocumentObserver_StyleRuleChanged,
687 nsDocumentObserver_StyleRuleAdded,
688 nsDocumentObserver_StyleRuleRemoved,
689 nsDocumentObserver_BindToDocument,
690 nsDocumentObserver_AttemptToExecuteScript
693 void init_document_mutation(HTMLDocumentNode *doc)
698 doc->nsIDocumentObserver_iface.lpVtbl = &nsDocumentObserverVtbl;
700 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
701 if(NS_FAILED(nsres)) {
702 ERR("Could not get nsIDocument: %08x\n", nsres);
706 nsIContentUtils_AddDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
707 nsIDocument_Release(nsdoc);
710 void release_document_mutation(HTMLDocumentNode *doc)
715 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
716 if(NS_FAILED(nsres)) {
717 ERR("Could not get nsIDocument: %08x\n", nsres);
721 nsIContentUtils_RemoveDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
722 nsIDocument_Release(nsdoc);
725 void init_mutation(nsIComponentManager *component_manager)
730 if(!component_manager) {
732 nsIContentUtils_Release(content_utils);
733 content_utils = NULL;
738 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_ICONTENTUTILS_CID,
739 &IID_nsIFactory, (void**)&factory);
740 if(NS_FAILED(nsres)) {
741 ERR("Could not create nsIContentUtils service: %08x\n", nsres);
745 nsres = nsIFactory_CreateInstance(factory, NULL, &IID_nsIContentUtils, (void**)&content_utils);
746 nsIFactory_Release(factory);
748 ERR("Could not create nsIContentUtils instance: %08x\n", nsres);