Bug 749329 - Cleanup TypeInState; r=ehsan

This commit is contained in:
Ms2ger 2012-05-05 11:00:06 +02:00
parent 552a17939d
commit f051ed6486
4 changed files with 96 additions and 107 deletions

View File

@ -154,68 +154,58 @@ void TypeInState::Reset()
}
nsresult TypeInState::SetProp(nsIAtom *aProp, const nsString &aAttr, const nsString &aValue)
void
TypeInState::SetProp(nsIAtom* aProp, const nsAString& aAttr,
const nsAString& aValue)
{
// special case for big/small, these nest
if (nsEditProperty::big == aProp)
{
if (nsEditProperty::big == aProp) {
mRelativeFontSize++;
return NS_OK;
return;
}
if (nsEditProperty::small == aProp)
{
if (nsEditProperty::small == aProp) {
mRelativeFontSize--;
return NS_OK;
return;
}
PRInt32 index;
PropItem *item;
if (IsPropSet(aProp,aAttr,nsnull,index))
{
if (IsPropSet(aProp, aAttr, NULL, index)) {
// if it's already set, update the value
item = mSetArray[index];
item->value = aValue;
mSetArray[index]->value = aValue;
return;
}
else
{
// make a new propitem
item = new PropItem(aProp,aAttr,aValue);
NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
// add it to the list of set properties
mSetArray.AppendElement(item);
// remove it from the list of cleared properties, if we have a match
RemovePropFromClearedList(aProp,aAttr);
}
return NS_OK;
// Make a new propitem and add it to the list of set properties.
mSetArray.AppendElement(new PropItem(aProp, aAttr, aValue));
// remove it from the list of cleared properties, if we have a match
RemovePropFromClearedList(aProp, aAttr);
}
nsresult TypeInState::ClearAllProps()
void
TypeInState::ClearAllProps()
{
// null prop means "all" props
return ClearProp(nsnull,EmptyString());
ClearProp(nsnull, EmptyString());
}
nsresult TypeInState::ClearProp(nsIAtom *aProp, const nsString &aAttr)
void
TypeInState::ClearProp(nsIAtom* aProp, const nsAString& aAttr)
{
// if it's already cleared we are done
if (IsPropCleared(aProp,aAttr)) return NS_OK;
if (IsPropCleared(aProp, aAttr)) {
return;
}
// make a new propitem
PropItem *item = new PropItem(aProp,aAttr,EmptyString());
NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
PropItem* item = new PropItem(aProp, aAttr, EmptyString());
// remove it from the list of set properties, if we have a match
RemovePropFromSetList(aProp,aAttr);
RemovePropFromSetList(aProp, aAttr);
// add it to the list of cleared properties
mClearedArray.AppendElement(item);
return NS_OK;
}
@ -223,47 +213,46 @@ nsresult TypeInState::ClearProp(nsIAtom *aProp, const nsString &aAttr)
* TakeClearProperty: hands back next property item on the clear list.
* caller assumes ownership of PropItem and must delete it.
*/
nsresult TypeInState::TakeClearProperty(PropItem **outPropItem)
PropItem*
TypeInState::TakeClearProperty()
{
NS_ENSURE_TRUE(outPropItem, NS_ERROR_NULL_POINTER);
*outPropItem = nsnull;
PRUint32 count = mClearedArray.Length();
if (count)
{
count--; // indizes are zero based
*outPropItem = mClearedArray[count];
mClearedArray.RemoveElementAt(count);
if (!count) {
return NULL;
}
return NS_OK;
--count; // indices are zero based
PropItem* propItem = mClearedArray[count];
mClearedArray.RemoveElementAt(count);
return propItem;
}
/***************************************************************************
* TakeSetProperty: hands back next poroperty item on the set list.
* caller assumes ownership of PropItem and must delete it.
*/
nsresult TypeInState::TakeSetProperty(PropItem **outPropItem)
PropItem*
TypeInState::TakeSetProperty()
{
NS_ENSURE_TRUE(outPropItem, NS_ERROR_NULL_POINTER);
*outPropItem = nsnull;
PRUint32 count = mSetArray.Length();
if (count)
{
count--; // indizes are zero based
*outPropItem = mSetArray[count];
mSetArray.RemoveElementAt(count);
if (!count) {
return NULL;
}
return NS_OK;
count--; // indices are zero based
PropItem* propItem = mSetArray[count];
mSetArray.RemoveElementAt(count);
return propItem;
}
//**************************************************************************
// TakeRelativeFontSize: hands back relative font value, which is then
// cleared out.
nsresult TypeInState::TakeRelativeFontSize(PRInt32 *outRelSize)
PRInt32
TypeInState::TakeRelativeFontSize()
{
NS_ENSURE_TRUE(outRelSize, NS_ERROR_NULL_POINTER);
*outRelSize = mRelativeFontSize;
PRInt32 relSize = mRelativeFontSize;
mRelativeFontSize = 0;
return NS_OK;
return relSize;
}
nsresult TypeInState::GetTypingState(bool &isSet, bool &theSetting, nsIAtom *aProp)
@ -300,8 +289,8 @@ nsresult TypeInState::GetTypingState(bool &isSet,
* protected methods
*******************************************************************/
nsresult TypeInState::RemovePropFromSetList(nsIAtom *aProp,
const nsString &aAttr)
nsresult TypeInState::RemovePropFromSetList(nsIAtom* aProp,
const nsAString& aAttr)
{
PRInt32 index;
if (!aProp)
@ -322,8 +311,8 @@ nsresult TypeInState::RemovePropFromSetList(nsIAtom *aProp,
}
nsresult TypeInState::RemovePropFromClearedList(nsIAtom *aProp,
const nsString &aAttr)
nsresult TypeInState::RemovePropFromClearedList(nsIAtom* aProp,
const nsAString& aAttr)
{
PRInt32 index;
if (FindPropInList(aProp, aAttr, nsnull, mClearedArray, index))
@ -335,19 +324,19 @@ nsresult TypeInState::RemovePropFromClearedList(nsIAtom *aProp,
}
bool TypeInState::IsPropSet(nsIAtom *aProp,
const nsString &aAttr,
nsString* outValue)
bool TypeInState::IsPropSet(nsIAtom *aProp,
const nsAString& aAttr,
nsAString* outValue)
{
PRInt32 i;
return IsPropSet(aProp, aAttr, outValue, i);
}
bool TypeInState::IsPropSet(nsIAtom *aProp,
const nsString &aAttr,
nsString *outValue,
PRInt32 &outIndex)
bool TypeInState::IsPropSet(nsIAtom* aProp,
const nsAString& aAttr,
nsAString* outValue,
PRInt32& outIndex)
{
// linear search. list should be short.
PRUint32 i, count = mSetArray.Length();
@ -366,17 +355,17 @@ bool TypeInState::IsPropSet(nsIAtom *aProp,
}
bool TypeInState::IsPropCleared(nsIAtom *aProp,
const nsString &aAttr)
bool TypeInState::IsPropCleared(nsIAtom* aProp,
const nsAString& aAttr)
{
PRInt32 i;
return IsPropCleared(aProp, aAttr, i);
}
bool TypeInState::IsPropCleared(nsIAtom *aProp,
const nsString &aAttr,
PRInt32 &outIndex)
bool TypeInState::IsPropCleared(nsIAtom* aProp,
const nsAString& aAttr,
PRInt32& outIndex)
{
if (FindPropInList(aProp, aAttr, nsnull, mClearedArray, outIndex))
return true;

View File

@ -74,25 +74,25 @@ public:
// nsISelectionListener
NS_DECL_NSISELECTIONLISTENER
nsresult SetProp(nsIAtom *aProp, const nsString &aAttr, const nsString &aValue);
void SetProp(nsIAtom* aProp, const nsAString& aAttr, const nsAString& aValue);
nsresult ClearAllProps();
nsresult ClearProp(nsIAtom *aProp, const nsString &aAttr);
void ClearAllProps();
void ClearProp(nsIAtom* aProp, const nsAString& aAttr);
//**************************************************************************
// TakeClearProperty: hands back next property item on the clear list.
// caller assumes ownership of PropItem and must delete it.
nsresult TakeClearProperty(PropItem **outPropItem);
PropItem* TakeClearProperty();
//**************************************************************************
// TakeSetProperty: hands back next property item on the set list.
// caller assumes ownership of PropItem and must delete it.
nsresult TakeSetProperty(PropItem **outPropItem);
PropItem* TakeSetProperty();
//**************************************************************************
// TakeRelativeFontSize: hands back relative font value, which is then
// cleared out.
nsresult TakeRelativeFontSize(PRInt32 *outRelSize);
PRInt32 TakeRelativeFontSize();
nsresult GetTypingState(bool &isSet, bool &theSetting, nsIAtom *aProp);
nsresult GetTypingState(bool &isSet, bool &theSetting, nsIAtom *aProp,
@ -102,12 +102,12 @@ public:
protected:
nsresult RemovePropFromSetList(nsIAtom *aProp, const nsString &aAttr);
nsresult RemovePropFromClearedList(nsIAtom *aProp, const nsString &aAttr);
bool IsPropSet(nsIAtom *aProp, const nsString &aAttr, nsString* outValue);
bool IsPropSet(nsIAtom *aProp, const nsString &aAttr, nsString* outValue, PRInt32 &outIndex);
bool IsPropCleared(nsIAtom *aProp, const nsString &aAttr);
bool IsPropCleared(nsIAtom *aProp, const nsString &aAttr, PRInt32 &outIndex);
nsresult RemovePropFromSetList(nsIAtom* aProp, const nsAString& aAttr);
nsresult RemovePropFromClearedList(nsIAtom* aProp, const nsAString& aAttr);
bool IsPropSet(nsIAtom* aProp, const nsAString& aAttr, nsAString* outValue);
bool IsPropSet(nsIAtom* aProp, const nsAString& aAttr, nsAString* outValue, PRInt32& outIndex);
bool IsPropCleared(nsIAtom* aProp, const nsAString& aAttr);
bool IsPropCleared(nsIAtom* aProp, const nsAString& aAttr, PRInt32& outIndex);
nsTArray<PropItem*> mSetArray;
nsTArray<PropItem*> mClearedArray;

View File

@ -4373,7 +4373,6 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
PRInt32 offset;
nsresult res = mHTMLEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(node), &offset);
NS_ENSURE_SUCCESS(res, res);
nsAutoPtr<PropItem> item;
// if we deleted selection then also for cached styles
if (mDidDeleteSelection &&
@ -4421,7 +4420,7 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
NS_ENSURE_SUCCESS(res, res);
// process clearing any styles first
mHTMLEditor->mTypeInState->TakeClearProperty(getter_Transfers(item));
nsAutoPtr<PropItem> item(mHTMLEditor->mTypeInState->TakeClearProperty());
while (item && node != rootElement)
{
nsCOMPtr<nsIDOMNode> leftNode, rightNode, secondSplitParent, newSelParent, savedBR;
@ -4487,17 +4486,13 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
node = newSelParent;
offset = newSelOffset;
}
mHTMLEditor->mTypeInState->TakeClearProperty(getter_Transfers(item));
item = mHTMLEditor->mTypeInState->TakeClearProperty();
weDidSometing = true;
}
// then process setting any styles
PRInt32 relFontSize;
res = mHTMLEditor->mTypeInState->TakeRelativeFontSize(&relFontSize);
NS_ENSURE_SUCCESS(res, res);
res = mHTMLEditor->mTypeInState->TakeSetProperty(getter_Transfers(item));
NS_ENSURE_SUCCESS(res, res);
PRInt32 relFontSize = mHTMLEditor->mTypeInState->TakeRelativeFontSize();
item = mHTMLEditor->mTypeInState->TakeSetProperty();
if (item || relFontSize) // we have at least one style to add; make a
{ // new text node to insert style nodes above.
@ -4542,7 +4537,7 @@ nsHTMLEditRules::CreateStyleForInsertText(nsISelection *aSelection, nsIDOMDocume
{
res = mHTMLEditor->SetInlinePropertyOnNode(node, item->tag, &item->attr, &item->value);
NS_ENSURE_SUCCESS(res, res);
mHTMLEditor->mTypeInState->TakeSetProperty(getter_Transfers(item));
item = mHTMLEditor->mTypeInState->TakeSetProperty();
}
}
if (weDidSometing)

View File

@ -133,9 +133,8 @@ nsHTMLEditor::SetInlineProperty(nsIAtom *aProperty,
if (isCollapsed) {
// manipulating text attributes on a collapsed selection only sets state
// for the next text insertion
nsString tAttr(aAttribute);//MJUDGE SCC NEED HELP
nsString tVal(aValue);//MJUDGE SCC NEED HELP
return mTypeInState->SetProp(aProperty, tAttr, tVal);
mTypeInState->SetProp(aProperty, aAttribute, aValue);
return NS_OK;
}
nsAutoEditBatch batchIt(this);
@ -1239,6 +1238,7 @@ NS_IMETHODIMP nsHTMLEditor::RemoveInlineProperty(nsIAtom *aProperty, const nsASt
nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAString *aAttribute)
{
MOZ_ASSERT_IF(aProperty, aAttribute);
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
ForceCompositionEnd();
@ -1253,8 +1253,7 @@ nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAStr
selection->GetIsCollapsed(&isCollapsed);
bool useCSS = IsCSSEnabled();
if (isCollapsed)
{
if (isCollapsed) {
// manipulating text attributes on a collapsed selection only sets state for the next text insertion
// For links, aProperty uses "href", use "a" instead
@ -1262,9 +1261,14 @@ nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAStr
aProperty == nsEditProperty::name)
aProperty = nsEditProperty::a;
if (aProperty) return mTypeInState->ClearProp(aProperty, nsAutoString(*aAttribute));
else return mTypeInState->ClearAllProps();
if (aProperty) {
mTypeInState->ClearProp(aProperty, *aAttribute);
} else {
mTypeInState->ClearAllProps();
}
return NS_OK;
}
nsAutoEditBatch batchIt(this);
nsAutoRules beginRulesSniffing(this, kOpRemoveTextProperty, nsIEditor::eNext);
nsAutoSelectionReset selectionResetter(selection, this);
@ -1468,7 +1472,8 @@ nsHTMLEditor::RelativeFontChange( PRInt32 aSizeChange)
}
// manipulating text attributes on a collapsed selection only sets state for the next text insertion
return mTypeInState->SetProp(atom, EmptyString(), EmptyString());
mTypeInState->SetProp(atom, EmptyString(), EmptyString());
return NS_OK;
}
// wrap with txn batching, rules sniffing, and selection preservation code