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 #define IE_MAJOR_VERSION 7
44 #define IE_MINOR_VERSION 0
46 static BOOL handle_insert_comment(HTMLDocumentNode *doc, const PRUnichar *comment)
49 int majorv = 0, minorv = 0;
50 const PRUnichar *ptr, *end;
63 static const PRUnichar endifW[] = {'<','!','[','e','n','d','i','f',']'};
65 if(comment[0] != '[' || comment[1] != 'i' || comment[2] != 'f')
72 if(ptr[0] == 'l' && ptr[1] == 't') {
80 }else if(ptr[0] == 'g' && ptr[1] == 't') {
95 if(ptr[0] != 'I' || ptr[1] != 'E')
101 while(isspaceW(*ptr))
106 while(isdigitW(*ptr))
107 majorv = majorv*10 + (*ptr++ - '0');
113 while(isdigitW(*ptr))
114 minorv = minorv*10 + (*ptr++ - '0');
117 while(isspaceW(*ptr))
119 if(ptr[0] != ']' || ptr[1] != '>')
124 if(len < sizeof(endifW)/sizeof(WCHAR))
127 end = ptr + len-sizeof(endifW)/sizeof(WCHAR);
128 if(memcmp(end, endifW, sizeof(endifW)))
133 if(majorv == IE_MAJOR_VERSION && minorv == IE_MINOR_VERSION)
137 if(majorv > IE_MAJOR_VERSION)
139 if(majorv == IE_MAJOR_VERSION && minorv > IE_MINOR_VERSION)
143 if(majorv > IE_MAJOR_VERSION)
145 if(majorv == IE_MAJOR_VERSION && minorv >= IE_MINOR_VERSION)
149 if(majorv < IE_MAJOR_VERSION)
151 if(majorv == IE_MAJOR_VERSION && minorv < IE_MINOR_VERSION)
155 if(majorv < IE_MAJOR_VERSION)
157 if(majorv == IE_MAJOR_VERSION && minorv <= IE_MINOR_VERSION)
162 buf = heap_alloc((end-ptr+1)*sizeof(WCHAR));
166 memcpy(buf, ptr, (end-ptr)*sizeof(WCHAR));
168 nsAString_Init(&nsstr, buf);
171 /* FIXME: Find better way to insert HTML to document. */
172 nsres = nsIDOMHTMLDocument_Write(doc->nsdoc, &nsstr);
173 nsAString_Finish(&nsstr);
174 if(NS_FAILED(nsres)) {
175 ERR("Write failed: %08x\n", nsres);
182 static void add_script_runner(HTMLDocumentNode *This)
184 nsIDOMNSDocument *nsdoc;
187 nsres = nsIDOMHTMLDocument_QueryInterface(This->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
188 if(NS_FAILED(nsres)) {
189 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
193 nsIDOMNSDocument_WineAddScriptRunner(nsdoc, NSRUNNABLE(This));
194 nsIDOMNSDocument_Release(nsdoc);
197 #define NSRUNNABLE_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IRunnable, iface)
199 static nsresult NSAPI nsRunnable_QueryInterface(nsIRunnable *iface,
200 nsIIDRef riid, nsQIResult result)
202 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
204 if(IsEqualGUID(riid, &IID_nsISupports)) {
205 TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
206 *result = NSRUNNABLE(This);
207 }else if(IsEqualGUID(riid, &IID_nsIRunnable)) {
208 TRACE("(%p)->(IID_nsIRunnable %p)\n", This, result);
209 *result = NSRUNNABLE(This);
212 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
213 return NS_NOINTERFACE;
216 nsISupports_AddRef((nsISupports*)*result);
220 static nsrefcnt NSAPI nsRunnable_AddRef(nsIRunnable *iface)
222 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
223 return htmldoc_addref(&This->basedoc);
226 static nsrefcnt NSAPI nsRunnable_Release(nsIRunnable *iface)
228 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
229 return htmldoc_release(&This->basedoc);
232 static void pop_mutation_queue(HTMLDocumentNode *doc)
234 mutation_queue_t *tmp = doc->mutation_queue;
239 doc->mutation_queue = tmp->next;
241 doc->mutation_queue_tail = NULL;
243 nsISupports_Release(tmp->nsiface);
247 static nsresult NSAPI nsRunnable_Run(nsIRunnable *iface)
249 HTMLDocumentNode *This = NSRUNNABLE_THIS(iface);
252 TRACE("(%p)\n", This);
254 while(This->mutation_queue) {
255 switch(This->mutation_queue->type) {
256 case MUTATION_COMMENT: {
257 nsIDOMComment *nscomment;
258 nsAString comment_str;
259 BOOL remove_comment = FALSE;
261 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMComment, (void**)&nscomment);
262 if(NS_FAILED(nsres)) {
263 ERR("Could not get nsIDOMComment iface:%08x\n", nsres);
267 nsAString_Init(&comment_str, NULL);
268 nsres = nsIDOMComment_GetData(nscomment, &comment_str);
269 if(NS_SUCCEEDED(nsres)) {
270 const PRUnichar *comment;
272 nsAString_GetData(&comment_str, &comment);
273 remove_comment = handle_insert_comment(This, comment);
276 nsAString_Finish(&comment_str);
279 nsIDOMNode *nsparent, *tmp;
282 static const PRUnichar remove_comment_magicW[] =
283 {'#','!','w','i','n','e', 'r','e','m','o','v','e','!','#',0};
285 nsAString_Init(&magic_str, remove_comment_magicW);
286 nsres = nsIDOMComment_SetData(nscomment, &magic_str);
287 nsAString_Finish(&magic_str);
289 ERR("SetData failed: %08x\n", nsres);
291 nsIDOMComment_GetParentNode(nscomment, &nsparent);
293 nsIDOMNode_RemoveChild(nsparent, (nsIDOMNode*)nscomment, &tmp);
294 nsIDOMNode_Release(nsparent);
295 nsIDOMNode_Release(tmp);
299 nsIDOMComment_Release(nscomment);
303 case MUTATION_SCRIPT: {
304 nsIDOMHTMLScriptElement *nsscript;
306 nsres = nsISupports_QueryInterface(This->mutation_queue->nsiface, &IID_nsIDOMHTMLScriptElement,
308 if(NS_FAILED(nsres)) {
309 ERR("Could not get nsIDOMHTMLScriptElement: %08x\n", nsres);
313 doc_insert_script(This->basedoc.window, nsscript);
314 nsIDOMHTMLScriptElement_Release(nsscript);
319 ERR("invalid type %d\n", This->mutation_queue->type);
322 pop_mutation_queue(This);
328 #undef NSRUNNABLE_THIS
330 static const nsIRunnableVtbl nsRunnableVtbl = {
331 nsRunnable_QueryInterface,
337 #define NSDOCOBS_THIS(iface) DEFINE_THIS(HTMLDocumentNode, IDocumentObserver, iface)
339 static nsresult NSAPI nsDocumentObserver_QueryInterface(nsIDocumentObserver *iface,
340 nsIIDRef riid, nsQIResult result)
342 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
344 if(IsEqualGUID(&IID_nsISupports, riid)) {
345 TRACE("(%p)->(IID_nsISupports, %p)\n", This, result);
346 *result = NSDOCOBS(This);
347 }else if(IsEqualGUID(&IID_nsIMutationObserver, riid)) {
348 TRACE("(%p)->(IID_nsIMutationObserver %p)\n", This, result);
349 *result = NSDOCOBS(This);
350 }else if(IsEqualGUID(&IID_nsIDocumentObserver, riid)) {
351 TRACE("(%p)->(IID_nsIDocumentObserver %p)\n", This, result);
352 *result = NSDOCOBS(This);
355 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
356 return NS_NOINTERFACE;
359 htmldoc_addref(&This->basedoc);
363 static nsrefcnt NSAPI nsDocumentObserver_AddRef(nsIDocumentObserver *iface)
365 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
366 return htmldoc_addref(&This->basedoc);
369 static nsrefcnt NSAPI nsDocumentObserver_Release(nsIDocumentObserver *iface)
371 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
372 return htmldoc_release(&This->basedoc);
375 static void NSAPI nsDocumentObserver_CharacterDataWillChange(nsIDocumentObserver *iface,
376 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
380 static void NSAPI nsDocumentObserver_CharacterDataChanged(nsIDocumentObserver *iface,
381 nsIDocument *aDocument, nsIContent *aContent, void /*CharacterDataChangeInfo*/ *aInfo)
385 static void NSAPI nsDocumentObserver_AttributeWillChange(nsIDocumentObserver *iface, nsIDocument *aDocument,
386 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType)
390 static void NSAPI nsDocumentObserver_AttributeChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
391 nsIContent *aContent, PRInt32 aNameSpaceID, nsIAtom *aAttribute, PRInt32 aModType, PRUint32 aStateMask)
395 static void NSAPI nsDocumentObserver_ContentAppended(nsIDocumentObserver *iface, nsIDocument *aDocument,
396 nsIContent *aContainer, PRInt32 aNewIndexInContainer)
400 static void NSAPI nsDocumentObserver_ContentInserted(nsIDocumentObserver *iface, nsIDocument *aDocument,
401 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
405 static void NSAPI nsDocumentObserver_ContentRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
406 nsIContent *aContainer, nsIContent *aChild, PRInt32 aIndexInContainer)
410 static void NSAPI nsDocumentObserver_NodeWillBeDestroyed(nsIDocumentObserver *iface, const nsINode *aNode)
414 static void NSAPI nsDocumentObserver_ParentChainChanged(nsIDocumentObserver *iface, nsIContent *aContent)
418 static void NSAPI nsDocumentObserver_BeginUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
419 nsUpdateType aUpdateType)
423 static void NSAPI nsDocumentObserver_EndUpdate(nsIDocumentObserver *iface, nsIDocument *aDocument,
424 nsUpdateType aUpdateType)
428 static void NSAPI nsDocumentObserver_BeginLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
432 static void NSAPI nsDocumentObserver_EndLoad(nsIDocumentObserver *iface, nsIDocument *aDocument)
434 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
438 This->content_ready = TRUE;
440 if(This == This->basedoc.doc_obj->basedoc.doc_node) {
442 task = heap_alloc(sizeof(task_t));
444 task->doc = &This->basedoc.doc_obj->basedoc;
445 task->task_id = TASK_PARSECOMPLETE;
449 * This should be done in the worker thread that parses HTML,
450 * but we don't have such thread (Gecko parses HTML for us).
456 static void NSAPI nsDocumentObserver_ContentStatesChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
457 nsIContent *aContent1, nsIContent *aContent2, PRInt32 aStateMask)
461 static void NSAPI nsDocumentObserver_StyleSheetAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
462 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
466 static void NSAPI nsDocumentObserver_StyleSheetRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
467 nsIStyleSheet *aStyleSheet, PRBool aDocumentSheet)
471 static void NSAPI nsDocumentObserver_StyleSheetApplicableStateChanged(nsIDocumentObserver *iface,
472 nsIDocument *aDocument, nsIStyleSheet *aStyleSheet, PRBool aApplicable)
476 static void NSAPI nsDocumentObserver_StyleRuleChanged(nsIDocumentObserver *iface, nsIDocument *aDocument,
477 nsIStyleSheet *aStyleSheet, nsIStyleRule *aOldStyleRule, nsIStyleSheet *aNewStyleRule)
481 static void NSAPI nsDocumentObserver_StyleRuleAdded(nsIDocumentObserver *iface, nsIDocument *aDocument,
482 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
486 static void NSAPI nsDocumentObserver_StyleRuleRemoved(nsIDocumentObserver *iface, nsIDocument *aDocument,
487 nsIStyleSheet *aStyleSheet, nsIStyleRule *aStyleRule)
491 static void push_mutation_queue(HTMLDocumentNode *doc, DWORD type, nsISupports *nsiface)
493 mutation_queue_t *elem;
495 elem = heap_alloc(sizeof(mutation_queue_t));
501 elem->nsiface = nsiface;
502 nsISupports_AddRef(nsiface);
504 if(doc->mutation_queue_tail)
505 doc->mutation_queue_tail = doc->mutation_queue_tail->next = elem;
507 doc->mutation_queue = doc->mutation_queue_tail = elem;
510 static void NSAPI nsDocumentObserver_BindToDocument(nsIDocumentObserver *iface, nsIDocument *aDocument,
511 nsIContent *aContent)
513 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
514 nsIDOMComment *nscomment;
515 nsIDOMElement *nselem;
518 TRACE("(%p)\n", This);
520 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMElement, (void**)&nselem);
521 if(NS_SUCCEEDED(nsres)) {
522 check_event_attr(This, nselem);
523 nsIDOMElement_Release(nselem);
526 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMComment, (void**)&nscomment);
527 if(NS_SUCCEEDED(nsres)) {
528 TRACE("comment node\n");
530 push_mutation_queue(This, MUTATION_COMMENT, (nsISupports*)nscomment);
531 nsIDOMComment_Release(nscomment);
532 add_script_runner(This);
536 static void NSAPI nsDocumentObserver_DoneAddingChildren(nsIDocumentObserver *iface, nsIContent *aContent,
537 PRBool aHaveNotified)
539 HTMLDocumentNode *This = NSDOCOBS_THIS(iface);
540 nsIDOMHTMLScriptElement *nsscript;
543 TRACE("(%p)->(%p %x)\n", This, aContent, aHaveNotified);
545 nsres = nsISupports_QueryInterface(aContent, &IID_nsIDOMHTMLScriptElement, (void**)&nsscript);
546 if(NS_SUCCEEDED(nsres)) {
547 push_mutation_queue(This, MUTATION_SCRIPT, (nsISupports*)nsscript);
548 nsIDOMHTMLScriptElement_Release(nsscript);
549 add_script_runner(This);
553 #undef NSMUTATIONOBS_THIS
555 static const nsIDocumentObserverVtbl nsDocumentObserverVtbl = {
556 nsDocumentObserver_QueryInterface,
557 nsDocumentObserver_AddRef,
558 nsDocumentObserver_Release,
559 nsDocumentObserver_CharacterDataWillChange,
560 nsDocumentObserver_CharacterDataChanged,
561 nsDocumentObserver_AttributeWillChange,
562 nsDocumentObserver_AttributeChanged,
563 nsDocumentObserver_ContentAppended,
564 nsDocumentObserver_ContentInserted,
565 nsDocumentObserver_ContentRemoved,
566 nsDocumentObserver_NodeWillBeDestroyed,
567 nsDocumentObserver_ParentChainChanged,
568 nsDocumentObserver_BeginUpdate,
569 nsDocumentObserver_EndUpdate,
570 nsDocumentObserver_BeginLoad,
571 nsDocumentObserver_EndLoad,
572 nsDocumentObserver_ContentStatesChanged,
573 nsDocumentObserver_StyleSheetAdded,
574 nsDocumentObserver_StyleSheetRemoved,
575 nsDocumentObserver_StyleSheetApplicableStateChanged,
576 nsDocumentObserver_StyleRuleChanged,
577 nsDocumentObserver_StyleRuleAdded,
578 nsDocumentObserver_StyleRuleRemoved,
579 nsDocumentObserver_BindToDocument,
580 nsDocumentObserver_DoneAddingChildren
583 void init_mutation(HTMLDocumentNode *doc)
585 nsIDOMNSDocument *nsdoc;
588 doc->lpIDocumentObserverVtbl = &nsDocumentObserverVtbl;
589 doc->lpIRunnableVtbl = &nsRunnableVtbl;
591 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
592 if(NS_FAILED(nsres)) {
593 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
597 nsIDOMNSDocument_WineAddObserver(nsdoc, NSDOCOBS(doc));
598 nsIDOMNSDocument_Release(nsdoc);
601 void release_mutation(HTMLDocumentNode *doc)
603 nsIDOMNSDocument *nsdoc;
606 nsres = nsIDOMHTMLDocument_QueryInterface(doc->nsdoc, &IID_nsIDOMNSDocument, (void**)&nsdoc);
607 if(NS_FAILED(nsres)) {
608 ERR("Could not get nsIDOMNSDocument: %08x\n", nsres);
612 nsIDOMNSDocument_WineRemoveObserver(nsdoc, NSDOCOBS(doc));
613 nsIDOMNSDocument_Release(nsdoc);