1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Netscape Public License
5
* Version 1.1 (the "License"); you may not use this file except in
6
* compliance with the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/NPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is mozilla.org code.
16
* The Initial Developer of the Original Code is
17
* Netscape Communications Corporation.
18
* Portions created by the Initial Developer are Copyright (C) 2001
19
* the Initial Developer. All Rights Reserved.
22
* Joe Hewitt <hewitt@netscape.com> (original author)
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "inCSSValueSearch.h"
41
#include "nsIComponentManager.h"
42
#include "nsIServiceManager.h"
43
#include "nsVoidArray.h"
44
#include "nsReadableUtils.h"
45
#include "nsIDOMDocumentStyle.h"
46
#include "nsIDOM3Node.h"
47
#include "nsIDOMStyleSheetList.h"
48
#include "nsIDOMCSSStyleSheet.h"
49
#include "nsIDOMCSSRuleList.h"
50
#include "nsIDOMCSSStyleRule.h"
51
#include "nsIDOMCSSStyleDeclaration.h"
52
#include "nsIDOMCSSImportRule.h"
53
#include "nsIDOMCSSMediaRule.h"
55
#include "nsNetUtil.h"
57
static NS_DEFINE_CID(kInspectorCSSUtilsCID, NS_INSPECTORCSSUTILS_CID);
59
///////////////////////////////////////////////////////////////////////////////
60
inCSSValueSearch::inCSSValueSearch()
66
mHoldResults(PR_TRUE),
67
mReturnRelativeURLs(PR_TRUE),
68
mNormalizeChromeURLs(PR_FALSE)
70
mProperties = new nsCSSProperty[100];
71
mCSSUtils = do_GetService(kInspectorCSSUtilsCID);
74
inCSSValueSearch::~inCSSValueSearch()
80
NS_IMPL_ISUPPORTS2(inCSSValueSearch, inISearchProcess, inICSSValueSearch)
82
///////////////////////////////////////////////////////////////////////////////
86
inCSSValueSearch::GetIsActive(PRBool *aIsActive)
88
*aIsActive = mIsActive;
93
inCSSValueSearch::GetResultCount(PRInt32 *aResultCount)
95
*aResultCount = mResultCount;
100
inCSSValueSearch::GetHoldResults(PRBool *aHoldResults)
102
*aHoldResults = mHoldResults;
107
inCSSValueSearch::SetHoldResults(PRBool aHoldResults)
109
mHoldResults = aHoldResults;
114
inCSSValueSearch::SearchSync()
118
nsCOMPtr<nsIURI> baseURL;
119
nsCOMPtr<nsIDOM3Node> dom3Node = do_QueryInterface(mDocument);
122
dom3Node->GetBaseURI(uri);
123
NS_NewURI(getter_AddRefs(baseURL), uri);
126
nsCOMPtr<nsIDOMDocumentStyle> doc = do_QueryInterface(mDocument);
128
nsCOMPtr<nsIDOMStyleSheetList> sheets;
129
nsresult rv = doc->GetStyleSheets(getter_AddRefs(sheets));
130
NS_ENSURE_SUCCESS(rv, NS_OK);
133
sheets->GetLength(&length);
134
for (PRUint32 i = 0; i < length; ++i) {
135
nsCOMPtr<nsIDOMStyleSheet> sheet;
136
sheets->Item(i, getter_AddRefs(sheet));
137
nsCOMPtr<nsIDOMCSSStyleSheet> cssSheet = do_QueryInterface(sheet);
139
SearchStyleSheet(cssSheet, baseURL);
143
// XXX would be nice to search inline style as well.
149
inCSSValueSearch::SearchAsync(inISearchObserver *aObserver)
152
mObserver = aObserver;
159
inCSSValueSearch::SearchStop()
161
KillSearch(inISearchObserver::INTERRUPTED);
166
inCSSValueSearch::SearchStep(PRBool* _retval)
174
inCSSValueSearch::GetStringResultAt(PRInt32 aIndex, nsAString& _retval)
177
nsAutoString* result = (nsAutoString*)mResults->ElementAt(aIndex);
179
} else if (aIndex == mResultCount-1) {
180
_retval = mLastResult;
182
return NS_ERROR_FAILURE;
188
inCSSValueSearch::GetIntResultAt(PRInt32 aIndex, PRInt32 *_retval)
190
return NS_ERROR_NOT_IMPLEMENTED;
194
inCSSValueSearch::GetUIntResultAt(PRInt32 aIndex, PRUint32 *_retval)
196
return NS_ERROR_NOT_IMPLEMENTED;
199
///////////////////////////////////////////////////////////////////////////////
203
inCSSValueSearch::GetDocument(nsIDOMDocument** aDocument)
205
*aDocument = mDocument;
206
NS_IF_ADDREF(*aDocument);
211
inCSSValueSearch::SetDocument(nsIDOMDocument* aDocument)
213
mDocument = aDocument;
218
inCSSValueSearch::GetBaseURL(PRUnichar** aBaseURL)
220
if (!(*aBaseURL = ToNewUnicode(mBaseURL)))
221
return NS_ERROR_OUT_OF_MEMORY;
226
inCSSValueSearch::SetBaseURL(const PRUnichar* aBaseURL)
228
mBaseURL.Assign(aBaseURL);
233
inCSSValueSearch::GetReturnRelativeURLs(PRBool* aReturnRelativeURLs)
235
*aReturnRelativeURLs = mReturnRelativeURLs;
240
inCSSValueSearch::SetReturnRelativeURLs(PRBool aReturnRelativeURLs)
242
mReturnRelativeURLs = aReturnRelativeURLs;
247
inCSSValueSearch::GetNormalizeChromeURLs(PRBool *aNormalizeChromeURLs)
249
*aNormalizeChromeURLs = mNormalizeChromeURLs;
254
inCSSValueSearch::SetNormalizeChromeURLs(PRBool aNormalizeChromeURLs)
256
mNormalizeChromeURLs = aNormalizeChromeURLs;
261
inCSSValueSearch::AddPropertyCriteria(const PRUnichar *aPropName)
264
mCSSUtils->LookupCSSProperty(nsDependentString(aPropName), &prop);
265
mProperties[mPropertyCount] = prop;
271
inCSSValueSearch::GetTextCriteria(PRUnichar** aTextCriteria)
273
if (!(*aTextCriteria = ToNewUnicode(mTextCriteria)))
274
return NS_ERROR_OUT_OF_MEMORY;
279
inCSSValueSearch::SetTextCriteria(const PRUnichar* aTextCriteria)
281
mTextCriteria.Assign(aTextCriteria);
285
///////////////////////////////////////////////////////////////////////////////
289
inCSSValueSearch::InitSearch()
292
mResults = new nsVoidArray();
301
inCSSValueSearch::KillSearch(PRInt16 aResult)
304
mObserver->OnSearchEnd(this, aResult);
310
inCSSValueSearch::SearchStyleSheet(nsIDOMCSSStyleSheet* aStyleSheet, nsIURI* aBaseURL)
312
nsCOMPtr<nsIURI> baseURL;
314
aStyleSheet->GetHref(href);
318
NS_NewURI(getter_AddRefs(baseURL), href, nsnull, aBaseURL);
320
nsCOMPtr<nsIDOMCSSRuleList> rules;
321
nsresult rv = aStyleSheet->GetCssRules(getter_AddRefs(rules));
322
NS_ENSURE_SUCCESS(rv, rv);
324
return SearchRuleList(rules, baseURL);
328
inCSSValueSearch::SearchRuleList(nsIDOMCSSRuleList* aRuleList, nsIURI* aBaseURL)
331
aRuleList->GetLength(&length);
332
for (PRUint32 i = 0; i < length; ++i) {
333
nsCOMPtr<nsIDOMCSSRule> rule;
334
aRuleList->Item(i, getter_AddRefs(rule));
336
rule->GetType(&type);
338
case nsIDOMCSSRule::STYLE_RULE: {
339
nsCOMPtr<nsIDOMCSSStyleRule> styleRule = do_QueryInterface(rule);
340
SearchStyleRule(styleRule, aBaseURL);
342
case nsIDOMCSSRule::IMPORT_RULE: {
343
nsCOMPtr<nsIDOMCSSImportRule> importRule = do_QueryInterface(rule);
344
nsCOMPtr<nsIDOMCSSStyleSheet> childSheet;
345
importRule->GetStyleSheet(getter_AddRefs(childSheet));
347
SearchStyleSheet(childSheet, aBaseURL);
349
case nsIDOMCSSRule::MEDIA_RULE: {
350
nsCOMPtr<nsIDOMCSSMediaRule> mediaRule = do_QueryInterface(rule);
351
nsCOMPtr<nsIDOMCSSRuleList> childRules;
352
mediaRule->GetCssRules(getter_AddRefs(childRules));
353
SearchRuleList(childRules, aBaseURL);
356
// XXX handle nsIDOMCSSRule::PAGE_RULE if we ever support it
364
inCSSValueSearch::SearchStyleRule(nsIDOMCSSStyleRule* aStyleRule, nsIURI* aBaseURL)
366
nsCOMPtr<nsIDOMCSSStyleDeclaration> decl;
367
nsresult rv = aStyleRule->GetStyle(getter_AddRefs(decl));
368
NS_ENSURE_SUCCESS(rv, rv);
371
decl->GetLength(&length);
372
nsAutoString property, value;
373
for (PRUint32 i = 0; i < length; ++i) {
374
decl->Item(i, property);
375
// XXX This probably ought to use GetPropertyCSSValue if it were
377
decl->GetPropertyValue(property, value);
378
SearchStyleValue(value, aBaseURL);
384
inCSSValueSearch::SearchStyleValue(const nsAFlatString& aValue, nsIURI* aBaseURL)
386
if (StringBeginsWith(aValue, NS_LITERAL_STRING("url(")) &&
387
StringEndsWith(aValue, NS_LITERAL_STRING(")"))) {
388
const nsASingleFragmentString &url =
389
Substring(aValue, 4, aValue.Length() - 5);
390
// XXXldb Need to do more with |mReturnRelativeURLs|, perhaps?
391
nsCOMPtr<nsIURI> uri;
392
nsresult rv = NS_NewURI(getter_AddRefs(uri), url, nsnull, aBaseURL);
393
NS_ENSURE_SUCCESS(rv, rv);
396
nsAutoString *result = new NS_ConvertUTF8toUTF16(spec);
397
if (mReturnRelativeURLs)
399
mResults->AppendElement(result);
407
inCSSValueSearch::EqualizeURL(nsAutoString* aURL)
409
if (mNormalizeChromeURLs) {
410
if (aURL->Find("chrome://", PR_FALSE, 0, 1) >= 0) {
411
PRUint32 len = aURL->Length();
412
char* result = new char[len-8];
413
const PRUnichar* src = aURL->get();
415
PRUint32 milestone = 0;
421
if (milestone != 1) {
422
result[i-9-s] = src[i];
430
aURL->AssignWithConversion(result);