1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "StyleSheetTransactions.h"
7 
8 #include <stddef.h>  // for nullptr
9 
10 #include "nsAString.h"
11 #include "nsCOMPtr.h"            // for nsCOMPtr, do_QueryInterface, etc.
12 #include "mozilla/StyleSheet.h"  // for mozilla::StyleSheet
13 #include "mozilla/StyleSheetInlines.h"
14 #include "nsDebug.h"              // for NS_ENSURE_TRUE
15 #include "nsError.h"              // for NS_OK, etc.
16 #include "nsIDocument.h"          // for nsIDocument
17 #include "nsIDocumentObserver.h"  // for UPDATE_STYLE
18 
19 namespace mozilla {
20 
AddStyleSheet(EditorBase & aEditor,StyleSheet * aSheet)21 static void AddStyleSheet(EditorBase& aEditor, StyleSheet* aSheet) {
22   nsCOMPtr<nsIDocument> doc = aEditor.GetDocument();
23   if (doc) {
24     doc->BeginUpdate(UPDATE_STYLE);
25     doc->AddStyleSheet(aSheet);
26     doc->EndUpdate(UPDATE_STYLE);
27   }
28 }
29 
RemoveStyleSheet(EditorBase & aEditor,StyleSheet * aSheet)30 static void RemoveStyleSheet(EditorBase& aEditor, StyleSheet* aSheet) {
31   nsCOMPtr<nsIDocument> doc = aEditor.GetDocument();
32   if (doc) {
33     doc->BeginUpdate(UPDATE_STYLE);
34     doc->RemoveStyleSheet(aSheet);
35     doc->EndUpdate(UPDATE_STYLE);
36   }
37 }
38 
39 /******************************************************************************
40  * AddStyleSheetTransaction
41  ******************************************************************************/
42 
AddStyleSheetTransaction(EditorBase & aEditorBase,StyleSheet & aStyleSheet)43 AddStyleSheetTransaction::AddStyleSheetTransaction(EditorBase& aEditorBase,
44                                                    StyleSheet& aStyleSheet)
45     : mEditorBase(&aEditorBase), mSheet(&aStyleSheet) {}
46 
NS_IMPL_CYCLE_COLLECTION_INHERITED(AddStyleSheetTransaction,EditTransactionBase,mEditorBase,mSheet)47 NS_IMPL_CYCLE_COLLECTION_INHERITED(AddStyleSheetTransaction,
48                                    EditTransactionBase, mEditorBase, mSheet)
49 
50 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AddStyleSheetTransaction)
51 NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
52 
53 NS_IMETHODIMP
54 AddStyleSheetTransaction::DoTransaction() {
55   if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
56     return NS_ERROR_NOT_INITIALIZED;
57   }
58   AddStyleSheet(*mEditorBase, mSheet);
59   return NS_OK;
60 }
61 
62 NS_IMETHODIMP
UndoTransaction()63 AddStyleSheetTransaction::UndoTransaction() {
64   if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
65     return NS_ERROR_NOT_INITIALIZED;
66   }
67   RemoveStyleSheet(*mEditorBase, mSheet);
68   return NS_OK;
69 }
70 
71 /******************************************************************************
72  * RemoveStyleSheetTransaction
73  ******************************************************************************/
74 
RemoveStyleSheetTransaction(EditorBase & aEditorBase,StyleSheet & aStyleSheet)75 RemoveStyleSheetTransaction::RemoveStyleSheetTransaction(
76     EditorBase& aEditorBase, StyleSheet& aStyleSheet)
77     : mEditorBase(&aEditorBase), mSheet(&aStyleSheet) {}
78 
NS_IMPL_CYCLE_COLLECTION_INHERITED(RemoveStyleSheetTransaction,EditTransactionBase,mEditorBase,mSheet)79 NS_IMPL_CYCLE_COLLECTION_INHERITED(RemoveStyleSheetTransaction,
80                                    EditTransactionBase, mEditorBase, mSheet)
81 
82 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RemoveStyleSheetTransaction)
83 NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
84 
85 NS_IMETHODIMP
86 RemoveStyleSheetTransaction::DoTransaction() {
87   if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
88     return NS_ERROR_NOT_INITIALIZED;
89   }
90   RemoveStyleSheet(*mEditorBase, mSheet);
91   return NS_OK;
92 }
93 
94 NS_IMETHODIMP
UndoTransaction()95 RemoveStyleSheetTransaction::UndoTransaction() {
96   if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
97     return NS_ERROR_NOT_INITIALIZED;
98   }
99   AddStyleSheet(*mEditorBase, mSheet);
100   return NS_OK;
101 }
102 
103 }  // namespace mozilla
104