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
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44 #define IE_MAJOR_VERSION 7
45 #define IE_MINOR_VERSION 0
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_Init(&nsstr, buf);
172 /* FIXME: Find better way to insert HTML to document. */
173 nsres = nsIDOMHTMLDocument_Write(doc->nsdoc, &nsstr);
174 nsAString_Finish(&nsstr);
175 if(NS_FAILED(nsres)) {
176 ERR("Write failed: %08x\n", nsres);
183 static void add_script_runner(HTMLDocumentNode *This)
185 nsIDOMNSDocument *nsdoc;
188 nsres = nsIDOMHTMLDocument_QueryInterface(This->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
189 if(NS_FAILED(nsres)) {
190 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
194 nsIDOMNSDocument_WineAddScriptRunner(nsdoc, NSRUNNABLE(This));
195 nsIDOMNSDocument_Release(nsdoc);
198 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IRunnable, iface)
200 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
201 nsIIDRef riid, nsQIResult result)
203 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
205 if(IsEqualGUID(riid, &IID_nsISupports)) {
206 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
207 *result = NSRUNNABLE(This);
208 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
209 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
210 *result = NSRUNNABLE(This);
213 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
214 return NS_NOINTERFACE;
217 nsISupports_AddRef((nsISupports*)*result);
221 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
223 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
224 return htmldoc_addref(&This->basedoc);
227 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
229 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
230 return htmldoc_release(&This->basedoc);
233 static void pop_mutation_queue(HTMLDocumentNode *doc)
235 mutation_queue_t *tmp = doc->mutation_queue;
240 doc->mutation_queue = tmp->next;
242 doc->mutation_queue_tail = NULL;
244 nsISupports_Release(tmp->nsiface);
248 static nsresult init_iframe_window(HTMLDocumentNode *doc, nsISupports *nsunk)
250 nsIDOMHTMLIFrameElement *nsiframe;
251 nsIDOMWindow *nswindow;
252 nsIDOMDocument *nsdoc;
255 nsres = nsISupports_QueryInterface(nsunk, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
256 if(NS_FAILED(nsres)) {
257 ERR("Could not get nsIDOMHTMLIFrameElement: %08x\n", nsres);
261 nsres = nsIDOMHTMLIFrameElement_GetContentDocument(nsiframe, &nsdoc);
262 nsIDOMHTMLIFrameElement_Release(nsiframe);
263 if(NS_FAILED(nsres) || !nsdoc) {
264 ERR("GetContentDocument failed: %08x\n", nsres);
268 nswindow = get_nsdoc_window(nsdoc);
269 nsIDOMDocument_Release(nsdoc);
271 return NS_ERROR_FAILURE;
273 if(!nswindow_to_window(nswindow)) {
277 hres = HTMLWindow_Create(doc->basedoc.doc_obj, nswindow, doc->basedoc.window, &window);
279 IHTMLWindow2_Release(HTMLWINDOW2(window));
282 nsIDOMWindow_Release(nswindow);
286 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
288 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
291 TRACE("(%p)\n", This);
293 while(This->mutation_queue) {
294 switch(This->mutation_queue->type) {
295 case MUTATION_COMMENT: {
296 nsIDOMComment *nscomment;
297 nsAString comment_str;
298 BOOL remove_comment = FALSE;
300 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMComment, (void**)&nscomment);
301 if(NS_FAILED(nsres)) {
302 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
306 nsAString_Init(&comment_str, NULL);
307 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
308 if(NS_SUCCEEDED(nsres)) {
309 const PRUnichar *comment;
311 nsAString_GetData(&comment_str, &comment);
312 remove_comment = handle_insert_comment(This, comment);
315 nsAString_Finish(&comment_str);
318 nsIDOMNode *nsparent, *tmp;
321 static const PRUnichar remove_comment_magicW[] =
322 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
324 nsAString_Init(&magic_str, remove_comment_magicW);
325 nsres = nsIDOMComment_SetData(nscomment, &magic_str);
326 nsAString_Finish(&magic_str);
328 ERR("SetData failed: %08x\n", nsres);
330 nsIDOMComment_GetParentNode(nscomment, &nsparent);
332 nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
333 nsIDOMNode_Release(nsparent);
334 nsIDOMNode_Release(tmp);
338 nsIDOMComment_Release(nscomment);
342 case MUTATION_IFRAME:
343 return init_iframe_window(This, This->mutation_queue->nsiface);
345 case MUTATION_SCRIPT: {
346 nsIDOMHTMLScriptElement *nsscript;
348 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMHTMLScriptElement,
350 if(NS_FAILED(nsres)) {
351 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
355 doc_insert_script(This->basedoc.window, nsscript);
356 nsIDOMHTMLScriptElement_Release(nsscript);
361 ERR("invalid type %d\n", This->mutation_queue->type);
364 pop_mutation_queue(This);
370 #undef NSRUNNABLE_THIS
372 static const nsIRunnableVtbl nsRunnableVtbl = {
373 nsRunnable_QueryInterface,
379 #define NSDOCOBS_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IDocumentObserver, iface)
381 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
382 nsIIDRef riid, nsQIResult result)
384 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
386 if(IsEqualGUID(&IID_nsISupports, riid)) {
387 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
388 *result = NSDOCOBS(This);
389 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
390 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
391 *result = NSDOCOBS(This);
392 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
393 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
394 *result = NSDOCOBS(This);
397 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
398 return NS_NOINTERFACE;
401 htmldoc_addref(&This->basedoc);
405 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
407 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
408 return htmldoc_addref(&This->basedoc);
411 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
413 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
414 return htmldoc_release(&This->basedoc);
417 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
418 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
422 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
423 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
427 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
428 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
432 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
433 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType, PRUint32 aStateMask)
437 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
438 nsIContent *aContainer, PRInt32 aNewIndexInContainer)
442 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
443 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
447 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
448 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
452 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
456 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
460 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
461 nsUpdateType aUpdateType)
465 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
466 nsUpdateType aUpdateType)
470 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
474 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
476 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
480 This->content_ready = TRUE;
482 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
484 task = heap_alloc(sizeof(task_t));
486 task->doc = &This->basedoc.doc_obj->basedoc;
487 task->task_id = TASK_PARSECOMPLETE;
491 * This should be done in the worker thread that parses HTML,
492 * but we don't have such thread (Gecko parses HTML for us).
498 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
499 nsIContent *aContent1, nsIContent *aContent2, PRInt32 aStateMask)
503 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
504 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
508 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
509 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
513 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
514 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
518 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
519 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
523 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
524 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
528 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
529 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
533 static void push_mutation_queue(HTMLDocumentNode *doc, DWORD type, nsISupports *nsiface)
535 mutation_queue_t *elem;
537 elem = heap_alloc(sizeof(mutation_queue_t));
543 elem->nsiface = nsiface;
544 nsISupports_AddRef(nsiface);
546 if(doc->mutation_queue_tail)
547 doc->mutation_queue_tail = doc->mutation_queue_tail->next = elem;
549 doc->mutation_queue = doc->mutation_queue_tail = elem;
552 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
553 nsIContent *aContent)
555 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
556 nsIDOMHTMLIFrameElement *nsiframe;
557 nsIDOMComment *nscomment;
558 nsIDOMElement *nselem;
561 TRACE("(%p)\n", This);
563 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
564 if(NS_SUCCEEDED(nsres)) {
565 check_event_attr(This, nselem);
566 nsIDOMElement_Release(nselem);
569 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
570 if(NS_SUCCEEDED(nsres)) {
571 TRACE("comment node\n");
573 push_mutation_queue(This, MUTATION_COMMENT, (nsISupports*)nscomment);
574 nsIDOMComment_Release(nscomment);
575 add_script_runner(This);
578 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLIFrameElement, (void**)&nsiframe);
579 if(NS_SUCCEEDED(nsres)) {
580 push_mutation_queue(This, MUTATION_IFRAME, (nsISupports*)nsiframe);
581 nsIDOMHTMLIFrameElement_Release(nsiframe);
582 add_script_runner(This);
586 static void NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
587 PRBool aHaveNotified)
589 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
590 nsIDOMHTMLScriptElement *nsscript;
593 TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
595 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
596 if(NS_SUCCEEDED(nsres)) {
597 push_mutation_queue(This, MUTATION_SCRIPT, (nsISupports*)nsscript);
598 nsIDOMHTMLScriptElement_Release(nsscript);
599 add_script_runner(This);
603 #undef NSMUTATIONOBS_THIS
605 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
606 nsDocumentObserver_QueryInterface,
607 nsDocumentObserver_AddRef,
608 nsDocumentObserver_Release,
609 nsDocumentObserver_CharacterDataWillChange,
610 nsDocumentObserver_CharacterDataChanged,
611 nsDocumentObserver_AttributeWillChange,
612 nsDocumentObserver_AttributeChanged,
613 nsDocumentObserver_ContentAppended,
614 nsDocumentObserver_ContentInserted,
615 nsDocumentObserver_ContentRemoved,
616 nsDocumentObserver_NodeWillBeDestroyed,
617 nsDocumentObserver_ParentChainChanged,
618 nsDocumentObserver_BeginUpdate,
619 nsDocumentObserver_EndUpdate,
620 nsDocumentObserver_BeginLoad,
621 nsDocumentObserver_EndLoad,
622 nsDocumentObserver_ContentStatesChanged,
623 nsDocumentObserver_StyleSheetAdded,
624 nsDocumentObserver_StyleSheetRemoved,
625 nsDocumentObserver_StyleSheetApplicableStateChanged,
626 nsDocumentObserver_StyleRuleChanged,
627 nsDocumentObserver_StyleRuleAdded,
628 nsDocumentObserver_StyleRuleRemoved,
629 nsDocumentObserver_BindToDocument,
630 nsDocumentObserver_DoneAddingChildren
633 void init_mutation(HTMLDocumentNode *doc)
635 nsIDOMNSDocument *nsdoc;
638 doc->lpIDocumentObserverVtbl = &nsDocumentObserverVtbl;
639 doc->lpIRunnableVtbl = &nsRunnableVtbl;
641 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
642 if(NS_FAILED(nsres)) {
643 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
647 nsIDOMNSDocument_WineAddObserver(nsdoc, NSDOCOBS(doc));
648 nsIDOMNSDocument_Release(nsdoc);
651 void release_mutation(HTMLDocumentNode *doc)
653 nsIDOMNSDocument *nsdoc;
656 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
657 if(NS_FAILED(nsres)) {
658 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
662 nsIDOMNSDocument_WineRemoveObserver(nsdoc, NSDOCOBS(doc));
663 nsIDOMNSDocument_Release(nsdoc);