Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions | Friends
ChangeCSSInlineStyleTxn Class Reference

A transaction that changes the value of a CSS inline style of a content node. More...

#include <ChangeCSSInlineStyleTxn.h>

Inheritance diagram for ChangeCSSInlineStyleTxn:
Inheritance graph
[legend]
Collaboration diagram for ChangeCSSInlineStyleTxn:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual ~ChangeCSSInlineStyleTxn ()
NS_IMETHOD Init (nsIEditor *aEditor, nsIDOMElement *aElement, nsIAtom *aProperty, const nsAString &aValue, PRBool aRemoveProperty)
 Initialize the transaction.
NS_IMETHOD AddValueToMultivalueProperty (nsAString &aValues, const nsAString &aNewValue)
 adds the value aNewValue to the list of white-space separated values aValues
NS_IMETHOD DoTransaction (void)
NS_IMETHOD UndoTransaction (void)
NS_IMETHOD RedoTransaction (void)
NS_IMETHOD Merge (nsITransaction *aTransaction, PRBool *aDidMerge)
NS_IMETHOD GetTxnDescription (nsAString &aTxnDescription)
NS_IMETHOD GetIsTransient (PRBool *aIsTransient)
void doTransaction ()
 Executes the transaction.
void undoTransaction ()
 Restores the state to what it was before the transaction was executed.
void redoTransaction ()
 Executes the transaction again.
boolean merge (in nsITransaction aTransaction)
 Attempts to merge a transaction into "this" transaction.

Static Public Member Functions

static const nsIIDGetCID ()
static PRBool ValueIncludes (const nsAString &aValueList, const nsAString &aValue, PRBool aCaseSensitive)
 returns true if the list of white-space separated values contains aValue

Public Attributes

readonly attribute boolean isTransient
 The transaction's transient state.
readonly attribute DOMString txnDescription

Protected Attributes

nsIEditormEditor
 the editor that created this transaction
nsCOMPtr< nsIDOMElementmElement
 the element to operate upon
nsIAtommProperty
 the CSS property to change
nsString mValue
 the value to set the property to (ignored if mRemoveProperty==PR_TRUE)
nsString mUndoValue
 the value to set the property to for undo
nsString mRedoValue
 the value to set the property to for redo
PRBool mUndoAttributeWasSet
 true if the style attribute was present and not empty before DoTransaction
PRBool mRedoAttributeWasSet
 true if the style attribute is present and not empty after DoTransaction
PRBool mRemoveProperty
 PR_TRUE if the operation is to remove mProperty from mElement.

Private Member Functions

 ChangeCSSInlineStyleTxn ()
PRBool AcceptsMoreThanOneValue (nsIAtom *aCSSProperty)
 returns true if the property accepts more than one value
void RemoveValueFromListOfValues (nsAString &aValues, const nsAString &aRemoveValue)
 remove a value from a list of white-space separated values
void AppendDeclaration (nsAString &aOutputString, const nsAString &aProperty, const nsAString &aValues)
 appends 'property : value' to the string
nsresult SetStyle (PRBool aAttributeWasSet, nsAString &aValue)
 If the boolean is true and if the value is not the empty string, set the property in the transaction to that value; if the value is empty, remove the property from element's styles.

Friends

class TransactionFactory

Detailed Description

A transaction that changes the value of a CSS inline style of a content node.

This transaction covers add, remove, and change a property's value.

Definition at line 56 of file ChangeCSSInlineStyleTxn.h.


Constructor & Destructor Documentation

Definition at line 146 of file ChangeCSSInlineStyleTxn.cpp.

{
}

Definition at line 141 of file ChangeCSSInlineStyleTxn.cpp.

  : EditTxn()
{
}

Member Function Documentation

returns true if the property accepts more than one value

Returns:
true if the property accepts more than one value
Parameters:
aCSSProperty[IN] the CSS property

Definition at line 333 of file ChangeCSSInlineStyleTxn.cpp.

{
  nsIAtom * textDecorationAtom = NS_NewAtom("text-decoration");
  PRBool res = (textDecorationAtom == aCSSProperty);
  NS_IF_RELEASE(textDecorationAtom);
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP ChangeCSSInlineStyleTxn::AddValueToMultivalueProperty ( nsAString &  aValues,
const nsAString &  aNewValue 
)

adds the value aNewValue to the list of white-space separated values aValues

Parameters:
aValues[IN/OUT] a list of wite-space separated values
aNewValue[IN] a value this code adds to aValues if it is not already in

Definition at line 343 of file ChangeCSSInlineStyleTxn.cpp.

{
  if (aValues.IsEmpty()
      || aValues.LowerCaseEqualsLiteral("none")) {
    // the list of values is empty of the value is 'none'
    aValues.Assign(aNewValue);
  }
  else if (!ValueIncludes(aValues, aNewValue, PR_FALSE)) {
    // we already have another value but not this one; add it
    aValues.Append(PRUnichar(' '));
    aValues.Append(aNewValue);
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ChangeCSSInlineStyleTxn::AppendDeclaration ( nsAString &  aOutputString,
const nsAString &  aProperty,
const nsAString &  aValues 
) [private]

appends 'property : value' to the string

Parameters:
aOutputString[IN/OUT] the string
aPropert[IN] the css property string
aValues[IN] the value string for the css property

Definition at line 51 of file ChangeCSSInlineStyleTxn.cpp.

{
  aOutputString.Append(aProperty
                       + NS_LITERAL_STRING(": ")
                       + aValues
                       + NS_LITERAL_STRING("; "));
}

Executes the transaction.

Reimplemented from EditTxn.

Definition at line 172 of file ChangeCSSInlineStyleTxn.cpp.

{
  NS_ASSERTION(mEditor && mElement, "bad state");
  if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }

  nsCOMPtr<nsIDOMElementCSSInlineStyle> inlineStyles = do_QueryInterface(mElement);
  if (!inlineStyles) return NS_ERROR_NULL_POINTER;

  nsCOMPtr<nsIDOMCSSStyleDeclaration> cssDecl;
  nsresult result = inlineStyles->GetStyle(getter_AddRefs(cssDecl));
  if (NS_FAILED(result)) return result;
  if (!cssDecl) return NS_ERROR_NULL_POINTER;

  nsAutoString propertyNameString;
  mProperty->ToString(propertyNameString);

  NS_NAMED_LITERAL_STRING(styleAttr, "style");
  result = mElement->HasAttribute(styleAttr, &mUndoAttributeWasSet);
  if (NS_FAILED(result)) return result;

  nsAutoString values;
  result = cssDecl->GetPropertyValue(propertyNameString, values);
  if (NS_FAILED(result)) return result;     
  mUndoValue.Assign(values);

  // does this property accept more than 1 value ?
  // we need to know that because of bug 62682
  PRBool multiple = AcceptsMoreThanOneValue(mProperty);
  
  if (mRemoveProperty) {
    nsAutoString returnString;
    if (multiple) {
      // the property can have more than one value, let's remove only
      // the value we have to remove and not the others

      // the 2 lines below are a workaround because nsDOMCSSDeclaration::GetPropertyCSSValue
      // is not yet implemented (bug 62682)
      RemoveValueFromListOfValues(values, NS_LITERAL_STRING("none"));
      RemoveValueFromListOfValues(values, mValue);
      if (values.IsEmpty()) {
        result = cssDecl->RemoveProperty(propertyNameString, returnString);
        if (NS_FAILED(result)) return result;     
      }
      else {
        nsAutoString priority;
        result = cssDecl->GetPropertyPriority(propertyNameString, priority);
        if (NS_FAILED(result)) return result;     
        result = cssDecl->SetProperty(propertyNameString, values,
                                      priority);
        if (NS_FAILED(result)) return result;     
      }
    }
    else {
      result = cssDecl->RemoveProperty(propertyNameString, returnString);
      if (NS_FAILED(result)) return result;     
    }
  }
  else {
    nsAutoString priority;
    result = cssDecl->GetPropertyPriority(propertyNameString, priority);
    if (NS_FAILED(result)) return result;
    if (multiple) {
      // the property can have more than one value, let's add
      // the value we have to add to the others

      // the line below is a workaround because nsDOMCSSDeclaration::GetPropertyCSSValue
      // is not yet implemented (bug 62682)
      AddValueToMultivalueProperty(values, mValue);
    }
    else
      values.Assign(mValue);
    result = cssDecl->SetProperty(propertyNameString, values,
                                  priority);
    if (NS_FAILED(result)) return result;     
  }

  // let's be sure we don't keep an empty style attribute
  PRUint32 length;
  result = cssDecl->GetLength(&length);
  if (NS_FAILED(result)) return result;     
  if (!length) {
    result = mElement->RemoveAttribute(styleAttr);
    if (NS_FAILED(result)) return result;     
  }
  else
    mRedoAttributeWasSet = PR_TRUE;

  return cssDecl->GetPropertyValue(propertyNameString, mRedoValue);
}

Here is the call graph for this function:

static const nsIID& ChangeCSSInlineStyleTxn::GetCID ( ) [inline, static]

Reimplemented from EditTxn.

Definition at line 60 of file ChangeCSSInlineStyleTxn.h.

{ static const nsIID iid = CHANGE_CSSINLINESTYLE_TXN_CID; return iid; }

Here is the caller graph for this function:

NS_IMETHODIMP EditTxn::GetIsTransient ( PRBool aIsTransient) [inherited]

Reimplemented in JoinElementTxn, SetDocTitleTxn, and EditAggregateTxn.

Definition at line 66 of file EditTxn.cpp.

{
  if (aIsTransient)
    *aIsTransient = PR_FALSE;
  return NS_OK;
}

Reimplemented from EditTxn.

Definition at line 317 of file ChangeCSSInlineStyleTxn.cpp.

{
  aString.AssignLiteral("ChangeCSSInlineStyleTxn: [mRemoveProperty == ");

  if (!mRemoveProperty)
    aString.AppendLiteral("false] ");
  else
    aString.AppendLiteral("true] ");
  nsAutoString tempString;
  mProperty->ToString(tempString);
  aString += tempString;
  return NS_OK;
}
NS_IMETHODIMP ChangeCSSInlineStyleTxn::Init ( nsIEditor aEditor,
nsIDOMElement aElement,
nsIAtom aProperty,
const nsAString &  aValue,
PRBool  aRemoveProperty 
)

Initialize the transaction.

Parameters:
aEditor[IN] the object providing core editing operations
aNode[IN] the node whose style attribute will be changed
aProperty[IN] the name of the property to change
aValue[IN] the new value for aProperty, if aRemoveProperty is false
aRemoveProperty[IN] if PR_TRUE, remove aProperty from style attribute

Definition at line 150 of file ChangeCSSInlineStyleTxn.cpp.

{
  NS_ASSERTION(aEditor && aElement, "bad arg");
  if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }

  mEditor = aEditor;
  mElement = do_QueryInterface(aElement);
  mProperty = aProperty;
  NS_ADDREF(mProperty);
  mValue.Assign(aValue);
  mRemoveProperty = aRemoveProperty;
  mUndoAttributeWasSet = PR_FALSE;
  mRedoAttributeWasSet = PR_FALSE;
  mUndoValue.Truncate();
  mRedoValue.Truncate();
  return NS_OK;
}

Here is the call graph for this function:

boolean nsITransaction::merge ( in nsITransaction  aTransaction) [inherited]

Attempts to merge a transaction into "this" transaction.

Both transactions must be in their undo state, doTransaction() methods already called. The transaction manager calls this method to coalesce a new transaction with the transaction on the top of the undo stack. This method returns a boolean value that indicates the merge result. A true value indicates that the transactions were merged successfully, a false value if the merge was not possible or failed. If true, the transaction manager will Release() the new transacton instead of pushing it on the undo stack.

Parameters:
aTransactionthe previously executed transaction to merge.
NS_IMETHODIMP ChangeCSSInlineStyleTxn::Merge ( nsITransaction aTransaction,
PRBool aDidMerge 
)

Reimplemented from EditTxn.

Definition at line 310 of file ChangeCSSInlineStyleTxn.cpp.

{
  if (aDidMerge)
    *aDidMerge = PR_FALSE;
  return NS_OK;
}

Executes the transaction again.

Can only be called on a transaction that was previously undone.

In most cases, the redoTransaction() method will actually call the doTransaction() method to execute the transaction again.

Reimplemented from EditTxn.

Definition at line 305 of file ChangeCSSInlineStyleTxn.cpp.

Here is the call graph for this function:

void ChangeCSSInlineStyleTxn::RemoveValueFromListOfValues ( nsAString &  aValues,
const nsAString &  aRemoveValue 
) [private]

remove a value from a list of white-space separated values

Parameters:
aValues[IN] a list of white-space separated values
aRemoveValue[IN] the value to remove from the list

Definition at line 109 of file ChangeCSSInlineStyleTxn.cpp.

{
  nsAutoString  classStr(aValues);  // copy to work buffer   nsAutoString  rv(aRemoveValue);
  nsAutoString  outString;
  classStr.Append(kNullCh);  // put an extra null at the end

  PRUnichar *start = classStr.BeginWriting();
  PRUnichar *end   = start;

  while (kNullCh != *start) {
    while ((kNullCh != *start) && nsCRT::IsAsciiSpace(*start)) {  // skip leading space
      start++;
    }
    end = start;

    while ((kNullCh != *end) && (PR_FALSE == nsCRT::IsAsciiSpace(*end))) { // look for space or end
      end++;
    }
    *end = kNullCh; // end string here

    if (start < end) {
      if (!aRemoveValue.Equals(start)) {
        outString.Append(start);
        outString.Append(PRUnichar(' '));
      }
    }

    start = ++end;
  }
  aValues.Assign(outString);
}

Here is the caller graph for this function:

nsresult ChangeCSSInlineStyleTxn::SetStyle ( PRBool  aAttributeWasSet,
nsAString &  aValue 
) [private]

If the boolean is true and if the value is not the empty string, set the property in the transaction to that value; if the value is empty, remove the property from element's styles.

If the boolean is false, just remove the style attribute.

Definition at line 262 of file ChangeCSSInlineStyleTxn.cpp.

{
  NS_ASSERTION(mEditor && mElement, "bad state");
  if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }

  nsresult result;
  if (aAttributeWasSet) {
    // the style attribute was set and not empty, let's recreate the declaration
    nsAutoString propertyNameString;
    mProperty->ToString(propertyNameString);

    nsCOMPtr<nsIDOMElementCSSInlineStyle> inlineStyles = do_QueryInterface(mElement);
    if (!inlineStyles) return NS_ERROR_NULL_POINTER;
    nsCOMPtr<nsIDOMCSSStyleDeclaration> cssDecl;
    result = inlineStyles->GetStyle(getter_AddRefs(cssDecl));
    if (NS_FAILED(result)) return result;
    if (!cssDecl) return NS_ERROR_NULL_POINTER;

    if (aValue.IsEmpty()) {
      // an empty value means we have to remove the property
      nsAutoString returnString;
      result = cssDecl->RemoveProperty(propertyNameString, returnString);
    }
    else {
      // let's recreate the declaration as it was
      nsAutoString priority;
      result = cssDecl->GetPropertyPriority(propertyNameString, priority);
      if (NS_FAILED(result)) return result;
      result = cssDecl->SetProperty(propertyNameString, aValue, priority);
    }
  }
  else
    result = mElement->RemoveAttribute(NS_LITERAL_STRING("style"));

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Restores the state to what it was before the transaction was executed.

Reimplemented from EditTxn.

Definition at line 300 of file ChangeCSSInlineStyleTxn.cpp.

Here is the call graph for this function:

PRBool ChangeCSSInlineStyleTxn::ValueIncludes ( const nsAString &  aValueList,
const nsAString &  aValue,
PRBool  aCaseSensitive 
) [static]

returns true if the list of white-space separated values contains aValue

Returns:
true if the value is in the list of values
Parameters:
aValueList[IN] a list of white-space separated values
aValue[IN] the value to look for in the list
aCaseSensitive[IN] a boolean being true if a case-sensitive search is needed

Definition at line 64 of file ChangeCSSInlineStyleTxn.cpp.

{
  nsAutoString  valueList(aValueList);
  PRBool result = PR_FALSE;

  valueList.Append(kNullCh);  // put an extra null at the end

  PRUnichar *value = ToNewUnicode(aValue);
  PRUnichar *start = valueList.BeginWriting();
  PRUnichar *end   = start;

  while (kNullCh != *start) {
    while ((kNullCh != *start) && nsCRT::IsAsciiSpace(*start)) {  // skip leading space
      start++;
    }
    end = start;

    while ((kNullCh != *end) && (PR_FALSE == nsCRT::IsAsciiSpace(*end))) { // look for space or end
      end++;
    }
    *end = kNullCh; // end string here

    if (start < end) {
      if (aCaseSensitive) {
        if (!nsCRT::strcmp(value, start)) {
          result = PR_TRUE;
          break;
        }
      }
      else {
        if (nsDependentString(value).Equals(nsDependentString(start),
                                            nsCaseInsensitiveStringComparator())) {
          result = PR_TRUE;
          break;
        }
      }
    }
    start = ++end;
  }
  NS_Free(value);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class TransactionFactory [friend]

Definition at line 164 of file ChangeCSSInlineStyleTxn.h.


Member Data Documentation

The transaction's transient state.

This attribute is checked by the transaction manager after the transaction's Execute() method is called. If the transient state is false, a reference to the transaction is held by the transaction manager so that the transactions' undoTransaction() and redoTransaction() methods can be called. If the transient state is true, the transaction manager returns immediately after the transaction's doTransaction() method is called, no references to the transaction are maintained. Transient transactions cannot be undone or redone by the transaction manager.

Definition at line 79 of file nsITransaction.idl.

the editor that created this transaction

Definition at line 141 of file ChangeCSSInlineStyleTxn.h.

the element to operate upon

Definition at line 144 of file ChangeCSSInlineStyleTxn.h.

the CSS property to change

Definition at line 147 of file ChangeCSSInlineStyleTxn.h.

true if the style attribute is present and not empty after DoTransaction

Definition at line 159 of file ChangeCSSInlineStyleTxn.h.

the value to set the property to for redo

Definition at line 155 of file ChangeCSSInlineStyleTxn.h.

PR_TRUE if the operation is to remove mProperty from mElement.

Definition at line 162 of file ChangeCSSInlineStyleTxn.h.

true if the style attribute was present and not empty before DoTransaction

Definition at line 157 of file ChangeCSSInlineStyleTxn.h.

the value to set the property to for undo

Definition at line 153 of file ChangeCSSInlineStyleTxn.h.

the value to set the property to (ignored if mRemoveProperty==PR_TRUE)

Definition at line 150 of file ChangeCSSInlineStyleTxn.h.

readonly attribute DOMString nsPIEditorTransaction::txnDescription [inherited]

Definition at line 50 of file nsPIEditorTransaction.idl.


The documentation for this class was generated from the following files: