1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/editor/libeditor/base/nsStyleSheetTxns.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +#include <stddef.h> // for nullptr 1.11 + 1.12 +#include "nsAString.h" 1.13 +#include "nsCOMPtr.h" // for nsCOMPtr, do_QueryInterface, etc 1.14 +#include "nsCSSStyleSheet.h" // for nsCSSStyleSheet 1.15 +#include "nsDebug.h" // for NS_ENSURE_TRUE 1.16 +#include "nsError.h" // for NS_OK, etc 1.17 +#include "nsIDOMDocument.h" // for nsIDOMDocument 1.18 +#include "nsIDocument.h" // for nsIDocument 1.19 +#include "nsIDocumentObserver.h" // for UPDATE_STYLE 1.20 +#include "nsIEditor.h" // for nsIEditor 1.21 +#include "nsStyleSheetTxns.h" 1.22 + 1.23 +class nsIStyleSheet; 1.24 + 1.25 +static void 1.26 +AddStyleSheet(nsIEditor* aEditor, nsIStyleSheet* aSheet) 1.27 +{ 1.28 + nsCOMPtr<nsIDOMDocument> domDoc; 1.29 + aEditor->GetDocument(getter_AddRefs(domDoc)); 1.30 + nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); 1.31 + if (doc) { 1.32 + doc->BeginUpdate(UPDATE_STYLE); 1.33 + doc->AddStyleSheet(aSheet); 1.34 + doc->EndUpdate(UPDATE_STYLE); 1.35 + } 1.36 +} 1.37 + 1.38 +static void 1.39 +RemoveStyleSheet(nsIEditor *aEditor, nsIStyleSheet *aSheet) 1.40 +{ 1.41 + nsCOMPtr<nsIDOMDocument> domDoc; 1.42 + aEditor->GetDocument(getter_AddRefs(domDoc)); 1.43 + nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); 1.44 + if (doc) { 1.45 + doc->BeginUpdate(UPDATE_STYLE); 1.46 + doc->RemoveStyleSheet(aSheet); 1.47 + doc->EndUpdate(UPDATE_STYLE); 1.48 + } 1.49 +} 1.50 + 1.51 +AddStyleSheetTxn::AddStyleSheetTxn() 1.52 +: EditTxn() 1.53 +, mEditor(nullptr) 1.54 +{ 1.55 +} 1.56 + 1.57 +NS_IMPL_CYCLE_COLLECTION_INHERITED(AddStyleSheetTxn, EditTxn, 1.58 + mSheet) 1.59 + 1.60 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AddStyleSheetTxn) 1.61 +NS_INTERFACE_MAP_END_INHERITING(EditTxn) 1.62 + 1.63 +NS_IMETHODIMP 1.64 +AddStyleSheetTxn::Init(nsIEditor *aEditor, nsCSSStyleSheet *aSheet) 1.65 +{ 1.66 + NS_ENSURE_TRUE(aEditor && aSheet, NS_ERROR_INVALID_ARG); 1.67 + 1.68 + mEditor = aEditor; 1.69 + mSheet = aSheet; 1.70 + 1.71 + return NS_OK; 1.72 +} 1.73 + 1.74 + 1.75 +NS_IMETHODIMP 1.76 +AddStyleSheetTxn::DoTransaction() 1.77 +{ 1.78 + NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); 1.79 + 1.80 + AddStyleSheet(mEditor, mSheet); 1.81 + return NS_OK; 1.82 +} 1.83 + 1.84 +NS_IMETHODIMP 1.85 +AddStyleSheetTxn::UndoTransaction() 1.86 +{ 1.87 + NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); 1.88 + 1.89 + RemoveStyleSheet(mEditor, mSheet); 1.90 + return NS_OK; 1.91 +} 1.92 + 1.93 +NS_IMETHODIMP 1.94 +AddStyleSheetTxn::GetTxnDescription(nsAString& aString) 1.95 +{ 1.96 + aString.AssignLiteral("AddStyleSheetTxn"); 1.97 + return NS_OK; 1.98 +} 1.99 + 1.100 + 1.101 +RemoveStyleSheetTxn::RemoveStyleSheetTxn() 1.102 +: EditTxn() 1.103 +, mEditor(nullptr) 1.104 +{ 1.105 +} 1.106 + 1.107 +NS_IMPL_CYCLE_COLLECTION_INHERITED(RemoveStyleSheetTxn, EditTxn, 1.108 + mSheet) 1.109 + 1.110 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RemoveStyleSheetTxn) 1.111 +NS_INTERFACE_MAP_END_INHERITING(EditTxn) 1.112 + 1.113 +NS_IMETHODIMP 1.114 +RemoveStyleSheetTxn::Init(nsIEditor *aEditor, nsCSSStyleSheet *aSheet) 1.115 +{ 1.116 + NS_ENSURE_TRUE(aEditor && aSheet, NS_ERROR_INVALID_ARG); 1.117 + 1.118 + mEditor = aEditor; 1.119 + mSheet = aSheet; 1.120 + 1.121 + return NS_OK; 1.122 +} 1.123 + 1.124 + 1.125 +NS_IMETHODIMP 1.126 +RemoveStyleSheetTxn::DoTransaction() 1.127 +{ 1.128 + NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); 1.129 + 1.130 + RemoveStyleSheet(mEditor, mSheet); 1.131 + return NS_OK; 1.132 +} 1.133 + 1.134 +NS_IMETHODIMP 1.135 +RemoveStyleSheetTxn::UndoTransaction() 1.136 +{ 1.137 + NS_ENSURE_TRUE(mEditor && mSheet, NS_ERROR_NOT_INITIALIZED); 1.138 + 1.139 + AddStyleSheet(mEditor, mSheet); 1.140 + return NS_OK; 1.141 +} 1.142 + 1.143 +NS_IMETHODIMP 1.144 +RemoveStyleSheetTxn::GetTxnDescription(nsAString& aString) 1.145 +{ 1.146 + aString.AssignLiteral("RemoveStyleSheetTxn"); 1.147 + return NS_OK; 1.148 +}