From 433c44cb19de1d0d40579155380fd435140b59fb Mon Sep 17 00:00:00 2001 From: Christian Persch Date: Tue, 8 Jun 2004 18:47:00 +0000 Subject: Fix CSS background detection, based on a galeon patch by Crispin 2004-06-08 Christian Persch * embed/mozilla/EventContext.cpp: Fix CSS background detection, based on a galeon patch by Crispin Flowerday. Fixed bug #143602. --- embed/mozilla/EventContext.cpp | 119 +++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 53 deletions(-) (limited to 'embed/mozilla') diff --git a/embed/mozilla/EventContext.cpp b/embed/mozilla/EventContext.cpp index 1ad257010..5b815a756 100644 --- a/embed/mozilla/EventContext.cpp +++ b/embed/mozilla/EventContext.cpp @@ -1,6 +1,7 @@ /* * Copyright (C) 2000-2004 Marco Pesenti Gritti * Copyright (C) 2003, 2004 Christian Persch + * Copyright (C) 2004 Crispin Flowerday * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -45,11 +46,15 @@ #include #include #include +#include +#include +#include #ifdef ALLOW_PRIVATE_API #include #include #include +#include #endif #define KEY_CODE 256 @@ -180,6 +185,7 @@ nsresult EventContext::GetEventContext (nsIDOMEventTarget *EventTarget, 'w', 'w', '.', 'w', '3', '.', 'o', 'r', 'g', '/', '1', '9', '9', '9', '/', 'x', 'l', 'i', 'n', 'k', '\0'}; + const PRUnichar bodyLiteral[] = { 'b', 'o', 'd', 'y', '\0' }; mEmbedEvent = info; @@ -214,6 +220,8 @@ nsresult EventContext::GetEventContext (nsIDOMEventTarget *EventTarget, rv = node->GetNodeType(&type); if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + PRBool has_background = PR_FALSE; + nsCOMPtr element = do_QueryInterface(node); if ((nsIDOMNode::ELEMENT_NODE == type) && element) { @@ -313,6 +321,35 @@ nsresult EventContext::GetEventContext (nsIDOMEventTarget *EventTarget, return NS_OK; } } + else if (g_ascii_strcasecmp (tag.get(), "html") == 0) + { + /* Clicked on part of the page without a , so + * look for a background image in the body tag */ + nsCOMPtr nodeList; + + rv = mDOMDocument->GetElementsByTagName (nsEmbedString(bodyLiteral), + getter_AddRefs (nodeList)); + if (NS_SUCCEEDED (rv) && nodeList) + { + nsCOMPtr bodyNode; + nodeList->Item (0, getter_AddRefs (bodyNode)); + + nsEmbedString cssurl; + rv = GetCSSBackground (bodyNode, cssurl); + if (NS_SUCCEEDED (rv)) + { + nsEmbedCString bgimg; + rv = ResolveBaseURL (cssurl, bgimg); + if (NS_FAILED (rv)) + return NS_ERROR_FAILURE; + + SetStringProperty ("background_image", + bgimg.get()); + + has_background = PR_TRUE; + } + } + } } /* Is page framed ? */ @@ -465,48 +502,6 @@ nsresult EventContext::GetEventContext (nsIDOMEventTarget *EventTarget, info->context |= EMBED_CONTEXT_INPUT; } - nsCOMPtr domelement; - domelement = do_QueryInterface (node); - if (!domelement) return NS_ERROR_FAILURE; - - PRBool has_background = PR_FALSE; - - nsEmbedString value; - domelement->GetAttribute (nsEmbedString(bgLiteral), value); - - if (value.Length()) - { - nsEmbedCString bgimg; - - rv = ResolveBaseURL (value, bgimg); - if (NS_FAILED(rv)) return NS_ERROR_FAILURE; - - SetStringProperty ("background_image", bgimg.get()); - } - else - { - nsCOMPtr bgelement; - bgelement = do_QueryInterface (node); - if (bgelement) - { - nsEmbedString value; - bgelement->GetBackground (value); - - if (value.Length()) - { - nsEmbedCString bgimg; - - rv = ResolveBaseURL (value, bgimg); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - SetStringProperty ("background_image", - bgimg.get()); - has_background = PR_TRUE; - } - } - } - if (!has_background) { nsEmbedString cssurl; @@ -522,6 +517,8 @@ nsresult EventContext::GetEventContext (nsIDOMEventTarget *EventTarget, bgimg.get()); if (NS_FAILED (rv)) return NS_ERROR_FAILURE; + + has_background = PR_TRUE; } } } @@ -536,28 +533,44 @@ nsresult EventContext::GetEventContext (nsIDOMEventTarget *EventTarget, nsresult EventContext::GetCSSBackground (nsIDOMNode *node, nsAString& url) { - nsresult result; + nsresult rv; const PRUnichar bgimage[] = {'b', 'a', 'c', 'k', 'g', 'r', 'o', 'u', 'n', 'd', '-', 'i', 'm', 'a', 'g', 'e', '\0'}; - nsCOMPtr style; - style = do_QueryInterface (node); - if (!style) return NS_ERROR_FAILURE; + nsCOMPtr element = do_QueryInterface (node); + NS_ENSURE_TRUE (element, NS_ERROR_FAILURE); + + nsCOMPtr docView = do_QueryInterface (mDOMDocument); + NS_ENSURE_TRUE (docView, NS_ERROR_FAILURE); + + nsCOMPtr abstractView; + docView->GetDefaultView (getter_AddRefs (abstractView)); + + nsCOMPtr viewCSS = do_QueryInterface (abstractView); + NS_ENSURE_TRUE (viewCSS, NS_ERROR_FAILURE); nsCOMPtr decl; - result = style->GetStyle (getter_AddRefs(decl)); - if (NS_FAILED(result)) return NS_ERROR_FAILURE; + viewCSS->GetComputedStyle (element, nsEmbedString(), + getter_AddRefs (decl)); + NS_ENSURE_TRUE (decl, NS_ERROR_FAILURE); - nsCOMPtr cssValue; + nsCOMPtr CSSValue; decl->GetPropertyCSSValue (nsEmbedString(bgimage), - getter_AddRefs(cssValue)); - nsCOMPtr primitiveValue; + getter_AddRefs (CSSValue)); - primitiveValue = do_QueryInterface(cssValue); + nsCOMPtr primitiveValue = + do_QueryInterface (CSSValue); if (!primitiveValue) return NS_ERROR_FAILURE; + + PRUint16 type; + rv = primitiveValue->GetPrimitiveType (&type); + NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE); + + if (type != nsIDOMCSSPrimitiveValue::CSS_URI) return NS_ERROR_FAILURE; - primitiveValue->GetStringValue(url); + rv = primitiveValue->GetStringValue (url); + NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE); return NS_OK; } -- cgit v1.2.3