2001-09-25 22:53:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
#include "SetDocTitleTxn.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "mozilla/dom/Element.h" // for Element
|
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsCOMPtr.h" // for nsCOMPtr, getter_AddRefs, etc
|
|
|
|
#include "nsDebug.h" // for NS_ENSURE_SUCCESS, etc
|
|
|
|
#include "nsError.h" // for NS_OK, NS_ERROR_FAILURE, etc
|
|
|
|
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
|
|
|
|
#include "nsIDOMDocument.h" // for nsIDOMDocument
|
|
|
|
#include "nsIDOMElement.h" // for nsIDOMElement
|
|
|
|
#include "nsIDOMNode.h" // for nsIDOMNode
|
|
|
|
#include "nsIDOMNodeList.h" // for nsIDOMNodeList
|
|
|
|
#include "nsIDOMText.h" // for nsIDOMText
|
|
|
|
#include "nsIDocument.h" // for nsIDocument
|
|
|
|
#include "nsIEditor.h" // for nsIEditor
|
|
|
|
#include "nsIHTMLEditor.h" // for nsIHTMLEditor
|
|
|
|
#include "nsLiteralString.h" // for NS_LITERAL_STRING
|
2012-05-18 08:29:40 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
// note that aEditor is not refcounted
|
|
|
|
SetDocTitleTxn::SetDocTitleTxn()
|
|
|
|
: EditTxn()
|
2011-10-17 14:59:28 +00:00
|
|
|
, mIsTransient(false)
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP SetDocTitleTxn::Init(nsIHTMLEditor *aEditor,
|
2002-03-23 22:16:54 +00:00
|
|
|
const nsAString *aValue)
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aEditor && aValue, "null args");
|
|
|
|
if (!aEditor || !aValue) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
|
|
|
|
mEditor = aEditor;
|
|
|
|
mValue = *aValue;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP SetDocTitleTxn::DoTransaction(void)
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
2008-08-18 02:10:28 +00:00
|
|
|
return SetDomTitle(mValue);
|
2001-02-16 03:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP SetDocTitleTxn::UndoTransaction(void)
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
2008-08-18 02:10:28 +00:00
|
|
|
// No extra work required; the DOM changes alone are enough
|
|
|
|
return NS_OK;
|
2001-02-16 03:15:35 +00:00
|
|
|
}
|
|
|
|
|
2001-03-09 14:23:59 +00:00
|
|
|
NS_IMETHODIMP SetDocTitleTxn::RedoTransaction(void)
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
2008-08-18 02:10:28 +00:00
|
|
|
// No extra work required; the DOM changes alone are enough
|
|
|
|
return NS_OK;
|
2001-02-16 03:15:35 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 22:16:54 +00:00
|
|
|
nsresult SetDocTitleTxn::SetDomTitle(const nsAString& aTitle)
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = do_QueryInterface(mEditor);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(editor, NS_ERROR_FAILURE);
|
2003-06-30 14:57:59 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
2001-02-16 03:15:35 +00:00
|
|
|
nsresult res = editor->GetDocument(getter_AddRefs(domDoc));
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(domDoc, NS_ERROR_FAILURE);
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNodeList> titleList;
|
|
|
|
res = domDoc->GetElementsByTagName(NS_LITERAL_STRING("title"), getter_AddRefs(titleList));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
// First assume we will NOT really do anything
|
|
|
|
// (transaction will not be pushed on stack)
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsTransient = true;
|
2001-02-16 03:15:35 +00:00
|
|
|
|
2003-06-30 14:57:59 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>titleNode;
|
2001-02-16 03:15:35 +00:00
|
|
|
if(titleList)
|
|
|
|
{
|
|
|
|
res = titleList->Item(0, getter_AddRefs(titleNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-02-16 03:15:35 +00:00
|
|
|
if (titleNode)
|
|
|
|
{
|
|
|
|
// Delete existing child textnode of title node
|
|
|
|
// (Note: all contents under a TITLE node are always in a single text node)
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
res = titleNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
if(child)
|
|
|
|
{
|
|
|
|
// Save current text as the undo value
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(child);
|
|
|
|
if(textNode)
|
|
|
|
{
|
|
|
|
textNode->GetData(mUndoValue);
|
|
|
|
|
|
|
|
// If title text is identical to what already exists,
|
|
|
|
// quit now (mIsTransient is now TRUE)
|
|
|
|
if (mUndoValue == aTitle)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
res = editor->DeleteNode(child);
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't return above, thus we really will be changing the title
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsTransient = false;
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
// Get the <HEAD> node, create a <TITLE> and insert it under the HEAD
|
2012-05-18 08:29:40 +00:00
|
|
|
nsCOMPtr<nsIDocument> document = do_QueryInterface(domDoc);
|
|
|
|
NS_ENSURE_STATE(document);
|
|
|
|
|
|
|
|
dom::Element* head = document->GetHeadElement();
|
|
|
|
NS_ENSURE_STATE(head);
|
2001-02-16 03:15:35 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool newTitleNode = false;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t newTitleIndex = 0;
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
if (!titleNode)
|
|
|
|
{
|
|
|
|
// Didn't find one above: Create a new one
|
|
|
|
nsCOMPtr<nsIDOMElement>titleElement;
|
|
|
|
res = domDoc->CreateElement(NS_LITERAL_STRING("title"), getter_AddRefs(titleElement));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(titleElement, NS_ERROR_FAILURE);
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
titleNode = do_QueryInterface(titleElement);
|
2011-10-17 14:59:28 +00:00
|
|
|
newTitleNode = true;
|
2001-02-16 03:15:35 +00:00
|
|
|
|
2012-05-18 08:29:40 +00:00
|
|
|
// Get index so we append new title node after all existing HEAD children.
|
|
|
|
newTitleIndex = head->GetChildCount();
|
2001-02-16 03:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append a text node under the TITLE
|
|
|
|
// only if the title text isn't empty
|
2003-05-23 21:34:47 +00:00
|
|
|
if (titleNode && !aTitle.IsEmpty())
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMText> textNode;
|
|
|
|
res = domDoc->CreateTextNode(aTitle, getter_AddRefs(textNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-02-16 03:15:35 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> newNode = do_QueryInterface(textNode);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(newNode, NS_ERROR_FAILURE);
|
2001-02-16 03:15:35 +00:00
|
|
|
|
|
|
|
if (newTitleNode)
|
|
|
|
{
|
|
|
|
// Not undoable: We will insert newTitleNode below
|
2003-06-30 14:57:59 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> resultNode;
|
2001-02-16 03:15:35 +00:00
|
|
|
res = titleNode->AppendChild(newNode, getter_AddRefs(resultNode));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is an undoable transaction
|
|
|
|
res = editor->InsertNode(newNode, titleNode, 0);
|
|
|
|
}
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-02-16 03:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newTitleNode)
|
|
|
|
{
|
|
|
|
// Undoable transaction to insert title+text together
|
2012-05-18 08:29:40 +00:00
|
|
|
res = editor->InsertNode(titleNode, head->AsDOMNode(), newTitleIndex);
|
2001-02-16 03:15:35 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-03-23 22:16:54 +00:00
|
|
|
NS_IMETHODIMP SetDocTitleTxn::GetTxnDescription(nsAString& aString)
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
2004-06-17 00:13:25 +00:00
|
|
|
aString.AssignLiteral("SetDocTitleTxn: ");
|
2001-03-09 14:23:59 +00:00
|
|
|
aString += mValue;
|
2001-02-16 03:15:35 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP SetDocTitleTxn::GetIsTransient(bool *aIsTransient)
|
2001-02-16 03:15:35 +00:00
|
|
|
{
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aIsTransient, NS_ERROR_NULL_POINTER);
|
2001-02-16 03:15:35 +00:00
|
|
|
*aIsTransient = mIsTransient;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-06-30 14:57:59 +00:00
|
|
|
|