|
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 |
|
7 #include <stddef.h> // for nullptr |
|
8 |
|
9 #include "nsAString.h" |
|
10 #include "nsCOMPtr.h" // for nsCOMPtr, do_QueryInterface, etc |
|
11 #include "nsCSSStyleSheet.h" // for nsCSSStyleSheet |
|
12 #include "nsDebug.h" // for NS_ENSURE_TRUE |
|
13 #include "nsError.h" // for NS_OK, etc |
|
14 #include "nsIDOMDocument.h" // for nsIDOMDocument |
|
15 #include "nsIDocument.h" // for nsIDocument |
|
16 #include "nsIDocumentObserver.h" // for UPDATE_STYLE |
|
17 #include "nsIEditor.h" // for nsIEditor |
|
18 #include "nsStyleSheetTxns.h" |
|
19 |
|
20 class nsIStyleSheet; |
|
21 |
|
22 static void |
|
23 AddStyleSheet(nsIEditor* aEditor, nsIStyleSheet* aSheet) |
|
24 { |
|
25 nsCOMPtr<nsIDOMDocument> domDoc; |
|
26 aEditor->GetDocument(getter_AddRefs(domDoc)); |
|
27 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); |
|
28 if (doc) { |
|
29 doc->BeginUpdate(UPDATE_STYLE); |
|
30 doc->AddStyleSheet(aSheet); |
|
31 doc->EndUpdate(UPDATE_STYLE); |
|
32 } |
|
33 } |
|
34 |
|
35 static void |
|
36 RemoveStyleSheet(nsIEditor *aEditor, nsIStyleSheet *aSheet) |
|
37 { |
|
38 nsCOMPtr<nsIDOMDocument> domDoc; |
|
39 aEditor->GetDocument(getter_AddRefs(domDoc)); |
|
40 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); |
|
41 if (doc) { |
|
42 doc->BeginUpdate(UPDATE_STYLE); |
|
43 doc->RemoveStyleSheet(aSheet); |
|
44 doc->EndUpdate(UPDATE_STYLE); |
|
45 } |
|
46 } |
|
47 |
|
48 AddStyleSheetTxn::AddStyleSheetTxn() |
|
49 : EditTxn() |
|
50 , mEditor(nullptr) |
|
51 { |
|
52 } |
|
53 |
|
54 NS_IMPL_CYCLE_COLLECTION_INHERITED(AddStyleSheetTxn, EditTxn, |
|
55 mSheet) |
|
56 |
|
57 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AddStyleSheetTxn) |
|
58 NS_INTERFACE_MAP_END_INHERITING(EditTxn) |
|
59 |
|
60 NS_IMETHODIMP |
|
61 AddStyleSheetTxn::Init(nsIEditor *aEditor, nsCSSStyleSheet *aSheet) |
|
62 { |
|
63 NS_ENSURE_TRUE(aEditor && aSheet, NS_ERROR_INVALID_ARG); |
|
64 |
|
65 mEditor = aEditor; |
|
66 mSheet = aSheet; |
|
67 |
|
68 return NS_OK; |
|
69 } |
|
70 |
|
71 |
|
72 NS_IMETHODIMP |
|
73 AddStyleSheetTxn::DoTransaction() |
|
74 { |
|
75 NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); |
|
76 |
|
77 AddStyleSheet(mEditor, mSheet); |
|
78 return NS_OK; |
|
79 } |
|
80 |
|
81 NS_IMETHODIMP |
|
82 AddStyleSheetTxn::UndoTransaction() |
|
83 { |
|
84 NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); |
|
85 |
|
86 RemoveStyleSheet(mEditor, mSheet); |
|
87 return NS_OK; |
|
88 } |
|
89 |
|
90 NS_IMETHODIMP |
|
91 AddStyleSheetTxn::GetTxnDescription(nsAString& aString) |
|
92 { |
|
93 aString.AssignLiteral("AddStyleSheetTxn"); |
|
94 return NS_OK; |
|
95 } |
|
96 |
|
97 |
|
98 RemoveStyleSheetTxn::RemoveStyleSheetTxn() |
|
99 : EditTxn() |
|
100 , mEditor(nullptr) |
|
101 { |
|
102 } |
|
103 |
|
104 NS_IMPL_CYCLE_COLLECTION_INHERITED(RemoveStyleSheetTxn, EditTxn, |
|
105 mSheet) |
|
106 |
|
107 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RemoveStyleSheetTxn) |
|
108 NS_INTERFACE_MAP_END_INHERITING(EditTxn) |
|
109 |
|
110 NS_IMETHODIMP |
|
111 RemoveStyleSheetTxn::Init(nsIEditor *aEditor, nsCSSStyleSheet *aSheet) |
|
112 { |
|
113 NS_ENSURE_TRUE(aEditor && aSheet, NS_ERROR_INVALID_ARG); |
|
114 |
|
115 mEditor = aEditor; |
|
116 mSheet = aSheet; |
|
117 |
|
118 return NS_OK; |
|
119 } |
|
120 |
|
121 |
|
122 NS_IMETHODIMP |
|
123 RemoveStyleSheetTxn::DoTransaction() |
|
124 { |
|
125 NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); |
|
126 |
|
127 RemoveStyleSheet(mEditor, mSheet); |
|
128 return NS_OK; |
|
129 } |
|
130 |
|
131 NS_IMETHODIMP |
|
132 RemoveStyleSheetTxn::UndoTransaction() |
|
133 { |
|
134 NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); |
|
135 |
|
136 AddStyleSheet(mEditor, mSheet); |
|
137 return NS_OK; |
|
138 } |
|
139 |
|
140 NS_IMETHODIMP |
|
141 RemoveStyleSheetTxn::GetTxnDescription(nsAString& aString) |
|
142 { |
|
143 aString.AssignLiteral("RemoveStyleSheetTxn"); |
|
144 return NS_OK; |
|
145 } |