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 /* FIXME: IE7 calls EnableModelless(TRUE), EnableModelless(FALSE) and sets interactive state here */
271 static nsresult run_end_load(HTMLDocumentNode *This, nsISupports *arg1, nsISupports *arg2)
273 TRACE("(%p)\n", This);
275 if(!This->basedoc.doc_obj)
278 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
280 * This should be done in the worker thread that parses HTML,
281 * but we don't have such thread (Gecko parses HTML for us).
283 parse_complete(This->basedoc.doc_obj);
286 set_ready_state(This->basedoc.window, READYSTATE_INTERACTIVE);
290 static nsresult run_insert_script(HTMLDocumentNode *doc, nsISupports *script_iface, nsISupports *parser_iface)
292 nsIDOMHTMLScriptElement *nsscript;
293 nsIParser *nsparser = NULL;
296 TRACE("(%p)->(%p)\n", doc, script_iface);
298 nsres = nsISupports_QueryInterface(script_iface, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
299 if(NS_FAILED(nsres)) {
300 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
305 nsres = nsISupports_QueryInterface(parser_iface, &IID_nsIParser, (void**)&nsparser);
306 if(NS_FAILED(nsres)) {
307 ERR("Could not get nsIParser iface: %08x\n", nsres);
313 nsIParser_BeginEvaluatingParserInsertedScript(nsparser);
315 doc_insert_script(doc->basedoc.window, nsscript);
318 nsIParser_EndEvaluatingParserInsertedScript(nsparser);
319 nsIParser_Release(nsparser);
322 nsIDOMHTMLScriptElement_Release(nsscript);
326 typedef struct nsRunnable nsRunnable;
328 typedef nsresult (*runnable_proc_t)(HTMLDocumentNode*,nsISupports*,nsISupports*);
331 nsIRunnable nsIRunnable_iface;
335 runnable_proc_t proc;
337 HTMLDocumentNode *doc;
342 static inline nsRunnable *impl_from_nsIRunnable(nsIRunnable *iface)
344 return CONTAINING_RECORD(iface, nsRunnable, nsIRunnable_iface);
347 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
348 nsIIDRef riid, void **result)
350 nsRunnable *This = impl_from_nsIRunnable(iface);
352 if(IsEqualGUID(riid, &IID_nsISupports)) {
353 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
354 *result = &This->nsIRunnable_iface;
355 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
356 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
357 *result = &This->nsIRunnable_iface;
360 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
361 return NS_NOINTERFACE;
364 nsISupports_AddRef((nsISupports*)*result);
368 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
370 nsRunnable *This = impl_from_nsIRunnable(iface);
371 LONG ref = InterlockedIncrement(&This->ref);
373 TRACE("(%p) ref=%d\n", This, ref);
378 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
380 nsRunnable *This = impl_from_nsIRunnable(iface);
381 LONG ref = InterlockedDecrement(&This->ref);
383 TRACE("(%p) ref=%d\n", This, ref);
386 htmldoc_release(&This->doc->basedoc);
388 nsISupports_Release(This->arg1);
390 nsISupports_Release(This->arg2);
397 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
399 nsRunnable *This = impl_from_nsIRunnable(iface);
401 return This->proc(This->doc, This->arg1, This->arg2);
404 static const nsIRunnableVtbl nsRunnableVtbl = {
405 nsRunnable_QueryInterface,
411 static void add_script_runner(HTMLDocumentNode *This, runnable_proc_t proc, nsISupports *arg1, nsISupports *arg2)
413 nsRunnable *runnable;
415 runnable = heap_alloc_zero(sizeof(*runnable));
419 runnable->nsIRunnable_iface.lpVtbl = &nsRunnableVtbl;
422 htmldoc_addref(&This->basedoc);
423 runnable->doc = This;
424 runnable->proc = proc;
427 nsISupports_AddRef(arg1);
428 runnable->arg1 = arg1;
431 nsISupports_AddRef(arg2);
432 runnable->arg2 = arg2;
434 nsIContentUtils_AddScriptRunner(content_utils, &runnable->nsIRunnable_iface);
436 nsIRunnable_Release(&runnable->nsIRunnable_iface);
439 static inline HTMLDocumentNode *impl_from_nsIDocumentObserver(nsIDocumentObserver *iface)
441 return CONTAINING_RECORD(iface, HTMLDocumentNode, nsIDocumentObserver_iface);
444 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
445 nsIIDRef riid, void **result)
447 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
449 if(IsEqualGUID(&IID_nsISupports, riid)) {
450 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
451 *result = &This->nsIDocumentObserver_iface;
452 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
453 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
454 *result = &This->nsIDocumentObserver_iface;
455 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
456 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
457 *result = &This->nsIDocumentObserver_iface;
460 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
461 return NS_NOINTERFACE;
464 htmldoc_addref(&This->basedoc);
468 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
470 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
471 return htmldoc_addref(&This->basedoc);
474 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
476 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
477 return htmldoc_release(&This->basedoc);
480 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
481 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
485 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
486 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
490 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
491 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
495 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
496 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
500 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
501 nsIContent *aContainer, nsIContent *aFirstNewContent, PRInt32 aNewIndexInContainer)
505 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
506 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
510 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
511 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer,
512 nsIContent *aProviousSibling)
516 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
520 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
524 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
525 nsUpdateType aUpdateType)
529 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
530 nsUpdateType aUpdateType)
534 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
538 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
540 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
542 TRACE("(%p)\n", This);
544 if(This->skip_mutation_notif)
547 This->content_ready = TRUE;
548 add_script_runner(This, run_end_load, NULL, NULL);
551 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
552 nsIContent *aContent1, nsIContent *aContent2, nsEventStates aStateMask)
556 static void NSAPI nsDocumentObserver_DocumentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
557 nsEventStates aStateMask)
561 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
562 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
566 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
567 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
571 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
572 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
576 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
577 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
581 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
582 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
586 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
587 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
591 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
592 nsIContent *aContent)
594 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
595 nsIDOMHTMLIFrameElement *nsiframe;
596 nsIDOMHTMLFrameElement *nsframe;
597 nsIDOMComment *nscomment;
598 nsIDOMElement *nselem;
601 TRACE("(%p)\n", This);
603 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
604 if(NS_SUCCEEDED(nsres)) {
605 check_event_attr(This, nselem);
606 nsIDOMElement_Release(nselem);
609 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
610 if(NS_SUCCEEDED(nsres)) {
611 TRACE("comment node\n");
613 add_script_runner(This, run_insert_comment, (nsISupports*)nscomment, NULL);
614 nsIDOMComment_Release(nscomment);
617 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
618 if(NS_SUCCEEDED(nsres)) {
619 TRACE("iframe node\n");
621 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsiframe, NULL);
622 nsIDOMHTMLIFrameElement_Release(nsiframe);
625 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLFrameElement, (void**)&nsframe);
626 if(NS_SUCCEEDED(nsres)) {
627 TRACE("frame node\n");
629 add_script_runner(This, run_bind_to_tree, (nsISupports*)nsframe, NULL);
630 nsIDOMHTMLFrameElement_Release(nsframe);
634 static nsresult NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
635 PRBool aHaveNotified, nsIParser *aParser)
637 HTMLDocumentNode *This = impl_from_nsIDocumentObserver(iface);
638 nsIDOMHTMLScriptElement *nsscript;
641 TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
643 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
644 if(NS_SUCCEEDED(nsres)) {
645 TRACE("script node\n");
647 add_script_runner(This, run_insert_script, (nsISupports*)nsscript, (nsISupports*)aParser);
648 nsIDOMHTMLScriptElement_Release(nsscript);
654 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
655 nsDocumentObserver_QueryInterface,
656 nsDocumentObserver_AddRef,
657 nsDocumentObserver_Release,
658 nsDocumentObserver_CharacterDataWillChange,
659 nsDocumentObserver_CharacterDataChanged,
660 nsDocumentObserver_AttributeWillChange,
661 nsDocumentObserver_AttributeChanged,
662 nsDocumentObserver_ContentAppended,
663 nsDocumentObserver_ContentInserted,
664 nsDocumentObserver_ContentRemoved,
665 nsDocumentObserver_NodeWillBeDestroyed,
666 nsDocumentObserver_ParentChainChanged,
667 nsDocumentObserver_BeginUpdate,
668 nsDocumentObserver_EndUpdate,
669 nsDocumentObserver_BeginLoad,
670 nsDocumentObserver_EndLoad,
671 nsDocumentObserver_ContentStatesChanged,
672 nsDocumentObserver_DocumentStatesChanged,
673 nsDocumentObserver_StyleSheetAdded,
674 nsDocumentObserver_StyleSheetRemoved,
675 nsDocumentObserver_StyleSheetApplicableStateChanged,
676 nsDocumentObserver_StyleRuleChanged,
677 nsDocumentObserver_StyleRuleAdded,
678 nsDocumentObserver_StyleRuleRemoved,
679 nsDocumentObserver_BindToDocument,
680 nsDocumentObserver_DoneAddingChildren
683 void init_document_mutation(HTMLDocumentNode *doc)
688 doc->nsIDocumentObserver_iface.lpVtbl = &nsDocumentObserverVtbl;
690 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
691 if(NS_FAILED(nsres)) {
692 ERR("Could not get nsIDocument: %08x\n", nsres);
696 nsIContentUtils_AddDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
697 nsIDocument_Release(nsdoc);
700 void release_document_mutation(HTMLDocumentNode *doc)
705 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDocument, (void**)&nsdoc);
706 if(NS_FAILED(nsres)) {
707 ERR("Could not get nsIDocument: %08x\n", nsres);
711 nsIContentUtils_RemoveDocumentObserver(content_utils, nsdoc, &doc->nsIDocumentObserver_iface);
712 nsIDocument_Release(nsdoc);
715 void init_mutation(nsIComponentManager *component_manager)
720 if(!component_manager) {
722 nsIContentUtils_Release(content_utils);
723 content_utils = NULL;
728 nsres = nsIComponentManager_GetClassObject(component_manager, &NS_ICONTENTUTILS_CID,
729 &IID_nsIFactory, (void**)&factory);
730 if(NS_FAILED(nsres)) {
731 ERR("Could not create nsIContentUtils service: %08x\n", nsres);
735 nsres = nsIFactory_CreateInstance(factory, NULL, &IID_nsIContentUtils, (void**)&content_utils);
736 nsIFactory_Release(factory);
738 ERR("Could not create nsIContentUtils instance: %08x\n", nsres);