Bug 1153629 part 9 - Clean up nsHTMLEditRules::MakeTransitionList; r=ehsan

This commit is contained in:
Aryeh Gregor 2015-04-24 14:27:35 +03:00
parent 29c872f3fa
commit 78fd0e1b01
2 changed files with 28 additions and 45 deletions

View File

@ -4636,18 +4636,18 @@ nsHTMLEditRules::WillAlign(Selection* aSelection,
// block parent, and then further expands to include any ancestors // block parent, and then further expands to include any ancestors
// whose children are all in the range // whose children are all in the range
*aHandled = true; *aHandled = true;
nsTArray<nsCOMPtr<nsINode>> array; nsTArray<nsCOMPtr<nsINode>> nodeArray;
res = GetNodesFromSelection(*aSelection, EditAction::align, array); res = GetNodesFromSelection(*aSelection, EditAction::align, nodeArray);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
// if we don't have any nodes, or we have only a single br, then we are // if we don't have any nodes, or we have only a single br, then we are
// creating an empty alignment div. We have to do some different things for these. // creating an empty alignment div. We have to do some different things for these.
bool emptyDiv = false; bool emptyDiv = false;
int32_t listCount = array.Length(); int32_t listCount = nodeArray.Length();
if (!listCount) emptyDiv = true; if (!listCount) emptyDiv = true;
if (listCount == 1) if (listCount == 1)
{ {
nsCOMPtr<nsINode> theNode = array[0]; nsCOMPtr<nsINode> theNode = nodeArray[0];
if (nsHTMLEditUtils::SupportsAlignAttr(GetAsDOMNode(theNode))) { if (nsHTMLEditUtils::SupportsAlignAttr(GetAsDOMNode(theNode))) {
// the node is a table element, an horiz rule, a paragraph, a div // the node is a table element, an horiz rule, a paragraph, a div
@ -4670,9 +4670,9 @@ nsHTMLEditRules::WillAlign(Selection* aSelection,
// //
// XXX: It seems a little error prone for the emptyDiv special // XXX: It seems a little error prone for the emptyDiv special
// case code to assume that the start node of the selection // case code to assume that the start node of the selection
// is the parent of the single node in the arrayOfNodes, as // is the parent of the single node in the nodeArray, as
// the paragraph above points out. Do we rely on the selection // the paragraph above points out. Do we rely on the selection
// start node because of the fact that arrayOfNodes can be empty? // start node because of the fact that nodeArray can be empty?
// We should probably revisit this issue. - kin // We should probably revisit this issue. - kin
nsCOMPtr<nsIDOMNode> parent; nsCOMPtr<nsIDOMNode> parent;
@ -4740,14 +4740,8 @@ nsHTMLEditRules::WillAlign(Selection* aSelection,
// Next we detect all the transitions in the array, where a transition // Next we detect all the transitions in the array, where a transition
// means that adjacent nodes in the array don't have the same parent. // means that adjacent nodes in the array don't have the same parent.
nsCOMArray<nsIDOMNode> arrayOfNodes;
for (auto& node : array) {
arrayOfNodes.AppendObject(GetAsDOMNode(node));
}
nsTArray<bool> transitionList; nsTArray<bool> transitionList;
res = MakeTransitionList(arrayOfNodes, transitionList); MakeTransitionList(nodeArray, transitionList);
NS_ENSURE_SUCCESS(res, res);
// Ok, now go through all the nodes and give them an align attrib or put them in a div, // Ok, now go through all the nodes and give them an align attrib or put them in a div,
// or whatever is appropriate. Wohoo! // or whatever is appropriate. Wohoo!
@ -4757,7 +4751,7 @@ nsHTMLEditRules::WillAlign(Selection* aSelection,
bool useCSS = mHTMLEditor->IsCSSEnabled(); bool useCSS = mHTMLEditor->IsCSSEnabled();
for (int32_t i = 0; i < listCount; ++i) { for (int32_t i = 0; i < listCount; ++i) {
// here's where we actually figure out what to do // here's where we actually figure out what to do
nsCOMPtr<nsIDOMNode> curNode = arrayOfNodes[i]; nsCOMPtr<nsIDOMNode> curNode = nodeArray[i]->AsDOMNode();
nsCOMPtr<nsIContent> curContent = do_QueryInterface(curNode); nsCOMPtr<nsIContent> curContent = do_QueryInterface(curNode);
NS_ENSURE_STATE(curContent); NS_ENSURE_STATE(curContent);
@ -6266,38 +6260,27 @@ nsHTMLEditRules::GetNodesFromSelection(Selection& aSelection,
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// MakeTransitionList: detect all the transitions in the array, where a // MakeTransitionList: Detect all the transitions in the array, where a
// transition means that adjacent nodes in the array // transition means that adjacent nodes in the array don't
// don't have the same parent. // have the same parent.
// void
nsresult nsHTMLEditRules::MakeTransitionList(nsTArray<nsCOMPtr<nsINode>>& aNodeArray,
nsHTMLEditRules::MakeTransitionList(nsCOMArray<nsIDOMNode>& inArrayOfNodes, nsTArray<bool>& aTransitionArray)
nsTArray<bool> &inTransitionArray)
{ {
uint32_t listCount = inArrayOfNodes.Count(); nsCOMPtr<nsINode> prevParent;
inTransitionArray.EnsureLengthAtLeast(listCount);
uint32_t i; aTransitionArray.EnsureLengthAtLeast(aNodeArray.Length());
nsCOMPtr<nsIDOMNode> prevElementParent; for (uint32_t i = 0; i < aNodeArray.Length(); i++) {
nsCOMPtr<nsIDOMNode> curElementParent; if (aNodeArray[i]->GetParentNode() != prevParent) {
// Different parents: transition point
for (i=0; i<listCount; i++) aTransitionArray[i] = true;
{ } else {
nsIDOMNode* transNode = inArrayOfNodes[i]; // Same parents: these nodes grew up together
transNode->GetParentNode(getter_AddRefs(curElementParent)); aTransitionArray[i] = false;
if (curElementParent != prevElementParent)
{
// different parents, or separated by <br>: transition point
inTransitionArray[i] = true;
} }
else prevParent = aNodeArray[i]->GetParentNode();
{
// same parents: these nodes grew up together
inTransitionArray[i] = false;
}
prevElementParent = curElementParent;
} }
return NS_OK;
} }

View File

@ -296,8 +296,8 @@ protected:
nsresult BustUpInlinesAtBRs(nsINode& aNode, nsresult BustUpInlinesAtBRs(nsINode& aNode,
nsTArray<nsCOMPtr<nsINode>>& aOutArrayOfNodes); nsTArray<nsCOMPtr<nsINode>>& aOutArrayOfNodes);
nsCOMPtr<nsIDOMNode> GetHighestInlineParent(nsIDOMNode* aNode); nsCOMPtr<nsIDOMNode> GetHighestInlineParent(nsIDOMNode* aNode);
nsresult MakeTransitionList(nsCOMArray<nsIDOMNode>& inArrayOfNodes, void MakeTransitionList(nsTArray<nsCOMPtr<nsINode>>& aNodeArray,
nsTArray<bool> &inTransitionArray); nsTArray<bool>& aTransitionArray);
nsresult RemoveBlockStyle(nsTArray<nsCOMPtr<nsINode>>& aNodeArray); nsresult RemoveBlockStyle(nsTArray<nsCOMPtr<nsINode>>& aNodeArray);
nsresult ApplyBlockStyle(nsTArray<nsCOMPtr<nsINode>>& aNodeArray, nsresult ApplyBlockStyle(nsTArray<nsCOMPtr<nsINode>>& aNodeArray,
nsIAtom& aBlockTag); nsIAtom& aBlockTag);