editor/composer/src/nsComposerCommands.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6
michael@0 7 #include <stdio.h> // for printf
michael@0 8
michael@0 9 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
michael@0 10 #include "nsAString.h"
michael@0 11 #include "nsCOMPtr.h" // for nsCOMPtr, do_QueryInterface, etc
michael@0 12 #include "nsComponentManagerUtils.h" // for do_CreateInstance
michael@0 13 #include "nsComposerCommands.h"
michael@0 14 #include "nsDebug.h" // for NS_ENSURE_TRUE, etc
michael@0 15 #include "nsError.h" // for NS_OK, NS_ERROR_FAILURE, etc
michael@0 16 #include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::font, etc
michael@0 17 #include "nsIAtom.h" // for nsIAtom, etc
michael@0 18 #include "nsIClipboard.h" // for nsIClipboard, etc
michael@0 19 #include "nsICommandParams.h" // for nsICommandParams, etc
michael@0 20 #include "nsID.h"
michael@0 21 #include "nsIDOMElement.h" // for nsIDOMElement
michael@0 22 #include "nsIEditor.h" // for nsIEditor
michael@0 23 #include "nsIHTMLAbsPosEditor.h" // for nsIHTMLAbsPosEditor
michael@0 24 #include "nsIHTMLEditor.h" // for nsIHTMLEditor, etc
michael@0 25 #include "nsLiteralString.h" // for NS_LITERAL_STRING
michael@0 26 #include "nsReadableUtils.h" // for EmptyString
michael@0 27 #include "nsString.h" // for nsAutoString, nsString, etc
michael@0 28 #include "nsStringFwd.h" // for nsAFlatString
michael@0 29
michael@0 30 class nsISupports;
michael@0 31
michael@0 32 //prototype
michael@0 33 nsresult GetListState(nsIHTMLEditor* aEditor, bool* aMixed,
michael@0 34 nsAString& aLocalName);
michael@0 35 nsresult RemoveOneProperty(nsIHTMLEditor* aEditor, const nsAString& aProp);
michael@0 36 nsresult RemoveTextProperty(nsIHTMLEditor* aEditor, const nsAString& aProp);
michael@0 37 nsresult SetTextProperty(nsIHTMLEditor *aEditor, const nsAString& aProp);
michael@0 38
michael@0 39
michael@0 40 //defines
michael@0 41 #define STATE_ENABLED "state_enabled"
michael@0 42 #define STATE_ALL "state_all"
michael@0 43 #define STATE_ANY "state_any"
michael@0 44 #define STATE_MIXED "state_mixed"
michael@0 45 #define STATE_BEGIN "state_begin"
michael@0 46 #define STATE_END "state_end"
michael@0 47 #define STATE_ATTRIBUTE "state_attribute"
michael@0 48 #define STATE_DATA "state_data"
michael@0 49
michael@0 50
michael@0 51 nsBaseComposerCommand::nsBaseComposerCommand()
michael@0 52 {
michael@0 53 }
michael@0 54
michael@0 55 NS_IMPL_ISUPPORTS(nsBaseComposerCommand, nsIControllerCommand)
michael@0 56
michael@0 57
michael@0 58 nsBaseStateUpdatingCommand::nsBaseStateUpdatingCommand(nsIAtom* aTagName)
michael@0 59 : nsBaseComposerCommand()
michael@0 60 , mTagName(aTagName)
michael@0 61 {
michael@0 62 MOZ_ASSERT(mTagName);
michael@0 63 }
michael@0 64
michael@0 65 nsBaseStateUpdatingCommand::~nsBaseStateUpdatingCommand()
michael@0 66 {
michael@0 67 }
michael@0 68
michael@0 69 NS_IMPL_ISUPPORTS_INHERITED0(nsBaseStateUpdatingCommand, nsBaseComposerCommand)
michael@0 70
michael@0 71 NS_IMETHODIMP
michael@0 72 nsBaseStateUpdatingCommand::IsCommandEnabled(const char *aCommandName,
michael@0 73 nsISupports *refCon,
michael@0 74 bool *outCmdEnabled)
michael@0 75 {
michael@0 76 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 77 if (editor)
michael@0 78 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 79
michael@0 80 *outCmdEnabled = false;
michael@0 81 return NS_OK;
michael@0 82 }
michael@0 83
michael@0 84
michael@0 85 NS_IMETHODIMP
michael@0 86 nsBaseStateUpdatingCommand::DoCommand(const char *aCommandName,
michael@0 87 nsISupports *refCon)
michael@0 88 {
michael@0 89 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 90 NS_ENSURE_TRUE(editor, NS_ERROR_NOT_INITIALIZED);
michael@0 91
michael@0 92 return ToggleState(editor);
michael@0 93 }
michael@0 94
michael@0 95 NS_IMETHODIMP
michael@0 96 nsBaseStateUpdatingCommand::DoCommandParams(const char *aCommandName,
michael@0 97 nsICommandParams *aParams,
michael@0 98 nsISupports *refCon)
michael@0 99 {
michael@0 100 return DoCommand(aCommandName, refCon);
michael@0 101 }
michael@0 102
michael@0 103 NS_IMETHODIMP
michael@0 104 nsBaseStateUpdatingCommand::GetCommandStateParams(const char *aCommandName,
michael@0 105 nsICommandParams *aParams,
michael@0 106 nsISupports *refCon)
michael@0 107 {
michael@0 108 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 109 if (editor)
michael@0 110 return GetCurrentState(editor, aParams);
michael@0 111
michael@0 112 return NS_OK;
michael@0 113 }
michael@0 114
michael@0 115 NS_IMETHODIMP
michael@0 116 nsPasteNoFormattingCommand::IsCommandEnabled(const char * aCommandName,
michael@0 117 nsISupports *refCon,
michael@0 118 bool *outCmdEnabled)
michael@0 119 {
michael@0 120 NS_ENSURE_ARG_POINTER(outCmdEnabled);
michael@0 121 *outCmdEnabled = false;
michael@0 122
michael@0 123 // This command is only implemented by nsIHTMLEditor, since
michael@0 124 // pasting in a plaintext editor automatically only supplies
michael@0 125 // "unformatted" text
michael@0 126 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 127 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED);
michael@0 128
michael@0 129 nsCOMPtr<nsIEditor> editor = do_QueryInterface(htmlEditor);
michael@0 130 NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG);
michael@0 131
michael@0 132 return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled);
michael@0 133 }
michael@0 134
michael@0 135
michael@0 136 NS_IMETHODIMP
michael@0 137 nsPasteNoFormattingCommand::DoCommand(const char *aCommandName,
michael@0 138 nsISupports *refCon)
michael@0 139 {
michael@0 140 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 141 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED);
michael@0 142
michael@0 143 return htmlEditor->PasteNoFormatting(nsIClipboard::kGlobalClipboard);
michael@0 144 }
michael@0 145
michael@0 146 NS_IMETHODIMP
michael@0 147 nsPasteNoFormattingCommand::DoCommandParams(const char *aCommandName,
michael@0 148 nsICommandParams *aParams,
michael@0 149 nsISupports *refCon)
michael@0 150 {
michael@0 151 return DoCommand(aCommandName, refCon);
michael@0 152 }
michael@0 153
michael@0 154 NS_IMETHODIMP
michael@0 155 nsPasteNoFormattingCommand::GetCommandStateParams(const char *aCommandName,
michael@0 156 nsICommandParams *aParams,
michael@0 157 nsISupports *refCon)
michael@0 158 {
michael@0 159 NS_ENSURE_ARG_POINTER(aParams);
michael@0 160
michael@0 161 bool enabled = false;
michael@0 162 nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
michael@0 163 NS_ENSURE_SUCCESS(rv, rv);
michael@0 164
michael@0 165 return aParams->SetBooleanValue(STATE_ENABLED, enabled);
michael@0 166 }
michael@0 167
michael@0 168 nsStyleUpdatingCommand::nsStyleUpdatingCommand(nsIAtom* aTagName)
michael@0 169 : nsBaseStateUpdatingCommand(aTagName)
michael@0 170 {
michael@0 171 }
michael@0 172
michael@0 173 nsresult
michael@0 174 nsStyleUpdatingCommand::GetCurrentState(nsIEditor *aEditor,
michael@0 175 nsICommandParams *aParams)
michael@0 176 {
michael@0 177 NS_ASSERTION(aEditor, "Need editor here");
michael@0 178 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 179 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_INITIALIZED);
michael@0 180
michael@0 181 bool firstOfSelectionHasProp = false;
michael@0 182 bool anyOfSelectionHasProp = false;
michael@0 183 bool allOfSelectionHasProp = false;
michael@0 184
michael@0 185 nsresult rv = htmlEditor->GetInlineProperty(mTagName, EmptyString(),
michael@0 186 EmptyString(),
michael@0 187 &firstOfSelectionHasProp,
michael@0 188 &anyOfSelectionHasProp,
michael@0 189 &allOfSelectionHasProp);
michael@0 190
michael@0 191 aParams->SetBooleanValue(STATE_ENABLED, NS_SUCCEEDED(rv));
michael@0 192 aParams->SetBooleanValue(STATE_ALL, allOfSelectionHasProp);
michael@0 193 aParams->SetBooleanValue(STATE_ANY, anyOfSelectionHasProp);
michael@0 194 aParams->SetBooleanValue(STATE_MIXED, anyOfSelectionHasProp
michael@0 195 && !allOfSelectionHasProp);
michael@0 196 aParams->SetBooleanValue(STATE_BEGIN, firstOfSelectionHasProp);
michael@0 197 aParams->SetBooleanValue(STATE_END, allOfSelectionHasProp);//not completely accurate
michael@0 198 return NS_OK;
michael@0 199 }
michael@0 200
michael@0 201 nsresult
michael@0 202 nsStyleUpdatingCommand::ToggleState(nsIEditor *aEditor)
michael@0 203 {
michael@0 204 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 205 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NO_INTERFACE);
michael@0 206
michael@0 207 //create some params now...
michael@0 208 nsresult rv;
michael@0 209 nsCOMPtr<nsICommandParams> params =
michael@0 210 do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv);
michael@0 211 if (NS_FAILED(rv) || !params)
michael@0 212 return rv;
michael@0 213
michael@0 214 // tags "href" and "name" are special cases in the core editor
michael@0 215 // they are used to remove named anchor/link and shouldn't be used for insertion
michael@0 216 bool doTagRemoval;
michael@0 217 if (mTagName == nsGkAtoms::href || mTagName == nsGkAtoms::name) {
michael@0 218 doTagRemoval = true;
michael@0 219 } else {
michael@0 220 // check current selection; set doTagRemoval if formatting should be removed
michael@0 221 rv = GetCurrentState(aEditor, params);
michael@0 222 NS_ENSURE_SUCCESS(rv, rv);
michael@0 223 rv = params->GetBooleanValue(STATE_ALL, &doTagRemoval);
michael@0 224 NS_ENSURE_SUCCESS(rv, rv);
michael@0 225 }
michael@0 226
michael@0 227 if (doTagRemoval) {
michael@0 228 // Also remove equivalent properties (bug 317093)
michael@0 229 if (mTagName == nsGkAtoms::b) {
michael@0 230 rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("strong"));
michael@0 231 NS_ENSURE_SUCCESS(rv, rv);
michael@0 232 } else if (mTagName == nsGkAtoms::i) {
michael@0 233 rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("em"));
michael@0 234 NS_ENSURE_SUCCESS(rv, rv);
michael@0 235 } else if (mTagName == nsGkAtoms::strike) {
michael@0 236 rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("s"));
michael@0 237 NS_ENSURE_SUCCESS(rv, rv);
michael@0 238 }
michael@0 239
michael@0 240 rv = RemoveTextProperty(htmlEditor, nsDependentAtomString(mTagName));
michael@0 241 } else {
michael@0 242 // Superscript and Subscript styles are mutually exclusive
michael@0 243 aEditor->BeginTransaction();
michael@0 244
michael@0 245 nsDependentAtomString tagName(mTagName);
michael@0 246 if (mTagName == nsGkAtoms::sub || mTagName == nsGkAtoms::sup) {
michael@0 247 rv = RemoveTextProperty(htmlEditor, tagName);
michael@0 248 }
michael@0 249 if (NS_SUCCEEDED(rv))
michael@0 250 rv = SetTextProperty(htmlEditor, tagName);
michael@0 251
michael@0 252 aEditor->EndTransaction();
michael@0 253 }
michael@0 254
michael@0 255 return rv;
michael@0 256 }
michael@0 257
michael@0 258 nsListCommand::nsListCommand(nsIAtom* aTagName)
michael@0 259 : nsBaseStateUpdatingCommand(aTagName)
michael@0 260 {
michael@0 261 }
michael@0 262
michael@0 263 nsresult
michael@0 264 nsListCommand::GetCurrentState(nsIEditor* aEditor, nsICommandParams* aParams)
michael@0 265 {
michael@0 266 NS_ASSERTION(aEditor, "Need editor here");
michael@0 267 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 268 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NO_INTERFACE);
michael@0 269
michael@0 270 bool bMixed;
michael@0 271 nsAutoString localName;
michael@0 272 nsresult rv = GetListState(htmlEditor, &bMixed, localName);
michael@0 273 NS_ENSURE_SUCCESS(rv, rv);
michael@0 274
michael@0 275 bool inList = mTagName->Equals(localName);
michael@0 276 aParams->SetBooleanValue(STATE_ALL, !bMixed && inList);
michael@0 277 aParams->SetBooleanValue(STATE_MIXED, bMixed);
michael@0 278 aParams->SetBooleanValue(STATE_ENABLED, true);
michael@0 279 return NS_OK;
michael@0 280 }
michael@0 281
michael@0 282 nsresult
michael@0 283 nsListCommand::ToggleState(nsIEditor *aEditor)
michael@0 284 {
michael@0 285 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(aEditor);
michael@0 286 NS_ENSURE_TRUE(editor, NS_NOINTERFACE);
michael@0 287
michael@0 288 nsresult rv;
michael@0 289 nsCOMPtr<nsICommandParams> params =
michael@0 290 do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv);
michael@0 291 if (NS_FAILED(rv) || !params)
michael@0 292 return rv;
michael@0 293
michael@0 294 rv = GetCurrentState(aEditor, params);
michael@0 295 NS_ENSURE_SUCCESS(rv, rv);
michael@0 296
michael@0 297 bool inList;
michael@0 298 rv = params->GetBooleanValue(STATE_ALL,&inList);
michael@0 299 NS_ENSURE_SUCCESS(rv, rv);
michael@0 300
michael@0 301 nsDependentAtomString listType(mTagName);
michael@0 302 if (inList) {
michael@0 303 rv = editor->RemoveList(listType);
michael@0 304 } else {
michael@0 305 rv = editor->MakeOrChangeList(listType, false, EmptyString());
michael@0 306 }
michael@0 307
michael@0 308 return rv;
michael@0 309 }
michael@0 310
michael@0 311 nsListItemCommand::nsListItemCommand(nsIAtom* aTagName)
michael@0 312 : nsBaseStateUpdatingCommand(aTagName)
michael@0 313 {
michael@0 314 }
michael@0 315
michael@0 316 nsresult
michael@0 317 nsListItemCommand::GetCurrentState(nsIEditor* aEditor,
michael@0 318 nsICommandParams *aParams)
michael@0 319 {
michael@0 320 NS_ASSERTION(aEditor, "Need editor here");
michael@0 321 // 39584
michael@0 322 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 323 NS_ENSURE_TRUE(htmlEditor, NS_NOINTERFACE);
michael@0 324
michael@0 325 bool bMixed, bLI, bDT, bDD;
michael@0 326 nsresult rv = htmlEditor->GetListItemState(&bMixed, &bLI, &bDT, &bDD);
michael@0 327 NS_ENSURE_SUCCESS(rv, rv);
michael@0 328
michael@0 329 bool inList = false;
michael@0 330 if (!bMixed)
michael@0 331 {
michael@0 332 if (bLI) {
michael@0 333 inList = mTagName == nsGkAtoms::li;
michael@0 334 } else if (bDT) {
michael@0 335 inList = mTagName == nsGkAtoms::dt;
michael@0 336 } else if (bDD) {
michael@0 337 inList = mTagName == nsGkAtoms::dd;
michael@0 338 }
michael@0 339 }
michael@0 340
michael@0 341 aParams->SetBooleanValue(STATE_ALL, !bMixed && inList);
michael@0 342 aParams->SetBooleanValue(STATE_MIXED, bMixed);
michael@0 343
michael@0 344 return NS_OK;
michael@0 345 }
michael@0 346
michael@0 347 nsresult
michael@0 348 nsListItemCommand::ToggleState(nsIEditor *aEditor)
michael@0 349 {
michael@0 350 NS_ASSERTION(aEditor, "Need editor here");
michael@0 351 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 352 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_INITIALIZED);
michael@0 353
michael@0 354 bool inList;
michael@0 355 // Need to use mTagName????
michael@0 356 nsresult rv;
michael@0 357 nsCOMPtr<nsICommandParams> params =
michael@0 358 do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv);
michael@0 359 if (NS_FAILED(rv) || !params)
michael@0 360 return rv;
michael@0 361 rv = GetCurrentState(aEditor, params);
michael@0 362 rv = params->GetBooleanValue(STATE_ALL,&inList);
michael@0 363 NS_ENSURE_SUCCESS(rv, rv);
michael@0 364 NS_ENSURE_SUCCESS(rv, rv);
michael@0 365
michael@0 366 if (inList) {
michael@0 367 // To remove a list, first get what kind of list we're in
michael@0 368 bool bMixed;
michael@0 369 nsAutoString localName;
michael@0 370 rv = GetListState(htmlEditor, &bMixed, localName);
michael@0 371 NS_ENSURE_SUCCESS(rv, rv);
michael@0 372 if (localName.IsEmpty() || bMixed) {
michael@0 373 return rv;
michael@0 374 }
michael@0 375 return htmlEditor->RemoveList(localName);
michael@0 376 }
michael@0 377
michael@0 378 // Set to the requested paragraph type
michael@0 379 //XXX Note: This actually doesn't work for "LI",
michael@0 380 // but we currently don't use this for non DL lists anyway.
michael@0 381 // Problem: won't this replace any current block paragraph style?
michael@0 382 return htmlEditor->SetParagraphFormat(nsDependentAtomString(mTagName));
michael@0 383 }
michael@0 384
michael@0 385 NS_IMETHODIMP
michael@0 386 nsRemoveListCommand::IsCommandEnabled(const char * aCommandName,
michael@0 387 nsISupports *refCon,
michael@0 388 bool *outCmdEnabled)
michael@0 389 {
michael@0 390 *outCmdEnabled = false;
michael@0 391 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 392 NS_ENSURE_TRUE(editor, NS_OK);
michael@0 393
michael@0 394 bool isEditable = false;
michael@0 395 nsresult rv = editor->GetIsSelectionEditable(&isEditable);
michael@0 396 NS_ENSURE_SUCCESS(rv, rv);
michael@0 397 if (!isEditable) {
michael@0 398 return NS_OK;
michael@0 399 }
michael@0 400
michael@0 401 // It is enabled if we are in any list type
michael@0 402 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 403 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NO_INTERFACE);
michael@0 404
michael@0 405 bool bMixed;
michael@0 406 nsAutoString localName;
michael@0 407 rv = GetListState(htmlEditor, &bMixed, localName);
michael@0 408 NS_ENSURE_SUCCESS(rv, rv);
michael@0 409
michael@0 410 *outCmdEnabled = bMixed || !localName.IsEmpty();
michael@0 411 return NS_OK;
michael@0 412 }
michael@0 413
michael@0 414
michael@0 415 NS_IMETHODIMP
michael@0 416 nsRemoveListCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
michael@0 417 {
michael@0 418 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 419
michael@0 420 nsresult rv = NS_OK;
michael@0 421 if (editor)
michael@0 422 {
michael@0 423 // This removes any list type
michael@0 424 rv = editor->RemoveList(EmptyString());
michael@0 425 }
michael@0 426
michael@0 427 return rv;
michael@0 428 }
michael@0 429
michael@0 430 NS_IMETHODIMP
michael@0 431 nsRemoveListCommand::DoCommandParams(const char *aCommandName,
michael@0 432 nsICommandParams *aParams,
michael@0 433 nsISupports *refCon)
michael@0 434 {
michael@0 435 return DoCommand(aCommandName, refCon);
michael@0 436 }
michael@0 437
michael@0 438 NS_IMETHODIMP
michael@0 439 nsRemoveListCommand::GetCommandStateParams(const char *aCommandName,
michael@0 440 nsICommandParams *aParams,
michael@0 441 nsISupports *refCon)
michael@0 442 {
michael@0 443 bool outCmdEnabled = false;
michael@0 444 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 445 return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled);
michael@0 446 }
michael@0 447
michael@0 448 NS_IMETHODIMP
michael@0 449 nsIndentCommand::IsCommandEnabled(const char * aCommandName,
michael@0 450 nsISupports *refCon, bool *outCmdEnabled)
michael@0 451 {
michael@0 452 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 453 if (editor)
michael@0 454 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 455
michael@0 456 *outCmdEnabled = false;
michael@0 457 return NS_OK;
michael@0 458 }
michael@0 459
michael@0 460
michael@0 461 NS_IMETHODIMP
michael@0 462 nsIndentCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
michael@0 463 {
michael@0 464 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 465
michael@0 466 nsresult rv = NS_OK;
michael@0 467 if (editor)
michael@0 468 {
michael@0 469 rv = editor->Indent(NS_LITERAL_STRING("indent"));
michael@0 470 }
michael@0 471
michael@0 472 return rv;
michael@0 473 }
michael@0 474
michael@0 475 NS_IMETHODIMP
michael@0 476 nsIndentCommand::DoCommandParams(const char *aCommandName,
michael@0 477 nsICommandParams *aParams,
michael@0 478 nsISupports *refCon)
michael@0 479 {
michael@0 480 return DoCommand(aCommandName, refCon);
michael@0 481 }
michael@0 482
michael@0 483 NS_IMETHODIMP
michael@0 484 nsIndentCommand::GetCommandStateParams(const char *aCommandName,
michael@0 485 nsICommandParams *aParams,
michael@0 486 nsISupports *refCon)
michael@0 487 {
michael@0 488 bool outCmdEnabled = false;
michael@0 489 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 490 return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled);
michael@0 491 }
michael@0 492
michael@0 493
michael@0 494 //OUTDENT
michael@0 495
michael@0 496 NS_IMETHODIMP
michael@0 497 nsOutdentCommand::IsCommandEnabled(const char * aCommandName,
michael@0 498 nsISupports *refCon,
michael@0 499 bool *outCmdEnabled)
michael@0 500 {
michael@0 501 *outCmdEnabled = false;
michael@0 502
michael@0 503 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 504 if (editor) {
michael@0 505 nsresult rv = editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 506 NS_ENSURE_SUCCESS(rv, rv);
michael@0 507 }
michael@0 508
michael@0 509 return NS_OK;
michael@0 510 }
michael@0 511
michael@0 512
michael@0 513 NS_IMETHODIMP
michael@0 514 nsOutdentCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
michael@0 515 {
michael@0 516 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 517 if (htmlEditor)
michael@0 518 return htmlEditor->Indent(NS_LITERAL_STRING("outdent"));
michael@0 519
michael@0 520 return NS_OK;
michael@0 521 }
michael@0 522
michael@0 523 NS_IMETHODIMP
michael@0 524 nsOutdentCommand::DoCommandParams(const char *aCommandName,
michael@0 525 nsICommandParams *aParams,
michael@0 526 nsISupports *refCon)
michael@0 527 {
michael@0 528 return DoCommand(aCommandName, refCon);
michael@0 529 }
michael@0 530
michael@0 531 NS_IMETHODIMP
michael@0 532 nsOutdentCommand::GetCommandStateParams(const char *aCommandName,
michael@0 533 nsICommandParams *aParams,
michael@0 534 nsISupports *refCon)
michael@0 535 {
michael@0 536 bool outCmdEnabled = false;
michael@0 537 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 538 return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled);
michael@0 539 }
michael@0 540
michael@0 541 nsMultiStateCommand::nsMultiStateCommand()
michael@0 542 : nsBaseComposerCommand()
michael@0 543 {
michael@0 544 }
michael@0 545
michael@0 546 nsMultiStateCommand::~nsMultiStateCommand()
michael@0 547 {
michael@0 548 }
michael@0 549
michael@0 550 NS_IMPL_ISUPPORTS_INHERITED0(nsMultiStateCommand, nsBaseComposerCommand)
michael@0 551
michael@0 552 NS_IMETHODIMP
michael@0 553 nsMultiStateCommand::IsCommandEnabled(const char * aCommandName,
michael@0 554 nsISupports *refCon,
michael@0 555 bool *outCmdEnabled)
michael@0 556 {
michael@0 557 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 558 // should be disabled sometimes, like if the current selection is an image
michael@0 559 if (editor)
michael@0 560 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 561
michael@0 562 *outCmdEnabled = false;
michael@0 563 return NS_OK;
michael@0 564 }
michael@0 565
michael@0 566
michael@0 567 NS_IMETHODIMP
michael@0 568 nsMultiStateCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
michael@0 569 {
michael@0 570 #ifdef DEBUG
michael@0 571 printf("who is calling nsMultiStateCommand::DoCommand \
michael@0 572 (no implementation)? %s\n", aCommandName);
michael@0 573 #endif
michael@0 574
michael@0 575 return NS_OK;
michael@0 576 }
michael@0 577
michael@0 578 NS_IMETHODIMP
michael@0 579 nsMultiStateCommand::DoCommandParams(const char *aCommandName,
michael@0 580 nsICommandParams *aParams,
michael@0 581 nsISupports *refCon)
michael@0 582 {
michael@0 583 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 584
michael@0 585 nsresult rv = NS_OK;
michael@0 586 if (editor)
michael@0 587 {
michael@0 588 nsAutoString tString;
michael@0 589
michael@0 590 if (aParams) {
michael@0 591 nsXPIDLCString s;
michael@0 592 rv = aParams->GetCStringValue(STATE_ATTRIBUTE, getter_Copies(s));
michael@0 593 if (NS_SUCCEEDED(rv))
michael@0 594 tString.AssignWithConversion(s);
michael@0 595 else
michael@0 596 rv = aParams->GetStringValue(STATE_ATTRIBUTE, tString);
michael@0 597 }
michael@0 598
michael@0 599 rv = SetState(editor, tString);
michael@0 600 }
michael@0 601
michael@0 602 return rv;
michael@0 603 }
michael@0 604
michael@0 605 NS_IMETHODIMP
michael@0 606 nsMultiStateCommand::GetCommandStateParams(const char *aCommandName,
michael@0 607 nsICommandParams *aParams,
michael@0 608 nsISupports *refCon)
michael@0 609 {
michael@0 610 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 611 nsresult rv = NS_OK;
michael@0 612 if (editor)
michael@0 613 {
michael@0 614 rv = GetCurrentState(editor, aParams);
michael@0 615 }
michael@0 616 return rv;
michael@0 617 }
michael@0 618
michael@0 619 nsParagraphStateCommand::nsParagraphStateCommand()
michael@0 620 : nsMultiStateCommand()
michael@0 621 {
michael@0 622 }
michael@0 623
michael@0 624 nsresult
michael@0 625 nsParagraphStateCommand::GetCurrentState(nsIEditor *aEditor,
michael@0 626 nsICommandParams *aParams)
michael@0 627 {
michael@0 628 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 629
michael@0 630 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 631 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 632
michael@0 633 bool outMixed;
michael@0 634 nsAutoString outStateString;
michael@0 635 nsresult rv = htmlEditor->GetParagraphState(&outMixed, outStateString);
michael@0 636 if (NS_SUCCEEDED(rv))
michael@0 637 {
michael@0 638 nsAutoCString tOutStateString;
michael@0 639 tOutStateString.AssignWithConversion(outStateString);
michael@0 640 aParams->SetBooleanValue(STATE_MIXED,outMixed);
michael@0 641 aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
michael@0 642 }
michael@0 643 return rv;
michael@0 644 }
michael@0 645
michael@0 646
michael@0 647 nsresult
michael@0 648 nsParagraphStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
michael@0 649 {
michael@0 650 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 651 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 652 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 653
michael@0 654 return htmlEditor->SetParagraphFormat(newState);
michael@0 655 }
michael@0 656
michael@0 657 nsFontFaceStateCommand::nsFontFaceStateCommand()
michael@0 658 : nsMultiStateCommand()
michael@0 659 {
michael@0 660 }
michael@0 661
michael@0 662 nsresult
michael@0 663 nsFontFaceStateCommand::GetCurrentState(nsIEditor *aEditor,
michael@0 664 nsICommandParams *aParams)
michael@0 665 {
michael@0 666 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 667 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 668 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 669
michael@0 670 nsAutoString outStateString;
michael@0 671 bool outMixed;
michael@0 672 nsresult rv = htmlEditor->GetFontFaceState(&outMixed, outStateString);
michael@0 673 if (NS_SUCCEEDED(rv))
michael@0 674 {
michael@0 675 aParams->SetBooleanValue(STATE_MIXED,outMixed);
michael@0 676 aParams->SetCStringValue(STATE_ATTRIBUTE, NS_ConvertUTF16toUTF8(outStateString).get());
michael@0 677 }
michael@0 678 return rv;
michael@0 679 }
michael@0 680
michael@0 681
michael@0 682 nsresult
michael@0 683 nsFontFaceStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
michael@0 684 {
michael@0 685 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 686 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 687 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 688
michael@0 689 if (newState.EqualsLiteral("tt")) {
michael@0 690 // The old "teletype" attribute
michael@0 691 nsresult rv = htmlEditor->SetInlineProperty(nsGkAtoms::tt, EmptyString(),
michael@0 692 EmptyString());
michael@0 693 NS_ENSURE_SUCCESS(rv, rv);
michael@0 694 // Clear existing font face
michael@0 695 return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
michael@0 696 NS_LITERAL_STRING("face"));
michael@0 697 }
michael@0 698
michael@0 699 // Remove any existing TT nodes
michael@0 700 nsresult rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::tt, EmptyString());
michael@0 701 NS_ENSURE_SUCCESS(rv, rv);
michael@0 702
michael@0 703 if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
michael@0 704 return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
michael@0 705 NS_LITERAL_STRING("face"));
michael@0 706 }
michael@0 707
michael@0 708 return htmlEditor->SetInlineProperty(nsGkAtoms::font,
michael@0 709 NS_LITERAL_STRING("face"), newState);
michael@0 710 }
michael@0 711
michael@0 712 nsFontSizeStateCommand::nsFontSizeStateCommand()
michael@0 713 : nsMultiStateCommand()
michael@0 714 {
michael@0 715 }
michael@0 716
michael@0 717 // nsAutoCString tOutStateString;
michael@0 718 // tOutStateString.AssignWithConversion(outStateString);
michael@0 719 nsresult
michael@0 720 nsFontSizeStateCommand::GetCurrentState(nsIEditor *aEditor,
michael@0 721 nsICommandParams *aParams)
michael@0 722 {
michael@0 723 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 724 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 725 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_INVALID_ARG);
michael@0 726
michael@0 727 nsAutoString outStateString;
michael@0 728 nsCOMPtr<nsIAtom> fontAtom = do_GetAtom("font");
michael@0 729 bool firstHas, anyHas, allHas;
michael@0 730 nsresult rv = htmlEditor->GetInlinePropertyWithAttrValue(fontAtom,
michael@0 731 NS_LITERAL_STRING("size"),
michael@0 732 EmptyString(),
michael@0 733 &firstHas, &anyHas, &allHas,
michael@0 734 outStateString);
michael@0 735 NS_ENSURE_SUCCESS(rv, rv);
michael@0 736
michael@0 737 nsAutoCString tOutStateString;
michael@0 738 tOutStateString.AssignWithConversion(outStateString);
michael@0 739 aParams->SetBooleanValue(STATE_MIXED, anyHas && !allHas);
michael@0 740 aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
michael@0 741 aParams->SetBooleanValue(STATE_ENABLED, true);
michael@0 742
michael@0 743 return rv;
michael@0 744 }
michael@0 745
michael@0 746
michael@0 747 // acceptable values for "newState" are:
michael@0 748 // -2
michael@0 749 // -1
michael@0 750 // 0
michael@0 751 // +1
michael@0 752 // +2
michael@0 753 // +3
michael@0 754 // medium
michael@0 755 // normal
michael@0 756 nsresult
michael@0 757 nsFontSizeStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
michael@0 758 {
michael@0 759 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 760 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 761 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_INVALID_ARG);
michael@0 762
michael@0 763 if (!newState.IsEmpty() &&
michael@0 764 !newState.EqualsLiteral("normal") &&
michael@0 765 !newState.EqualsLiteral("medium")) {
michael@0 766 return htmlEditor->SetInlineProperty(nsGkAtoms::font,
michael@0 767 NS_LITERAL_STRING("size"), newState);
michael@0 768 }
michael@0 769
michael@0 770 // remove any existing font size, big or small
michael@0 771 nsresult rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
michael@0 772 NS_LITERAL_STRING("size"));
michael@0 773 NS_ENSURE_SUCCESS(rv, rv);
michael@0 774
michael@0 775 rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::big, EmptyString());
michael@0 776 NS_ENSURE_SUCCESS(rv, rv);
michael@0 777
michael@0 778 return htmlEditor->RemoveInlineProperty(nsGkAtoms::small, EmptyString());
michael@0 779 }
michael@0 780
michael@0 781 nsFontColorStateCommand::nsFontColorStateCommand()
michael@0 782 : nsMultiStateCommand()
michael@0 783 {
michael@0 784 }
michael@0 785
michael@0 786 nsresult
michael@0 787 nsFontColorStateCommand::GetCurrentState(nsIEditor *aEditor,
michael@0 788 nsICommandParams *aParams)
michael@0 789 {
michael@0 790 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 791
michael@0 792 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 793 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 794
michael@0 795 bool outMixed;
michael@0 796 nsAutoString outStateString;
michael@0 797 nsresult rv = htmlEditor->GetFontColorState(&outMixed, outStateString);
michael@0 798 NS_ENSURE_SUCCESS(rv, rv);
michael@0 799
michael@0 800 nsAutoCString tOutStateString;
michael@0 801 tOutStateString.AssignWithConversion(outStateString);
michael@0 802 aParams->SetBooleanValue(STATE_MIXED, outMixed);
michael@0 803 aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
michael@0 804 return NS_OK;
michael@0 805 }
michael@0 806
michael@0 807 nsresult
michael@0 808 nsFontColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
michael@0 809 {
michael@0 810 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 811 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 812 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 813
michael@0 814 if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
michael@0 815 return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
michael@0 816 NS_LITERAL_STRING("color"));
michael@0 817 }
michael@0 818
michael@0 819 return htmlEditor->SetInlineProperty(nsGkAtoms::font,
michael@0 820 NS_LITERAL_STRING("color"), newState);
michael@0 821 }
michael@0 822
michael@0 823 nsHighlightColorStateCommand::nsHighlightColorStateCommand()
michael@0 824 : nsMultiStateCommand()
michael@0 825 {
michael@0 826 }
michael@0 827
michael@0 828 nsresult
michael@0 829 nsHighlightColorStateCommand::GetCurrentState(nsIEditor *aEditor,
michael@0 830 nsICommandParams *aParams)
michael@0 831 {
michael@0 832 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 833 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 834 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 835
michael@0 836 bool outMixed;
michael@0 837 nsAutoString outStateString;
michael@0 838 nsresult rv = htmlEditor->GetHighlightColorState(&outMixed, outStateString);
michael@0 839 NS_ENSURE_SUCCESS(rv, rv);
michael@0 840
michael@0 841 nsAutoCString tOutStateString;
michael@0 842 tOutStateString.AssignWithConversion(outStateString);
michael@0 843 aParams->SetBooleanValue(STATE_MIXED, outMixed);
michael@0 844 aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
michael@0 845 return NS_OK;
michael@0 846 }
michael@0 847
michael@0 848 nsresult
michael@0 849 nsHighlightColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
michael@0 850 {
michael@0 851 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 852 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 853 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 854
michael@0 855 if (newState.IsEmpty() || newState.EqualsLiteral("normal")) {
michael@0 856 return htmlEditor->RemoveInlineProperty(nsGkAtoms::font,
michael@0 857 NS_LITERAL_STRING("bgcolor"));
michael@0 858 }
michael@0 859
michael@0 860 return htmlEditor->SetInlineProperty(nsGkAtoms::font,
michael@0 861 NS_LITERAL_STRING("bgcolor"),
michael@0 862 newState);
michael@0 863 }
michael@0 864
michael@0 865 NS_IMETHODIMP
michael@0 866 nsHighlightColorStateCommand::IsCommandEnabled(const char * aCommandName,
michael@0 867 nsISupports *refCon,
michael@0 868 bool *outCmdEnabled)
michael@0 869 {
michael@0 870 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 871 if (editor)
michael@0 872 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 873
michael@0 874 *outCmdEnabled = false;
michael@0 875 return NS_OK;
michael@0 876 }
michael@0 877
michael@0 878
michael@0 879 nsBackgroundColorStateCommand::nsBackgroundColorStateCommand()
michael@0 880 : nsMultiStateCommand()
michael@0 881 {
michael@0 882 }
michael@0 883
michael@0 884 nsresult
michael@0 885 nsBackgroundColorStateCommand::GetCurrentState(nsIEditor *aEditor,
michael@0 886 nsICommandParams *aParams)
michael@0 887 {
michael@0 888 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 889
michael@0 890 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 891 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 892
michael@0 893 bool outMixed;
michael@0 894 nsAutoString outStateString;
michael@0 895 nsresult rv = htmlEditor->GetBackgroundColorState(&outMixed, outStateString);
michael@0 896 NS_ENSURE_SUCCESS(rv, rv);
michael@0 897
michael@0 898 nsAutoCString tOutStateString;
michael@0 899 tOutStateString.AssignWithConversion(outStateString);
michael@0 900 aParams->SetBooleanValue(STATE_MIXED, outMixed);
michael@0 901 aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
michael@0 902 return NS_OK;
michael@0 903 }
michael@0 904
michael@0 905 nsresult
michael@0 906 nsBackgroundColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState)
michael@0 907 {
michael@0 908 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 909
michael@0 910 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 911 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 912
michael@0 913 return htmlEditor->SetBackgroundColor(newState);
michael@0 914 }
michael@0 915
michael@0 916 nsAlignCommand::nsAlignCommand()
michael@0 917 : nsMultiStateCommand()
michael@0 918 {
michael@0 919 }
michael@0 920
michael@0 921 nsresult
michael@0 922 nsAlignCommand::GetCurrentState(nsIEditor *aEditor, nsICommandParams *aParams)
michael@0 923 {
michael@0 924 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 925
michael@0 926 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 927 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 928
michael@0 929 nsIHTMLEditor::EAlignment firstAlign;
michael@0 930 bool outMixed;
michael@0 931 nsresult rv = htmlEditor->GetAlignment(&outMixed, &firstAlign);
michael@0 932
michael@0 933 NS_ENSURE_SUCCESS(rv, rv);
michael@0 934
michael@0 935 nsAutoString outStateString;
michael@0 936 switch (firstAlign)
michael@0 937 {
michael@0 938 default:
michael@0 939 case nsIHTMLEditor::eLeft:
michael@0 940 outStateString.AssignLiteral("left");
michael@0 941 break;
michael@0 942
michael@0 943 case nsIHTMLEditor::eCenter:
michael@0 944 outStateString.AssignLiteral("center");
michael@0 945 break;
michael@0 946
michael@0 947 case nsIHTMLEditor::eRight:
michael@0 948 outStateString.AssignLiteral("right");
michael@0 949 break;
michael@0 950
michael@0 951 case nsIHTMLEditor::eJustify:
michael@0 952 outStateString.AssignLiteral("justify");
michael@0 953 break;
michael@0 954 }
michael@0 955 nsAutoCString tOutStateString;
michael@0 956 tOutStateString.AssignWithConversion(outStateString);
michael@0 957 aParams->SetBooleanValue(STATE_MIXED,outMixed);
michael@0 958 aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get());
michael@0 959 return NS_OK;
michael@0 960 }
michael@0 961
michael@0 962 nsresult
michael@0 963 nsAlignCommand::SetState(nsIEditor *aEditor, nsString& newState)
michael@0 964 {
michael@0 965 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 966
michael@0 967 nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 968 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 969
michael@0 970 return htmlEditor->Align(newState);
michael@0 971 }
michael@0 972
michael@0 973 nsAbsolutePositioningCommand::nsAbsolutePositioningCommand()
michael@0 974 : nsBaseStateUpdatingCommand(nsGkAtoms::_empty)
michael@0 975 {
michael@0 976 }
michael@0 977
michael@0 978 NS_IMETHODIMP
michael@0 979 nsAbsolutePositioningCommand::IsCommandEnabled(const char * aCommandName,
michael@0 980 nsISupports *aCommandRefCon,
michael@0 981 bool *outCmdEnabled)
michael@0 982 {
michael@0 983 nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
michael@0 984 nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(aCommandRefCon);
michael@0 985 if (htmlEditor)
michael@0 986 {
michael@0 987 bool isEditable = false;
michael@0 988 nsresult rv = editor->GetIsSelectionEditable(&isEditable);
michael@0 989 NS_ENSURE_SUCCESS(rv, rv);
michael@0 990 if (isEditable)
michael@0 991 return htmlEditor->GetAbsolutePositioningEnabled(outCmdEnabled);
michael@0 992 }
michael@0 993
michael@0 994 *outCmdEnabled = false;
michael@0 995 return NS_OK;
michael@0 996 }
michael@0 997
michael@0 998 nsresult
michael@0 999 nsAbsolutePositioningCommand::GetCurrentState(nsIEditor *aEditor, nsICommandParams *aParams)
michael@0 1000 {
michael@0 1001 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 1002
michael@0 1003 nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 1004 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 1005
michael@0 1006 bool isEnabled;
michael@0 1007 htmlEditor->GetAbsolutePositioningEnabled(&isEnabled);
michael@0 1008 if (!isEnabled) {
michael@0 1009 aParams->SetBooleanValue(STATE_MIXED,false);
michael@0 1010 aParams->SetCStringValue(STATE_ATTRIBUTE, "");
michael@0 1011 return NS_OK;
michael@0 1012 }
michael@0 1013
michael@0 1014 nsCOMPtr<nsIDOMElement> elt;
michael@0 1015 nsresult rv = htmlEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
michael@0 1016 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1017
michael@0 1018 nsAutoString outStateString;
michael@0 1019 if (elt)
michael@0 1020 outStateString.AssignLiteral("absolute");
michael@0 1021
michael@0 1022 aParams->SetBooleanValue(STATE_MIXED,false);
michael@0 1023 aParams->SetCStringValue(STATE_ATTRIBUTE, NS_ConvertUTF16toUTF8(outStateString).get());
michael@0 1024 return NS_OK;
michael@0 1025 }
michael@0 1026
michael@0 1027 nsresult
michael@0 1028 nsAbsolutePositioningCommand::ToggleState(nsIEditor *aEditor)
michael@0 1029 {
michael@0 1030 NS_ASSERTION(aEditor, "Need an editor here");
michael@0 1031
michael@0 1032 nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(aEditor);
michael@0 1033 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 1034
michael@0 1035 nsCOMPtr<nsIDOMElement> elt;
michael@0 1036 nsresult rv = htmlEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt));
michael@0 1037 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1038
michael@0 1039 return htmlEditor->AbsolutePositionSelection(!elt);
michael@0 1040 }
michael@0 1041
michael@0 1042
michael@0 1043 NS_IMETHODIMP
michael@0 1044 nsDecreaseZIndexCommand::IsCommandEnabled(const char * aCommandName,
michael@0 1045 nsISupports *refCon,
michael@0 1046 bool *outCmdEnabled)
michael@0 1047 {
michael@0 1048 nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 1049 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 1050
michael@0 1051 htmlEditor->GetAbsolutePositioningEnabled(outCmdEnabled);
michael@0 1052 if (!(*outCmdEnabled))
michael@0 1053 return NS_OK;
michael@0 1054
michael@0 1055 nsCOMPtr<nsIDOMElement> positionedElement;
michael@0 1056 htmlEditor->GetPositionedElement(getter_AddRefs(positionedElement));
michael@0 1057 *outCmdEnabled = false;
michael@0 1058 if (positionedElement) {
michael@0 1059 int32_t z;
michael@0 1060 nsresult res = htmlEditor->GetElementZIndex(positionedElement, &z);
michael@0 1061 NS_ENSURE_SUCCESS(res, res);
michael@0 1062 *outCmdEnabled = (z > 0);
michael@0 1063 }
michael@0 1064
michael@0 1065 return NS_OK;
michael@0 1066 }
michael@0 1067
michael@0 1068 NS_IMETHODIMP
michael@0 1069 nsDecreaseZIndexCommand::DoCommand(const char *aCommandName,
michael@0 1070 nsISupports *refCon)
michael@0 1071 {
michael@0 1072 nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 1073 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED);
michael@0 1074
michael@0 1075 return htmlEditor->RelativeChangeZIndex(-1);
michael@0 1076 }
michael@0 1077
michael@0 1078 NS_IMETHODIMP
michael@0 1079 nsDecreaseZIndexCommand::DoCommandParams(const char *aCommandName,
michael@0 1080 nsICommandParams *aParams,
michael@0 1081 nsISupports *refCon)
michael@0 1082 {
michael@0 1083 return DoCommand(aCommandName, refCon);
michael@0 1084 }
michael@0 1085
michael@0 1086 NS_IMETHODIMP
michael@0 1087 nsDecreaseZIndexCommand::GetCommandStateParams(const char *aCommandName,
michael@0 1088 nsICommandParams *aParams,
michael@0 1089 nsISupports *refCon)
michael@0 1090 {
michael@0 1091 NS_ENSURE_ARG_POINTER(aParams);
michael@0 1092
michael@0 1093 bool enabled = false;
michael@0 1094 nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
michael@0 1095 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1096
michael@0 1097 return aParams->SetBooleanValue(STATE_ENABLED, enabled);
michael@0 1098 }
michael@0 1099
michael@0 1100 NS_IMETHODIMP
michael@0 1101 nsIncreaseZIndexCommand::IsCommandEnabled(const char * aCommandName,
michael@0 1102 nsISupports *refCon,
michael@0 1103 bool *outCmdEnabled)
michael@0 1104 {
michael@0 1105 nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 1106 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE);
michael@0 1107
michael@0 1108 htmlEditor->GetAbsolutePositioningEnabled(outCmdEnabled);
michael@0 1109 if (!(*outCmdEnabled))
michael@0 1110 return NS_OK;
michael@0 1111
michael@0 1112 nsCOMPtr<nsIDOMElement> positionedElement;
michael@0 1113 htmlEditor->GetPositionedElement(getter_AddRefs(positionedElement));
michael@0 1114 *outCmdEnabled = (nullptr != positionedElement);
michael@0 1115 return NS_OK;
michael@0 1116 }
michael@0 1117
michael@0 1118 NS_IMETHODIMP
michael@0 1119 nsIncreaseZIndexCommand::DoCommand(const char *aCommandName,
michael@0 1120 nsISupports *refCon)
michael@0 1121 {
michael@0 1122 nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon);
michael@0 1123 NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED);
michael@0 1124
michael@0 1125 return htmlEditor->RelativeChangeZIndex(1);
michael@0 1126 }
michael@0 1127
michael@0 1128 NS_IMETHODIMP
michael@0 1129 nsIncreaseZIndexCommand::DoCommandParams(const char *aCommandName,
michael@0 1130 nsICommandParams *aParams,
michael@0 1131 nsISupports *refCon)
michael@0 1132 {
michael@0 1133 return DoCommand(aCommandName, refCon);
michael@0 1134 }
michael@0 1135
michael@0 1136 NS_IMETHODIMP
michael@0 1137 nsIncreaseZIndexCommand::GetCommandStateParams(const char *aCommandName,
michael@0 1138 nsICommandParams *aParams,
michael@0 1139 nsISupports *refCon)
michael@0 1140 {
michael@0 1141 NS_ENSURE_ARG_POINTER(aParams);
michael@0 1142
michael@0 1143 bool enabled = false;
michael@0 1144 nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
michael@0 1145 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1146
michael@0 1147 return aParams->SetBooleanValue(STATE_ENABLED, enabled);
michael@0 1148 }
michael@0 1149
michael@0 1150
michael@0 1151 NS_IMETHODIMP
michael@0 1152 nsRemoveStylesCommand::IsCommandEnabled(const char * aCommandName,
michael@0 1153 nsISupports *refCon,
michael@0 1154 bool *outCmdEnabled)
michael@0 1155 {
michael@0 1156 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 1157 // test if we have any styles?
michael@0 1158 if (editor)
michael@0 1159 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 1160
michael@0 1161 *outCmdEnabled = false;
michael@0 1162 return NS_OK;
michael@0 1163 }
michael@0 1164
michael@0 1165
michael@0 1166
michael@0 1167 NS_IMETHODIMP
michael@0 1168 nsRemoveStylesCommand::DoCommand(const char *aCommandName,
michael@0 1169 nsISupports *refCon)
michael@0 1170 {
michael@0 1171 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 1172
michael@0 1173 nsresult rv = NS_OK;
michael@0 1174 if (editor)
michael@0 1175 {
michael@0 1176 rv = editor->RemoveAllInlineProperties();
michael@0 1177 }
michael@0 1178
michael@0 1179 return rv;
michael@0 1180 }
michael@0 1181
michael@0 1182 NS_IMETHODIMP
michael@0 1183 nsRemoveStylesCommand::DoCommandParams(const char *aCommandName,
michael@0 1184 nsICommandParams *aParams,
michael@0 1185 nsISupports *refCon)
michael@0 1186 {
michael@0 1187 return DoCommand(aCommandName, refCon);
michael@0 1188 }
michael@0 1189
michael@0 1190 NS_IMETHODIMP
michael@0 1191 nsRemoveStylesCommand::GetCommandStateParams(const char *aCommandName,
michael@0 1192 nsICommandParams *aParams,
michael@0 1193 nsISupports *refCon)
michael@0 1194 {
michael@0 1195 bool outCmdEnabled = false;
michael@0 1196 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 1197 return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled);
michael@0 1198 }
michael@0 1199
michael@0 1200 NS_IMETHODIMP
michael@0 1201 nsIncreaseFontSizeCommand::IsCommandEnabled(const char * aCommandName,
michael@0 1202 nsISupports *refCon,
michael@0 1203 bool *outCmdEnabled)
michael@0 1204 {
michael@0 1205 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 1206 // test if we are at max size?
michael@0 1207 if (editor)
michael@0 1208 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 1209
michael@0 1210 *outCmdEnabled = false;
michael@0 1211 return NS_OK;
michael@0 1212 }
michael@0 1213
michael@0 1214
michael@0 1215 NS_IMETHODIMP
michael@0 1216 nsIncreaseFontSizeCommand::DoCommand(const char *aCommandName,
michael@0 1217 nsISupports *refCon)
michael@0 1218 {
michael@0 1219 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 1220
michael@0 1221 nsresult rv = NS_OK;
michael@0 1222 if (editor)
michael@0 1223 {
michael@0 1224 rv = editor->IncreaseFontSize();
michael@0 1225 }
michael@0 1226
michael@0 1227 return rv;
michael@0 1228 }
michael@0 1229
michael@0 1230 NS_IMETHODIMP
michael@0 1231 nsIncreaseFontSizeCommand::DoCommandParams(const char *aCommandName,
michael@0 1232 nsICommandParams *aParams,
michael@0 1233 nsISupports *refCon)
michael@0 1234 {
michael@0 1235 return DoCommand(aCommandName, refCon);
michael@0 1236 }
michael@0 1237
michael@0 1238 NS_IMETHODIMP
michael@0 1239 nsIncreaseFontSizeCommand::GetCommandStateParams(const char *aCommandName,
michael@0 1240 nsICommandParams *aParams,
michael@0 1241 nsISupports *refCon)
michael@0 1242 {
michael@0 1243 bool outCmdEnabled = false;
michael@0 1244 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 1245 return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled);
michael@0 1246 }
michael@0 1247
michael@0 1248 NS_IMETHODIMP
michael@0 1249 nsDecreaseFontSizeCommand::IsCommandEnabled(const char * aCommandName,
michael@0 1250 nsISupports *refCon,
michael@0 1251 bool *outCmdEnabled)
michael@0 1252 {
michael@0 1253 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 1254 // test if we are at min size?
michael@0 1255 if (editor)
michael@0 1256 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 1257
michael@0 1258 *outCmdEnabled = false;
michael@0 1259 return NS_OK;
michael@0 1260 }
michael@0 1261
michael@0 1262
michael@0 1263 NS_IMETHODIMP
michael@0 1264 nsDecreaseFontSizeCommand::DoCommand(const char *aCommandName,
michael@0 1265 nsISupports *refCon)
michael@0 1266 {
michael@0 1267 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 1268
michael@0 1269 nsresult rv = NS_OK;
michael@0 1270 if (editor)
michael@0 1271 {
michael@0 1272 rv = editor->DecreaseFontSize();
michael@0 1273 }
michael@0 1274
michael@0 1275 return rv;
michael@0 1276 }
michael@0 1277
michael@0 1278 NS_IMETHODIMP
michael@0 1279 nsDecreaseFontSizeCommand::DoCommandParams(const char *aCommandName,
michael@0 1280 nsICommandParams *aParams,
michael@0 1281 nsISupports *refCon)
michael@0 1282 {
michael@0 1283 return DoCommand(aCommandName, refCon);
michael@0 1284 }
michael@0 1285
michael@0 1286 NS_IMETHODIMP
michael@0 1287 nsDecreaseFontSizeCommand::GetCommandStateParams(const char *aCommandName,
michael@0 1288 nsICommandParams *aParams,
michael@0 1289 nsISupports *refCon)
michael@0 1290 {
michael@0 1291 bool outCmdEnabled = false;
michael@0 1292 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 1293 return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled);
michael@0 1294 }
michael@0 1295
michael@0 1296 NS_IMETHODIMP
michael@0 1297 nsInsertHTMLCommand::IsCommandEnabled(const char * aCommandName,
michael@0 1298 nsISupports *refCon,
michael@0 1299 bool *outCmdEnabled)
michael@0 1300 {
michael@0 1301 NS_ENSURE_ARG_POINTER(outCmdEnabled);
michael@0 1302 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 1303 if (editor)
michael@0 1304 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 1305
michael@0 1306 *outCmdEnabled = false;
michael@0 1307 return NS_OK;
michael@0 1308 }
michael@0 1309
michael@0 1310
michael@0 1311 NS_IMETHODIMP
michael@0 1312 nsInsertHTMLCommand::DoCommand(const char *aCommandName, nsISupports *refCon)
michael@0 1313 {
michael@0 1314 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 1315 }
michael@0 1316
michael@0 1317 NS_IMETHODIMP
michael@0 1318 nsInsertHTMLCommand::DoCommandParams(const char *aCommandName,
michael@0 1319 nsICommandParams *aParams,
michael@0 1320 nsISupports *refCon)
michael@0 1321 {
michael@0 1322 NS_ENSURE_ARG_POINTER(aParams);
michael@0 1323 NS_ENSURE_ARG_POINTER(refCon);
michael@0 1324
michael@0 1325 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 1326 NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
michael@0 1327
michael@0 1328 // Get HTML source string to insert from command params
michael@0 1329 nsAutoString html;
michael@0 1330 nsresult rv = aParams->GetStringValue(STATE_DATA, html);
michael@0 1331 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1332
michael@0 1333 if (!html.IsEmpty())
michael@0 1334 return editor->InsertHTML(html);
michael@0 1335
michael@0 1336 return NS_OK;
michael@0 1337 }
michael@0 1338
michael@0 1339 NS_IMETHODIMP
michael@0 1340 nsInsertHTMLCommand::GetCommandStateParams(const char *aCommandName,
michael@0 1341 nsICommandParams *aParams,
michael@0 1342 nsISupports *refCon)
michael@0 1343 {
michael@0 1344 NS_ENSURE_ARG_POINTER(aParams);
michael@0 1345 NS_ENSURE_ARG_POINTER(refCon);
michael@0 1346
michael@0 1347 bool outCmdEnabled = false;
michael@0 1348 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 1349 return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
michael@0 1350 }
michael@0 1351
michael@0 1352 NS_IMPL_ISUPPORTS_INHERITED0(nsInsertTagCommand, nsBaseComposerCommand)
michael@0 1353
michael@0 1354 nsInsertTagCommand::nsInsertTagCommand(nsIAtom* aTagName)
michael@0 1355 : nsBaseComposerCommand()
michael@0 1356 , mTagName(aTagName)
michael@0 1357 {
michael@0 1358 MOZ_ASSERT(mTagName);
michael@0 1359 }
michael@0 1360
michael@0 1361 nsInsertTagCommand::~nsInsertTagCommand()
michael@0 1362 {
michael@0 1363 }
michael@0 1364
michael@0 1365 NS_IMETHODIMP
michael@0 1366 nsInsertTagCommand::IsCommandEnabled(const char * aCommandName,
michael@0 1367 nsISupports *refCon,
michael@0 1368 bool *outCmdEnabled)
michael@0 1369 {
michael@0 1370 NS_ENSURE_ARG_POINTER(outCmdEnabled);
michael@0 1371 nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
michael@0 1372 if (editor)
michael@0 1373 return editor->GetIsSelectionEditable(outCmdEnabled);
michael@0 1374
michael@0 1375 *outCmdEnabled = false;
michael@0 1376 return NS_OK;
michael@0 1377 }
michael@0 1378
michael@0 1379
michael@0 1380 // corresponding STATE_ATTRIBUTE is: src (img) and href (a)
michael@0 1381 NS_IMETHODIMP
michael@0 1382 nsInsertTagCommand::DoCommand(const char *aCmdName, nsISupports *refCon)
michael@0 1383 {
michael@0 1384 NS_ENSURE_TRUE(mTagName == nsGkAtoms::hr, NS_ERROR_NOT_IMPLEMENTED);
michael@0 1385
michael@0 1386 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 1387 NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
michael@0 1388
michael@0 1389 nsCOMPtr<nsIDOMElement> domElem;
michael@0 1390 nsresult rv = editor->CreateElementWithDefaults(
michael@0 1391 nsDependentAtomString(mTagName), getter_AddRefs(domElem));
michael@0 1392 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1393
michael@0 1394 return editor->InsertElementAtSelection(domElem, true);
michael@0 1395 }
michael@0 1396
michael@0 1397 NS_IMETHODIMP
michael@0 1398 nsInsertTagCommand::DoCommandParams(const char *aCommandName,
michael@0 1399 nsICommandParams *aParams,
michael@0 1400 nsISupports *refCon)
michael@0 1401 {
michael@0 1402 NS_ENSURE_ARG_POINTER(refCon);
michael@0 1403
michael@0 1404 // inserting an hr shouldn't have an parameters, just call DoCommand for that
michael@0 1405 if (mTagName == nsGkAtoms::hr) {
michael@0 1406 return DoCommand(aCommandName, refCon);
michael@0 1407 }
michael@0 1408
michael@0 1409 NS_ENSURE_ARG_POINTER(aParams);
michael@0 1410
michael@0 1411 nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon);
michael@0 1412 NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED);
michael@0 1413
michael@0 1414 // do we have an href to use for creating link?
michael@0 1415 nsXPIDLCString s;
michael@0 1416 nsresult rv = aParams->GetCStringValue(STATE_ATTRIBUTE, getter_Copies(s));
michael@0 1417 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1418 nsAutoString attrib; attrib.AssignWithConversion(s);
michael@0 1419
michael@0 1420 if (attrib.IsEmpty())
michael@0 1421 return NS_ERROR_INVALID_ARG;
michael@0 1422
michael@0 1423 // filter out tags we don't know how to insert
michael@0 1424 nsAutoString attributeType;
michael@0 1425 if (mTagName == nsGkAtoms::a) {
michael@0 1426 attributeType.AssignLiteral("href");
michael@0 1427 } else if (mTagName == nsGkAtoms::img) {
michael@0 1428 attributeType.AssignLiteral("src");
michael@0 1429 } else {
michael@0 1430 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 1431 }
michael@0 1432
michael@0 1433 nsCOMPtr<nsIDOMElement> domElem;
michael@0 1434 rv = editor->CreateElementWithDefaults(nsDependentAtomString(mTagName),
michael@0 1435 getter_AddRefs(domElem));
michael@0 1436 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1437
michael@0 1438 rv = domElem->SetAttribute(attributeType, attrib);
michael@0 1439 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1440
michael@0 1441 // do actual insertion
michael@0 1442 if (mTagName == nsGkAtoms::a)
michael@0 1443 return editor->InsertLinkAroundSelection(domElem);
michael@0 1444
michael@0 1445 return editor->InsertElementAtSelection(domElem, true);
michael@0 1446 }
michael@0 1447
michael@0 1448 NS_IMETHODIMP
michael@0 1449 nsInsertTagCommand::GetCommandStateParams(const char *aCommandName,
michael@0 1450 nsICommandParams *aParams,
michael@0 1451 nsISupports *refCon)
michael@0 1452 {
michael@0 1453 NS_ENSURE_ARG_POINTER(aParams);
michael@0 1454 NS_ENSURE_ARG_POINTER(refCon);
michael@0 1455
michael@0 1456 bool outCmdEnabled = false;
michael@0 1457 IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
michael@0 1458 return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
michael@0 1459 }
michael@0 1460
michael@0 1461
michael@0 1462 /****************************/
michael@0 1463 //HELPER METHODS
michael@0 1464 /****************************/
michael@0 1465
michael@0 1466 nsresult
michael@0 1467 GetListState(nsIHTMLEditor* aEditor, bool* aMixed, nsAString& aLocalName)
michael@0 1468 {
michael@0 1469 MOZ_ASSERT(aEditor);
michael@0 1470 MOZ_ASSERT(aMixed);
michael@0 1471
michael@0 1472 *aMixed = false;
michael@0 1473 aLocalName.Truncate();
michael@0 1474
michael@0 1475 bool bOL, bUL, bDL;
michael@0 1476 nsresult rv = aEditor->GetListState(aMixed, &bOL, &bUL, &bDL);
michael@0 1477 NS_ENSURE_SUCCESS(rv, rv);
michael@0 1478
michael@0 1479 if (*aMixed) {
michael@0 1480 return NS_OK;
michael@0 1481 }
michael@0 1482
michael@0 1483 if (bOL) {
michael@0 1484 aLocalName.AssignLiteral("ol");
michael@0 1485 } else if (bUL) {
michael@0 1486 aLocalName.AssignLiteral("ul");
michael@0 1487 } else if (bDL) {
michael@0 1488 aLocalName.AssignLiteral("dl");
michael@0 1489 }
michael@0 1490 return NS_OK;
michael@0 1491 }
michael@0 1492
michael@0 1493 nsresult
michael@0 1494 RemoveOneProperty(nsIHTMLEditor* aEditor, const nsAString& aProp)
michael@0 1495 {
michael@0 1496 MOZ_ASSERT(aEditor);
michael@0 1497
michael@0 1498 /// XXX Hack alert! Look in nsIEditProperty.h for this
michael@0 1499 nsCOMPtr<nsIAtom> styleAtom = do_GetAtom(aProp);
michael@0 1500 NS_ENSURE_TRUE(styleAtom, NS_ERROR_OUT_OF_MEMORY);
michael@0 1501
michael@0 1502 return aEditor->RemoveInlineProperty(styleAtom, EmptyString());
michael@0 1503 }
michael@0 1504
michael@0 1505
michael@0 1506 // the name of the attribute here should be the contents of the appropriate
michael@0 1507 // tag, e.g. 'b' for bold, 'i' for italics.
michael@0 1508 nsresult
michael@0 1509 RemoveTextProperty(nsIHTMLEditor* aEditor, const nsAString& aProp)
michael@0 1510 {
michael@0 1511 MOZ_ASSERT(aEditor);
michael@0 1512
michael@0 1513 if (aProp.LowerCaseEqualsLiteral("all")) {
michael@0 1514 return aEditor->RemoveAllInlineProperties();
michael@0 1515 }
michael@0 1516
michael@0 1517 return RemoveOneProperty(aEditor, aProp);
michael@0 1518 }
michael@0 1519
michael@0 1520 // the name of the attribute here should be the contents of the appropriate
michael@0 1521 // tag, e.g. 'b' for bold, 'i' for italics.
michael@0 1522 nsresult
michael@0 1523 SetTextProperty(nsIHTMLEditor* aEditor, const nsAString& aProp)
michael@0 1524 {
michael@0 1525 MOZ_ASSERT(aEditor);
michael@0 1526
michael@0 1527 /// XXX Hack alert! Look in nsIEditProperty.h for this
michael@0 1528 nsCOMPtr<nsIAtom> styleAtom = do_GetAtom(aProp);
michael@0 1529 NS_ENSURE_TRUE(styleAtom, NS_ERROR_OUT_OF_MEMORY);
michael@0 1530
michael@0 1531 return aEditor->SetInlineProperty(styleAtom, EmptyString(), EmptyString());
michael@0 1532 }

mercurial