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);
43 void set_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
45 nsIDOMNSDocument *nsdoc;
48 nsres = nsIDOMHTMLDocument_QueryInterface(nshtmldoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
49 if(NS_FAILED(nsres)) {
50 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
54 nsIDOMNSDocument_WineAddObserver(nsdoc, NSDOCOBS(nscontainer));
55 nsIDOMNSDocument_Release(nsdoc);
58 void remove_mutation_observer(NSContainer *nscontainer, nsIDOMHTMLDocument *nshtmldoc)
60 nsIDOMNSDocument *nsdoc;
63 nsres = nsIDOMHTMLDocument_QueryInterface(nshtmldoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
64 if(NS_FAILED(nsres)) {
65 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
69 nsIDOMNSDocument_WineRemoveObserver(nsdoc, NSDOCOBS(nscontainer));
70 nsIDOMNSDocument_Release(nsdoc);
73 #define IE_MAJOR_VERSION 7
74 #define IE_MINOR_VERSION 0
76 static BOOL handle_insert_comment(HTMLDocument *doc, const PRUnichar *comment)
79 int majorv = 0, minorv = 0;
80 const PRUnichar *ptr, *end;
93 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
95 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
102 if(ptr[0] == 'l' && ptr[1] == 't') {
110 }else if(ptr[0] == 'g' && ptr[1] == 't') {
120 if(!isspaceW(*ptr++))
122 while(isspaceW(*ptr))
125 if(ptr[0] != 'I' || ptr[1] != 'E')
129 if(!isspaceW(*ptr++))
131 while(isspaceW(*ptr))
136 while(isdigitW(*ptr))
137 majorv = majorv*10 + (*ptr++ - '0');
143 while(isdigitW(*ptr))
144 minorv = minorv*10 + (*ptr++ - '0');
147 while(isspaceW(*ptr))
149 if(ptr[0] != ']' || ptr[1] != '>')
154 if(len < sizeof(endifW)/sizeof(WCHAR))
157 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
158 if(memcmp(end, endifW, sizeof(endifW)))
163 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
167 if(majorv > IE_MAJOR_VERSION)
169 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
173 if(majorv > IE_MAJOR_VERSION)
175 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
179 if(majorv < IE_MAJOR_VERSION)
181 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
185 if(majorv < IE_MAJOR_VERSION)
187 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
192 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
196 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
198 nsAString_Init(&nsstr, buf);
201 /* FIXME: Find better way to insert HTML to document. */
202 nsres = nsIDOMHTMLDocument_Write(doc->nsdoc, &nsstr);
203 nsAString_Finish(&nsstr);
204 if(NS_FAILED(nsres)) {
205 ERR("Write failed: %08x\n", nsres);
212 static void add_script_runner(NSContainer *This)
214 nsIDOMNSDocument *nsdoc;
217 nsres = nsIDOMHTMLDocument_QueryInterface(This->doc->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
218 if(NS_FAILED(nsres)) {
219 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
223 nsIDOMNSDocument_WineAddScriptRunner(nsdoc, NSRUNNABLE(This));
224 nsIDOMNSDocument_Release(nsdoc);
227 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(NSContainer, Runnable, iface)
229 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
230 nsIIDRef riid, nsQIResult result)
232 NSContainer *This = NSRUNNABLE_THIS(iface);
234 if(IsEqualGUID(riid, &IID_nsISupports)) {
235 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
236 *result = NSRUNNABLE(This);
237 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
238 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
239 *result = NSRUNNABLE(This);
242 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
243 return NS_NOINTERFACE;
246 nsISupports_AddRef((nsISupports*)*result);
250 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
252 NSContainer *This = NSRUNNABLE_THIS(iface);
253 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
256 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
258 NSContainer *This = NSRUNNABLE_THIS(iface);
259 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
262 static void pop_mutation_queue(NSContainer *nscontainer)
264 mutation_queue_t *tmp = nscontainer->mutation_queue;
269 nscontainer->mutation_queue = tmp->next;
271 nscontainer->mutation_queue_tail = NULL;
273 nsISupports_Release(tmp->nsiface);
277 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
279 NSContainer *This = NSRUNNABLE_THIS(iface);
282 TRACE("(%p)\n", This);
284 while(This->mutation_queue) {
285 switch(This->mutation_queue->type) {
286 case MUTATION_COMMENT: {
287 nsIDOMComment *nscomment;
288 nsAString comment_str;
289 BOOL remove_comment = FALSE;
291 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMComment, (void**)&nscomment);
292 if(NS_FAILED(nsres)) {
293 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
297 nsAString_Init(&comment_str, NULL);
298 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
299 if(NS_SUCCEEDED(nsres)) {
300 const PRUnichar *comment;
302 nsAString_GetData(&comment_str, &comment);
303 remove_comment = handle_insert_comment(This->doc, comment);
306 nsAString_Finish(&comment_str);
309 nsIDOMNode *nsparent, *tmp;
312 static const PRUnichar remove_comment_magicW[] =
313 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
315 nsAString_Init(&magic_str, remove_comment_magicW);
316 nsres = nsIDOMComment_SetData(nscomment, &magic_str);
317 nsAString_Finish(&magic_str);
319 ERR("SetData failed: %08x\n", nsres);
321 nsIDOMComment_GetParentNode(nscomment, &nsparent);
323 nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
324 nsIDOMNode_Release(nsparent);
325 nsIDOMNode_Release(tmp);
329 nsIDOMComment_Release(nscomment);
333 case MUTATION_SCRIPT: {
334 nsIDOMHTMLScriptElement *nsscript;
336 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMHTMLScriptElement,
338 if(NS_FAILED(nsres)) {
339 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
343 doc_insert_script(This->doc, nsscript);
344 nsIDOMHTMLScriptElement_Release(nsscript);
349 ERR("invalid type %d\n", This->mutation_queue->type);
352 pop_mutation_queue(This);
358 #undef NSRUNNABLE_THIS
360 static const nsIRunnableVtbl nsRunnableVtbl = {
361 nsRunnable_QueryInterface,
367 #define NSDOCOBS_THIS(iface) DEFINE_THIS(NSContainer, DocumentObserver, iface)
369 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
370 nsIIDRef riid, nsQIResult result)
372 NSContainer *This = NSDOCOBS_THIS(iface);
374 if(IsEqualGUID(&IID_nsISupports, riid)) {
375 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
376 *result = NSDOCOBS(This);
377 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
378 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
379 *result = NSDOCOBS(This);
380 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
381 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
382 *result = NSDOCOBS(This);
385 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
386 return NS_NOINTERFACE;
389 nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
393 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
395 NSContainer *This = NSDOCOBS_THIS(iface);
396 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This));
399 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
401 NSContainer *This = NSDOCOBS_THIS(iface);
402 return nsIWebBrowserChrome_Release(NSWBCHROME(This));
405 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
406 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
410 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
411 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
415 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
416 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType, PRUint32 aStateMask)
420 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
421 nsIContent *aContainer, PRInt32 aNewIndexInContainer)
425 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
426 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
430 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
431 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
435 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
439 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
443 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
444 nsUpdateType aUpdateType)
448 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
449 nsUpdateType aUpdateType)
453 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
457 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
461 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
462 nsIContent *aContent1, nsIContent *aContent2, PRInt32 aStateMask)
466 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
467 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
471 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
472 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
476 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
477 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
481 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
482 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
486 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
487 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
491 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
492 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
496 static void push_mutation_queue(NSContainer *nscontainer, DWORD type, nsISupports *nsiface)
498 mutation_queue_t *elem;
500 elem = heap_alloc(sizeof(mutation_queue_t));
506 elem->nsiface = nsiface;
507 nsISupports_AddRef(nsiface);
509 if(nscontainer->mutation_queue_tail)
510 nscontainer->mutation_queue_tail = nscontainer->mutation_queue_tail->next = elem;
512 nscontainer->mutation_queue = nscontainer->mutation_queue_tail = elem;
515 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
516 nsIContent *aContent)
518 NSContainer *This = NSDOCOBS_THIS(iface);
519 nsIDOMComment *nscomment;
520 nsIDOMElement *nselem;
523 TRACE("(%p)\n", This);
525 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
526 if(NS_SUCCEEDED(nsres)) {
527 check_event_attr(This->doc, nselem);
528 nsIDOMElement_Release(nselem);
531 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
532 if(NS_SUCCEEDED(nsres)) {
533 TRACE("comment node\n");
535 push_mutation_queue(This, MUTATION_COMMENT, (nsISupports*)nscomment);
536 nsIDOMComment_Release(nscomment);
537 add_script_runner(This);
541 static void NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
542 PRBool aHaveNotified)
544 NSContainer *This = NSDOCOBS_THIS(iface);
545 nsIDOMHTMLScriptElement *nsscript;
548 TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
550 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
551 if(NS_SUCCEEDED(nsres)) {
552 push_mutation_queue(This, MUTATION_SCRIPT, (nsISupports*)nsscript);
553 nsIDOMHTMLScriptElement_Release(nsscript);
554 add_script_runner(This);
558 #undef NSMUTATIONOBS_THIS
560 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
561 nsDocumentObserver_QueryInterface,
562 nsDocumentObserver_AddRef,
563 nsDocumentObserver_Release,
564 nsDocumentObserver_CharacterDataWillChange,
565 nsDocumentObserver_CharacterDataChanged,
566 nsDocumentObserver_AttributeChanged,
567 nsDocumentObserver_ContentAppended,
568 nsDocumentObserver_ContentInserted,
569 nsDocumentObserver_ContentRemoved,
570 nsDocumentObserver_NodeWillBeDestroyed,
571 nsDocumentObserver_ParentChainChanged,
572 nsDocumentObserver_BeginUpdate,
573 nsDocumentObserver_EndUpdate,
574 nsDocumentObserver_BeginLoad,
575 nsDocumentObserver_EndLoad,
576 nsDocumentObserver_ContentStatesChanged,
577 nsDocumentObserver_StyleSheetAdded,
578 nsDocumentObserver_StyleSheetRemoved,
579 nsDocumentObserver_StyleSheetApplicableStateChanged,
580 nsDocumentObserver_StyleRuleChanged,
581 nsDocumentObserver_StyleRuleAdded,
582 nsDocumentObserver_StyleRuleRemoved,
583 nsDocumentObserver_BindToDocument,
584 nsDocumentObserver_DoneAddingChildren
587 void init_mutation(NSContainer *nscontainer)
589 nscontainer->lpDocumentObserverVtbl = &nsDocumentObserverVtbl;
590 nscontainer->lpRunnableVtbl = &nsRunnableVtbl;