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 BOOL handle_insert_comment(HTMLDocumentNode *doc, const PRUnichar *comment)
50 int majorv = 0, minorv = 0;
51 const PRUnichar *ptr, *end;
64 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
66 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
73 if(ptr[0] == 'l' && ptr[1] == 't') {
81 }else if(ptr[0] == 'g' && ptr[1] == 't') {
96 if(ptr[0] != 'I' || ptr[1] != 'E')
100 if(!isspaceW(*ptr++))
102 while(isspaceW(*ptr))
107 while(isdigitW(*ptr))
108 majorv = majorv*10 + (*ptr++ - '0');
114 while(isdigitW(*ptr))
115 minorv = minorv*10 + (*ptr++ - '0');
118 while(isspaceW(*ptr))
120 if(ptr[0] != ']' || ptr[1] != '>')
125 if(len < sizeof(endifW)/sizeof(WCHAR))
128 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
129 if(memcmp(end, endifW, sizeof(endifW)))
134 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
138 if(majorv > IE_MAJOR_VERSION)
140 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
144 if(majorv > IE_MAJOR_VERSION)
146 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
150 if(majorv < IE_MAJOR_VERSION)
152 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
156 if(majorv < IE_MAJOR_VERSION)
158 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
163 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
167 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
169 nsAString_InitDepend(&nsstr, buf);
171 /* FIXME: Find better way to insert HTML to document. */
172 nsres = nsIDOMHTMLDocument_Write(doc->nsdoc, &nsstr);
173 nsAString_Finish(&nsstr);
175 if(NS_FAILED(nsres)) {
176 ERR("Write failed: %08x\n", nsres);
183 static nsresult run_insert_comment(HTMLDocumentNode *doc, nsISupports *comment_iface, nsISupports *arg2)
185 const PRUnichar *comment;
186 nsIDOMComment *nscomment;
187 nsAString comment_str;
191 nsres = nsISupports_QueryInterface(comment_iface, &IID_nsIDOMComment, (void**)&nscomment);
192 if(NS_FAILED(nsres)) {
193 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
197 nsAString_Init(&comment_str, NULL);
198 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
202 nsAString_GetData(&comment_str, &comment);
203 remove_comment = handle_insert_comment(doc, comment);
204 nsAString_Finish(&comment_str);
207 nsIDOMNode *nsparent, *tmp;
210 static const PRUnichar remove_comment_magicW[] =
211 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
213 nsAString_InitDepend(&magic_str, remove_comment_magicW);
214 nsres = nsIDOMComment_SetData(nscomment, &magic_str);
215 nsAString_Finish(&magic_str);
217 ERR("SetData failed: %08x\n", nsres);
219 nsIDOMComment_GetParentNode(nscomment, &nsparent);
221 nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
222 nsIDOMNode_Release(nsparent);
223 nsIDOMNode_Release(tmp);
227 nsIDOMComment_Release(nscomment);
231 static nsresult run_bind_to_tree(HTMLDocumentNode *doc, nsISupports *nsiface, nsISupports *arg2)
238 TRACE("(%p)->(%p)\n", doc, nsiface);
240 nsres = nsISupports_QueryInterface(nsiface, &IID_nsIDOMNode, (void**)&nsnode);
244 hres = get_node(doc, nsnode, TRUE, &node);
245 nsIDOMNode_Release(nsnode);
247 ERR("Could not get node\n");
251 if(node->vtbl->bind_to_tree)
252 node->vtbl->bind_to_tree(node);
257 /* Calls undocumented 69 cmd of CGID_Explorer */
258 static void call_explorer_69(HTMLDocumentObj *doc)
260 IOleCommandTarget *olecmd;
267 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
272 hres = IOleCommandTarget_Exec(olecmd, &CGID_Explorer, 69, 0, NULL, &var);
273 IOleCommandTarget_Release(olecmd);
274 if(SUCCEEDED(hres) && V_VT(&var) != VT_NULL)
275 FIXME("handle result\n");
278 static void parse_complete(HTMLDocumentObj *doc)
280 TRACE("(%p)\n", doc);
282 if(doc->usermode == EDITMODE)
283 init_editor(&doc->basedoc);
285 call_explorer_69(doc);
287 IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
288 call_property_onchanged(&doc->basedoc.cp_propnotif, 1005);
289 call_explorer_69(doc);
291 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
294 static nsresult run_end_load(HTMLDocumentNode *This, nsISupports *arg1, nsISupports *arg2)
296 TRACE("(%p)\n", This);
298 if(!This->basedoc.doc_obj)
301 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
303 * This should be done in the worker thread that parses HTML,
304 * but we don't have such thread (Gecko parses HTML for us).
306 parse_complete(This->basedoc.doc_obj);
309 set_ready_state(This->basedoc.window, READYSTATE_INTERACTIVE);
313 static nsresult run_insert_script(HTMLDocumentNode *doc, nsISupports *script_iface, nsISupports *parser_iface)
315 nsIDOMHTMLScriptElement *nsscript;
316 nsIParser *nsparser = NULL;
319 TRACE("(%p)->(%p)\n", doc, script_iface);
321 nsres = nsISupports_QueryInterface(script_iface, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
322 if(NS_FAILED(nsres)) {
323 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
328 nsres = nsISupports_QueryInterface(parser_iface, &IID_nsIParser, (void**)&nsparser);
329 if(NS_FAILED(nsres)) {
330 ERR("Could not get nsIParser iface: %08x\n", nsres);
336 nsIParser_BeginEvaluatingParserInsertedScript(nsparser);
338 doc_insert_script(doc->basedoc.window, nsscript);
341 nsIParser_EndEvaluatingParserInsertedScript(nsparser);
342 nsIParser_Release(nsparser);
345 nsIDOMHTMLScriptElement_Release(nsscript);
349 typedef struct nsRunnable nsRunnable;
351 typedef nsresult (*runnable_proc_t)(HTMLDocumentNode*,nsISupports*,nsISupports*);
354 nsIRunnable nsIRunnable_iface;
358 runnable_proc_t proc;
360 HTMLDocumentNode *doc;
365 static inline nsRunnable *impl_from_nsIRunnable(nsIRunnable *iface)
367 return CONTAINING_RECORD(iface, nsRunnable, nsIRunnable_iface);
370 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
371 nsIIDRef riid, void **result)
373 nsRunnable *This = impl_from_nsIRunnable(iface);
375 if(IsEqualGUID(riid, &IID_nsISupports)) {
376 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
377 *result = &This->nsIRunnable_iface;
378 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
379 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
380 *result = &This->nsIRunnable_iface;
383 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
384 return NS_NOINTERFACE;
387 nsISupports_AddRef((nsISupports*)*result);
391 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
393 nsRunnable *This = impl_from_nsIRunnable(iface);
394 LONG ref = InterlockedIncrement(&This->ref);
396 TRACE("(%p) ref=%d\n", This, ref);
401 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
403 nsRunnable *This = impl_from_nsIRunnable(iface);
404 LONG ref = InterlockedDecrement(&This->ref);
406 TRACE("(%p) ref=%d\n", This, ref);
409 htmldoc_release(&This->doc->basedoc);
411 nsISupports_Release(This->arg1);
413 nsISupports_Release(This->arg2);
420 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
422 nsRunnable *This = impl_from_nsIRunnable(iface);
424 return This->proc(This->doc, This->arg1, This->arg2);
427 static const nsIRunnableVtbl nsRunnableVtbl = {
428 nsRunnable_QueryInterface,
434 static void add_script_runner(HTMLDocumentNode *This, runnable_proc_t proc, nsISupports *arg1, nsISupports *arg2)
436 nsRunnable *runnable;
438 runnable = heap_alloc_zero(sizeof(*runnable));
442 runnable->nsIRunnable_iface.lpVtbl = &nsRunnableVtbl;
445 htmldoc_addref(&This->basedoc);
446 runnable->doc = This;
447 runnable->proc = proc;
450 nsISupports_AddRef(arg1);
451 runnable->arg1 = arg1;
454 nsISupports_AddRef(arg2);
455 runnable->arg2 = arg2;
457 nsIContentUtils_AddScriptRunner(content_utils, &runnable->nsIRunnable_iface);
459 nsIRunnable_Release(&runnable->nsIRunnable_iface);
462 static inline HTMLDocumentNode *impl_from_nsIDocumentObserver(nsIDocumentObserver *iface)
464 return CONTAINING_RECORD(iface, HTMLDocumentNode, nsIDocumentObserver_iface);
467 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
468 nsIIDRef riid, void **result)
470 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
472 if(IsEqualGUID(&IID_nsISupports, riid)) {
473 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
474 *result = &This->nsIDocumentObserver_iface;
475 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
476 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
477 *result = &This->nsIDocumentObserver_iface;
478 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
479 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
480 *result = &This->nsIDocumentObserver_iface;
483 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
484 return NS_NOINTERFACE;
487 htmldoc_addref(&This->basedoc);
491 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
493 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
494 return htmldoc_addref(&This->basedoc);
497 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
499 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
500 return htmldoc_release(&This->basedoc);
503 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
504 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
508 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
509 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
513 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
514 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
518 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
519 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
523 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
524 nsIContent *aContainer, nsIContent *aFirstNewContent, PRInt32 aNewIndexInContainer)
528 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
529 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
533 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
534 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer,
535 nsIContent *aProviousSibling)
539 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
543 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
547 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
548 nsUpdateType aUpdateType)
552 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
553 nsUpdateType aUpdateType)
557 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
561 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
563 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
565 TRACE("(%p)\n", This);
567 if(This->skip_mutation_notif)
570 This->content_ready = TRUE;
571 add_script_runner(This, run_end_load, NULL, NULL);
574 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
575 nsIContent *aContent1, nsIContent *aContent2, nsEventStates aStateMask)
579 static void NSAPI nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
580 nsEventStates aStateMask)
584 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
585 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
589 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
590 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
594 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
595 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
599 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
600 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
604 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
605 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
609 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
610 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
614 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
615 nsIContent *aContent)
617 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
618 nsIDOMHTMLIFrameElement *nsiframe;
619 nsIDOMHTMLFrameElement *nsframe;
620 nsIDOMComment *nscomment;
621 nsIDOMElement *nselem;
624 TRACE("(%p)\n", This);
626 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
627 if(NS_SUCCEEDED(nsres)) {
628 check_event_attr(This, nselem);
629 nsIDOMElement_Release(nselem);
632 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
633 if(NS_SUCCEEDED(nsres)) {
634 TRACE("comment node\n");
636 add_script_runner(This, run_insert_comment, (nsISupports*)nscomment, NULL);
637 nsIDOMComment_Release(nscomment);
640 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
641 if(NS_SUCCEEDED(nsres)) {
642 TRACE("iframe node\n");
644 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsiframe, NULL);
645 nsIDOMHTMLIFrameElement_Release(nsiframe);
648 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLFrameElement, (void**)&nsframe);
649 if(NS_SUCCEEDED(nsres)) {
650 TRACE("frame node\n");
652 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsframe, NULL);
653 nsIDOMHTMLFrameElement_Release(nsframe);
657 static nsresult NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
658 PRBool aHaveNotified, nsIParser *aParser)
660 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
661 nsIDOMHTMLScriptElement *nsscript;
664 TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
666 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
667 if(NS_SUCCEEDED(nsres)) {
668 TRACE("script node\n");
670 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, (nsISupports*)aParser);
671 nsIDOMHTMLScriptElement_Release(nsscript);
677 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
678 nsDocumentObserver_QueryInterface,
679 nsDocumentObserver_AddRef,
680 nsDocumentObserver_Release,
681 nsDocumentObserver_CharacterDataWillChange,
682 nsDocumentObserver_CharacterDataChanged,
683 nsDocumentObserver_AttributeWillChange,
684 nsDocumentObserver_AttributeChanged,
685 nsDocumentObserver_ContentAppended,
686 nsDocumentObserver_ContentInserted,
687 nsDocumentObserver_ContentRemoved,
688 nsDocumentObserver_NodeWillBeDestroyed,
689 nsDocumentObserver_ParentChainChanged,
690 nsDocumentObserver_BeginUpdate,
691 nsDocumentObserver_EndUpdate,
692 nsDocumentObserver_BeginLoad,
693 nsDocumentObserver_EndLoad,
694 nsDocumentObserver_ContentStatesChanged,
695 nsDocumentObserver_DocumentStatesChanged,
696 nsDocumentObserver_StyleSheetAdded,
697 nsDocumentObserver_StyleSheetRemoved,
698 nsDocumentObserver_StyleSheetApplicableStateChanged,
699 nsDocumentObserver_StyleRuleChanged,
700 nsDocumentObserver_StyleRuleAdded,
701 nsDocumentObserver_StyleRuleRemoved,
702 nsDocumentObserver_BindToDocument,
703 nsDocumentObserver_DoneAddingChildren
706 void init_document_mutation(HTMLDocumentNode *doc)
711 doc->nsIDocumentObserver_iface.lpVtbl = &nsDocumentObserverVtbl;
713 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
714 if(NS_FAILED(nsres)) {
715 ERR("Could not get nsIDocument: %08x\n", nsres);
719 nsIContentUtils_AddDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
720 nsIDocument_Release(nsdoc);
723 void release_document_mutation(HTMLDocumentNode *doc)
728 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
729 if(NS_FAILED(nsres)) {
730 ERR("Could not get nsIDocument: %08x\n", nsres);
734 nsIContentUtils_RemoveDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
735 nsIDocument_Release(nsdoc);
738 void init_mutation(nsIComponentManager *component_manager)
743 if(!component_manager) {
745 nsIContentUtils_Release(content_utils);
746 content_utils = NULL;
751 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_ICONTENTUTILS_CID,
752 &IID_nsIFactory, (void**)&factory);
753 if(NS_FAILED(nsres)) {
754 ERR("Could not create nsIContentUtils service: %08x\n", nsres);
758 nsres = nsIFactory_CreateInstance(factory, NULL, &IID_nsIContentUtils, (void**)&content_utils);
759 nsIFactory_Release(factory);
761 ERR("Could not create nsIContentUtils instance: %08x\n", nsres);