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);
234 /* Calls undocumented 69 cmd of CGID_Explorer */
235 static void call_explorer_69(HTMLDocumentObj *doc)
237 IOleCommandTarget *olecmd;
244 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
249 hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
250 IOleCommandTarget_Release(olecmd);
251 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
252 FIXME("handle result\n");
255 static void parse_complete(HTMLDocumentObj *doc)
257 TRACE("(%p)\n", doc);
259 if(doc->usermode == EDITMODE)
260 init_editor(&doc->basedoc);
262 call_explorer_69(doc);
264 IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
265 call_property_onchanged(&doc->basedoc.cp_propnotif, 1005);
266 call_explorer_69(doc);
268 if(doc->is_webbrowser && doc->usermode != EDITMODE)
269 IDocObjectService_FireNavigateComplete2(doc->doc_object_service, &doc->basedoc.window->IHTMLWindow2_iface, 0);
271 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
274 static nsresult run_end_load(HTMLDocumentNode *This, nsISupports *arg1, nsISupports *arg2)
276 TRACE("(%p)\n", This);
278 if(!This->basedoc.doc_obj)
281 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
283 * This should be done in the worker thread that parses HTML,
284 * but we don't have such thread (Gecko parses HTML for us).
286 parse_complete(This->basedoc.doc_obj);
289 set_ready_state(This->basedoc.window, READYSTATE_INTERACTIVE);
293 static nsresult run_insert_script(HTMLDocumentNode *doc, nsISupports *script_iface, nsISupports *parser_iface)
295 nsIDOMHTMLScriptElement *nsscript;
296 nsIParser *nsparser = NULL;
299 TRACE("(%p)->(%p)\n", doc, script_iface);
301 nsres = nsISupports_QueryInterface(script_iface, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
302 if(NS_FAILED(nsres)) {
303 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
308 nsres = nsISupports_QueryInterface(parser_iface, &IID_nsIParser, (void**)&nsparser);
309 if(NS_FAILED(nsres)) {
310 ERR("Could not get nsIParser iface: %08x\n", nsres);
316 nsIParser_BeginEvaluatingParserInsertedScript(nsparser);
318 doc_insert_script(doc->basedoc.window, nsscript);
321 nsIParser_EndEvaluatingParserInsertedScript(nsparser);
322 nsIParser_Release(nsparser);
325 nsIDOMHTMLScriptElement_Release(nsscript);
329 typedef struct nsRunnable nsRunnable;
331 typedef nsresult (*runnable_proc_t)(HTMLDocumentNode*,nsISupports*,nsISupports*);
334 nsIRunnable nsIRunnable_iface;
338 runnable_proc_t proc;
340 HTMLDocumentNode *doc;
345 static inline nsRunnable *impl_from_nsIRunnable(nsIRunnable *iface)
347 return CONTAINING_RECORD(iface, nsRunnable, nsIRunnable_iface);
350 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
351 nsIIDRef riid, void **result)
353 nsRunnable *This = impl_from_nsIRunnable(iface);
355 if(IsEqualGUID(riid, &IID_nsISupports)) {
356 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
357 *result = &This->nsIRunnable_iface;
358 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
359 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
360 *result = &This->nsIRunnable_iface;
363 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
364 return NS_NOINTERFACE;
367 nsISupports_AddRef((nsISupports*)*result);
371 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
373 nsRunnable *This = impl_from_nsIRunnable(iface);
374 LONG ref = InterlockedIncrement(&This->ref);
376 TRACE("(%p) ref=%d\n", This, ref);
381 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
383 nsRunnable *This = impl_from_nsIRunnable(iface);
384 LONG ref = InterlockedDecrement(&This->ref);
386 TRACE("(%p) ref=%d\n", This, ref);
389 htmldoc_release(&This->doc->basedoc);
391 nsISupports_Release(This->arg1);
393 nsISupports_Release(This->arg2);
400 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
402 nsRunnable *This = impl_from_nsIRunnable(iface);
404 return This->proc(This->doc, This->arg1, This->arg2);
407 static const nsIRunnableVtbl nsRunnableVtbl = {
408 nsRunnable_QueryInterface,
414 static void add_script_runner(HTMLDocumentNode *This, runnable_proc_t proc, nsISupports *arg1, nsISupports *arg2)
416 nsRunnable *runnable;
418 runnable = heap_alloc_zero(sizeof(*runnable));
422 runnable->nsIRunnable_iface.lpVtbl = &nsRunnableVtbl;
425 htmldoc_addref(&This->basedoc);
426 runnable->doc = This;
427 runnable->proc = proc;
430 nsISupports_AddRef(arg1);
431 runnable->arg1 = arg1;
434 nsISupports_AddRef(arg2);
435 runnable->arg2 = arg2;
437 nsIContentUtils_AddScriptRunner(content_utils, &runnable->nsIRunnable_iface);
439 nsIRunnable_Release(&runnable->nsIRunnable_iface);
442 static inline HTMLDocumentNode *impl_from_nsIDocumentObserver(nsIDocumentObserver *iface)
444 return CONTAINING_RECORD(iface, HTMLDocumentNode, nsIDocumentObserver_iface);
447 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
448 nsIIDRef riid, void **result)
450 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
452 if(IsEqualGUID(&IID_nsISupports, riid)) {
453 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
454 *result = &This->nsIDocumentObserver_iface;
455 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
456 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
457 *result = &This->nsIDocumentObserver_iface;
458 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
459 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
460 *result = &This->nsIDocumentObserver_iface;
463 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
464 return NS_NOINTERFACE;
467 htmldoc_addref(&This->basedoc);
471 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
473 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
474 return htmldoc_addref(&This->basedoc);
477 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
479 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
480 return htmldoc_release(&This->basedoc);
483 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
484 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
488 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
489 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
493 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
494 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
498 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
499 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
503 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
504 nsIContent *aContainer, nsIContent *aFirstNewContent, PRInt32 aNewIndexInContainer)
508 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
509 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
513 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
514 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer,
515 nsIContent *aProviousSibling)
519 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
523 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
527 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
528 nsUpdateType aUpdateType)
532 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
533 nsUpdateType aUpdateType)
537 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
541 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
543 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
545 TRACE("(%p)\n", This);
547 if(This->skip_mutation_notif)
550 This->content_ready = TRUE;
551 add_script_runner(This, run_end_load, NULL, NULL);
554 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
555 nsIContent *aContent, nsEventStates *aStateMask)
559 static void NSAPI nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
560 nsEventStates *aStateMask)
564 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
565 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
569 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
570 nsIStyleSheet *aStyleSheet, cpp_bool aDocumentSheet)
574 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
575 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, cpp_bool aApplicable)
579 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
580 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
584 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
585 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
589 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
590 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
594 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
595 nsIContent *aContent)
597 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
598 nsIDOMHTMLIFrameElement *nsiframe;
599 nsIDOMHTMLFrameElement *nsframe;
600 nsIDOMComment *nscomment;
601 nsIDOMElement *nselem;
604 TRACE("(%p)\n", This);
606 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
607 if(NS_SUCCEEDED(nsres)) {
608 check_event_attr(This, nselem);
609 nsIDOMElement_Release(nselem);
612 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
613 if(NS_SUCCEEDED(nsres)) {
614 TRACE("comment node\n");
616 add_script_runner(This, run_insert_comment, (nsISupports*)nscomment, NULL);
617 nsIDOMComment_Release(nscomment);
620 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
621 if(NS_SUCCEEDED(nsres)) {
622 TRACE("iframe node\n");
624 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsiframe, NULL);
625 nsIDOMHTMLIFrameElement_Release(nsiframe);
628 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLFrameElement, (void**)&nsframe);
629 if(NS_SUCCEEDED(nsres)) {
630 TRACE("frame node\n");
632 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsframe, NULL);
633 nsIDOMHTMLFrameElement_Release(nsframe);
637 static void NSAPI nsDocumentObserver_AttemptToExecuteScript(nsIDocumentObserver *iface, nsIContent *aContent,
638 nsIParser *aParser, cpp_bool *aBlock)
640 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
641 nsIDOMHTMLScriptElement *nsscript;
644 TRACE("(%p)->(%p %p %p)\n", This, aContent, aParser, aBlock);
646 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
647 if(NS_SUCCEEDED(nsres)) {
648 TRACE("script node\n");
650 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, (nsISupports*)aParser);
651 nsIDOMHTMLScriptElement_Release(nsscript);
655 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
656 nsDocumentObserver_QueryInterface,
657 nsDocumentObserver_AddRef,
658 nsDocumentObserver_Release,
659 nsDocumentObserver_CharacterDataWillChange,
660 nsDocumentObserver_CharacterDataChanged,
661 nsDocumentObserver_AttributeWillChange,
662 nsDocumentObserver_AttributeChanged,
663 nsDocumentObserver_ContentAppended,
664 nsDocumentObserver_ContentInserted,
665 nsDocumentObserver_ContentRemoved,
666 nsDocumentObserver_NodeWillBeDestroyed,
667 nsDocumentObserver_ParentChainChanged,
668 nsDocumentObserver_BeginUpdate,
669 nsDocumentObserver_EndUpdate,
670 nsDocumentObserver_BeginLoad,
671 nsDocumentObserver_EndLoad,
672 nsDocumentObserver_ContentStatesChanged,
673 nsDocumentObserver_DocumentStatesChanged,
674 nsDocumentObserver_StyleSheetAdded,
675 nsDocumentObserver_StyleSheetRemoved,
676 nsDocumentObserver_StyleSheetApplicableStateChanged,
677 nsDocumentObserver_StyleRuleChanged,
678 nsDocumentObserver_StyleRuleAdded,
679 nsDocumentObserver_StyleRuleRemoved,
680 nsDocumentObserver_BindToDocument,
681 nsDocumentObserver_AttemptToExecuteScript
684 void init_document_mutation(HTMLDocumentNode *doc)
689 doc->nsIDocumentObserver_iface.lpVtbl = &nsDocumentObserverVtbl;
691 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
692 if(NS_FAILED(nsres)) {
693 ERR("Could not get nsIDocument: %08x\n", nsres);
697 nsIContentUtils_AddDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
698 nsIDocument_Release(nsdoc);
701 void release_document_mutation(HTMLDocumentNode *doc)
706 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
707 if(NS_FAILED(nsres)) {
708 ERR("Could not get nsIDocument: %08x\n", nsres);
712 nsIContentUtils_RemoveDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
713 nsIDocument_Release(nsdoc);
716 void init_mutation(nsIComponentManager *component_manager)
721 if(!component_manager) {
723 nsIContentUtils_Release(content_utils);
724 content_utils = NULL;
729 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_ICONTENTUTILS_CID,
730 &IID_nsIFactory, (void**)&factory);
731 if(NS_FAILED(nsres)) {
732 ERR("Could not create nsIContentUtils service: %08x\n", nsres);
736 nsres = nsIFactory_CreateInstance(factory, NULL, &IID_nsIContentUtils, (void**)&content_utils);
737 nsIFactory_Release(factory);
739 ERR("Could not create nsIContentUtils instance: %08x\n", nsres);