Bug 1451672 - part 12: Create HTMLEditor::RemoveStyleSheetWithTransaction() as implementation of nsIEditorStyleSheets::RemoveStyleSheet() r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 12 Apr 2018 17:20:21 +0900
changeset 786095 540fa41311b99e4cc800fba3263624114e58aca9
parent 786094 f4ec14078759dc96a31255b8ce666e83ef77c200
child 786096 de5516a4b06cb0091240f5fb188f9368a3c4f8d5
push id107393
push usermasayuki@d-toybox.com
push dateSat, 21 Apr 2018 04:40:12 +0000
reviewersm_kato
bugs1451672
milestone61.0a1
Bug 1451672 - part 12: Create HTMLEditor::RemoveStyleSheetWithTransaction() as implementation of nsIEditorStyleSheets::RemoveStyleSheet() r?m_kato MozReview-Commit-ID: BIXU3jzD1rU
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -2917,18 +2917,26 @@ HTMLEditor::ReplaceStyleSheet(const nsAS
 
   return ps->GetDocument()->CSSLoader()->LoadSheet(
     uaURI, false, nullptr, nullptr, this);
 }
 
 NS_IMETHODIMP
 HTMLEditor::RemoveStyleSheet(const nsAString& aURL)
 {
+  return RemoveStyleSheetWithTransaction(aURL);
+}
+
+nsresult
+HTMLEditor::RemoveStyleSheetWithTransaction(const nsAString& aURL)
+{
   RefPtr<StyleSheet> sheet = GetStyleSheetForURL(aURL);
-  NS_ENSURE_TRUE(sheet, NS_ERROR_UNEXPECTED);
+  if (NS_WARN_IF(!sheet)) {
+    return NS_ERROR_UNEXPECTED;
+  }
 
   RefPtr<RemoveStyleSheetTransaction> transaction =
     RemoveStyleSheetTransaction::Create(*this, *sheet);
   nsresult rv = DoTransaction(transaction);
   if (NS_SUCCEEDED(rv)) {
     mLastStyleSheetURL.Truncate();        // forget it
   }
   // Remove it from our internal list
@@ -3420,18 +3428,19 @@ HTMLEditor::DebugUnitTests(int32_t* outN
 
 NS_IMETHODIMP
 HTMLEditor::StyleSheetLoaded(StyleSheet* aSheet,
                              bool aWasAlternate,
                              nsresult aStatus)
 {
   AutoPlaceholderBatch batchIt(this);
 
-  if (!mLastStyleSheetURL.IsEmpty())
-    RemoveStyleSheet(mLastStyleSheetURL);
+  if (!mLastStyleSheetURL.IsEmpty()) {
+    RemoveStyleSheetWithTransaction(mLastStyleSheetURL);
+  }
 
   RefPtr<AddStyleSheetTransaction> transaction =
     AddStyleSheetTransaction::Create(*this, *aSheet);
   nsresult rv = DoTransaction(transaction);
   if (NS_SUCCEEDED(rv)) {
     // Get the URI, then url spec from the sheet
     nsAutoCString spec;
     rv = aSheet->GetSheetURI()->GetSpec(spec);
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -1188,16 +1188,22 @@ protected:
                                   int32_t aDirection, bool aSelected);
 
   /**
    * A more C++-friendly version of nsIHTMLEditor::GetSelectedElement
    * that just returns null on errors.
    */
   already_AddRefed<dom::Element> GetSelectedElement(const nsAString& aTagName);
 
+  /**
+   * RemoveStyleSheetWithTransaction() removes the given URL stylesheet
+   * from mStyleSheets and mStyleSheetURLs.
+   */
+  nsresult RemoveStyleSheetWithTransaction(const nsAString& aURL);
+
 protected:
   RefPtr<TypeInState> mTypeInState;
   RefPtr<ComposerCommandsUpdater> mComposerCommandsUpdater;
 
   bool mCRInParagraphCreatesParagraph;
 
   bool mCSSAware;
   UniquePtr<CSSEditUtils> mCSSEditUtils;