From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- layout/inspector/inCSSValueSearch.cpp | 407 ++++++ layout/inspector/inCSSValueSearch.h | 60 + layout/inspector/inDOMUtils.cpp | 1351 ++++++++++++++++++++ layout/inspector/inDOMUtils.h | 42 + layout/inspector/inDOMView.cpp | 1283 +++++++++++++++++++ layout/inspector/inDOMView.h | 96 ++ layout/inspector/inDeepTreeWalker.cpp | 442 +++++++ layout/inspector/inDeepTreeWalker.h | 65 + layout/inspector/inICSSValueSearch.idl | 29 + layout/inspector/inIDOMUtils.idl | 213 +++ layout/inspector/inIDOMView.idl | 24 + layout/inspector/inIDeepTreeWalker.idl | 47 + layout/inspector/inISearchObserver.idl | 21 + layout/inspector/inISearchProcess.idl | 49 + layout/inspector/inLayoutUtils.cpp | 71 + layout/inspector/inLayoutUtils.h | 27 + layout/inspector/inSearchLoop.cpp | 58 + layout/inspector/inSearchLoop.h | 28 + layout/inspector/moz.build | 49 + layout/inspector/nsFontFace.cpp | 225 ++++ layout/inspector/nsFontFace.h | 37 + layout/inspector/nsFontFaceList.cpp | 83 ++ layout/inspector/nsFontFaceList.h | 34 + layout/inspector/nsIDOMFontFace.idl | 32 + layout/inspector/nsIDOMFontFaceList.idl | 14 + layout/inspector/tests/bug1202095-2.css | 7 + layout/inspector/tests/bug1202095.css | 7 + layout/inspector/tests/bug856317.css | 23 + layout/inspector/tests/chrome/GentiumPlus-R.woff | Bin 0 -> 660480 bytes layout/inspector/tests/chrome/chrome.ini | 12 + layout/inspector/tests/chrome/test_bug467669.css | 8 + layout/inspector/tests/chrome/test_bug467669.xul | 174 +++ layout/inspector/tests/chrome/test_bug695639.css | 8 + layout/inspector/tests/chrome/test_bug695639.xul | 80 ++ layout/inspector/tests/chrome/test_bug708874.css | 33 + layout/inspector/tests/chrome/test_bug708874.xul | 296 +++++ layout/inspector/tests/chrome/test_bug727834.css | 7 + layout/inspector/tests/chrome/test_bug727834.xul | 88 ++ layout/inspector/tests/file_bug522601.html | 17 + layout/inspector/tests/mochitest.ini | 29 + layout/inspector/tests/test_bug1006595.html | 118 ++ layout/inspector/tests/test_bug462787.html | 100 ++ layout/inspector/tests/test_bug462789.html | 94 ++ layout/inspector/tests/test_bug522601.xhtml | 274 ++++ layout/inspector/tests/test_bug536379-2.html | 35 + layout/inspector/tests/test_bug536379.html | 46 + layout/inspector/tests/test_bug557726.html | 95 ++ layout/inspector/tests/test_bug609549.xhtml | 67 + layout/inspector/tests/test_bug806192.html | 26 + layout/inspector/tests/test_bug856317.html | 84 ++ layout/inspector/tests/test_bug877690.html | 260 ++++ layout/inspector/tests/test_color_to_rgba.html | 55 + .../tests/test_css_property_is_shorthand.html | 51 + .../tests/test_css_property_is_valid.html | 105 ++ .../tests/test_getCSSPseudoElementNames.html | 59 + .../inspector/tests/test_getRelativeRuleLine.html | 69 + .../inspector/tests/test_get_all_style_sheets.html | 44 + .../inspector/tests/test_is_valid_css_color.html | 91 ++ .../tests/test_isinheritableproperty.html | 38 + layout/inspector/tests/test_parseStyleSheet.html | 34 + .../tests/test_parseStyleSheetImport.html | 83 ++ .../tests/test_selectormatcheselement.html | 90 ++ 62 files changed, 7494 insertions(+) create mode 100644 layout/inspector/inCSSValueSearch.cpp create mode 100644 layout/inspector/inCSSValueSearch.h create mode 100644 layout/inspector/inDOMUtils.cpp create mode 100644 layout/inspector/inDOMUtils.h create mode 100644 layout/inspector/inDOMView.cpp create mode 100644 layout/inspector/inDOMView.h create mode 100644 layout/inspector/inDeepTreeWalker.cpp create mode 100644 layout/inspector/inDeepTreeWalker.h create mode 100644 layout/inspector/inICSSValueSearch.idl create mode 100644 layout/inspector/inIDOMUtils.idl create mode 100644 layout/inspector/inIDOMView.idl create mode 100644 layout/inspector/inIDeepTreeWalker.idl create mode 100644 layout/inspector/inISearchObserver.idl create mode 100644 layout/inspector/inISearchProcess.idl create mode 100644 layout/inspector/inLayoutUtils.cpp create mode 100644 layout/inspector/inLayoutUtils.h create mode 100644 layout/inspector/inSearchLoop.cpp create mode 100644 layout/inspector/inSearchLoop.h create mode 100644 layout/inspector/moz.build create mode 100644 layout/inspector/nsFontFace.cpp create mode 100644 layout/inspector/nsFontFace.h create mode 100644 layout/inspector/nsFontFaceList.cpp create mode 100644 layout/inspector/nsFontFaceList.h create mode 100644 layout/inspector/nsIDOMFontFace.idl create mode 100644 layout/inspector/nsIDOMFontFaceList.idl create mode 100644 layout/inspector/tests/bug1202095-2.css create mode 100644 layout/inspector/tests/bug1202095.css create mode 100644 layout/inspector/tests/bug856317.css create mode 100644 layout/inspector/tests/chrome/GentiumPlus-R.woff create mode 100644 layout/inspector/tests/chrome/chrome.ini create mode 100644 layout/inspector/tests/chrome/test_bug467669.css create mode 100644 layout/inspector/tests/chrome/test_bug467669.xul create mode 100644 layout/inspector/tests/chrome/test_bug695639.css create mode 100644 layout/inspector/tests/chrome/test_bug695639.xul create mode 100644 layout/inspector/tests/chrome/test_bug708874.css create mode 100644 layout/inspector/tests/chrome/test_bug708874.xul create mode 100644 layout/inspector/tests/chrome/test_bug727834.css create mode 100644 layout/inspector/tests/chrome/test_bug727834.xul create mode 100644 layout/inspector/tests/file_bug522601.html create mode 100644 layout/inspector/tests/mochitest.ini create mode 100644 layout/inspector/tests/test_bug1006595.html create mode 100644 layout/inspector/tests/test_bug462787.html create mode 100644 layout/inspector/tests/test_bug462789.html create mode 100644 layout/inspector/tests/test_bug522601.xhtml create mode 100644 layout/inspector/tests/test_bug536379-2.html create mode 100644 layout/inspector/tests/test_bug536379.html create mode 100644 layout/inspector/tests/test_bug557726.html create mode 100644 layout/inspector/tests/test_bug609549.xhtml create mode 100644 layout/inspector/tests/test_bug806192.html create mode 100644 layout/inspector/tests/test_bug856317.html create mode 100644 layout/inspector/tests/test_bug877690.html create mode 100644 layout/inspector/tests/test_color_to_rgba.html create mode 100644 layout/inspector/tests/test_css_property_is_shorthand.html create mode 100644 layout/inspector/tests/test_css_property_is_valid.html create mode 100644 layout/inspector/tests/test_getCSSPseudoElementNames.html create mode 100644 layout/inspector/tests/test_getRelativeRuleLine.html create mode 100644 layout/inspector/tests/test_get_all_style_sheets.html create mode 100644 layout/inspector/tests/test_is_valid_css_color.html create mode 100644 layout/inspector/tests/test_isinheritableproperty.html create mode 100644 layout/inspector/tests/test_parseStyleSheet.html create mode 100644 layout/inspector/tests/test_parseStyleSheetImport.html create mode 100644 layout/inspector/tests/test_selectormatcheselement.html (limited to 'layout/inspector') diff --git a/layout/inspector/inCSSValueSearch.cpp b/layout/inspector/inCSSValueSearch.cpp new file mode 100644 index 000000000..ecde09993 --- /dev/null +++ b/layout/inspector/inCSSValueSearch.cpp @@ -0,0 +1,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 document = do_QueryInterface(mDocument); + MOZ_ASSERT(document); + + nsCOMPtr baseURI = document->GetBaseURI(); + + RefPtr sheets = document->StyleSheets(); + MOZ_ASSERT(sheets); + + uint32_t length = sheets->Length(); + for (uint32_t i = 0; i < length; ++i) { + RefPtr 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(); + } + + 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 baseURL; + nsAutoString href; + aStyleSheet->GetHref(href); + if (href.IsEmpty()) + baseURL = aBaseURL; + else + NS_NewURI(getter_AddRefs(baseURL), href, nullptr, aBaseURL); + + nsCOMPtr 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 rule; + aRuleList->Item(i, getter_AddRefs(rule)); + uint16_t type; + rule->GetType(&type); + switch (type) { + case nsIDOMCSSRule::STYLE_RULE: { + nsCOMPtr styleRule = do_QueryInterface(rule); + SearchStyleRule(styleRule, aBaseURL); + } break; + case nsIDOMCSSRule::IMPORT_RULE: { + nsCOMPtr importRule = do_QueryInterface(rule); + nsCOMPtr childSheet; + importRule->GetStyleSheet(getter_AddRefs(childSheet)); + if (childSheet) + SearchStyleSheet(childSheet, aBaseURL); + } break; + case nsIDOMCSSRule::MEDIA_RULE: { + nsCOMPtr mediaRule = do_QueryInterface(rule); + nsCOMPtr childRules; + mediaRule->GetCssRules(getter_AddRefs(childRules)); + SearchRuleList(childRules, aBaseURL); + } break; + case nsIDOMCSSRule::SUPPORTS_RULE: { + nsCOMPtr supportsRule = do_QueryInterface(rule); + nsCOMPtr 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 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 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; +} diff --git a/layout/inspector/inCSSValueSearch.h b/layout/inspector/inCSSValueSearch.h new file mode 100644 index 000000000..3741fd331 --- /dev/null +++ b/layout/inspector/inCSSValueSearch.h @@ -0,0 +1,60 @@ +/* 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/. */ + +#ifndef __inCSSValueSearch_h__ +#define __inCSSValueSearch_h__ + +#include "inICSSValueSearch.h" + +#include "nsCOMPtr.h" +#include "nsString.h" +#include "nsIDOMDocument.h" +#include "inISearchObserver.h" +#include "nsTArray.h" +#include "nsCSSProps.h" + +class nsIDOMCSSStyleSheet; +class nsIDOMCSSRuleList; +class nsIDOMCSSStyleRule; +class nsIURI; + +class inCSSValueSearch final : public inICSSValueSearch +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_INISEARCHPROCESS + NS_DECL_INICSSVALUESEARCH + + inCSSValueSearch(); + +protected: + virtual ~inCSSValueSearch(); + nsCOMPtr mObserver; + nsCOMPtr mDocument; + nsTArray* mResults; + nsCSSPropertyID* mProperties; + nsString mLastResult; + nsString mBaseURL; + nsString mTextCriteria; + int32_t mResultCount; + uint32_t mPropertyCount; + bool mIsActive; + bool mHoldResults; + bool mReturnRelativeURLs; + bool mNormalizeChromeURLs; + + nsresult InitSearch(); + nsresult KillSearch(int16_t aResult); + nsresult SearchStyleSheet(nsIDOMCSSStyleSheet* aStyleSheet, nsIURI* aBaseURI); + nsresult SearchRuleList(nsIDOMCSSRuleList* aRuleList, nsIURI* aBaseURI); + nsresult SearchStyleRule(nsIDOMCSSStyleRule* aStyleRule, nsIURI* aBaseURI); + nsresult SearchStyleValue(const nsAFlatString& aValue, nsIURI* aBaseURI); + nsresult EqualizeURL(nsAutoString* aURL); +}; + +// {4D977F60-FBE7-4583-8CB7-F5ED882293EF} +#define IN_CSSVALUESEARCH_CID \ +{ 0x4d977f60, 0xfbe7, 0x4583, { 0x8c, 0xb7, 0xf5, 0xed, 0x88, 0x22, 0x93, 0xef } } + +#endif // __inCSSValueSearch_h__ diff --git a/layout/inspector/inDOMUtils.cpp b/layout/inspector/inDOMUtils.cpp new file mode 100644 index 000000000..9f1dcaad3 --- /dev/null +++ b/layout/inspector/inDOMUtils.cpp @@ -0,0 +1,1351 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "mozilla/ArrayUtils.h" +#include "mozilla/EventStates.h" + +#include "inDOMUtils.h" +#include "inLayoutUtils.h" + +#include "nsArray.h" +#include "nsAutoPtr.h" +#include "nsIServiceManager.h" +#include "nsString.h" +#include "nsIStyleSheetLinkingElement.h" +#include "nsIContentInlines.h" +#include "nsIDOMElement.h" +#include "nsIDocument.h" +#include "nsIPresShell.h" +#include "nsIDOMDocument.h" +#include "nsIDOMCharacterData.h" +#include "nsRuleNode.h" +#include "nsIStyleRule.h" +#include "mozilla/css/StyleRule.h" +#include "nsICSSStyleRuleDOMWrapper.h" +#include "nsIDOMWindow.h" +#include "nsXBLBinding.h" +#include "nsXBLPrototypeBinding.h" +#include "nsIMutableArray.h" +#include "nsBindingManager.h" +#include "ChildIterator.h" +#include "nsComputedDOMStyle.h" +#include "mozilla/EventStateManager.h" +#include "nsIAtom.h" +#include "nsRange.h" +#include "nsContentList.h" +#include "mozilla/StyleSheetInlines.h" +#include "mozilla/dom/Element.h" +#include "nsRuleWalker.h" +#include "nsRuleProcessorData.h" +#include "nsCSSPseudoClasses.h" +#include "nsCSSRuleProcessor.h" +#include "mozilla/dom/CSSLexer.h" +#include "mozilla/dom/InspectorUtilsBinding.h" +#include "mozilla/dom/ToJSValue.h" +#include "nsCSSParser.h" +#include "nsCSSProps.h" +#include "nsCSSValue.h" +#include "nsColor.h" +#include "nsStyleSet.h" +#include "nsStyleUtil.h" +#include "nsQueryObject.h" + +using namespace mozilla; +using namespace mozilla::css; +using namespace mozilla::dom; + +/////////////////////////////////////////////////////////////////////////////// + +inDOMUtils::inDOMUtils() +{ +} + +inDOMUtils::~inDOMUtils() +{ +} + +NS_IMPL_ISUPPORTS(inDOMUtils, inIDOMUtils) + +/////////////////////////////////////////////////////////////////////////////// +// inIDOMUtils + +NS_IMETHODIMP +inDOMUtils::GetAllStyleSheets(nsIDOMDocument *aDocument, uint32_t *aLength, + nsISupports ***aSheets) +{ + NS_ENSURE_ARG_POINTER(aDocument); + + nsTArray> sheets; + + nsCOMPtr document = do_QueryInterface(aDocument); + MOZ_ASSERT(document); + + // Get the agent, then user and finally xbl sheets in the style set. + nsIPresShell* presShell = document->GetShell(); + + if (presShell && presShell->StyleSet()->IsServo()) { + // XXXheycam ServoStyleSets don't have the ability to expose their + // sheets in a script-accessible way yet. + NS_ERROR("stylo: ServoStyleSets cannot expose their sheets to script yet"); + return NS_ERROR_FAILURE; + } + + if (presShell) { + nsStyleSet* styleSet = presShell->StyleSet()->AsGecko(); + SheetType sheetType = SheetType::Agent; + for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) { + sheets.AppendElement(styleSet->StyleSheetAt(sheetType, i)); + } + sheetType = SheetType::User; + for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) { + sheets.AppendElement(styleSet->StyleSheetAt(sheetType, i)); + } + AutoTArray xblSheetArray; + styleSet->AppendAllXBLStyleSheets(xblSheetArray); + + // The XBL stylesheet array will quite often be full of duplicates. Cope: + nsTHashtable> sheetSet; + for (CSSStyleSheet* sheet : xblSheetArray) { + if (!sheetSet.Contains(sheet)) { + sheetSet.PutEntry(sheet); + sheets.AppendElement(sheet); + } + } + } + + // Get the document sheets. + for (int32_t i = 0; i < document->GetNumberOfStyleSheets(); i++) { + // XXXheycam ServoStyleSets don't have the ability to expose their + // sheets in a script-accessible way yet. + sheets.AppendElement(document->GetStyleSheetAt(i)->AsGecko()); + } + + nsISupports** ret = static_cast(moz_xmalloc(sheets.Length() * + sizeof(nsISupports*))); + + for (size_t i = 0; i < sheets.Length(); i++) { + NS_ADDREF(ret[i] = NS_ISUPPORTS_CAST(nsIDOMCSSStyleSheet*, sheets[i])); + } + + *aLength = sheets.Length(); + *aSheets = ret; + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::IsIgnorableWhitespace(nsIDOMCharacterData *aDataNode, + bool *aReturn) +{ + NS_PRECONDITION(aReturn, "Must have an out parameter"); + + NS_ENSURE_ARG_POINTER(aDataNode); + + *aReturn = false; + + nsCOMPtr content = do_QueryInterface(aDataNode); + NS_ASSERTION(content, "Does not implement nsIContent!"); + + if (!content->TextIsOnlyWhitespace()) { + return NS_OK; + } + + // Okay. We have only white space. Let's check the white-space + // property now and make sure that this isn't preformatted text... + nsIFrame* frame = content->GetPrimaryFrame(); + if (frame) { + const nsStyleText* text = frame->StyleText(); + *aReturn = !text->WhiteSpaceIsSignificant(); + } + else { + // empty inter-tag text node without frame, e.g., in between \n + *aReturn = true; + } + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetParentForNode(nsIDOMNode* aNode, + bool aShowingAnonymousContent, + nsIDOMNode** aParent) +{ + NS_ENSURE_ARG_POINTER(aNode); + + // First do the special cases -- document nodes and anonymous content + nsCOMPtr doc(do_QueryInterface(aNode)); + nsCOMPtr parent; + + if (doc) { + parent = inLayoutUtils::GetContainerFor(*doc); + } else if (aShowingAnonymousContent) { + nsCOMPtr content = do_QueryInterface(aNode); + if (content) { + nsIContent* bparent = content->GetFlattenedTreeParent(); + parent = do_QueryInterface(bparent); + } + } + + if (!parent) { + // Ok, just get the normal DOM parent node + aNode->GetParentNode(getter_AddRefs(parent)); + } + + NS_IF_ADDREF(*aParent = parent); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetChildrenForNode(nsIDOMNode* aNode, + bool aShowingAnonymousContent, + nsIDOMNodeList** aChildren) +{ + NS_ENSURE_ARG_POINTER(aNode); + NS_PRECONDITION(aChildren, "Must have an out parameter"); + + nsCOMPtr kids; + + if (aShowingAnonymousContent) { + nsCOMPtr content = do_QueryInterface(aNode); + if (content) { + kids = content->GetChildren(nsIContent::eAllChildren); + } + } + + if (!kids) { + aNode->GetChildNodes(getter_AddRefs(kids)); + } + + kids.forget(aChildren); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetCSSStyleRules(nsIDOMElement *aElement, + const nsAString& aPseudo, + nsIArrayExtensions **_retval) +{ + NS_ENSURE_ARG_POINTER(aElement); + + *_retval = nullptr; + + nsCOMPtr pseudoElt; + if (!aPseudo.IsEmpty()) { + pseudoElt = NS_Atomize(aPseudo); + } + + nsRuleNode* ruleNode = nullptr; + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_STATE(element); + RefPtr styleContext; + GetRuleNodeForElement(element, pseudoElt, getter_AddRefs(styleContext), &ruleNode); + if (!ruleNode) { + // This can fail for elements that are not in the document or + // if the document they're in doesn't have a presshell. Bail out. + return NS_OK; + } + + AutoTArray ruleNodes; + while (!ruleNode->IsRoot()) { + ruleNodes.AppendElement(ruleNode); + ruleNode = ruleNode->GetParent(); + } + + nsCOMPtr rules = nsArray::Create(); + for (nsRuleNode* ruleNode : Reversed(ruleNodes)) { + RefPtr decl = do_QueryObject(ruleNode->GetRule()); + if (decl) { + RefPtr styleRule = + do_QueryObject(decl->GetOwningRule()); + if (styleRule) { + nsCOMPtr domRule = styleRule->GetDOMRule(); + if (domRule) { + rules->AppendElement(domRule, /*weak =*/ false); + } + } + } + } + + rules.forget(_retval); + + return NS_OK; +} + +static already_AddRefed +GetRuleFromDOMRule(nsIDOMCSSStyleRule *aRule, ErrorResult& rv) +{ + nsCOMPtr rule = do_QueryInterface(aRule); + if (!rule) { + rv.Throw(NS_ERROR_INVALID_POINTER); + return nullptr; + } + + RefPtr cssrule; + rv = rule->GetCSSStyleRule(getter_AddRefs(cssrule)); + if (rv.Failed()) { + return nullptr; + } + + if (!cssrule) { + rv.Throw(NS_ERROR_FAILURE); + } + return cssrule.forget(); +} + +NS_IMETHODIMP +inDOMUtils::GetRuleLine(nsIDOMCSSRule* aRule, uint32_t* _retval) +{ + NS_ENSURE_ARG_POINTER(aRule); + + Rule* rule = aRule->GetCSSRule(); + if (!rule) { + return NS_ERROR_FAILURE; + } + + *_retval = rule->GetLineNumber(); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetRuleColumn(nsIDOMCSSRule* aRule, uint32_t* _retval) +{ + NS_ENSURE_ARG_POINTER(aRule); + + Rule* rule = aRule->GetCSSRule(); + if (!rule) { + return NS_ERROR_FAILURE; + } + + *_retval = rule->GetColumnNumber(); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetRelativeRuleLine(nsIDOMCSSRule* aRule, uint32_t* _retval) +{ + NS_ENSURE_ARG_POINTER(aRule); + + Rule* rule = aRule->GetCSSRule(); + if (!rule) { + return NS_ERROR_FAILURE; + } + + uint32_t lineNumber = rule->GetLineNumber(); + CSSStyleSheet* sheet = rule->GetStyleSheet(); + if (sheet && lineNumber != 0) { + nsINode* owningNode = sheet->GetOwnerNode(); + if (owningNode) { + nsCOMPtr link = + do_QueryInterface(owningNode); + if (link) { + lineNumber -= link->GetLineNumber() - 1; + } + } + } + + *_retval = lineNumber; + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetCSSLexer(const nsAString& aText, JSContext* aCx, + JS::MutableHandleValue aResult) +{ + MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx)); + JS::Rooted scope(aCx, JS::CurrentGlobalOrNull(aCx)); + nsAutoPtr lexer(new CSSLexer(aText)); + if (!WrapNewBindingNonWrapperCachedObject(aCx, scope, lexer, aResult)) { + return NS_ERROR_FAILURE; + } + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetSelectorCount(nsIDOMCSSStyleRule* aRule, uint32_t *aCount) +{ + ErrorResult rv; + RefPtr rule = GetRuleFromDOMRule(aRule, rv); + if (rv.Failed()) { + return rv.StealNSResult(); + } + + uint32_t count = 0; + for (nsCSSSelectorList* sel = rule->Selector(); sel; sel = sel->mNext) { + ++count; + } + *aCount = count; + return NS_OK; +} + +static nsCSSSelectorList* +GetSelectorAtIndex(nsIDOMCSSStyleRule* aRule, uint32_t aIndex, ErrorResult& rv) +{ + RefPtr rule = GetRuleFromDOMRule(aRule, rv); + if (rv.Failed()) { + return nullptr; + } + + for (nsCSSSelectorList* sel = rule->Selector(); sel; + sel = sel->mNext, --aIndex) { + if (aIndex == 0) { + return sel; + } + } + + // Ran out of selectors + rv.Throw(NS_ERROR_INVALID_ARG); + return nullptr; +} + +NS_IMETHODIMP +inDOMUtils::GetSelectorText(nsIDOMCSSStyleRule* aRule, + uint32_t aSelectorIndex, + nsAString& aText) +{ + ErrorResult rv; + nsCSSSelectorList* sel = GetSelectorAtIndex(aRule, aSelectorIndex, rv); + if (rv.Failed()) { + return rv.StealNSResult(); + } + + RefPtr rule = GetRuleFromDOMRule(aRule, rv); + MOZ_ASSERT(!rv.Failed(), "How could we get a selector but not a rule?"); + + sel->mSelectors->ToString(aText, rule->GetStyleSheet(), false); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetSpecificity(nsIDOMCSSStyleRule* aRule, + uint32_t aSelectorIndex, + uint64_t* aSpecificity) +{ + ErrorResult rv; + nsCSSSelectorList* sel = GetSelectorAtIndex(aRule, aSelectorIndex, rv); + if (rv.Failed()) { + return rv.StealNSResult(); + } + + *aSpecificity = sel->mWeight; + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::SelectorMatchesElement(nsIDOMElement* aElement, + nsIDOMCSSStyleRule* aRule, + uint32_t aSelectorIndex, + const nsAString& aPseudo, + bool* aMatches) +{ + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(element); + + ErrorResult rv; + nsCSSSelectorList* tail = GetSelectorAtIndex(aRule, aSelectorIndex, rv); + if (rv.Failed()) { + return rv.StealNSResult(); + } + + // We want just the one list item, not the whole list tail + nsAutoPtr sel(tail->Clone(false)); + + // Do not attempt to match if a pseudo element is requested and this is not + // a pseudo element selector, or vice versa. + if (aPseudo.IsEmpty() == sel->mSelectors->IsPseudoElement()) { + *aMatches = false; + return NS_OK; + } + + if (!aPseudo.IsEmpty()) { + // We need to make sure that the requested pseudo element type + // matches the selector pseudo element type before proceeding. + nsCOMPtr pseudoElt = NS_Atomize(aPseudo); + if (sel->mSelectors->PseudoType() != nsCSSPseudoElements:: + GetPseudoType(pseudoElt, CSSEnabledState::eIgnoreEnabledState)) { + *aMatches = false; + return NS_OK; + } + + // We have a matching pseudo element, now remove it so we can compare + // directly against |element| when proceeding into SelectorListMatches. + // It's OK to do this - we just cloned sel and nothing else is using it. + sel->RemoveRightmostSelector(); + } + + element->OwnerDoc()->FlushPendingLinkUpdates(); + // XXXbz what exactly should we do with visited state here? + TreeMatchContext matchingContext(false, + nsRuleWalker::eRelevantLinkUnvisited, + element->OwnerDoc(), + TreeMatchContext::eNeverMatchVisited); + *aMatches = nsCSSRuleProcessor::SelectorListMatches(element, matchingContext, + sel); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::IsInheritedProperty(const nsAString &aPropertyName, bool *_retval) +{ + nsCSSPropertyID prop = nsCSSProps:: + LookupProperty(aPropertyName, CSSEnabledState::eIgnoreEnabledState); + if (prop == eCSSProperty_UNKNOWN) { + *_retval = false; + return NS_OK; + } + + if (prop == eCSSPropertyExtra_variable) { + *_retval = true; + return NS_OK; + } + + if (nsCSSProps::IsShorthand(prop)) { + prop = nsCSSProps::SubpropertyEntryFor(prop)[0]; + } + + nsStyleStructID sid = nsCSSProps::kSIDTable[prop]; + *_retval = !nsCachedStyleData::IsReset(sid); + return NS_OK; +} + +extern const char* const kCSSRawProperties[]; + +NS_IMETHODIMP +inDOMUtils::GetCSSPropertyNames(uint32_t aFlags, uint32_t* aCount, + char16_t*** aProps) +{ + // maxCount is the largest number of properties we could have; our actual + // number might be smaller because properties might be disabled. + uint32_t maxCount; + if (aFlags & EXCLUDE_SHORTHANDS) { + maxCount = eCSSProperty_COUNT_no_shorthands; + } else { + maxCount = eCSSProperty_COUNT; + } + + if (aFlags & INCLUDE_ALIASES) { + maxCount += (eCSSProperty_COUNT_with_aliases - eCSSProperty_COUNT); + } + + char16_t** props = + static_cast(moz_xmalloc(maxCount * sizeof(char16_t*))); + +#define DO_PROP(_prop) \ + PR_BEGIN_MACRO \ + nsCSSPropertyID cssProp = nsCSSPropertyID(_prop); \ + if (nsCSSProps::IsEnabled(cssProp, CSSEnabledState::eForAllContent)) { \ + props[propCount] = \ + ToNewUnicode(nsDependentCString(kCSSRawProperties[_prop])); \ + ++propCount; \ + } \ + PR_END_MACRO + + // prop is the property id we're considering; propCount is how many properties + // we've put into props so far. + uint32_t prop = 0, propCount = 0; + for ( ; prop < eCSSProperty_COUNT_no_shorthands; ++prop) { + if (nsCSSProps::PropertyParseType(nsCSSPropertyID(prop)) != + CSS_PROPERTY_PARSE_INACCESSIBLE) { + DO_PROP(prop); + } + } + + if (!(aFlags & EXCLUDE_SHORTHANDS)) { + for ( ; prop < eCSSProperty_COUNT; ++prop) { + // Some shorthands are also aliases + if ((aFlags & INCLUDE_ALIASES) || + !nsCSSProps::PropHasFlags(nsCSSPropertyID(prop), + CSS_PROPERTY_IS_ALIAS)) { + DO_PROP(prop); + } + } + } + + if (aFlags & INCLUDE_ALIASES) { + for (prop = eCSSProperty_COUNT; prop < eCSSProperty_COUNT_with_aliases; ++prop) { + DO_PROP(prop); + } + } + +#undef DO_PROP + + *aCount = propCount; + *aProps = props; + + return NS_OK; +} + +static void InsertNoDuplicates(nsTArray& aArray, + const nsAString& aString) +{ + size_t i = aArray.IndexOfFirstElementGt(aString); + if (i > 0 && aArray[i-1].Equals(aString)) { + return; + } + aArray.InsertElementAt(i, aString); +} + +static void GetKeywordsForProperty(const nsCSSPropertyID aProperty, + nsTArray& aArray) +{ + if (nsCSSProps::IsShorthand(aProperty)) { + // Shorthand props have no keywords. + return; + } + const nsCSSProps::KTableEntry* keywordTable = + nsCSSProps::kKeywordTableTable[aProperty]; + if (keywordTable) { + for (size_t i = 0; keywordTable[i].mKeyword != eCSSKeyword_UNKNOWN; ++i) { + nsCSSKeyword word = keywordTable[i].mKeyword; + InsertNoDuplicates(aArray, + NS_ConvertASCIItoUTF16(nsCSSKeywords::GetStringValue(word))); + } + } +} + +static void GetColorsForProperty(const uint32_t aParserVariant, + nsTArray& aArray) +{ + if (aParserVariant & VARIANT_COLOR) { + // GetKeywordsForProperty and GetOtherValuesForProperty assume aArray is sorted, + // and if aArray is not empty here, then it's not going to be sorted coming out. + MOZ_ASSERT(aArray.Length() == 0); + size_t size; + const char * const *allColorNames = NS_AllColorNames(&size); + nsString* utf16Names = aArray.AppendElements(size); + for (size_t i = 0; i < size; i++) { + CopyASCIItoUTF16(allColorNames[i], utf16Names[i]); + } + InsertNoDuplicates(aArray, NS_LITERAL_STRING("currentColor")); + } + return; +} + +static void GetOtherValuesForProperty(const uint32_t aParserVariant, + nsTArray& aArray) +{ + if (aParserVariant & VARIANT_AUTO) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("auto")); + } + if (aParserVariant & VARIANT_NORMAL) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("normal")); + } + if(aParserVariant & VARIANT_ALL) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("all")); + } + if (aParserVariant & VARIANT_NONE) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("none")); + } + if (aParserVariant & VARIANT_ELEMENT) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("-moz-element")); + } + if (aParserVariant & VARIANT_IMAGE_RECT) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("-moz-image-rect")); + } + if (aParserVariant & VARIANT_COLOR) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("rgb")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("hsl")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("rgba")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("hsla")); + } + if (aParserVariant & VARIANT_TIMING_FUNCTION) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("cubic-bezier")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("steps")); + } + if (aParserVariant & VARIANT_CALC) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("calc")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("-moz-calc")); + } + if (aParserVariant & VARIANT_URL) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("url")); + } + if (aParserVariant & VARIANT_GRADIENT) { + InsertNoDuplicates(aArray, NS_LITERAL_STRING("linear-gradient")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("radial-gradient")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("repeating-linear-gradient")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("repeating-radial-gradient")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("-moz-linear-gradient")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("-moz-radial-gradient")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("-moz-repeating-linear-gradient")); + InsertNoDuplicates(aArray, NS_LITERAL_STRING("-moz-repeating-radial-gradient")); + } +} + +NS_IMETHODIMP +inDOMUtils::GetSubpropertiesForCSSProperty(const nsAString& aProperty, + uint32_t* aLength, + char16_t*** aValues) +{ + nsCSSPropertyID propertyID = + nsCSSProps::LookupProperty(aProperty, CSSEnabledState::eForAllContent); + + if (propertyID == eCSSProperty_UNKNOWN) { + return NS_ERROR_FAILURE; + } + + if (propertyID == eCSSPropertyExtra_variable) { + *aValues = static_cast(moz_xmalloc(sizeof(char16_t*))); + (*aValues)[0] = ToNewUnicode(aProperty); + *aLength = 1; + return NS_OK; + } + + if (!nsCSSProps::IsShorthand(propertyID)) { + *aValues = static_cast(moz_xmalloc(sizeof(char16_t*))); + (*aValues)[0] = ToNewUnicode(nsCSSProps::GetStringValue(propertyID)); + *aLength = 1; + return NS_OK; + } + + // Count up how many subproperties we have. + size_t subpropCount = 0; + for (const nsCSSPropertyID *props = nsCSSProps::SubpropertyEntryFor(propertyID); + *props != eCSSProperty_UNKNOWN; ++props) { + ++subpropCount; + } + + *aValues = + static_cast(moz_xmalloc(subpropCount * sizeof(char16_t*))); + *aLength = subpropCount; + for (const nsCSSPropertyID *props = nsCSSProps::SubpropertyEntryFor(propertyID), + *props_start = props; + *props != eCSSProperty_UNKNOWN; ++props) { + (*aValues)[props-props_start] = ToNewUnicode(nsCSSProps::GetStringValue(*props)); + } + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::CssPropertyIsShorthand(const nsAString& aProperty, bool *_retval) +{ + nsCSSPropertyID propertyID = + nsCSSProps::LookupProperty(aProperty, CSSEnabledState::eForAllContent); + if (propertyID == eCSSProperty_UNKNOWN) { + return NS_ERROR_FAILURE; + } + + if (propertyID == eCSSPropertyExtra_variable) { + *_retval = false; + } else { + *_retval = nsCSSProps::IsShorthand(propertyID); + } + return NS_OK; +} + +// A helper function that determines whether the given property +// supports the given type. +static bool +PropertySupportsVariant(nsCSSPropertyID aPropertyID, uint32_t aVariant) +{ + if (nsCSSProps::IsShorthand(aPropertyID)) { + // We need a special case for border here, because while it resets + // border-image, it can't actually parse an image. + if (aPropertyID == eCSSProperty_border) { + return (aVariant & (VARIANT_COLOR | VARIANT_LENGTH)) != 0; + } + + for (const nsCSSPropertyID* props = nsCSSProps::SubpropertyEntryFor(aPropertyID); + *props != eCSSProperty_UNKNOWN; ++props) { + if (PropertySupportsVariant(*props, aVariant)) { + return true; + } + } + return false; + } + + // Properties that are parsed by functions must have their + // attributes hand-maintained here. + if (nsCSSProps::PropHasFlags(aPropertyID, CSS_PROPERTY_VALUE_PARSER_FUNCTION) || + nsCSSProps::PropertyParseType(aPropertyID) == CSS_PROPERTY_PARSE_FUNCTION) { + // These must all be special-cased. + uint32_t supported; + switch (aPropertyID) { + case eCSSProperty_border_image_slice: + case eCSSProperty_grid_template: + case eCSSProperty_grid: + supported = VARIANT_PN; + break; + + case eCSSProperty_border_image_outset: + supported = VARIANT_LN; + break; + + case eCSSProperty_border_image_width: + case eCSSProperty_stroke_dasharray: + supported = VARIANT_LPN; + break; + + case eCSSProperty_border_top_left_radius: + case eCSSProperty_border_top_right_radius: + case eCSSProperty_border_bottom_left_radius: + case eCSSProperty_border_bottom_right_radius: + case eCSSProperty_background_position: + case eCSSProperty_background_position_x: + case eCSSProperty_background_position_y: + case eCSSProperty_background_size: +#ifdef MOZ_ENABLE_MASK_AS_SHORTHAND + case eCSSProperty_mask_position: + case eCSSProperty_mask_position_x: + case eCSSProperty_mask_position_y: + case eCSSProperty_mask_size: +#endif + case eCSSProperty_grid_auto_columns: + case eCSSProperty_grid_auto_rows: + case eCSSProperty_grid_template_columns: + case eCSSProperty_grid_template_rows: + case eCSSProperty_object_position: + case eCSSProperty_scroll_snap_coordinate: + case eCSSProperty_scroll_snap_destination: + case eCSSProperty_transform_origin: + case eCSSProperty_perspective_origin: + case eCSSProperty__moz_outline_radius_topLeft: + case eCSSProperty__moz_outline_radius_topRight: + case eCSSProperty__moz_outline_radius_bottomLeft: + case eCSSProperty__moz_outline_radius_bottomRight: + supported = VARIANT_LP; + break; + + case eCSSProperty_border_bottom_colors: + case eCSSProperty_border_left_colors: + case eCSSProperty_border_right_colors: + case eCSSProperty_border_top_colors: + supported = VARIANT_COLOR; + break; + + case eCSSProperty_text_shadow: + case eCSSProperty_box_shadow: + supported = VARIANT_LENGTH | VARIANT_COLOR; + break; + + case eCSSProperty_border_spacing: + supported = VARIANT_LENGTH; + break; + + case eCSSProperty_content: + case eCSSProperty_cursor: + case eCSSProperty_clip_path: + case eCSSProperty_shape_outside: + supported = VARIANT_URL; + break; + + case eCSSProperty_fill: + case eCSSProperty_stroke: + supported = VARIANT_COLOR | VARIANT_URL; + break; + + case eCSSProperty_image_orientation: + supported = VARIANT_ANGLE; + break; + + case eCSSProperty_filter: + supported = VARIANT_URL; + break; + + case eCSSProperty_grid_column_start: + case eCSSProperty_grid_column_end: + case eCSSProperty_grid_row_start: + case eCSSProperty_grid_row_end: + case eCSSProperty_font_weight: + case eCSSProperty_initial_letter: + supported = VARIANT_NUMBER; + break; + + default: + supported = 0; + break; + } + + return (supported & aVariant) != 0; + } + + return (nsCSSProps::ParserVariant(aPropertyID) & aVariant) != 0; +} + +NS_IMETHODIMP +inDOMUtils::CssPropertySupportsType(const nsAString& aProperty, uint32_t aType, + bool *_retval) +{ + nsCSSPropertyID propertyID = + nsCSSProps::LookupProperty(aProperty, CSSEnabledState::eForAllContent); + if (propertyID == eCSSProperty_UNKNOWN) { + return NS_ERROR_FAILURE; + } + + if (propertyID >= eCSSProperty_COUNT) { + *_retval = false; + return NS_OK; + } + + uint32_t variant; + switch (aType) { + case TYPE_LENGTH: + variant = VARIANT_LENGTH; + break; + case TYPE_PERCENTAGE: + variant = VARIANT_PERCENT; + break; + case TYPE_COLOR: + variant = VARIANT_COLOR; + break; + case TYPE_URL: + variant = VARIANT_URL; + break; + case TYPE_ANGLE: + variant = VARIANT_ANGLE; + break; + case TYPE_FREQUENCY: + variant = VARIANT_FREQUENCY; + break; + case TYPE_TIME: + variant = VARIANT_TIME; + break; + case TYPE_GRADIENT: + variant = VARIANT_GRADIENT; + break; + case TYPE_TIMING_FUNCTION: + variant = VARIANT_TIMING_FUNCTION; + break; + case TYPE_IMAGE_RECT: + variant = VARIANT_IMAGE_RECT; + break; + case TYPE_NUMBER: + // Include integers under "number"? + variant = VARIANT_NUMBER | VARIANT_INTEGER; + break; + default: + // Unknown type + return NS_ERROR_NOT_AVAILABLE; + } + + *_retval = PropertySupportsVariant(propertyID, variant); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetCSSValuesForProperty(const nsAString& aProperty, + uint32_t* aLength, + char16_t*** aValues) +{ + nsCSSPropertyID propertyID = nsCSSProps:: + LookupProperty(aProperty, CSSEnabledState::eForAllContent); + if (propertyID == eCSSProperty_UNKNOWN) { + return NS_ERROR_FAILURE; + } + + nsTArray array; + // We start collecting the values, BUT colors need to go in first, because array + // needs to stay sorted, and the colors are sorted, so we just append them. + if (propertyID == eCSSPropertyExtra_variable) { + // No other values we can report. + } else if (!nsCSSProps::IsShorthand(propertyID)) { + // Property is longhand. + uint32_t propertyParserVariant = nsCSSProps::ParserVariant(propertyID); + // Get colors first. + GetColorsForProperty(propertyParserVariant, array); + if (propertyParserVariant & VARIANT_KEYWORD) { + GetKeywordsForProperty(propertyID, array); + } + GetOtherValuesForProperty(propertyParserVariant, array); + } else { + // Property is shorthand. + CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(subproperty, propertyID, + CSSEnabledState::eForAllContent) { + // Get colors (once) first. + uint32_t propertyParserVariant = nsCSSProps::ParserVariant(*subproperty); + if (propertyParserVariant & VARIANT_COLOR) { + GetColorsForProperty(propertyParserVariant, array); + break; + } + } + CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(subproperty, propertyID, + CSSEnabledState::eForAllContent) { + uint32_t propertyParserVariant = nsCSSProps::ParserVariant(*subproperty); + if (propertyParserVariant & VARIANT_KEYWORD) { + GetKeywordsForProperty(*subproperty, array); + } + GetOtherValuesForProperty(propertyParserVariant, array); + } + } + // All CSS properties take initial, inherit and unset. + InsertNoDuplicates(array, NS_LITERAL_STRING("initial")); + InsertNoDuplicates(array, NS_LITERAL_STRING("inherit")); + InsertNoDuplicates(array, NS_LITERAL_STRING("unset")); + + *aLength = array.Length(); + char16_t** ret = + static_cast(moz_xmalloc(*aLength * sizeof(char16_t*))); + for (uint32_t i = 0; i < *aLength; ++i) { + ret[i] = ToNewUnicode(array[i]); + } + *aValues = ret; + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::ColorNameToRGB(const nsAString& aColorName, JSContext* aCx, + JS::MutableHandle aValue) +{ + nscolor color; + if (!NS_ColorNameToRGB(aColorName, &color)) { + return NS_ERROR_INVALID_ARG; + } + + InspectorRGBTriple triple; + triple.mR = NS_GET_R(color); + triple.mG = NS_GET_G(color); + triple.mB = NS_GET_B(color); + + if (!ToJSValue(aCx, triple, aValue)) { + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::RgbToColorName(uint8_t aR, uint8_t aG, uint8_t aB, + nsAString& aColorName) +{ + const char* color = NS_RGBToColorName(NS_RGB(aR, aG, aB)); + if (!color) { + aColorName.Truncate(); + return NS_ERROR_INVALID_ARG; + } + + aColorName.AssignASCII(color); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::ColorToRGBA(const nsAString& aColorString, JSContext* aCx, + JS::MutableHandle aValue) +{ + nscolor color = 0; + nsCSSParser cssParser; + nsCSSValue cssValue; + + bool isColor = cssParser.ParseColorString(aColorString, nullptr, 0, + cssValue, true); + + if (!isColor) { + aValue.setNull(); + return NS_OK; + } + + nsRuleNode::ComputeColor(cssValue, nullptr, nullptr, color); + + InspectorRGBATuple tuple; + tuple.mR = NS_GET_R(color); + tuple.mG = NS_GET_G(color); + tuple.mB = NS_GET_B(color); + tuple.mA = nsStyleUtil::ColorComponentToFloat(NS_GET_A(color)); + + if (!ToJSValue(aCx, tuple, aValue)) { + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::IsValidCSSColor(const nsAString& aColorString, bool *_retval) +{ + nsCSSParser cssParser; + nsCSSValue cssValue; + *_retval = cssParser.ParseColorString(aColorString, nullptr, 0, cssValue, true); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::CssPropertyIsValid(const nsAString& aPropertyName, + const nsAString& aPropertyValue, + bool *_retval) +{ + nsCSSPropertyID propertyID = nsCSSProps:: + LookupProperty(aPropertyName, CSSEnabledState::eIgnoreEnabledState); + + if (propertyID == eCSSProperty_UNKNOWN) { + *_retval = false; + return NS_OK; + } + + if (propertyID == eCSSPropertyExtra_variable) { + *_retval = true; + return NS_OK; + } + + // Get a parser, parse the property. + nsCSSParser parser; + *_retval = parser.IsValueValidForProperty(propertyID, aPropertyValue); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetBindingURLs(nsIDOMElement *aElement, nsIArray **_retval) +{ + NS_ENSURE_ARG_POINTER(aElement); + + *_retval = nullptr; + + nsCOMPtr urls = do_CreateInstance(NS_ARRAY_CONTRACTID); + if (!urls) + return NS_ERROR_FAILURE; + + nsCOMPtr content = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(content); + + nsXBLBinding *binding = content->GetXBLBinding(); + + while (binding) { + urls->AppendElement(binding->PrototypeBinding()->BindingURI(), false); + binding = binding->GetBaseBinding(); + } + + urls.forget(_retval); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::SetContentState(nsIDOMElement* aElement, + EventStates::InternalType aState, + bool* aRetVal) +{ + NS_ENSURE_ARG_POINTER(aElement); + + RefPtr esm = + inLayoutUtils::GetEventStateManagerFor(aElement); + NS_ENSURE_TRUE(esm, NS_ERROR_INVALID_ARG); + + nsCOMPtr content; + content = do_QueryInterface(aElement); + NS_ENSURE_TRUE(content, NS_ERROR_INVALID_ARG); + + *aRetVal = esm->SetContentState(content, EventStates(aState)); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::RemoveContentState(nsIDOMElement* aElement, + EventStates::InternalType aState, + bool* aRetVal) +{ + NS_ENSURE_ARG_POINTER(aElement); + + RefPtr esm = + inLayoutUtils::GetEventStateManagerFor(aElement); + NS_ENSURE_TRUE(esm, NS_ERROR_INVALID_ARG); + + *aRetVal = esm->SetContentState(nullptr, EventStates(aState)); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetContentState(nsIDOMElement* aElement, + EventStates::InternalType* aState) +{ + *aState = 0; + nsCOMPtr content = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(content); + + // NOTE: if this method is removed, + // please remove GetInternalValue from EventStates + *aState = content->AsElement()->State().GetInternalValue(); + return NS_OK; +} + +/* static */ nsresult +inDOMUtils::GetRuleNodeForElement(dom::Element* aElement, + nsIAtom* aPseudo, + nsStyleContext** aStyleContext, + nsRuleNode** aRuleNode) +{ + MOZ_ASSERT(aElement); + + *aRuleNode = nullptr; + *aStyleContext = nullptr; + + nsIDocument* doc = aElement->GetComposedDoc(); + NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED); + + nsIPresShell *presShell = doc->GetShell(); + NS_ENSURE_TRUE(presShell, NS_ERROR_UNEXPECTED); + + nsPresContext *presContext = presShell->GetPresContext(); + NS_ENSURE_TRUE(presContext, NS_ERROR_UNEXPECTED); + + presContext->EnsureSafeToHandOutCSSRules(); + + RefPtr sContext = + nsComputedDOMStyle::GetStyleContextForElement(aElement, aPseudo, presShell); + if (sContext) { + *aRuleNode = sContext->RuleNode(); + sContext.forget(aStyleContext); + } + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::GetUsedFontFaces(nsIDOMRange* aRange, + nsIDOMFontFaceList** aFontFaceList) +{ + return static_cast(aRange)->GetUsedFontFaces(aFontFaceList); +} + +static EventStates +GetStatesForPseudoClass(const nsAString& aStatePseudo) +{ + // An array of the states that are relevant for various pseudoclasses. + // XXXbz this duplicates code in nsCSSRuleProcessor + static const EventStates sPseudoClassStates[] = { +#define CSS_PSEUDO_CLASS(_name, _value, _flags, _pref) \ + EventStates(), +#define CSS_STATE_PSEUDO_CLASS(_name, _value, _flags, _pref, _states) \ + _states, +#include "nsCSSPseudoClassList.h" +#undef CSS_STATE_PSEUDO_CLASS +#undef CSS_PSEUDO_CLASS + + // Add more entries for our fake values to make sure we can't + // index out of bounds into this array no matter what. + EventStates(), + EventStates() + }; + static_assert(MOZ_ARRAY_LENGTH(sPseudoClassStates) == + static_cast(CSSPseudoClassType::MAX), + "Length of PseudoClassStates array is incorrect"); + + nsCOMPtr atom = NS_Atomize(aStatePseudo); + CSSPseudoClassType type = nsCSSPseudoClasses:: + GetPseudoType(atom, CSSEnabledState::eIgnoreEnabledState); + + // Ignore :any-link so we don't give the element simultaneous + // visited and unvisited style state + if (type == CSSPseudoClassType::anyLink || + type == CSSPseudoClassType::mozAnyLink) { + return EventStates(); + } + // Our array above is long enough that indexing into it with + // NotPseudo is ok. + return sPseudoClassStates[static_cast(type)]; +} + +NS_IMETHODIMP +inDOMUtils::GetCSSPseudoElementNames(uint32_t* aLength, char16_t*** aNames) +{ + nsTArray array; + + const CSSPseudoElementTypeBase pseudoCount = + static_cast(CSSPseudoElementType::Count); + for (CSSPseudoElementTypeBase i = 0; i < pseudoCount; ++i) { + CSSPseudoElementType type = static_cast(i); + if (nsCSSPseudoElements::IsEnabled(type, CSSEnabledState::eForAllContent)) { + nsIAtom* atom = nsCSSPseudoElements::GetPseudoAtom(type); + array.AppendElement(atom); + } + } + + *aLength = array.Length(); + char16_t** ret = + static_cast(moz_xmalloc(*aLength * sizeof(char16_t*))); + for (uint32_t i = 0; i < *aLength; ++i) { + ret[i] = ToNewUnicode(nsDependentAtomString(array[i])); + } + *aNames = ret; + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::AddPseudoClassLock(nsIDOMElement *aElement, + const nsAString &aPseudoClass) +{ + EventStates state = GetStatesForPseudoClass(aPseudoClass); + if (state.IsEmpty()) { + return NS_OK; + } + + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(element); + + element->LockStyleStates(state); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::RemovePseudoClassLock(nsIDOMElement *aElement, + const nsAString &aPseudoClass) +{ + EventStates state = GetStatesForPseudoClass(aPseudoClass); + if (state.IsEmpty()) { + return NS_OK; + } + + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(element); + + element->UnlockStyleStates(state); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::HasPseudoClassLock(nsIDOMElement *aElement, + const nsAString &aPseudoClass, + bool *_retval) +{ + EventStates state = GetStatesForPseudoClass(aPseudoClass); + if (state.IsEmpty()) { + *_retval = false; + return NS_OK; + } + + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(element); + + EventStates locks = element->LockedStyleStates(); + + *_retval = locks.HasAllStates(state); + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::ClearPseudoClassLocks(nsIDOMElement *aElement) +{ + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(element); + + element->ClearStyleStateLocks(); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMUtils::ParseStyleSheet(nsIDOMCSSStyleSheet *aSheet, + const nsAString& aInput) +{ + RefPtr sheet = do_QueryObject(aSheet); + NS_ENSURE_ARG_POINTER(sheet); + + return sheet->ReparseSheet(aInput); +} + +NS_IMETHODIMP +inDOMUtils::ScrollElementIntoView(nsIDOMElement *aElement) +{ + nsCOMPtr content = do_QueryInterface(aElement); + NS_ENSURE_ARG_POINTER(content); + + nsIPresShell* presShell = content->OwnerDoc()->GetShell(); + if (!presShell) { + return NS_OK; + } + + presShell->ScrollContentIntoView(content, + nsIPresShell::ScrollAxis(), + nsIPresShell::ScrollAxis(), + nsIPresShell::SCROLL_OVERFLOW_HIDDEN); + + return NS_OK; +} diff --git a/layout/inspector/inDOMUtils.h b/layout/inspector/inDOMUtils.h new file mode 100644 index 000000000..e12915d16 --- /dev/null +++ b/layout/inspector/inDOMUtils.h @@ -0,0 +1,42 @@ +/* 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/. */ + +#ifndef __inDOMUtils_h__ +#define __inDOMUtils_h__ + +#include "inIDOMUtils.h" + +class nsRuleNode; +class nsStyleContext; +class nsIAtom; + +namespace mozilla { +namespace dom { +class Element; +} // namespace dom +} // namespace mozilla + +class inDOMUtils final : public inIDOMUtils +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_INIDOMUTILS + + inDOMUtils(); + +private: + virtual ~inDOMUtils(); + + // aStyleContext must be released by the caller once he's done with aRuleNode. + static nsresult GetRuleNodeForElement(mozilla::dom::Element* aElement, + nsIAtom* aPseudo, + nsStyleContext** aStyleContext, + nsRuleNode** aRuleNode); +}; + +// {0a499822-a287-4089-ad3f-9ffcd4f40263} +#define IN_DOMUTILS_CID \ + {0x0a499822, 0xa287, 0x4089, {0xad, 0x3f, 0x9f, 0xfc, 0xd4, 0xf4, 0x02, 0x63}} + +#endif // __inDOMUtils_h__ diff --git a/layout/inspector/inDOMView.cpp b/layout/inspector/inDOMView.cpp new file mode 100644 index 000000000..967671ba3 --- /dev/null +++ b/layout/inspector/inDOMView.cpp @@ -0,0 +1,1283 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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 "inDOMView.h" +#include "inIDOMUtils.h" + +#include "inLayoutUtils.h" + +#include "nsString.h" +#include "nsReadableUtils.h" +#include "nsIDOMNode.h" +#include "nsIDOMNodeFilter.h" +#include "nsIDOMNodeList.h" +#include "nsIDOMCharacterData.h" +#include "nsIDOMAttr.h" +#include "nsIDOMMozNamedAttrMap.h" +#include "nsIDOMMutationEvent.h" +#include "nsBindingManager.h" +#include "nsNameSpaceManager.h" +#include "nsIDocument.h" +#include "nsIServiceManager.h" +#include "nsITreeColumns.h" +#include "nsITreeBoxObject.h" +#include "mozilla/dom/Element.h" +#include "mozilla/Services.h" + +#ifdef ACCESSIBILITY +#include "nsAccessibilityService.h" +#endif + +using namespace mozilla; + +//////////////////////////////////////////////////////////////////////// +// inDOMViewNode + +class inDOMViewNode +{ +public: + inDOMViewNode() {} + explicit inDOMViewNode(nsIDOMNode* aNode); + ~inDOMViewNode(); + + nsCOMPtr node; + + inDOMViewNode* parent; + inDOMViewNode* next; + inDOMViewNode* previous; + + int32_t level; + bool isOpen; + bool isContainer; + bool hasAnonymous; + bool hasSubDocument; +}; + +inDOMViewNode::inDOMViewNode(nsIDOMNode* aNode) : + node(aNode), + parent(nullptr), + next(nullptr), + previous(nullptr), + level(0), + isOpen(false), + isContainer(false), + hasAnonymous(false), + hasSubDocument(false) +{ + +} + +inDOMViewNode::~inDOMViewNode() +{ +} + +//////////////////////////////////////////////////////////////////////// + +inDOMView::inDOMView() : + mShowAnonymous(false), + mShowSubDocuments(false), + mShowWhitespaceNodes(true), + mShowAccessibleNodes(false), + mWhatToShow(nsIDOMNodeFilter::SHOW_ALL) +{ +} + +inDOMView::~inDOMView() +{ + SetRootNode(nullptr); +} + + +//////////////////////////////////////////////////////////////////////// +// nsISupports + +NS_IMPL_ISUPPORTS(inDOMView, + inIDOMView, + nsITreeView, + nsIMutationObserver) + +//////////////////////////////////////////////////////////////////////// +// inIDOMView + +NS_IMETHODIMP +inDOMView::GetRootNode(nsIDOMNode** aNode) +{ + *aNode = mRootNode; + NS_IF_ADDREF(*aNode); + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetRootNode(nsIDOMNode* aNode) +{ + if (mTree) + mTree->BeginUpdateBatch(); + + if (mRootDocument) { + // remove previous document observer + nsCOMPtr doc(do_QueryInterface(mRootDocument)); + if (doc) + doc->RemoveMutationObserver(this); + } + + RemoveAllNodes(); + + mRootNode = aNode; + + if (aNode) { + // If we are able to show element nodes, then start with the root node + // as the first node in the buffer + if (mWhatToShow & nsIDOMNodeFilter::SHOW_ELEMENT) { + // allocate new node array + AppendNode(CreateNode(aNode, nullptr)); + } else { + // place only the children of the root node in the buffer + ExpandNode(-1); + } + + // store an owning reference to document so that it isn't + // destroyed before we are + mRootDocument = do_QueryInterface(aNode); + if (!mRootDocument) { + aNode->GetOwnerDocument(getter_AddRefs(mRootDocument)); + } + + // add document observer + nsCOMPtr doc(do_QueryInterface(mRootDocument)); + if (doc) + doc->AddMutationObserver(this); + } else { + mRootDocument = nullptr; + } + + if (mTree) + mTree->EndUpdateBatch(); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetNodeFromRowIndex(int32_t rowIndex, nsIDOMNode **_retval) +{ + inDOMViewNode* viewNode = nullptr; + RowToNode(rowIndex, &viewNode); + if (!viewNode) return NS_ERROR_FAILURE; + *_retval = viewNode->node; + NS_IF_ADDREF(*_retval); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetRowIndexFromNode(nsIDOMNode *node, int32_t *_retval) +{ + NodeToRow(node, _retval); + return NS_OK; +} + + +NS_IMETHODIMP +inDOMView::GetShowAnonymousContent(bool *aShowAnonymousContent) +{ + *aShowAnonymousContent = mShowAnonymous; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetShowAnonymousContent(bool aShowAnonymousContent) +{ + mShowAnonymous = aShowAnonymousContent; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetShowSubDocuments(bool *aShowSubDocuments) +{ + *aShowSubDocuments = mShowSubDocuments; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetShowSubDocuments(bool aShowSubDocuments) +{ + mShowSubDocuments = aShowSubDocuments; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetShowWhitespaceNodes(bool *aShowWhitespaceNodes) +{ + *aShowWhitespaceNodes = mShowWhitespaceNodes; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetShowWhitespaceNodes(bool aShowWhitespaceNodes) +{ + mShowWhitespaceNodes = aShowWhitespaceNodes; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetShowAccessibleNodes(bool *aShowAccessibleNodes) +{ + *aShowAccessibleNodes = mShowAccessibleNodes; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetShowAccessibleNodes(bool aShowAccessibleNodes) +{ + mShowAccessibleNodes = aShowAccessibleNodes; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetWhatToShow(uint32_t *aWhatToShow) +{ + *aWhatToShow = mWhatToShow; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetWhatToShow(uint32_t aWhatToShow) +{ + mWhatToShow = aWhatToShow; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::Rebuild() +{ + nsCOMPtr root; + GetRootNode(getter_AddRefs(root)); + SetRootNode(root); + return NS_OK; +} + +//////////////////////////////////////////////////////////////////////// +// nsITreeView + +NS_IMETHODIMP +inDOMView::GetRowCount(int32_t *aRowCount) +{ + *aRowCount = GetRowCount(); + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetRowProperties(int32_t index, nsAString& aProps) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetCellProperties(int32_t row, nsITreeColumn* col, + nsAString& aProps) +{ + inDOMViewNode* node = nullptr; + RowToNode(row, &node); + if (!node) return NS_ERROR_FAILURE; + + nsCOMPtr content = do_QueryInterface(node->node); + if (content && content->IsInAnonymousSubtree()) { + aProps.AppendLiteral("anonymous "); + } + + uint16_t nodeType; + node->node->GetNodeType(&nodeType); + switch (nodeType) { + case nsIDOMNode::ELEMENT_NODE: + aProps.AppendLiteral("ELEMENT_NODE"); + break; + case nsIDOMNode::ATTRIBUTE_NODE: + aProps.AppendLiteral("ATTRIBUTE_NODE"); + break; + case nsIDOMNode::TEXT_NODE: + aProps.AppendLiteral("TEXT_NODE"); + break; + case nsIDOMNode::CDATA_SECTION_NODE: + aProps.AppendLiteral("CDATA_SECTION_NODE"); + break; + case nsIDOMNode::ENTITY_REFERENCE_NODE: + aProps.AppendLiteral("ENTITY_REFERENCE_NODE"); + break; + case nsIDOMNode::ENTITY_NODE: + aProps.AppendLiteral("ENTITY_NODE"); + break; + case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: + aProps.AppendLiteral("PROCESSING_INSTRUCTION_NODE"); + break; + case nsIDOMNode::COMMENT_NODE: + aProps.AppendLiteral("COMMENT_NODE"); + break; + case nsIDOMNode::DOCUMENT_NODE: + aProps.AppendLiteral("DOCUMENT_NODE"); + break; + case nsIDOMNode::DOCUMENT_TYPE_NODE: + aProps.AppendLiteral("DOCUMENT_TYPE_NODE"); + break; + case nsIDOMNode::DOCUMENT_FRAGMENT_NODE: + aProps.AppendLiteral("DOCUMENT_FRAGMENT_NODE"); + break; + case nsIDOMNode::NOTATION_NODE: + aProps.AppendLiteral("NOTATION_NODE"); + break; + } + +#ifdef ACCESSIBILITY + if (mShowAccessibleNodes) { + nsAccessibilityService* accService = GetOrCreateAccService(); + NS_ENSURE_TRUE(accService, NS_ERROR_FAILURE); + + if (accService->HasAccessible(node->node)) + aProps.AppendLiteral(" ACCESSIBLE_NODE"); + } +#endif + + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetColumnProperties(nsITreeColumn* col, nsAString& aProps) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetImageSrc(int32_t row, nsITreeColumn* col, nsAString& _retval) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetProgressMode(int32_t row, nsITreeColumn* col, int32_t* _retval) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetCellValue(int32_t row, nsITreeColumn* col, nsAString& _retval) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetCellText(int32_t row, nsITreeColumn* col, nsAString& _retval) +{ + inDOMViewNode* node = nullptr; + RowToNode(row, &node); + if (!node) return NS_ERROR_FAILURE; + + nsIDOMNode* domNode = node->node; + + nsAutoString colID; + col->GetId(colID); + if (colID.EqualsLiteral("colNodeName")) + domNode->GetNodeName(_retval); + else if (colID.EqualsLiteral("colLocalName")) + domNode->GetLocalName(_retval); + else if (colID.EqualsLiteral("colPrefix")) + domNode->GetPrefix(_retval); + else if (colID.EqualsLiteral("colNamespaceURI")) + domNode->GetNamespaceURI(_retval); + else if (colID.EqualsLiteral("colNodeType")) { + uint16_t nodeType; + domNode->GetNodeType(&nodeType); + nsAutoString temp; + temp.AppendInt(int32_t(nodeType)); + _retval = temp; + } else if (colID.EqualsLiteral("colNodeValue")) + domNode->GetNodeValue(_retval); + else { + if (StringBeginsWith(colID, NS_LITERAL_STRING("col@"))) { + nsCOMPtr el = do_QueryInterface(node->node); + if (el) { + nsAutoString attr; + colID.Right(attr, colID.Length()-4); // have to use this because Substring is crashing on me! + el->GetAttribute(attr, _retval); + } + } + } + + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::IsContainer(int32_t index, bool *_retval) +{ + inDOMViewNode* node = nullptr; + RowToNode(index, &node); + if (!node) return NS_ERROR_FAILURE; + + *_retval = node->isContainer; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::IsContainerOpen(int32_t index, bool *_retval) +{ + inDOMViewNode* node = nullptr; + RowToNode(index, &node); + if (!node) return NS_ERROR_FAILURE; + + *_retval = node->isOpen; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::IsContainerEmpty(int32_t index, bool *_retval) +{ + inDOMViewNode* node = nullptr; + RowToNode(index, &node); + if (!node) return NS_ERROR_FAILURE; + + *_retval = node->isContainer ? false : true; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetLevel(int32_t index, int32_t *_retval) +{ + inDOMViewNode* node = nullptr; + RowToNode(index, &node); + if (!node) return NS_ERROR_FAILURE; + + *_retval = node->level; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetParentIndex(int32_t rowIndex, int32_t *_retval) +{ + inDOMViewNode* node = nullptr; + RowToNode(rowIndex, &node); + if (!node) return NS_ERROR_FAILURE; + + // GetParentIndex returns -1 if there is no parent + *_retval = -1; + + inDOMViewNode* checkNode = nullptr; + int32_t i = rowIndex - 1; + do { + nsresult rv = RowToNode(i, &checkNode); + if (NS_FAILED(rv)) { + // No parent. Just break out. + break; + } + + if (checkNode == node->parent) { + *_retval = i; + return NS_OK; + } + --i; + } while (checkNode); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::HasNextSibling(int32_t rowIndex, int32_t afterIndex, bool *_retval) +{ + inDOMViewNode* node = nullptr; + RowToNode(rowIndex, &node); + if (!node) return NS_ERROR_FAILURE; + + *_retval = node->next != nullptr; + + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::ToggleOpenState(int32_t index) +{ + inDOMViewNode* node = nullptr; + RowToNode(index, &node); + if (!node) return NS_ERROR_FAILURE; + + int32_t oldCount = GetRowCount(); + if (node->isOpen) + CollapseNode(index); + else + ExpandNode(index); + + // Update the twisty. + mTree->InvalidateRow(index); + + mTree->RowCountChanged(index+1, GetRowCount() - oldCount); + + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetTree(nsITreeBoxObject *tree) +{ + mTree = tree; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::GetSelection(nsITreeSelection * *aSelection) +{ + *aSelection = mSelection; + NS_IF_ADDREF(*aSelection); + return NS_OK; +} + +NS_IMETHODIMP inDOMView::SetSelection(nsITreeSelection * aSelection) +{ + mSelection = aSelection; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SelectionChanged() +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetCellValue(int32_t row, nsITreeColumn* col, const nsAString& value) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::SetCellText(int32_t row, nsITreeColumn* col, const nsAString& value) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::CycleHeader(nsITreeColumn* col) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::CycleCell(int32_t row, nsITreeColumn* col) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::IsEditable(int32_t row, nsITreeColumn* col, bool *_retval) +{ + return NS_OK; +} + + +NS_IMETHODIMP +inDOMView::IsSelectable(int32_t row, nsITreeColumn* col, bool *_retval) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::IsSeparator(int32_t index, bool *_retval) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::IsSorted(bool *_retval) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::CanDrop(int32_t index, int32_t orientation, + nsIDOMDataTransfer* aDataTransfer, bool *_retval) +{ + *_retval = false; + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::Drop(int32_t row, int32_t orientation, nsIDOMDataTransfer* aDataTransfer) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::PerformAction(const char16_t *action) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::PerformActionOnRow(const char16_t *action, int32_t row) +{ + return NS_OK; +} + +NS_IMETHODIMP +inDOMView::PerformActionOnCell(const char16_t* action, int32_t row, nsITreeColumn* col) +{ + return NS_OK; +} + +/////////////////////////////////////////////////////////////////////// +// nsIMutationObserver + +void +inDOMView::NodeWillBeDestroyed(const nsINode* aNode) +{ + NS_NOTREACHED("Document destroyed while we're holding a strong ref to it"); +} + +void +inDOMView::AttributeChanged(nsIDocument* aDocument, dom::Element* aElement, + int32_t aNameSpaceID, nsIAtom* aAttribute, + int32_t aModType, + const nsAttrValue* aOldValue) +{ + if (!mTree) { + return; + } + + if (!(mWhatToShow & nsIDOMNodeFilter::SHOW_ATTRIBUTE)) { + return; + } + + nsCOMPtr kungFuDeathGrip(this); + + // get the dom attribute node, if there is any + nsCOMPtr el(do_QueryInterface(aElement)); + nsCOMPtr domAttr; + nsDependentAtomString attrStr(aAttribute); + if (aNameSpaceID) { + nsNameSpaceManager* nsm = nsNameSpaceManager::GetInstance(); + if (!nsm) { + // we can't find out which attribute we want :( + return; + } + nsString attrNS; + nsresult rv = nsm->GetNameSpaceURI(aNameSpaceID, attrNS); + if (NS_FAILED(rv)) { + return; + } + (void)el->GetAttributeNodeNS(attrNS, attrStr, getter_AddRefs(domAttr)); + } else { + (void)el->GetAttributeNode(attrStr, getter_AddRefs(domAttr)); + } + + if (aModType == nsIDOMMutationEvent::MODIFICATION) { + // No fancy stuff here, just invalidate the changed row + if (!domAttr) { + return; + } + int32_t row = 0; + NodeToRow(domAttr, &row); + mTree->InvalidateRange(row, row); + } else if (aModType == nsIDOMMutationEvent::ADDITION) { + if (!domAttr) { + return; + } + // get the number of attributes on this content node + nsCOMPtr attrs; + el->GetAttributes(getter_AddRefs(attrs)); + uint32_t attrCount; + attrs->GetLength(&attrCount); + + inDOMViewNode* contentNode = nullptr; + int32_t contentRow; + int32_t attrRow; + if (mRootNode == el && + !(mWhatToShow & nsIDOMNodeFilter::SHOW_ELEMENT)) { + // if this view has a root node but is not displaying it, + // it is ok to act as if the changed attribute is on the root. + attrRow = attrCount - 1; + } else { + if (NS_FAILED(NodeToRow(el, &contentRow))) { + return; + } + RowToNode(contentRow, &contentNode); + if (!contentNode->isOpen) { + return; + } + attrRow = contentRow + attrCount; + } + + inDOMViewNode* newNode = CreateNode(domAttr, contentNode); + inDOMViewNode* insertNode = nullptr; + RowToNode(attrRow, &insertNode); + if (insertNode) { + if (contentNode && + insertNode->level <= contentNode->level) { + RowToNode(attrRow-1, &insertNode); + InsertLinkAfter(newNode, insertNode); + } else + InsertLinkBefore(newNode, insertNode); + } + InsertNode(newNode, attrRow); + mTree->RowCountChanged(attrRow, 1); + } else if (aModType == nsIDOMMutationEvent::REMOVAL) { + // At this point, the attribute is already gone from the DOM, but is still represented + // in our mRows array. Search through the content node's children for the corresponding + // node and remove it. + + // get the row of the content node + inDOMViewNode* contentNode = nullptr; + int32_t contentRow; + int32_t baseLevel; + if (NS_SUCCEEDED(NodeToRow(el, &contentRow))) { + RowToNode(contentRow, &contentNode); + baseLevel = contentNode->level; + } else { + if (mRootNode == el) { + contentRow = -1; + baseLevel = -1; + } else + return; + } + + // search for the attribute node that was removed + inDOMViewNode* checkNode = nullptr; + int32_t row = 0; + for (row = contentRow+1; row < GetRowCount(); ++row) { + checkNode = GetNodeAt(row); + if (checkNode->level == baseLevel+1) { + domAttr = do_QueryInterface(checkNode->node); + if (domAttr) { + nsAutoString attrName; + domAttr->GetNodeName(attrName); + if (attrName.Equals(attrStr)) { + // we have found the row for the attribute that was removed + RemoveLink(checkNode); + RemoveNode(row); + mTree->RowCountChanged(row, -1); + break; + } + } + } + if (checkNode->level <= baseLevel) + break; + } + + } +} + +void +inDOMView::ContentAppended(nsIDocument *aDocument, + nsIContent* aContainer, + nsIContent* aFirstNewContent, + int32_t /* unused */) +{ + if (!mTree) { + return; + } + + for (nsIContent* cur = aFirstNewContent; cur; cur = cur->GetNextSibling()) { + // Our ContentInserted impl doesn't use the index + ContentInserted(aDocument, aContainer, cur, 0); + } +} + +void +inDOMView::ContentInserted(nsIDocument *aDocument, nsIContent* aContainer, + nsIContent* aChild, int32_t /* unused */) +{ + if (!mTree) + return; + + nsresult rv; + nsCOMPtr childDOMNode(do_QueryInterface(aChild)); + nsCOMPtr parent; + if (!mDOMUtils) { + mDOMUtils = services::GetInDOMUtils(); + if (!mDOMUtils) { + return; + } + } + mDOMUtils->GetParentForNode(childDOMNode, mShowAnonymous, + getter_AddRefs(parent)); + + // find the inDOMViewNode for the parent of the inserted content + int32_t parentRow = 0; + if (NS_FAILED(rv = NodeToRow(parent, &parentRow))) + return; + inDOMViewNode* parentNode = nullptr; + if (NS_FAILED(rv = RowToNode(parentRow, &parentNode))) + return; + + nsCOMPtr kungFuDeathGrip(this); + + if (!parentNode->isOpen) { + // Parent is not open, so don't bother creating tree rows for the + // kids. But do indicate that it's now a container, if needed. + if (!parentNode->isContainer) { + parentNode->isContainer = true; + mTree->InvalidateRow(parentRow); + } + return; + } + + // get the previous sibling of the inserted content + nsCOMPtr previous; + GetRealPreviousSibling(childDOMNode, parent, getter_AddRefs(previous)); + inDOMViewNode* previousNode = nullptr; + + int32_t row = 0; + if (previous) { + // find the inDOMViewNode for the previous sibling of the inserted content + int32_t previousRow = 0; + if (NS_FAILED(rv = NodeToRow(previous, &previousRow))) + return; + if (NS_FAILED(rv = RowToNode(previousRow, &previousNode))) + return; + + // get the last descendant of the previous row, which is the row + // after which to insert this new row + GetLastDescendantOf(previousNode, previousRow, &row); + ++row; + } else { + // there is no previous sibling, so the new row will be inserted after the parent + row = parentRow+1; + } + + inDOMViewNode* newNode = CreateNode(childDOMNode, parentNode); + + if (previous) { + InsertLinkAfter(newNode, previousNode); + } else { + int32_t firstChildRow; + if (NS_SUCCEEDED(GetFirstDescendantOf(parentNode, parentRow, &firstChildRow))) { + inDOMViewNode* firstChild; + RowToNode(firstChildRow, &firstChild); + InsertLinkBefore(newNode, firstChild); + } + } + + // insert new node + InsertNode(newNode, row); + + mTree->RowCountChanged(row, 1); +} + +void +inDOMView::ContentRemoved(nsIDocument *aDocument, nsIContent* aContainer, + nsIContent* aChild, int32_t aIndexInContainer, + nsIContent* aPreviousSibling) +{ + if (!mTree) + return; + + nsresult rv; + + // find the inDOMViewNode for the old child + nsCOMPtr oldDOMNode(do_QueryInterface(aChild)); + int32_t row = 0; + if (NS_FAILED(rv = NodeToRow(oldDOMNode, &row))) + return; + inDOMViewNode* oldNode; + if (NS_FAILED(rv = RowToNode(row, &oldNode))) + return; + + nsCOMPtr kungFuDeathGrip(this); + + // The parent may no longer be a container. Note that we don't want + // to access oldNode after calling RemoveNode, so do this now. + inDOMViewNode* parentNode = oldNode->parent; + bool isOnlyChild = oldNode->previous == nullptr && oldNode->next == nullptr; + + // Keep track of how many rows we are removing. It's at least one, + // but if we're open it's more. + int32_t oldCount = GetRowCount(); + + if (oldNode->isOpen) + CollapseNode(row); + + RemoveLink(oldNode); + RemoveNode(row); + + if (isOnlyChild) { + // Fix up the parent + parentNode->isContainer = false; + parentNode->isOpen = false; + mTree->InvalidateRow(NodeToRow(parentNode)); + } + + mTree->RowCountChanged(row, GetRowCount() - oldCount); +} + +/////////////////////////////////////////////////////////////////////// +// inDOMView + +//////// NODE MANAGEMENT + +inDOMViewNode* +inDOMView::GetNodeAt(int32_t aRow) +{ + return mNodes.ElementAt(aRow); +} + +int32_t +inDOMView::GetRowCount() +{ + return mNodes.Length(); +} + +int32_t +inDOMView::NodeToRow(inDOMViewNode* aNode) +{ + return mNodes.IndexOf(aNode); +} + +inDOMViewNode* +inDOMView::CreateNode(nsIDOMNode* aNode, inDOMViewNode* aParent) +{ + inDOMViewNode* viewNode = new inDOMViewNode(aNode); + viewNode->level = aParent ? aParent->level+1 : 0; + viewNode->parent = aParent; + + nsCOMArray grandKids; + GetChildNodesFor(aNode, grandKids); + viewNode->isContainer = (grandKids.Count() > 0); + return viewNode; +} + +bool +inDOMView::RowOutOfBounds(int32_t aRow, int32_t aCount) +{ + return aRow < 0 || aRow >= GetRowCount() || aCount+aRow > GetRowCount(); +} + +void +inDOMView::AppendNode(inDOMViewNode* aNode) +{ + mNodes.AppendElement(aNode); +} + +void +inDOMView::InsertNode(inDOMViewNode* aNode, int32_t aRow) +{ + if (RowOutOfBounds(aRow, 1)) + AppendNode(aNode); + else + mNodes.InsertElementAt(aRow, aNode); +} + +void +inDOMView::RemoveNode(int32_t aRow) +{ + if (RowOutOfBounds(aRow, 1)) + return; + + delete GetNodeAt(aRow); + mNodes.RemoveElementAt(aRow); +} + +void +inDOMView::ReplaceNode(inDOMViewNode* aNode, int32_t aRow) +{ + if (RowOutOfBounds(aRow, 1)) + return; + + delete GetNodeAt(aRow); + mNodes.ElementAt(aRow) = aNode; +} + +void +inDOMView::InsertNodes(nsTArray& aNodes, int32_t aRow) +{ + if (aRow < 0 || aRow > GetRowCount()) + return; + + mNodes.InsertElementsAt(aRow, aNodes); +} + +void +inDOMView::RemoveNodes(int32_t aRow, int32_t aCount) +{ + if (aRow < 0) + return; + + int32_t rowCount = GetRowCount(); + for (int32_t i = aRow; i < aRow+aCount && i < rowCount; ++i) { + delete GetNodeAt(i); + } + + mNodes.RemoveElementsAt(aRow, aCount); +} + +void +inDOMView::RemoveAllNodes() +{ + int32_t rowCount = GetRowCount(); + for (int32_t i = 0; i < rowCount; ++i) { + delete GetNodeAt(i); + } + + mNodes.Clear(); +} + +void +inDOMView::ExpandNode(int32_t aRow) +{ + inDOMViewNode* node = nullptr; + RowToNode(aRow, &node); + + nsCOMArray kids; + GetChildNodesFor(node ? node->node : mRootNode, + kids); + int32_t kidCount = kids.Count(); + + nsTArray list(kidCount); + + inDOMViewNode* newNode = nullptr; + inDOMViewNode* prevNode = nullptr; + + for (int32_t i = 0; i < kidCount; ++i) { + newNode = CreateNode(kids[i], node); + list.AppendElement(newNode); + + if (prevNode) + prevNode->next = newNode; + newNode->previous = prevNode; + prevNode = newNode; + } + + InsertNodes(list, aRow+1); + + if (node) + node->isOpen = true; +} + +void +inDOMView::CollapseNode(int32_t aRow) +{ + inDOMViewNode* node = nullptr; + nsresult rv = RowToNode(aRow, &node); + if (NS_FAILED(rv)) { + return; + } + + int32_t row = 0; + GetLastDescendantOf(node, aRow, &row); + + RemoveNodes(aRow+1, row-aRow); + + node->isOpen = false; +} + +//////// NODE AND ROW CONVERSION + +nsresult +inDOMView::RowToNode(int32_t aRow, inDOMViewNode** aNode) +{ + if (aRow < 0 || aRow >= GetRowCount()) + return NS_ERROR_FAILURE; + + *aNode = GetNodeAt(aRow); + return NS_OK; +} + +nsresult +inDOMView::NodeToRow(nsIDOMNode* aNode, int32_t* aRow) +{ + int32_t rowCount = GetRowCount(); + for (int32_t i = 0; i < rowCount; ++i) { + if (GetNodeAt(i)->node == aNode) { + *aRow = i; + return NS_OK; + } + } + + *aRow = -1; + return NS_ERROR_FAILURE; +} + +//////// NODE HIERARCHY MUTATION + +void +inDOMView::InsertLinkAfter(inDOMViewNode* aNode, inDOMViewNode* aInsertAfter) +{ + if (aInsertAfter->next) + aInsertAfter->next->previous = aNode; + aNode->next = aInsertAfter->next; + aInsertAfter->next = aNode; + aNode->previous = aInsertAfter; +} + +void +inDOMView::InsertLinkBefore(inDOMViewNode* aNode, inDOMViewNode* aInsertBefore) +{ + if (aInsertBefore->previous) + aInsertBefore->previous->next = aNode; + aNode->previous = aInsertBefore->previous; + aInsertBefore->previous = aNode; + aNode->next = aInsertBefore; +} + +void +inDOMView::RemoveLink(inDOMViewNode* aNode) +{ + if (aNode->previous) + aNode->previous->next = aNode->next; + if (aNode->next) + aNode->next->previous = aNode->previous; +} + +void +inDOMView::ReplaceLink(inDOMViewNode* aNewNode, inDOMViewNode* aOldNode) +{ + if (aOldNode->previous) + aOldNode->previous->next = aNewNode; + if (aOldNode->next) + aOldNode->next->previous = aNewNode; + aNewNode->next = aOldNode->next; + aNewNode->previous = aOldNode->previous; +} + +//////// NODE HIERARCHY UTILITIES + +nsresult +inDOMView::GetFirstDescendantOf(inDOMViewNode* aNode, int32_t aRow, int32_t* aResult) +{ + // get the first node that is a descendant of the previous sibling + int32_t row = 0; + inDOMViewNode* node; + for (row = aRow+1; row < GetRowCount(); ++row) { + node = GetNodeAt(row); + if (node->parent == aNode) { + *aResult = row; + return NS_OK; + } + if (node->level <= aNode->level) + break; + } + return NS_ERROR_FAILURE; +} + +nsresult +inDOMView::GetLastDescendantOf(inDOMViewNode* aNode, int32_t aRow, int32_t* aResult) +{ + // get the last node that is a descendant of the previous sibling + int32_t row = 0; + for (row = aRow+1; row < GetRowCount(); ++row) { + if (GetNodeAt(row)->level <= aNode->level) + break; + } + *aResult = row-1; + return NS_OK; +} + +//////// DOM UTILITIES + +nsresult +inDOMView::GetChildNodesFor(nsIDOMNode* aNode, nsCOMArray& aResult) +{ + NS_ENSURE_ARG(aNode); + // attribute nodes + if (mWhatToShow & nsIDOMNodeFilter::SHOW_ATTRIBUTE) { + nsCOMPtr element = do_QueryInterface(aNode); + if (element) { + nsCOMPtr attrs; + element->GetAttributes(getter_AddRefs(attrs)); + if (attrs) { + AppendAttrsToArray(attrs, aResult); + } + } + } + + if (mWhatToShow & nsIDOMNodeFilter::SHOW_ELEMENT) { + nsCOMPtr kids; + if (!mDOMUtils) { + mDOMUtils = services::GetInDOMUtils(); + if (!mDOMUtils) { + return NS_ERROR_FAILURE; + } + } + + mDOMUtils->GetChildrenForNode(aNode, mShowAnonymous, + getter_AddRefs(kids)); + + if (kids) { + AppendKidsToArray(kids, aResult); + } + } + + if (mShowSubDocuments) { + nsCOMPtr domdoc = + do_QueryInterface(inLayoutUtils::GetSubDocumentFor(aNode)); + if (domdoc) { + aResult.AppendObject(domdoc); + } + } + + return NS_OK; +} + +nsresult +inDOMView::GetRealPreviousSibling(nsIDOMNode* aNode, nsIDOMNode* aRealParent, nsIDOMNode** aSibling) +{ + // XXXjrh: This won't work for some cases during some situations where XBL insertion points + // are involved. Fix me! + aNode->GetPreviousSibling(aSibling); + return NS_OK; +} + +nsresult +inDOMView::AppendKidsToArray(nsIDOMNodeList* aKids, + nsCOMArray& aArray) +{ + uint32_t l = 0; + aKids->GetLength(&l); + nsCOMPtr kid; + uint16_t nodeType = 0; + + // Try and get DOM Utils in case we don't have one yet. + if (!mShowWhitespaceNodes && !mDOMUtils) { + mDOMUtils = services::GetInDOMUtils(); + } + + for (uint32_t i = 0; i < l; ++i) { + aKids->Item(i, getter_AddRefs(kid)); + kid->GetNodeType(&nodeType); + + NS_ASSERTION(nodeType && nodeType <= nsIDOMNode::NOTATION_NODE, + "Unknown node type. " + "Were new types added to the spec?"); + // As of DOM Level 2 Core and Traversal, each NodeFilter constant + // is defined as the lower nth bit in the NodeFilter bitmask, + // where n is the numeric constant of the nodeType it represents. + // If this invariant ever changes, we will need to update the + // following line. + uint32_t filterForNodeType = 1 << (nodeType - 1); + + if (mWhatToShow & filterForNodeType) { + if ((nodeType == nsIDOMNode::TEXT_NODE || + nodeType == nsIDOMNode::COMMENT_NODE) && + !mShowWhitespaceNodes && mDOMUtils) { + nsCOMPtr data = do_QueryInterface(kid); + NS_ASSERTION(data, "Does not implement nsIDOMCharacterData!"); + bool ignore; + mDOMUtils->IsIgnorableWhitespace(data, &ignore); + if (ignore) { + continue; + } + } + + aArray.AppendElement(kid.forget()); + } + } + + return NS_OK; +} + +nsresult +inDOMView::AppendAttrsToArray(nsIDOMMozNamedAttrMap* aAttributes, + nsCOMArray& aArray) +{ + uint32_t l = 0; + aAttributes->GetLength(&l); + nsCOMPtr attribute; + for (uint32_t i = 0; i < l; ++i) { + aAttributes->Item(i, getter_AddRefs(attribute)); + aArray.AppendElement(attribute.forget()); + } + return NS_OK; +} diff --git a/layout/inspector/inDOMView.h b/layout/inspector/inDOMView.h new file mode 100644 index 000000000..463d36316 --- /dev/null +++ b/layout/inspector/inDOMView.h @@ -0,0 +1,96 @@ +/* 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/. */ + +#ifndef __inDOMView_h__ +#define __inDOMView_h__ + +#include "inIDOMView.h" +#include "inIDOMUtils.h" + +#include "nsITreeView.h" +#include "nsITreeSelection.h" +#include "nsStubMutationObserver.h" +#include "nsIDOMNode.h" +#include "nsIDOMDocument.h" +#include "nsTArray.h" +#include "nsCOMArray.h" +#include "nsCOMPtr.h" + +class inDOMViewNode; +class nsIDOMMozNamedAttrMap; + +class inDOMView : public inIDOMView, + public nsITreeView, + public nsStubMutationObserver +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_INIDOMVIEW + NS_DECL_NSITREEVIEW + + inDOMView(); + + // nsIMutationObserver + NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED + NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED + NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED + NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED + NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED + +protected: + virtual ~inDOMView(); + + nsCOMPtr mTree; + nsCOMPtr mSelection; + nsCOMPtr mDOMUtils; + + bool mShowAnonymous; + bool mShowSubDocuments; + bool mShowWhitespaceNodes; + bool mShowAccessibleNodes; + uint32_t mWhatToShow; + + nsCOMPtr mRootNode; + nsCOMPtr mRootDocument; + + nsTArray mNodes; + + inDOMViewNode* GetNodeAt(int32_t aIndex); + int32_t GetRowCount(); + int32_t NodeToRow(inDOMViewNode* aNode); + bool RowOutOfBounds(int32_t aRow, int32_t aCount); + inDOMViewNode* CreateNode(nsIDOMNode* aNode, inDOMViewNode* aParent); + void AppendNode(inDOMViewNode* aNode); + void InsertNode(inDOMViewNode* aNode, int32_t aIndex); + void RemoveNode(int32_t aIndex); + void ReplaceNode(inDOMViewNode* aNode, int32_t aIndex); + void InsertNodes(nsTArray& aNodes, int32_t aIndex); + void RemoveNodes(int32_t aIndex, int32_t aCount); + void RemoveAllNodes(); + void ExpandNode(int32_t aRow); + void CollapseNode(int32_t aRow); + + nsresult RowToNode(int32_t aRow, inDOMViewNode** aNode); + nsresult NodeToRow(nsIDOMNode* aNode, int32_t* aRow); + + void InsertLinkAfter(inDOMViewNode* aNode, inDOMViewNode* aInsertAfter); + void InsertLinkBefore(inDOMViewNode* aNode, inDOMViewNode* aInsertBefore); + void RemoveLink(inDOMViewNode* aNode); + void ReplaceLink(inDOMViewNode* aNewNode, inDOMViewNode* aOldNode); + + nsresult GetChildNodesFor(nsIDOMNode* aNode, nsCOMArray& aResult); + nsresult AppendKidsToArray(nsIDOMNodeList* aKids, nsCOMArray& aArray); + nsresult AppendAttrsToArray(nsIDOMMozNamedAttrMap* aKids, nsCOMArray& aArray); + nsresult GetFirstDescendantOf(inDOMViewNode* aNode, int32_t aRow, int32_t* aResult); + nsresult GetLastDescendantOf(inDOMViewNode* aNode, int32_t aRow, int32_t* aResult); + nsresult GetRealPreviousSibling(nsIDOMNode* aNode, nsIDOMNode* aRealParent, nsIDOMNode** aSibling); +}; + +// {FB5C1775-1BBD-4b9c-ABB0-AE7ACD29E87E} +#define IN_DOMVIEW_CID \ +{ 0xfb5c1775, 0x1bbd, 0x4b9c, { 0xab, 0xb0, 0xae, 0x7a, 0xcd, 0x29, 0xe8, 0x7e } } + +#endif // __inDOMView_h__ + + diff --git a/layout/inspector/inDeepTreeWalker.cpp b/layout/inspector/inDeepTreeWalker.cpp new file mode 100644 index 000000000..a0c504980 --- /dev/null +++ b/layout/inspector/inDeepTreeWalker.cpp @@ -0,0 +1,442 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 sw=2 et tw=79: */ +/* 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 "inDeepTreeWalker.h" +#include "inLayoutUtils.h" + +#include "nsString.h" +#include "nsIDOMDocument.h" +#include "nsIDOMNodeFilter.h" +#include "nsIDOMNodeList.h" +#include "nsServiceManagerUtils.h" +#include "inIDOMUtils.h" +#include "nsIContent.h" +#include "nsContentList.h" +#include "ChildIterator.h" +#include "mozilla/dom/Element.h" + +/***************************************************************************** + * This implementation does not currently operaate according to the W3C spec. + * In particular it does NOT handle DOM mutations during the walk. It also + * ignores whatToShow and the filter. + *****************************************************************************/ + +//////////////////////////////////////////////////// + +inDeepTreeWalker::inDeepTreeWalker() + : mShowAnonymousContent(false), + mShowSubDocuments(false), + mShowDocumentsAsNodes(false), + mWhatToShow(nsIDOMNodeFilter::SHOW_ALL) +{ +} + +inDeepTreeWalker::~inDeepTreeWalker() +{ +} + +NS_IMPL_ISUPPORTS(inDeepTreeWalker, + inIDeepTreeWalker) + +//////////////////////////////////////////////////// +// inIDeepTreeWalker + +NS_IMETHODIMP +inDeepTreeWalker::GetShowAnonymousContent(bool *aShowAnonymousContent) +{ + *aShowAnonymousContent = mShowAnonymousContent; + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::SetShowAnonymousContent(bool aShowAnonymousContent) +{ + mShowAnonymousContent = aShowAnonymousContent; + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::GetShowSubDocuments(bool *aShowSubDocuments) +{ + *aShowSubDocuments = mShowSubDocuments; + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::SetShowSubDocuments(bool aShowSubDocuments) +{ + mShowSubDocuments = aShowSubDocuments; + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::GetShowDocumentsAsNodes(bool *aShowDocumentsAsNodes) +{ + *aShowDocumentsAsNodes = mShowDocumentsAsNodes; + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::SetShowDocumentsAsNodes(bool aShowDocumentsAsNodes) +{ + mShowDocumentsAsNodes = aShowDocumentsAsNodes; + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::Init(nsIDOMNode* aRoot, uint32_t aWhatToShow) +{ + if (!aRoot) { + return NS_ERROR_INVALID_ARG; + } + + mRoot = aRoot; + mCurrentNode = aRoot; + mWhatToShow = aWhatToShow; + + mDOMUtils = do_GetService("@mozilla.org/inspector/dom-utils;1"); + return mDOMUtils ? NS_OK : NS_ERROR_UNEXPECTED; +} + +//////////////////////////////////////////////////// +// nsIDOMTreeWalker + +NS_IMETHODIMP +inDeepTreeWalker::GetRoot(nsIDOMNode** aRoot) +{ + *aRoot = mRoot; + NS_IF_ADDREF(*aRoot); + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::GetWhatToShow(uint32_t* aWhatToShow) +{ + *aWhatToShow = mWhatToShow; + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::GetFilter(nsIDOMNodeFilter** aFilter) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +inDeepTreeWalker::GetCurrentNode(nsIDOMNode** aCurrentNode) +{ + *aCurrentNode = mCurrentNode; + NS_IF_ADDREF(*aCurrentNode); + return NS_OK; +} + +already_AddRefed +inDeepTreeWalker::GetParent() +{ + if (mCurrentNode == mRoot) { + return nullptr; + } + + nsCOMPtr parent; + MOZ_ASSERT(mDOMUtils, "mDOMUtils should have been initiated already in Init"); + mDOMUtils->GetParentForNode(mCurrentNode, mShowAnonymousContent, + getter_AddRefs(parent)); + + uint16_t nodeType = 0; + if (parent) { + parent->GetNodeType(&nodeType); + } + // For compatibility reasons by default we skip the document nodes + // from the walk. + if (!mShowDocumentsAsNodes && + nodeType == nsIDOMNode::DOCUMENT_NODE && + parent != mRoot) { + mDOMUtils->GetParentForNode(parent, mShowAnonymousContent, + getter_AddRefs(parent)); + } + + return parent.forget(); +} + +static already_AddRefed +GetChildren(nsIDOMNode* aParent, + bool aShowAnonymousContent, + bool aShowSubDocuments) +{ + MOZ_ASSERT(aParent); + + nsCOMPtr ret; + if (aShowSubDocuments) { + nsCOMPtr domdoc = inLayoutUtils::GetSubDocumentFor(aParent); + if (domdoc) { + aParent = domdoc; + } + } + + nsCOMPtr parentAsContent = do_QueryInterface(aParent); + if (parentAsContent && aShowAnonymousContent) { + ret = parentAsContent->GetChildren(nsIContent::eAllChildren); + } else { + // If it's not a content, then it's a document (or an attribute but we can ignore that + // case here). If aShowAnonymousContent is false we also want to fall back to ChildNodes + // so we can skip any native anon content that GetChildren would return. + nsCOMPtr parentNode = do_QueryInterface(aParent); + MOZ_ASSERT(parentNode); + ret = parentNode->ChildNodes(); + } + return ret.forget(); +} + +NS_IMETHODIMP +inDeepTreeWalker::SetCurrentNode(nsIDOMNode* aCurrentNode) +{ + // mCurrentNode can only be null if init either failed, or has not been + // called yet. + if (!mCurrentNode || !aCurrentNode) { + return NS_ERROR_FAILURE; + } + + // If Document nodes are skipped by the walk, we should not allow + // one to set one as the current node either. + uint16_t nodeType = 0; + aCurrentNode->GetNodeType(&nodeType); + if (!mShowDocumentsAsNodes && nodeType == nsIDOMNode::DOCUMENT_NODE) { + return NS_ERROR_FAILURE; + } + + return SetCurrentNode(aCurrentNode, nullptr); +} + + +nsresult +inDeepTreeWalker::SetCurrentNode(nsIDOMNode* aCurrentNode, + nsINodeList* aSiblings) +{ + MOZ_ASSERT(aCurrentNode); + + // We want to store the original state so in case of error + // we can restore that. + nsCOMPtr tmpSiblings = mSiblings; + nsCOMPtr tmpCurrent = mCurrentNode; + mSiblings = aSiblings; + mCurrentNode = aCurrentNode; + + // If siblings were not passed in as argument we have to + // get them from the parent node of aCurrentNode. + // Note: in the mShowDoucmentsAsNodes case when a sub document + // is set as the current, we don't want to get the children + // from the iframe accidentally here, so let's just skip this + // part for document nodes, they should never have siblings. + uint16_t nodeType = 0; + aCurrentNode->GetNodeType(&nodeType); + if (!mSiblings && nodeType != nsIDOMNode::DOCUMENT_NODE) { + nsCOMPtr parent = GetParent(); + if (parent) { + mSiblings = GetChildren(parent, + mShowAnonymousContent, + mShowSubDocuments); + } + } + + if (mSiblings && mSiblings->Length()) { + // We cached all the siblings (if there are any) of the current node, but we + // still have to set the index too, to be able to iterate over them. + nsCOMPtr currentAsContent = do_QueryInterface(mCurrentNode); + MOZ_ASSERT(currentAsContent); + int32_t index = mSiblings->IndexOf(currentAsContent); + if (index < 0) { + // If someone tries to set current node to some value that is not reachable + // otherwise, let's throw. (For example mShowAnonymousContent is false and some + // XBL anon content was passed in) + + // Restore state first. + mCurrentNode = tmpCurrent; + mSiblings = tmpSiblings; + return NS_ERROR_INVALID_ARG; + } + mCurrentIndex = index; + } else { + mCurrentIndex = -1; + } + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::ParentNode(nsIDOMNode** _retval) +{ + *_retval = nullptr; + if (!mCurrentNode || mCurrentNode == mRoot) { + return NS_OK; + } + + nsCOMPtr parent = GetParent(); + + if (!parent) { + return NS_OK; + } + + nsresult rv = SetCurrentNode(parent); + NS_ENSURE_SUCCESS(rv,rv); + + parent.forget(_retval); + return NS_OK; +} + +// FirstChild and LastChild are very similar methods, this is the generic +// version for internal use. With aReverse = true it returns the LastChild. +nsresult +inDeepTreeWalker::EdgeChild(nsIDOMNode** _retval, bool aFront) +{ + if (!mCurrentNode) { + return NS_ERROR_FAILURE; + } + + *_retval = nullptr; + + nsCOMPtr echild; + if (mShowSubDocuments && mShowDocumentsAsNodes) { + // GetChildren below, will skip the document node from + // the walk. But if mShowDocumentsAsNodes is set to true + // we want to include the (sub)document itself too. + echild = inLayoutUtils::GetSubDocumentFor(mCurrentNode); + } + + nsCOMPtr children; + if (!echild) { + children = GetChildren(mCurrentNode, + mShowAnonymousContent, + mShowSubDocuments); + if (children && children->Length() > 0) { + nsINode* childNode = children->Item(aFront ? 0 : children->Length() - 1); + echild = childNode ? childNode->AsDOMNode() : nullptr; + } + } + + if (echild) { + nsresult rv = SetCurrentNode(echild, children); + NS_ENSURE_SUCCESS(rv, rv); + NS_ADDREF(*_retval = mCurrentNode); + } + + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::FirstChild(nsIDOMNode** _retval) +{ + return EdgeChild(_retval, /* aFront = */ true); +} + +NS_IMETHODIMP +inDeepTreeWalker::LastChild(nsIDOMNode **_retval) +{ + return EdgeChild(_retval, /* aFront = */ false); +} + +NS_IMETHODIMP +inDeepTreeWalker::PreviousSibling(nsIDOMNode **_retval) +{ + *_retval = nullptr; + if (!mCurrentNode || !mSiblings || mCurrentIndex < 1) { + return NS_OK; + } + + nsIContent* prev = mSiblings->Item(--mCurrentIndex); + mCurrentNode = prev->AsDOMNode(); + NS_ADDREF(*_retval = mCurrentNode); + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::NextSibling(nsIDOMNode **_retval) +{ + *_retval = nullptr; + if (!mCurrentNode || !mSiblings || + mCurrentIndex + 1 >= (int32_t) mSiblings->Length()) { + return NS_OK; + } + + nsIContent* next = mSiblings->Item(++mCurrentIndex); + mCurrentNode = next->AsDOMNode(); + NS_ADDREF(*_retval = mCurrentNode); + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::PreviousNode(nsIDOMNode **_retval) +{ + if (!mCurrentNode || mCurrentNode == mRoot) { + // Nowhere to go from here + *_retval = nullptr; + return NS_OK; + } + + nsCOMPtr node; + PreviousSibling(getter_AddRefs(node)); + + if (!node) { + return ParentNode(_retval); + } + + // Now we're positioned at our previous sibling. But since the DOM tree + // traversal is depth-first, the previous node is its most deeply nested last + // child. Just loop until LastChild() returns null; since the LastChild() + // call that returns null won't affect our position, we will then be + // positioned at the correct node. + while (node) { + LastChild(getter_AddRefs(node)); + } + + NS_ADDREF(*_retval = mCurrentNode); + return NS_OK; +} + +NS_IMETHODIMP +inDeepTreeWalker::NextNode(nsIDOMNode **_retval) +{ + if (!mCurrentNode) { + return NS_OK; + } + + // First try our kids + FirstChild(_retval); + + if (*_retval) { + return NS_OK; + } + + // Now keep trying next siblings up the parent chain, but if we + // discover there's nothing else restore our state. +#ifdef DEBUG + nsIDOMNode* origCurrentNode = mCurrentNode; +#endif + uint32_t lastChildCallsToMake = 0; + while (1) { + NextSibling(_retval); + + if (*_retval) { + return NS_OK; + } + + nsCOMPtr parent; + ParentNode(getter_AddRefs(parent)); + if (!parent) { + // Nowhere else to go; we're done. Restore our state. + while (lastChildCallsToMake--) { + nsCOMPtr dummy; + LastChild(getter_AddRefs(dummy)); + } + NS_ASSERTION(mCurrentNode == origCurrentNode, + "Didn't go back to the right node?"); + *_retval = nullptr; + return NS_OK; + } + ++lastChildCallsToMake; + } + + NS_NOTREACHED("how did we get here?"); + return NS_OK; +} diff --git a/layout/inspector/inDeepTreeWalker.h b/layout/inspector/inDeepTreeWalker.h new file mode 100644 index 000000000..652722808 --- /dev/null +++ b/layout/inspector/inDeepTreeWalker.h @@ -0,0 +1,65 @@ +/* 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/. */ + +#ifndef __inDeepTreeWalker_h___ +#define __inDeepTreeWalker_h___ + +#include "inIDeepTreeWalker.h" + +#include "nsCOMPtr.h" +#include "nsIDOMNode.h" +#include "nsTArray.h" + +class nsINodeList; +class inIDOMUtils; + +class inDeepTreeWalker final : public inIDeepTreeWalker +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_INIDEEPTREEWALKER + + inDeepTreeWalker(); + + nsresult SetCurrentNode(nsIDOMNode* aCurrentNode, + nsINodeList* aSiblings); +protected: + virtual ~inDeepTreeWalker(); + + already_AddRefed GetParent(); + nsresult EdgeChild(nsIDOMNode** _retval, bool aReverse); + + bool mShowAnonymousContent; + bool mShowSubDocuments; + bool mShowDocumentsAsNodes; + + // The root node. previousNode and parentNode will return + // null from here. + nsCOMPtr mRoot; + nsCOMPtr mCurrentNode; + nsCOMPtr mDOMUtils; + + // We cache the siblings of mCurrentNode as a list of nodes. + // Notes: normally siblings are all the children of the parent + // of mCurrentNode (that are interesting for use for the walk) + // and mCurrentIndex is the index of mCurrentNode in that list + // But if mCurrentNode is a (sub) document then instead of + // storing a list that has only one element (the document) + // and setting mCurrentIndex to null, we set mSibilings to null. + // The reason for this is purely technical, since nsINodeList is + // nsIContent based hence we cannot use it to store a document node. + nsCOMPtr mSiblings; + + // Index of mCurrentNode in the mSiblings list. + int32_t mCurrentIndex; + + // Currently unused. Should be a filter for nodes. + uint32_t mWhatToShow; +}; + +// {BFCB82C2-5611-4318-90D6-BAF4A7864252} +#define IN_DEEPTREEWALKER_CID \ +{ 0xbfcb82c2, 0x5611, 0x4318, { 0x90, 0xd6, 0xba, 0xf4, 0xa7, 0x86, 0x42, 0x52 } } + +#endif // __inDeepTreeWalker_h___ diff --git a/layout/inspector/inICSSValueSearch.idl b/layout/inspector/inICSSValueSearch.idl new file mode 100644 index 000000000..a67d72b08 --- /dev/null +++ b/layout/inspector/inICSSValueSearch.idl @@ -0,0 +1,29 @@ +/* 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 "inISearchProcess.idl" + +interface nsIDOMDocument; + +[scriptable, uuid(e0d39e48-1dd1-11b2-81bd-9a0c117f0736)] +interface inICSSValueSearch : inISearchProcess { + + attribute nsIDOMDocument document; + + // the base url for all returned URL results, if returnRelativeURLs is true + attribute wstring baseURL; + + // strip off the baseURL for all URL results if true + attribute boolean returnRelativeURLs; + + // correct the paths on a chrome url, such as turning global/skin/blah into global/blah + attribute boolean normalizeChromeURLs; + + // add a css property to search for + void addPropertyCriteria(in wstring aPropName); + + // set the text value to search for in the properties specified (optional) + attribute wstring textCriteria; + +}; diff --git a/layout/inspector/inIDOMUtils.idl b/layout/inspector/inIDOMUtils.idl new file mode 100644 index 000000000..30c15003f --- /dev/null +++ b/layout/inspector/inIDOMUtils.idl @@ -0,0 +1,213 @@ +/* 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 "nsISupports.idl" + +interface nsIArray; +interface nsIArrayExtensions; +interface nsIDOMCharacterData; +interface nsIDOMElement; +interface nsIDOMDocument; +interface nsIDOMCSSRule; +interface nsIDOMCSSStyleRule; +interface nsIDOMNode; +interface nsIDOMNodeList; +interface nsIDOMFontFaceList; +interface nsIDOMRange; +interface nsIDOMCSSStyleSheet; + +[scriptable, uuid(362e98c3-82c2-4ad8-8dcb-00e8e4eab497)] +interface inIDOMUtils : nsISupports +{ + // CSS utilities + void getAllStyleSheets (in nsIDOMDocument aDoc, + [optional] out unsigned long aLength, + [array, size_is (aLength), retval] out nsISupports aSheets); + nsIArrayExtensions getCSSStyleRules(in nsIDOMElement aElement, [optional] in DOMString aPseudo); + + /** + * Get the line number of a rule. + * + * @param nsIDOMCSSRule aRule The rule. + * @return The rule's line number. Line numbers are 1-based. + */ + unsigned long getRuleLine(in nsIDOMCSSRule aRule); + + /** + * Get the column number of a rule. + * + * @param nsIDOMCSSRule aRule The rule. + * @return The rule's column number. Column numbers are 1-based. + */ + unsigned long getRuleColumn(in nsIDOMCSSRule aRule); + + /** + * Like getRuleLine, but if the rule is in a + + + Mozilla Bug 695639 +
Hello 你好 world
+ + + diff --git a/layout/inspector/tests/chrome/test_bug708874.css b/layout/inspector/tests/chrome/test_bug708874.css new file mode 100644 index 000000000..539c44538 --- /dev/null +++ b/layout/inspector/tests/chrome/test_bug708874.css @@ -0,0 +1,33 @@ +#test-div { + color: rgb(0, 0, 0); + font-family: serif; + font-weight: 400; +} + +#test-div:hover { + color: rgb(10, 0, 0); +} + +#test-div:active { + font-family: Arial; +} + +#test-div:focus { + font-weight: 800; +} + +#test-button { + color: rgb(0, 0, 0); +} + +#test-button:disabled { + color: rgb(40, 0, 0); +} + +#test-link:visited { + color: rgb(20, 0, 0); +} + +#test-link:link { + color: rgb(30, 0, 0); +} diff --git a/layout/inspector/tests/chrome/test_bug708874.xul b/layout/inspector/tests/chrome/test_bug708874.xul new file mode 100644 index 000000000..0896d1eca --- /dev/null +++ b/layout/inspector/tests/chrome/test_bug708874.xul @@ -0,0 +1,296 @@ + + + + + + + + + + Mozilla Bug 708874 + + + Mozilla Bug 708874 - API for locking pseudo-class state of an element + + + + test link + + +
+ test div +
+ + + + +
diff --git a/layout/inspector/tests/chrome/test_bug727834.css b/layout/inspector/tests/chrome/test_bug727834.css new file mode 100644 index 000000000..f21f7a54c --- /dev/null +++ b/layout/inspector/tests/chrome/test_bug727834.css @@ -0,0 +1,7 @@ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +body { + padding-top: 100px; +} diff --git a/layout/inspector/tests/chrome/test_bug727834.xul b/layout/inspector/tests/chrome/test_bug727834.xul new file mode 100644 index 000000000..dde8e1b03 --- /dev/null +++ b/layout/inspector/tests/chrome/test_bug727834.xul @@ -0,0 +1,88 @@ + + + + + + + + + + Mozilla Bug 727834 - Add an API to (re)parse a style sheet in place + + + diff --git a/layout/inspector/tests/file_bug522601.html b/layout/inspector/tests/file_bug522601.html new file mode 100644 index 000000000..c3e27a4b5 --- /dev/null +++ b/layout/inspector/tests/file_bug522601.html @@ -0,0 +1,17 @@ + + + + + Test for Bug 522601 + + + + +
+

Blah

+
+ + diff --git a/layout/inspector/tests/mochitest.ini b/layout/inspector/tests/mochitest.ini new file mode 100644 index 000000000..25209f57a --- /dev/null +++ b/layout/inspector/tests/mochitest.ini @@ -0,0 +1,29 @@ +[DEFAULT] +support-files = + bug1202095.css + bug1202095-2.css + bug856317.css + file_bug522601.html + +[test_bug462787.html] +[test_bug462789.html] +[test_bug522601.xhtml] +[test_bug536379.html] +[test_bug536379-2.html] +[test_bug557726.html] +[test_bug609549.xhtml] +[test_bug806192.html] +[test_bug856317.html] +[test_bug877690.html] +[test_bug1006595.html] +[test_color_to_rgba.html] +[test_css_property_is_shorthand.html] +[test_css_property_is_valid.html] +[test_getCSSPseudoElementNames.html] +[test_getRelativeRuleLine.html] +[test_get_all_style_sheets.html] +[test_is_valid_css_color.html] +[test_isinheritableproperty.html] +[test_parseStyleSheet.html] +[test_parseStyleSheetImport.html] +[test_selectormatcheselement.html] diff --git a/layout/inspector/tests/test_bug1006595.html b/layout/inspector/tests/test_bug1006595.html new file mode 100644 index 000000000..009269d37 --- /dev/null +++ b/layout/inspector/tests/test_bug1006595.html @@ -0,0 +1,118 @@ + + + + + + Test for Bug 1006595 + + + + + +Mozilla Bug 1006595 +

+ +
+
+ + diff --git a/layout/inspector/tests/test_bug462787.html b/layout/inspector/tests/test_bug462787.html new file mode 100644 index 000000000..88c5c466f --- /dev/null +++ b/layout/inspector/tests/test_bug462787.html @@ -0,0 +1,100 @@ + + + + + Test for Bug 462787 + + + + +Mozilla Bug 462787 +

+ +
+
+
+ + diff --git a/layout/inspector/tests/test_bug462789.html b/layout/inspector/tests/test_bug462789.html new file mode 100644 index 000000000..a027b85a6 --- /dev/null +++ b/layout/inspector/tests/test_bug462789.html @@ -0,0 +1,94 @@ + + + + + Test for Bug 462789 + + + + +Mozilla Bug 462789 +

+ +
+
+
+ + diff --git a/layout/inspector/tests/test_bug522601.xhtml b/layout/inspector/tests/test_bug522601.xhtml new file mode 100644 index 000000000..7c5a9e79c --- /dev/null +++ b/layout/inspector/tests/test_bug522601.xhtml @@ -0,0 +1,274 @@ + + + + Test for Bug 522601 + + + + + +
+
+
+ + +Mozilla Bug 522601 +

+ This is some text + More text + Even more ItalictextAnd more italic

+ +
+ +
+
+

light child

+
+
+
+
+ + diff --git a/layout/inspector/tests/test_bug536379-2.html b/layout/inspector/tests/test_bug536379-2.html new file mode 100644 index 000000000..6fe3d8f8d --- /dev/null +++ b/layout/inspector/tests/test_bug536379-2.html @@ -0,0 +1,35 @@ + + + + + Test for Bug 536379 + + + + + + +Mozilla Bug 536379 +

+
+
+
+ + diff --git a/layout/inspector/tests/test_bug536379.html b/layout/inspector/tests/test_bug536379.html new file mode 100644 index 000000000..8ac012953 --- /dev/null +++ b/layout/inspector/tests/test_bug536379.html @@ -0,0 +1,46 @@ + + + + + Test for Bug 536379 + + + + + + +Mozilla Bug 536379 +

+
+
+
+ + diff --git a/layout/inspector/tests/test_bug557726.html b/layout/inspector/tests/test_bug557726.html new file mode 100644 index 000000000..eda46bdfd --- /dev/null +++ b/layout/inspector/tests/test_bug557726.html @@ -0,0 +1,95 @@ + + + + + Test for Bug 557726 + + + + + + +Mozilla Bug 557726 + +
+ text with ::before, ::after, and ::first-letter pseudo-elements +
+ + + + + diff --git a/layout/inspector/tests/test_bug609549.xhtml b/layout/inspector/tests/test_bug609549.xhtml new file mode 100644 index 000000000..d7d115847 --- /dev/null +++ b/layout/inspector/tests/test_bug609549.xhtml @@ -0,0 +1,67 @@ + + + + Test for Bug 609549 + + + + + + +
x
x
+
+
+ + + +Mozilla Bug 609549 +

lorem ipsum dolor sit amet

sandwiched
+ + +
+
+
+ + diff --git a/layout/inspector/tests/test_bug806192.html b/layout/inspector/tests/test_bug806192.html new file mode 100644 index 000000000..2a19ab633 --- /dev/null +++ b/layout/inspector/tests/test_bug806192.html @@ -0,0 +1,26 @@ + + + + + Test for Bug 806192 + + + + +
+
+
+ + diff --git a/layout/inspector/tests/test_bug856317.html b/layout/inspector/tests/test_bug856317.html new file mode 100644 index 000000000..b07b6ec1d --- /dev/null +++ b/layout/inspector/tests/test_bug856317.html @@ -0,0 +1,84 @@ + + + + + Test for Bug 856317 + + + + + + + + + diff --git a/layout/inspector/tests/test_bug877690.html b/layout/inspector/tests/test_bug877690.html new file mode 100644 index 000000000..0480656d4 --- /dev/null +++ b/layout/inspector/tests/test_bug877690.html @@ -0,0 +1,260 @@ + + + + + +Test for Bug 877690 + + + + + +Mozilla Bug 877690 +

+ +
+
+ + diff --git a/layout/inspector/tests/test_color_to_rgba.html b/layout/inspector/tests/test_color_to_rgba.html new file mode 100644 index 000000000..d8279e384 --- /dev/null +++ b/layout/inspector/tests/test_color_to_rgba.html @@ -0,0 +1,55 @@ + + + + + Test inDOMUtils::ColorToRGBA + + + + + +

Test inDOMUtils::ColorToRGBA

+

+ +
+
+ + diff --git a/layout/inspector/tests/test_css_property_is_shorthand.html b/layout/inspector/tests/test_css_property_is_shorthand.html new file mode 100644 index 000000000..e5fbcc1c9 --- /dev/null +++ b/layout/inspector/tests/test_css_property_is_shorthand.html @@ -0,0 +1,51 @@ + + + + + Test inDOMUtils::CssPropertyIsShorthand + + + + + +

Test inDOMUtils::CssPropertyIsShorthand

+

+ +
+
+ + diff --git a/layout/inspector/tests/test_css_property_is_valid.html b/layout/inspector/tests/test_css_property_is_valid.html new file mode 100644 index 000000000..21f00bf18 --- /dev/null +++ b/layout/inspector/tests/test_css_property_is_valid.html @@ -0,0 +1,105 @@ + + + + + Test inDOMUtils::CssPropertyIsValid + + + + + +

Test inDOMUtils::CssPropertyIsValid

+

+ +
+
+ + diff --git a/layout/inspector/tests/test_getCSSPseudoElementNames.html b/layout/inspector/tests/test_getCSSPseudoElementNames.html new file mode 100644 index 000000000..cdb4572d4 --- /dev/null +++ b/layout/inspector/tests/test_getCSSPseudoElementNames.html @@ -0,0 +1,59 @@ + + + + + Test inDOMUtils::getCSSPseudoElementNames + + + + + +

Test inDOMUtils::getCSSPseudoElementNames

+

+ +
+
+ + diff --git a/layout/inspector/tests/test_getRelativeRuleLine.html b/layout/inspector/tests/test_getRelativeRuleLine.html new file mode 100644 index 000000000..a1481a49d --- /dev/null +++ b/layout/inspector/tests/test_getRelativeRuleLine.html @@ -0,0 +1,69 @@ + + + + + Test inDOMUtils::getRelativeRuleLine + + + + + + + + +

Test inDOMUtils::getRelativeRuleLine

+

+ +
+
+ + diff --git a/layout/inspector/tests/test_get_all_style_sheets.html b/layout/inspector/tests/test_get_all_style_sheets.html new file mode 100644 index 000000000..7e5a4d49b --- /dev/null +++ b/layout/inspector/tests/test_get_all_style_sheets.html @@ -0,0 +1,44 @@ + + + + + + Test for Bug 734861 + + + + + +Mozilla Bug 734861 +

+ +
+
+ + diff --git a/layout/inspector/tests/test_is_valid_css_color.html b/layout/inspector/tests/test_is_valid_css_color.html new file mode 100644 index 000000000..ef5eef32e --- /dev/null +++ b/layout/inspector/tests/test_is_valid_css_color.html @@ -0,0 +1,91 @@ + + + + + Test inDOMUtils::isValidCSSColor + + + + + +

Test inDOMUtils::isValidCSSColor

+

+ +
+
+ + diff --git a/layout/inspector/tests/test_isinheritableproperty.html b/layout/inspector/tests/test_isinheritableproperty.html new file mode 100644 index 000000000..6bbb1650f --- /dev/null +++ b/layout/inspector/tests/test_isinheritableproperty.html @@ -0,0 +1,38 @@ + + + + + Test for nsIDOMUtils::isInheritedProperty + + + + +
+
+
+ + diff --git a/layout/inspector/tests/test_parseStyleSheet.html b/layout/inspector/tests/test_parseStyleSheet.html new file mode 100644 index 000000000..f7ddf6e9e --- /dev/null +++ b/layout/inspector/tests/test_parseStyleSheet.html @@ -0,0 +1,34 @@ + + + + + Test for Bug 1195978 + + + + + + + + diff --git a/layout/inspector/tests/test_parseStyleSheetImport.html b/layout/inspector/tests/test_parseStyleSheetImport.html new file mode 100644 index 000000000..73fef2d51 --- /dev/null +++ b/layout/inspector/tests/test_parseStyleSheetImport.html @@ -0,0 +1,83 @@ + + + + + Test for Bug 1202095 + + + + + + + + diff --git a/layout/inspector/tests/test_selectormatcheselement.html b/layout/inspector/tests/test_selectormatcheselement.html new file mode 100644 index 000000000..a052f74d1 --- /dev/null +++ b/layout/inspector/tests/test_selectormatcheselement.html @@ -0,0 +1,90 @@ + + + + + Test for nsIDOMUtils::selectorMatchesElement + + + + + +
foo content
+
+
+
+ + -- cgit v1.2.3