Bug 1482022 - Create HTMLEditor::RemoveOverrideStyleSheetInternal() for internal use r=m_kato

HTMLEditor::RemoveOverrideStyleSheet() is an XPCOM method but used internally.
So, we should create non-virtual method for this.

Additionally, it calls GetStyleSheetForURL() and RemoveStyleSheetFromList(),
but they search index of internal override style sheet array redundantly.
Moreover, RemoveStyleSheetFromList() returns error only when given URL is
not found, but RemoveOverrideStyleSheet() which is the only one caller, ignores
the error.  Therefore, for saving the redundant cost, this patch makes
RemoveStyleSheetFromList() return removing StyleSheet which is retrieved
with the call of GetStyleSheetForURL().  So, RemoveOverrideStyleSheetInternal()
stops calling GetStyleSheetForURL().

Differential Revision: https://phabricator.services.mozilla.com/D3455

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2018-08-16 10:01:23 +00:00
parent 61a6f8e3f2
commit e9f6afbc49
2 changed files with 58 additions and 21 deletions

View File

@ -371,7 +371,10 @@ HTMLEditor::PreDestroy(bool aDestroyingFrames)
}
while (!mStyleSheetURLs.IsEmpty()) {
RemoveOverrideStyleSheet(mStyleSheetURLs[0]);
DebugOnly<nsresult> rv =
RemoveOverrideStyleSheetInternal(mStyleSheetURLs[0]);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to remove an override style sheet");
}
// Clean up after our anonymous content -- we don't want these nodes to
@ -3100,7 +3103,10 @@ HTMLEditor::ReplaceOverrideStyleSheet(const nsAString& aURL)
}
// Remove the previous sheet
if (!mLastOverrideStyleSheetURL.IsEmpty()) {
RemoveOverrideStyleSheet(mLastOverrideStyleSheetURL);
DebugOnly<nsresult> rv =
RemoveOverrideStyleSheetInternal(mLastOverrideStyleSheetURL);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to remove the last override style sheet");
}
nsresult rv = AddOverrideStyleSheetInternal(aURL);
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -3113,25 +3119,36 @@ HTMLEditor::ReplaceOverrideStyleSheet(const nsAString& aURL)
NS_IMETHODIMP
HTMLEditor::RemoveOverrideStyleSheet(const nsAString& aURL)
{
RefPtr<StyleSheet> sheet = GetStyleSheetForURL(aURL);
// Make sure we remove the stylesheet from our internal list in all
// cases.
nsresult rv = RemoveStyleSheetFromList(aURL);
NS_ENSURE_TRUE(sheet, NS_OK); /// Don't fail if sheet not found
nsresult rv = RemoveOverrideStyleSheetInternal(aURL);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
nsresult
HTMLEditor::RemoveOverrideStyleSheetInternal(const nsAString& aURL)
{
if (NS_WARN_IF(!IsInitialized())) {
return NS_ERROR_NOT_INITIALIZED;
}
nsCOMPtr<nsIPresShell> ps = GetPresShell();
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
ps->RemoveOverrideStyleSheet(sheet);
ps->ApplicableStylesChanged();
// Make sure we remove the stylesheet from our internal list in all
// cases.
RefPtr<StyleSheet> sheet = RemoveStyleSheetFromList(aURL);
if (!sheet) {
return NS_OK; // It's okay even if not found.
}
// Remove it from our internal list
return rv;
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
if (NS_WARN_IF(!presShell)) {
return NS_ERROR_NOT_INITIALIZED;
}
presShell->RemoveOverrideStyleSheet(sheet);
presShell->ApplicableStylesChanged();
return NS_OK;
}
NS_IMETHODIMP
@ -3187,21 +3204,23 @@ HTMLEditor::AddNewStyleSheetToList(const nsAString& aURL,
return mStyleSheets.AppendElement(aStyleSheet) ? NS_OK : NS_ERROR_UNEXPECTED;
}
nsresult
already_AddRefed<StyleSheet>
HTMLEditor::RemoveStyleSheetFromList(const nsAString& aURL)
{
// is it already in the list?
size_t foundIndex;
foundIndex = mStyleSheetURLs.IndexOf(aURL);
size_t foundIndex = mStyleSheetURLs.IndexOf(aURL);
if (foundIndex == mStyleSheetURLs.NoIndex) {
return NS_ERROR_FAILURE;
return nullptr;
}
RefPtr<StyleSheet> removingStyleSheet = mStyleSheets[foundIndex];
MOZ_ASSERT(removingStyleSheet);
// Attempt both removals; if one fails there's not much we can do.
mStyleSheets.RemoveElementAt(foundIndex);
mStyleSheetURLs.RemoveElementAt(foundIndex);
return NS_OK;
return removingStyleSheet.forget();
}
StyleSheet*

View File

@ -981,7 +981,16 @@ protected: // Shouldn't be used by friend classes
*/
nsresult AddNewStyleSheetToList(const nsAString &aURL,
StyleSheet* aStyleSheet);
nsresult RemoveStyleSheetFromList(const nsAString &aURL);
/**
* Removes style sheet from the internal lists.
*
* @param aURL URL to the style sheet.
* @return If the URL is in the internal list, returns the
* removed style sheet. Otherwise, i.e., not found,
* nullptr.
*/
already_AddRefed<StyleSheet> RemoveStyleSheetFromList(const nsAString& aURL);
/**
* Add and apply the style sheet synchronously.
@ -990,6 +999,15 @@ protected: // Shouldn't be used by friend classes
*/
nsresult AddOverrideStyleSheetInternal(const nsAString& aURL);
/**
* Remove the style sheet from this editor synchronously.
*
* @param aURL URL to the style sheet.
* @return Even if there is no specified style sheet in the
* internal lists, this returns NS_OK.
*/
nsresult RemoveOverrideStyleSheetInternal(const nsAString& aURL);
/**
* MaybeCollapseSelectionAtFirstEditableNode() may collapse selection at
* proper position to staring to edit. If there is a non-editable node