Line data Source code
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 :
21 : static void
22 0 : AddStyleSheet(EditorBase& aEditor, StyleSheet* aSheet)
23 : {
24 0 : nsCOMPtr<nsIDocument> doc = aEditor.GetDocument();
25 0 : if (doc) {
26 0 : doc->BeginUpdate(UPDATE_STYLE);
27 0 : doc->AddStyleSheet(aSheet);
28 0 : doc->EndUpdate(UPDATE_STYLE);
29 : }
30 0 : }
31 :
32 : static void
33 0 : RemoveStyleSheet(EditorBase& aEditor, StyleSheet* aSheet)
34 : {
35 0 : nsCOMPtr<nsIDocument> doc = aEditor.GetDocument();
36 0 : if (doc) {
37 0 : doc->BeginUpdate(UPDATE_STYLE);
38 0 : doc->RemoveStyleSheet(aSheet);
39 0 : doc->EndUpdate(UPDATE_STYLE);
40 : }
41 0 : }
42 :
43 : /******************************************************************************
44 : * AddStyleSheetTransaction
45 : ******************************************************************************/
46 :
47 0 : AddStyleSheetTransaction::AddStyleSheetTransaction(EditorBase& aEditorBase,
48 0 : StyleSheet* aSheet)
49 : : mEditorBase(&aEditorBase)
50 0 : , mSheet(aSheet)
51 : {
52 0 : MOZ_ASSERT(aSheet);
53 0 : }
54 :
55 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(AddStyleSheetTransaction,
56 : EditTransactionBase,
57 : mEditorBase,
58 : mSheet)
59 :
60 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AddStyleSheetTransaction)
61 0 : NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
62 :
63 : NS_IMETHODIMP
64 0 : AddStyleSheetTransaction::DoTransaction()
65 : {
66 0 : if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
67 0 : return NS_ERROR_NOT_INITIALIZED;
68 : }
69 0 : AddStyleSheet(*mEditorBase, mSheet);
70 0 : return NS_OK;
71 : }
72 :
73 : NS_IMETHODIMP
74 0 : AddStyleSheetTransaction::UndoTransaction()
75 : {
76 0 : if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
77 0 : return NS_ERROR_NOT_INITIALIZED;
78 : }
79 0 : RemoveStyleSheet(*mEditorBase, mSheet);
80 0 : return NS_OK;
81 : }
82 :
83 : NS_IMETHODIMP
84 0 : AddStyleSheetTransaction::GetTxnDescription(nsAString& aString)
85 : {
86 0 : aString.AssignLiteral("AddStyleSheetTransaction");
87 0 : return NS_OK;
88 : }
89 :
90 : /******************************************************************************
91 : * RemoveStyleSheetTransaction
92 : ******************************************************************************/
93 :
94 0 : RemoveStyleSheetTransaction::RemoveStyleSheetTransaction(
95 : EditorBase& aEditorBase,
96 0 : StyleSheet* aSheet)
97 : : mEditorBase(&aEditorBase)
98 0 : , mSheet(aSheet)
99 : {
100 0 : MOZ_ASSERT(aSheet);
101 0 : }
102 :
103 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(RemoveStyleSheetTransaction,
104 : EditTransactionBase,
105 : mEditorBase,
106 : mSheet)
107 :
108 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RemoveStyleSheetTransaction)
109 0 : NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
110 :
111 : NS_IMETHODIMP
112 0 : RemoveStyleSheetTransaction::DoTransaction()
113 : {
114 0 : if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
115 0 : return NS_ERROR_NOT_INITIALIZED;
116 : }
117 0 : RemoveStyleSheet(*mEditorBase, mSheet);
118 0 : return NS_OK;
119 : }
120 :
121 : NS_IMETHODIMP
122 0 : RemoveStyleSheetTransaction::UndoTransaction()
123 : {
124 0 : if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mSheet)) {
125 0 : return NS_ERROR_NOT_INITIALIZED;
126 : }
127 0 : AddStyleSheet(*mEditorBase, mSheet);
128 0 : return NS_OK;
129 : }
130 :
131 : NS_IMETHODIMP
132 0 : RemoveStyleSheetTransaction::GetTxnDescription(nsAString& aString)
133 : {
134 0 : aString.AssignLiteral("RemoveStyleSheetTransaction");
135 0 : return NS_OK;
136 : }
137 :
138 : } // namespace mozilla
|