1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "inCSSValueSearch.h"
#include "mozilla/StyleSheetInlines.h"
#include "mozilla/dom/StyleSheetList.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsReadableUtils.h"
#include "nsIDOMDocument.h"
#include "nsIDOMStyleSheetList.h"
#include "nsIDOMCSSStyleSheet.h"
#include "nsIDOMCSSRuleList.h"
#include "nsIDOMCSSStyleRule.h"
#include "nsIDOMCSSStyleDeclaration.h"
#include "nsIDOMCSSImportRule.h"
#include "nsIDOMCSSMediaRule.h"
#include "nsIDOMCSSSupportsRule.h"
#include "nsIURI.h"
#include "nsIDocument.h"
#include "nsNetUtil.h"
using namespace mozilla;
///////////////////////////////////////////////////////////////////////////////
inCSSValueSearch::inCSSValueSearch()
: mResults(nullptr),
mProperties(nullptr),
mResultCount(0),
mPropertyCount(0),
mIsActive(false),
mHoldResults(true),
mReturnRelativeURLs(true),
mNormalizeChromeURLs(false)
{
nsCSSProps::AddRefTable();
mProperties = new nsCSSPropertyID[100];
}
inCSSValueSearch::~inCSSValueSearch()
{
delete[] mProperties;
delete mResults;
nsCSSProps::ReleaseTable();
}
NS_IMPL_ISUPPORTS(inCSSValueSearch, inISearchProcess, inICSSValueSearch)
///////////////////////////////////////////////////////////////////////////////
// inISearchProcess
NS_IMETHODIMP
inCSSValueSearch::GetIsActive(bool *aIsActive)
{
*aIsActive = mIsActive;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetResultCount(int32_t *aResultCount)
{
*aResultCount = mResultCount;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetHoldResults(bool *aHoldResults)
{
*aHoldResults = mHoldResults;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SetHoldResults(bool aHoldResults)
{
mHoldResults = aHoldResults;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SearchSync()
{
InitSearch();
if (!mDocument) {
return NS_OK;
}
nsCOMPtr<nsIDocument> document = do_QueryInterface(mDocument);
MOZ_ASSERT(document);
nsCOMPtr<nsIURI> baseURI = document->GetBaseURI();
RefPtr<dom::StyleSheetList> sheets = document->StyleSheets();
MOZ_ASSERT(sheets);
uint32_t length = sheets->Length();
for (uint32_t i = 0; i < length; ++i) {
RefPtr<StyleSheet> sheet = sheets->Item(i);
SearchStyleSheet(sheet, baseURI);
}
// XXX would be nice to search inline style as well.
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SearchAsync(inISearchObserver *aObserver)
{
InitSearch();
mObserver = aObserver;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SearchStop()
{
KillSearch(inISearchObserver::IN_INTERRUPTED);
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SearchStep(bool* _retval)
{
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetStringResultAt(int32_t aIndex, nsAString& _retval)
{
if (mHoldResults) {
nsAutoString* result = mResults->ElementAt(aIndex);
_retval = *result;
} else if (aIndex == mResultCount-1) {
_retval = mLastResult;
} else {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetIntResultAt(int32_t aIndex, int32_t *_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
inCSSValueSearch::GetUIntResultAt(int32_t aIndex, uint32_t *_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
///////////////////////////////////////////////////////////////////////////////
// inICSSValueSearch
NS_IMETHODIMP
inCSSValueSearch::GetDocument(nsIDOMDocument** aDocument)
{
*aDocument = mDocument;
NS_IF_ADDREF(*aDocument);
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SetDocument(nsIDOMDocument* aDocument)
{
mDocument = aDocument;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetBaseURL(char16_t** aBaseURL)
{
if (!(*aBaseURL = ToNewUnicode(mBaseURL)))
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SetBaseURL(const char16_t* aBaseURL)
{
mBaseURL.Assign(aBaseURL);
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetReturnRelativeURLs(bool* aReturnRelativeURLs)
{
*aReturnRelativeURLs = mReturnRelativeURLs;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SetReturnRelativeURLs(bool aReturnRelativeURLs)
{
mReturnRelativeURLs = aReturnRelativeURLs;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetNormalizeChromeURLs(bool *aNormalizeChromeURLs)
{
*aNormalizeChromeURLs = mNormalizeChromeURLs;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SetNormalizeChromeURLs(bool aNormalizeChromeURLs)
{
mNormalizeChromeURLs = aNormalizeChromeURLs;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::AddPropertyCriteria(const char16_t *aPropName)
{
nsCSSPropertyID prop =
nsCSSProps::LookupProperty(nsDependentString(aPropName),
CSSEnabledState::eIgnoreEnabledState);
mProperties[mPropertyCount] = prop;
mPropertyCount++;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::GetTextCriteria(char16_t** aTextCriteria)
{
if (!(*aTextCriteria = ToNewUnicode(mTextCriteria)))
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP
inCSSValueSearch::SetTextCriteria(const char16_t* aTextCriteria)
{
mTextCriteria.Assign(aTextCriteria);
return NS_OK;
}
///////////////////////////////////////////////////////////////////////////////
// inCSSValueSearch
nsresult
inCSSValueSearch::InitSearch()
{
if (mHoldResults) {
mResults = new nsTArray<nsAutoString *>();
}
mResultCount = 0;
return NS_OK;
}
nsresult
inCSSValueSearch::KillSearch(int16_t aResult)
{
mIsActive = true;
mObserver->OnSearchEnd(this, aResult);
return NS_OK;
}
nsresult
inCSSValueSearch::SearchStyleSheet(nsIDOMCSSStyleSheet* aStyleSheet, nsIURI* aBaseURL)
{
nsCOMPtr<nsIURI> baseURL;
nsAutoString href;
aStyleSheet->GetHref(href);
if (href.IsEmpty())
baseURL = aBaseURL;
else
NS_NewURI(getter_AddRefs(baseURL), href, nullptr, aBaseURL);
nsCOMPtr<nsIDOMCSSRuleList> rules;
nsresult rv = aStyleSheet->GetCssRules(getter_AddRefs(rules));
NS_ENSURE_SUCCESS(rv, rv);
return SearchRuleList(rules, baseURL);
}
nsresult
inCSSValueSearch::SearchRuleList(nsIDOMCSSRuleList* aRuleList, nsIURI* aBaseURL)
{
uint32_t length;
aRuleList->GetLength(&length);
for (uint32_t i = 0; i < length; ++i) {
nsCOMPtr<nsIDOMCSSRule> rule;
aRuleList->Item(i, getter_AddRefs(rule));
uint16_t type;
rule->GetType(&type);
switch (type) {
case nsIDOMCSSRule::STYLE_RULE: {
nsCOMPtr<nsIDOMCSSStyleRule> styleRule = do_QueryInterface(rule);
SearchStyleRule(styleRule, aBaseURL);
} break;
case nsIDOMCSSRule::IMPORT_RULE: {
nsCOMPtr<nsIDOMCSSImportRule> importRule = do_QueryInterface(rule);
nsCOMPtr<nsIDOMCSSStyleSheet> childSheet;
importRule->GetStyleSheet(getter_AddRefs(childSheet));
if (childSheet)
SearchStyleSheet(childSheet, aBaseURL);
} break;
case nsIDOMCSSRule::MEDIA_RULE: {
nsCOMPtr<nsIDOMCSSMediaRule> mediaRule = do_QueryInterface(rule);
nsCOMPtr<nsIDOMCSSRuleList> childRules;
mediaRule->GetCssRules(getter_AddRefs(childRules));
SearchRuleList(childRules, aBaseURL);
} break;
case nsIDOMCSSRule::SUPPORTS_RULE: {
nsCOMPtr<nsIDOMCSSSupportsRule> supportsRule = do_QueryInterface(rule);
nsCOMPtr<nsIDOMCSSRuleList> childRules;
supportsRule->GetCssRules(getter_AddRefs(childRules));
SearchRuleList(childRules, aBaseURL);
} break;
default:
// XXX handle nsIDOMCSSRule::PAGE_RULE if we ever support it
break;
}
}
return NS_OK;
}
nsresult
inCSSValueSearch::SearchStyleRule(nsIDOMCSSStyleRule* aStyleRule, nsIURI* aBaseURL)
{
nsCOMPtr<nsIDOMCSSStyleDeclaration> decl;
nsresult rv = aStyleRule->GetStyle(getter_AddRefs(decl));
NS_ENSURE_SUCCESS(rv, rv);
uint32_t length;
decl->GetLength(&length);
nsAutoString property, value;
for (uint32_t i = 0; i < length; ++i) {
decl->Item(i, property);
// XXX This probably ought to use GetPropertyCSSValue if it were
// implemented.
decl->GetPropertyValue(property, value);
SearchStyleValue(value, aBaseURL);
}
return NS_OK;
}
nsresult
inCSSValueSearch::SearchStyleValue(const nsAFlatString& aValue, nsIURI* aBaseURL)
{
if (StringBeginsWith(aValue, NS_LITERAL_STRING("url(")) &&
StringEndsWith(aValue, NS_LITERAL_STRING(")"))) {
const nsASingleFragmentString &url =
Substring(aValue, 4, aValue.Length() - 5);
// XXXldb Need to do more with |mReturnRelativeURLs|, perhaps?
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), url, nullptr, aBaseURL);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString spec;
rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString *result = new NS_ConvertUTF8toUTF16(spec);
if (mReturnRelativeURLs)
EqualizeURL(result);
mResults->AppendElement(result);
++mResultCount;
}
return NS_OK;
}
nsresult
inCSSValueSearch::EqualizeURL(nsAutoString* aURL)
{
if (mNormalizeChromeURLs) {
if (aURL->Find("chrome://", false, 0, 1) >= 0) {
uint32_t len = aURL->Length();
char16_t* result = new char16_t[len-8];
const char16_t* src = aURL->get();
uint32_t i = 9;
uint32_t milestone = 0;
uint32_t s = 0;
while (i < len) {
if (src[i] == '/') {
milestone += 1;
}
if (milestone != 1) {
result[i-9-s] = src[i];
} else {
s++;
}
i++;
}
result[i-9-s] = 0;
aURL->Assign(result);
delete [] result;
}
} else {
}
return NS_OK;
}
|