2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "inCSSValueSearch.h"
|
|
|
|
|
2014-04-12 01:18:54 -07:00
|
|
|
#include "mozilla/dom/StyleSheetList.h"
|
|
|
|
#include "nsCSSStyleSheet.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsVoidArray.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2011-05-26 01:18:43 -07:00
|
|
|
#include "nsIDOMDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMStyleSheetList.h"
|
|
|
|
#include "nsIDOMCSSStyleSheet.h"
|
|
|
|
#include "nsIDOMCSSRuleList.h"
|
|
|
|
#include "nsIDOMCSSStyleRule.h"
|
|
|
|
#include "nsIDOMCSSStyleDeclaration.h"
|
|
|
|
#include "nsIDOMCSSImportRule.h"
|
|
|
|
#include "nsIDOMCSSMediaRule.h"
|
2012-08-01 17:32:12 -07:00
|
|
|
#include "nsIDOMCSSSupportsRule.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIURI.h"
|
2012-07-29 21:29:41 -07:00
|
|
|
#include "nsIDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2014-04-12 01:18:54 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
inCSSValueSearch::inCSSValueSearch()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mResults(nullptr),
|
|
|
|
mProperties(nullptr),
|
2007-03-22 10:30:00 -07:00
|
|
|
mResultCount(0),
|
|
|
|
mPropertyCount(0),
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsActive(false),
|
|
|
|
mHoldResults(true),
|
|
|
|
mReturnRelativeURLs(true),
|
|
|
|
mNormalizeChromeURLs(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-10 15:52:29 -07:00
|
|
|
nsCSSProps::AddRefTable();
|
2007-03-22 10:30:00 -07:00
|
|
|
mProperties = new nsCSSProperty[100];
|
|
|
|
}
|
|
|
|
|
|
|
|
inCSSValueSearch::~inCSSValueSearch()
|
|
|
|
{
|
|
|
|
delete[] mProperties;
|
|
|
|
delete mResults;
|
2009-08-10 15:52:29 -07:00
|
|
|
nsCSSProps::ReleaseTable();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(inCSSValueSearch, inISearchProcess, inICSSValueSearch)
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// inISearchProcess
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::GetIsActive(bool *aIsActive)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aIsActive = mIsActive;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
inCSSValueSearch::GetResultCount(int32_t *aResultCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aResultCount = mResultCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::GetHoldResults(bool *aHoldResults)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aHoldResults = mHoldResults;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::SetHoldResults(bool aHoldResults)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mHoldResults = aHoldResults;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
inCSSValueSearch::SearchSync()
|
|
|
|
{
|
|
|
|
InitSearch();
|
|
|
|
|
2011-05-26 01:18:43 -07:00
|
|
|
if (!mDocument) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-12 01:18:54 -07:00
|
|
|
nsCOMPtr<nsIDocument> document = do_QueryInterface(mDocument);
|
|
|
|
MOZ_ASSERT(document);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> baseURI = document->GetBaseURI();
|
2011-06-14 00:56:48 -07:00
|
|
|
|
2014-04-12 01:18:54 -07:00
|
|
|
nsRefPtr<dom::StyleSheetList> sheets = document->StyleSheets();
|
|
|
|
MOZ_ASSERT(sheets);
|
2011-05-26 01:18:43 -07:00
|
|
|
|
2014-04-12 01:18:54 -07:00
|
|
|
uint32_t length = sheets->Length();
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2014-04-12 01:18:54 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet = sheets->Item(i);
|
|
|
|
SearchStyleSheet(sheet, baseURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
2008-02-20 14:39:46 -08:00
|
|
|
KillSearch(inISearchObserver::IN_INTERRUPTED);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::SearchStep(bool* _retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
inCSSValueSearch::GetStringResultAt(int32_t aIndex, nsAString& _retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mHoldResults) {
|
2009-02-03 06:42:18 -08:00
|
|
|
nsAutoString* result = mResults->ElementAt(aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
_retval = *result;
|
|
|
|
} else if (aIndex == mResultCount-1) {
|
|
|
|
_retval = mLastResult;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
inCSSValueSearch::GetIntResultAt(int32_t aIndex, int32_t *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
inCSSValueSearch::GetUIntResultAt(int32_t aIndex, uint32_t *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
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
|
2014-01-04 07:02:17 -08:00
|
|
|
inCSSValueSearch::GetBaseURL(char16_t** aBaseURL)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!(*aBaseURL = ToNewUnicode(mBaseURL)))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
inCSSValueSearch::SetBaseURL(const char16_t* aBaseURL)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mBaseURL.Assign(aBaseURL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::GetReturnRelativeURLs(bool* aReturnRelativeURLs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aReturnRelativeURLs = mReturnRelativeURLs;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::SetReturnRelativeURLs(bool aReturnRelativeURLs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mReturnRelativeURLs = aReturnRelativeURLs;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::GetNormalizeChromeURLs(bool *aNormalizeChromeURLs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aNormalizeChromeURLs = mNormalizeChromeURLs;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
inCSSValueSearch::SetNormalizeChromeURLs(bool aNormalizeChromeURLs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mNormalizeChromeURLs = aNormalizeChromeURLs;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
inCSSValueSearch::AddPropertyCriteria(const char16_t *aPropName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-10 15:52:29 -07:00
|
|
|
nsCSSProperty prop =
|
2012-07-13 16:59:05 -07:00
|
|
|
nsCSSProps::LookupProperty(nsDependentString(aPropName),
|
2014-03-07 09:14:23 -08:00
|
|
|
nsCSSProps::eIgnoreEnabledState);
|
2007-03-22 10:30:00 -07:00
|
|
|
mProperties[mPropertyCount] = prop;
|
|
|
|
mPropertyCount++;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
inCSSValueSearch::GetTextCriteria(char16_t** aTextCriteria)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!(*aTextCriteria = ToNewUnicode(mTextCriteria)))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
inCSSValueSearch::SetTextCriteria(const char16_t* aTextCriteria)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mTextCriteria.Assign(aTextCriteria);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// inCSSValueSearch
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
inCSSValueSearch::InitSearch()
|
|
|
|
{
|
|
|
|
if (mHoldResults) {
|
2009-02-03 06:42:18 -08:00
|
|
|
mResults = new nsTArray<nsAutoString *>();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mResultCount = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
inCSSValueSearch::KillSearch(int16_t aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsActive = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
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
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_NewURI(getter_AddRefs(baseURL), href, nullptr, aBaseURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length;
|
2007-03-22 10:30:00 -07:00
|
|
|
aRuleList->GetLength(&length);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMCSSRule> rule;
|
|
|
|
aRuleList->Item(i, getter_AddRefs(rule));
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t type;
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2012-08-01 17:32:12 -07:00
|
|
|
case nsIDOMCSSRule::SUPPORTS_RULE: {
|
|
|
|
nsCOMPtr<nsIDOMCSSSupportsRule> supportsRule = do_QueryInterface(rule);
|
|
|
|
nsCOMPtr<nsIDOMCSSRuleList> childRules;
|
|
|
|
supportsRule->GetCssRules(getter_AddRefs(childRules));
|
|
|
|
SearchRuleList(childRules, aBaseURL);
|
|
|
|
} break;
|
2007-03-22 10:30:00 -07:00
|
|
|
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);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length;
|
2007-03-22 10:30:00 -07:00
|
|
|
decl->GetLength(&length);
|
|
|
|
nsAutoString property, value;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), url, nullptr, aBaseURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec;
|
2007-03-22 10:30:00 -07:00
|
|
|
uri->GetSpec(spec);
|
|
|
|
nsAutoString *result = new NS_ConvertUTF8toUTF16(spec);
|
|
|
|
if (mReturnRelativeURLs)
|
|
|
|
EqualizeURL(result);
|
|
|
|
mResults->AppendElement(result);
|
|
|
|
++mResultCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
inCSSValueSearch::EqualizeURL(nsAutoString* aURL)
|
|
|
|
{
|
|
|
|
if (mNormalizeChromeURLs) {
|
2011-10-17 07:59:28 -07:00
|
|
|
if (aURL->Find("chrome://", false, 0, 1) >= 0) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = aURL->Length();
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* result = new char16_t[len-8];
|
|
|
|
const char16_t* src = aURL->get();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i = 9;
|
|
|
|
uint32_t milestone = 0;
|
|
|
|
uint32_t s = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
}
|