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 set_ready_state(This->basedoc.window, READYSTATE_INTERACTIVE);
294 static nsresult run_insert_script(HTMLDocumentNode *doc, nsISupports *script_iface, nsISupports *parser_iface)
296 nsIDOMHTMLScriptElement *nsscript;
297 nsIParser *nsparser = NULL;
300 TRACE("(%p)->(%p)\n", doc, script_iface);
302 nsres = nsISupports_QueryInterface(script_iface, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
303 if(NS_FAILED(nsres)) {
304 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
309 nsres = nsISupports_QueryInterface(parser_iface, &IID_nsIParser, (void**)&nsparser);
310 if(NS_FAILED(nsres)) {
311 ERR("Could not get nsIParser iface: %08x\n", nsres);
317 nsIParser_BeginEvaluatingParserInsertedScript(nsparser);
319 doc_insert_script(doc->basedoc.window->base.inner_window, nsscript);
322 nsIParser_EndEvaluatingParserInsertedScript(nsparser);
323 nsIParser_Release(nsparser);
326 nsIDOMHTMLScriptElement_Release(nsscript);
330 typedef struct nsRunnable nsRunnable;
332 typedef nsresult (*runnable_proc_t)(HTMLDocumentNode*,nsISupports*,nsISupports*);
335 nsIRunnable nsIRunnable_iface;
339 runnable_proc_t proc;
341 HTMLDocumentNode *doc;
346 static inline nsRunnable *impl_from_nsIRunnable(nsIRunnable *iface)
348 return CONTAINING_RECORD(iface, nsRunnable, nsIRunnable_iface);
351 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
352 nsIIDRef riid, void **result)
354 nsRunnable *This = impl_from_nsIRunnable(iface);
356 if(IsEqualGUID(riid, &IID_nsISupports)) {
357 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
358 *result = &This->nsIRunnable_iface;
359 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
360 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
361 *result = &This->nsIRunnable_iface;
364 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
365 return NS_NOINTERFACE;
368 nsISupports_AddRef((nsISupports*)*result);
372 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
374 nsRunnable *This = impl_from_nsIRunnable(iface);
375 LONG ref = InterlockedIncrement(&This->ref);
377 TRACE("(%p) ref=%d\n", This, ref);
382 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
384 nsRunnable *This = impl_from_nsIRunnable(iface);
385 LONG ref = InterlockedDecrement(&This->ref);
387 TRACE("(%p) ref=%d\n", This, ref);
390 htmldoc_release(&This->doc->basedoc);
392 nsISupports_Release(This->arg1);
394 nsISupports_Release(This->arg2);
401 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
403 nsRunnable *This = impl_from_nsIRunnable(iface);
405 return This->proc(This->doc, This->arg1, This->arg2);
408 static const nsIRunnableVtbl nsRunnableVtbl = {
409 nsRunnable_QueryInterface,
415 static void add_script_runner(HTMLDocumentNode *This, runnable_proc_t proc, nsISupports *arg1, nsISupports *arg2)
417 nsRunnable *runnable;
419 runnable = heap_alloc_zero(sizeof(*runnable));
423 runnable->nsIRunnable_iface.lpVtbl = &nsRunnableVtbl;
426 htmldoc_addref(&This->basedoc);
427 runnable->doc = This;
428 runnable->proc = proc;
431 nsISupports_AddRef(arg1);
432 runnable->arg1 = arg1;
435 nsISupports_AddRef(arg2);
436 runnable->arg2 = arg2;
438 nsIContentUtils_AddScriptRunner(content_utils, &runnable->nsIRunnable_iface);
440 nsIRunnable_Release(&runnable->nsIRunnable_iface);
443 static inline HTMLDocumentNode *impl_from_nsIDocumentObserver(nsIDocumentObserver *iface)
445 return CONTAINING_RECORD(iface, HTMLDocumentNode, nsIDocumentObserver_iface);
448 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
449 nsIIDRef riid, void **result)
451 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
453 if(IsEqualGUID(&IID_nsISupports, riid)) {
454 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
455 *result = &This->nsIDocumentObserver_iface;
456 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
457 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
458 *result = &This->nsIDocumentObserver_iface;
459 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
460 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
461 *result = &This->nsIDocumentObserver_iface;
464 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
465 return NS_NOINTERFACE;
468 htmldoc_addref(&This->basedoc);
472 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
474 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
475 return htmldoc_addref(&This->basedoc);
478 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
480 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
481 return htmldoc_release(&This->basedoc);
484 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
485 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
489 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
490 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
494 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
495 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
499 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
500 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
504 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
505 nsIContent *aContainer, nsIContent *aFirstNewContent, PRInt32 aNewIndexInContainer)
509 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
510 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
514 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
515 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer,
516 nsIContent *aProviousSibling)
520 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
524 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
528 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
529 nsUpdateType aUpdateType)
533 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
534 nsUpdateType aUpdateType)
538 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
542 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
544 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
546 TRACE("(%p)\n", This);
548 if(This->skip_mutation_notif)
551 This->content_ready = TRUE;
552 add_script_runner(This, run_end_load, NULL, NULL);
555 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
556 nsIContent *aContent, nsEventStates *aStateMask)
560 static void NSAPI nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
561 nsEventStates *aStateMask)
565 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
566 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
570 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
571 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
575 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
576 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, cpp_bool aApplicable)
580 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
581 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
585 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
586 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
590 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
591 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
595 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
596 nsIContent *aContent)
598 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
599 nsIDOMHTMLIFrameElement *nsiframe;
600 nsIDOMHTMLFrameElement *nsframe;
601 nsIDOMComment *nscomment;
602 nsIDOMElement *nselem;
605 TRACE("(%p)\n", This);
607 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
608 if(NS_SUCCEEDED(nsres)) {
609 check_event_attr(This, nselem);
610 nsIDOMElement_Release(nselem);
613 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
614 if(NS_SUCCEEDED(nsres)) {
615 TRACE("comment node\n");
617 add_script_runner(This, run_insert_comment, (nsISupports*)nscomment, NULL);
618 nsIDOMComment_Release(nscomment);
621 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
622 if(NS_SUCCEEDED(nsres)) {
623 TRACE("iframe node\n");
625 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsiframe, NULL);
626 nsIDOMHTMLIFrameElement_Release(nsiframe);
629 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLFrameElement, (void**)&nsframe);
630 if(NS_SUCCEEDED(nsres)) {
631 TRACE("frame node\n");
633 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsframe, NULL);
634 nsIDOMHTMLFrameElement_Release(nsframe);
638 static void NSAPI nsDocumentObserver_AttemptToExecuteScript(nsIDocumentObserver *iface, nsIContent *aContent,
639 nsIParser *aParser, cpp_bool *aBlock)
641 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
642 nsIDOMHTMLScriptElement *nsscript;
645 TRACE("(%p)->(%p %p %p)\n", This, aContent, aParser, aBlock);
647 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
648 if(NS_SUCCEEDED(nsres)) {
649 TRACE("script node\n");
651 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, (nsISupports*)aParser);
652 nsIDOMHTMLScriptElement_Release(nsscript);
656 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
657 nsDocumentObserver_QueryInterface,
658 nsDocumentObserver_AddRef,
659 nsDocumentObserver_Release,
660 nsDocumentObserver_CharacterDataWillChange,
661 nsDocumentObserver_CharacterDataChanged,
662 nsDocumentObserver_AttributeWillChange,
663 nsDocumentObserver_AttributeChanged,
664 nsDocumentObserver_ContentAppended,
665 nsDocumentObserver_ContentInserted,
666 nsDocumentObserver_ContentRemoved,
667 nsDocumentObserver_NodeWillBeDestroyed,
668 nsDocumentObserver_ParentChainChanged,
669 nsDocumentObserver_BeginUpdate,
670 nsDocumentObserver_EndUpdate,
671 nsDocumentObserver_BeginLoad,
672 nsDocumentObserver_EndLoad,
673 nsDocumentObserver_ContentStatesChanged,
674 nsDocumentObserver_DocumentStatesChanged,
675 nsDocumentObserver_StyleSheetAdded,
676 nsDocumentObserver_StyleSheetRemoved,
677 nsDocumentObserver_StyleSheetApplicableStateChanged,
678 nsDocumentObserver_StyleRuleChanged,
679 nsDocumentObserver_StyleRuleAdded,
680 nsDocumentObserver_StyleRuleRemoved,
681 nsDocumentObserver_BindToDocument,
682 nsDocumentObserver_AttemptToExecuteScript
685 void init_document_mutation(HTMLDocumentNode *doc)
690 doc->nsIDocumentObserver_iface.lpVtbl = &nsDocumentObserverVtbl;
692 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
693 if(NS_FAILED(nsres)) {
694 ERR("Could not get nsIDocument: %08x\n", nsres);
698 nsIContentUtils_AddDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
699 nsIDocument_Release(nsdoc);
702 void release_document_mutation(HTMLDocumentNode *doc)
707 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
708 if(NS_FAILED(nsres)) {
709 ERR("Could not get nsIDocument: %08x\n", nsres);
713 nsIContentUtils_RemoveDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
714 nsIDocument_Release(nsdoc);
717 void init_mutation(nsIComponentManager *component_manager)
722 if(!component_manager) {
724 nsIContentUtils_Release(content_utils);
725 content_utils = NULL;
730 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_ICONTENTUTILS_CID,
731 &IID_nsIFactory, (void**)&factory);
732 if(NS_FAILED(nsres)) {
733 ERR("Could not create nsIContentUtils service: %08x\n", nsres);
737 nsres = nsIFactory_CreateInstance(factory, NULL, &IID_nsIContentUtils, (void**)&content_utils);
738 nsIFactory_Release(factory);
740 ERR("Could not create nsIContentUtils instance: %08x\n", nsres);