mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-29 07:42:04 +00:00
Bug 1191356 part 8 - Clean up nsHTMLEditRules::WillAbsolutePosition; r=ehsan
This commit is contained in:
parent
0d6d4b284b
commit
dccb99b0b6
@ -650,7 +650,7 @@ nsHTMLEditRules::WillDoAction(Selection* aSelection,
|
||||
case EditAction::outdent:
|
||||
return WillOutdent(*aSelection, aCancel, aHandled);
|
||||
case EditAction::setAbsolutePosition:
|
||||
return WillAbsolutePosition(aSelection, aCancel, aHandled);
|
||||
return WillAbsolutePosition(*aSelection, aCancel, aHandled);
|
||||
case EditAction::removeAbsolutePosition:
|
||||
return WillRemoveAbsolutePosition(aSelection, aCancel, aHandled);
|
||||
case EditAction::align:
|
||||
@ -3335,21 +3335,15 @@ nsHTMLEditRules::WillMakeBasicBlock(Selection& aSelection,
|
||||
bool* aCancel,
|
||||
bool* aHandled)
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
OwningNonNull<nsIAtom> blockType = NS_Atomize(*aBlockType);
|
||||
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
|
||||
// initialize out param
|
||||
=======
|
||||
MOZ_ASSERT(aCancel && aHandled);
|
||||
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
|
||||
|
||||
OwningNonNull<nsIAtom> blockType = do_GetAtom(aBlockType);
|
||||
OwningNonNull<nsIAtom> blockType = NS_Atomize(aBlockType);
|
||||
|
||||
WillInsert(aSelection, aCancel);
|
||||
// We want to ignore result of WillInsert()
|
||||
>>>>>>> Bug 1191356 part 6 - Clean up nsHTMLEditRules::WillMakeBasicBlock
|
||||
*aCancel = false;
|
||||
*aHandled = false;
|
||||
|
||||
@ -8476,112 +8470,105 @@ nsHTMLEditRules::ChangeIndentation(Element& aElement, Change aChange)
|
||||
//
|
||||
|
||||
nsresult
|
||||
nsHTMLEditRules::WillAbsolutePosition(Selection* aSelection,
|
||||
nsHTMLEditRules::WillAbsolutePosition(Selection& aSelection,
|
||||
bool* aCancel, bool* aHandled)
|
||||
{
|
||||
if (!aSelection || !aCancel || !aHandled) { return NS_ERROR_NULL_POINTER; }
|
||||
WillInsert(*aSelection, aCancel);
|
||||
MOZ_ASSERT(aCancel && aHandled);
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
|
||||
|
||||
// initialize out param
|
||||
// we want to ignore result of WillInsert()
|
||||
WillInsert(aSelection, aCancel);
|
||||
|
||||
// We want to ignore result of WillInsert()
|
||||
*aCancel = false;
|
||||
*aHandled = true;
|
||||
|
||||
nsCOMPtr<nsIDOMElement> focusElement;
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
nsresult res =
|
||||
mHTMLEditor->GetSelectionContainer(getter_AddRefs(focusElement));
|
||||
if (focusElement) {
|
||||
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(focusElement);
|
||||
if (nsHTMLEditUtils::IsImage(node)) {
|
||||
mNewBlock = node;
|
||||
return NS_OK;
|
||||
}
|
||||
nsCOMPtr<Element> focusElement = mHTMLEditor->GetSelectionContainer();
|
||||
if (focusElement && nsHTMLEditUtils::IsImage(focusElement)) {
|
||||
mNewBlock = focusElement->AsDOMNode();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
res = NormalizeSelection(aSelection);
|
||||
nsresult res = NormalizeSelection(&aSelection);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
nsAutoSelectionReset selectionResetter(aSelection, mHTMLEditor);
|
||||
nsAutoSelectionReset selectionResetter(&aSelection, mHTMLEditor);
|
||||
|
||||
// convert the selection ranges into "promoted" selection ranges:
|
||||
// this basically just expands the range to include the immediate
|
||||
// block parent, and then further expands to include any ancestors
|
||||
// whose children are all in the range
|
||||
// Convert the selection ranges into "promoted" selection ranges: this
|
||||
// basically just expands the range to include the immediate block parent,
|
||||
// and then further expands to include any ancestors whose children are all
|
||||
// in the range.
|
||||
|
||||
nsTArray<RefPtr<nsRange>> arrayOfRanges;
|
||||
GetPromotedRanges(*aSelection, arrayOfRanges,
|
||||
GetPromotedRanges(aSelection, arrayOfRanges,
|
||||
EditAction::setAbsolutePosition);
|
||||
|
||||
// use these ranges to contruct a list of nodes to act on.
|
||||
// Use these ranges to contruct a list of nodes to act on.
|
||||
nsTArray<OwningNonNull<nsINode>> arrayOfNodes;
|
||||
res = GetNodesForOperation(arrayOfRanges, arrayOfNodes,
|
||||
EditAction::setAbsolutePosition);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
|
||||
// if nothing visible in list, make an empty block
|
||||
if (ListIsEmptyLine(arrayOfNodes))
|
||||
{
|
||||
// get selection location
|
||||
NS_ENSURE_STATE(aSelection->RangeCount());
|
||||
nsCOMPtr<nsINode> parent = aSelection->GetRangeAt(0)->GetStartParent();
|
||||
int32_t offset = aSelection->GetRangeAt(0)->StartOffset();
|
||||
NS_ENSURE_STATE(parent);
|
||||
// If nothing visible in list, make an empty block
|
||||
if (ListIsEmptyLine(arrayOfNodes)) {
|
||||
// Get selection location
|
||||
NS_ENSURE_STATE(aSelection.GetRangeAt(0) &&
|
||||
aSelection.GetRangeAt(0)->GetStartParent());
|
||||
OwningNonNull<nsINode> parent =
|
||||
*aSelection.GetRangeAt(0)->GetStartParent();
|
||||
int32_t offset = aSelection.GetRangeAt(0)->StartOffset();
|
||||
|
||||
// make sure we can put a block here
|
||||
// Make sure we can put a block here
|
||||
res = SplitAsNeeded(*nsGkAtoms::div, parent, offset);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
nsCOMPtr<Element> thePositionedDiv =
|
||||
nsCOMPtr<Element> positionedDiv =
|
||||
mHTMLEditor->CreateNode(nsGkAtoms::div, parent, offset);
|
||||
NS_ENSURE_STATE(thePositionedDiv);
|
||||
// remember our new block for postprocessing
|
||||
mNewBlock = thePositionedDiv->AsDOMNode();
|
||||
// delete anything that was in the list of nodes
|
||||
NS_ENSURE_STATE(positionedDiv);
|
||||
// Remember our new block for postprocessing
|
||||
mNewBlock = positionedDiv->AsDOMNode();
|
||||
// Delete anything that was in the list of nodes
|
||||
while (!arrayOfNodes.IsEmpty()) {
|
||||
OwningNonNull<nsINode> curNode = arrayOfNodes[0];
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
res = mHTMLEditor->DeleteNode(curNode);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
arrayOfNodes.RemoveElementAt(0);
|
||||
}
|
||||
// put selection in new block
|
||||
res = aSelection->Collapse(thePositionedDiv,0);
|
||||
selectionResetter.Abort(); // to prevent selection reseter from overriding us.
|
||||
// Put selection in new block
|
||||
res = aSelection.Collapse(positionedDiv, 0);
|
||||
// Prevent selection resetter from overriding us.
|
||||
selectionResetter.Abort();
|
||||
*aHandled = true;
|
||||
return res;
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Ok, now go through all the nodes and put them in a blockquote,
|
||||
// or whatever is appropriate. Wohoo!
|
||||
nsCOMPtr<nsINode> curParent;
|
||||
nsCOMPtr<nsIDOMNode> indentedLI, sibling;
|
||||
nsCOMPtr<Element> curList, curPositionedDiv;
|
||||
// Okay, now go through all the nodes and put them in a blockquote, or
|
||||
// whatever is appropriate. Woohoo!
|
||||
nsCOMPtr<Element> curList, curPositionedDiv, indentedLI;
|
||||
for (uint32_t i = 0; i < arrayOfNodes.Length(); i++) {
|
||||
// here's where we actually figure out what to do
|
||||
// Here's where we actually figure out what to do
|
||||
NS_ENSURE_STATE(arrayOfNodes[i]->IsContent());
|
||||
nsCOMPtr<nsIContent> curNode = arrayOfNodes[i]->AsContent();
|
||||
OwningNonNull<nsIContent> curNode = *arrayOfNodes[i]->AsContent();
|
||||
|
||||
// Ignore all non-editable nodes. Leave them be.
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
if (!mHTMLEditor->IsEditable(curNode)) continue;
|
||||
if (!mHTMLEditor->IsEditable(curNode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
curParent = curNode->GetParentNode();
|
||||
nsCOMPtr<nsIContent> sibling;
|
||||
|
||||
nsCOMPtr<nsINode> curParent = curNode->GetParentNode();
|
||||
int32_t offset = curParent ? curParent->IndexOf(curNode) : -1;
|
||||
|
||||
// some logic for putting list items into nested lists...
|
||||
if (nsHTMLEditUtils::IsList(curParent))
|
||||
{
|
||||
// check to see if curList is still appropriate. Which it is if
|
||||
// curNode is still right after it in the same list.
|
||||
if (curList)
|
||||
{
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
sibling = GetAsDOMNode(mHTMLEditor->GetPriorHTMLSibling(curNode));
|
||||
// Some logic for putting list items into nested lists...
|
||||
if (nsHTMLEditUtils::IsList(curParent)) {
|
||||
// Check to see if curList is still appropriate. Which it is if curNode
|
||||
// is still right after it in the same list.
|
||||
if (curList) {
|
||||
sibling = mHTMLEditor->GetPriorHTMLSibling(curNode);
|
||||
}
|
||||
|
||||
if (!curList || (sibling && sibling != GetAsDOMNode(curList))) {
|
||||
// create a new nested list of correct type
|
||||
if (!curList || (sibling && sibling != curList)) {
|
||||
// Create a new nested list of correct type
|
||||
res = SplitAsNeeded(*curParent->NodeInfo()->NameAtom(), curParent,
|
||||
offset);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
@ -8589,53 +8576,42 @@ nsHTMLEditRules::WillAbsolutePosition(Selection* aSelection,
|
||||
nsCOMPtr<nsINode> curParentParent = curParent->GetParentNode();
|
||||
int32_t parentOffset = curParentParent
|
||||
? curParentParent->IndexOf(curParent) : -1;
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
curPositionedDiv = mHTMLEditor->CreateNode(nsGkAtoms::div, curParentParent,
|
||||
parentOffset);
|
||||
mNewBlock = GetAsDOMNode(curPositionedDiv);
|
||||
}
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
curList = mHTMLEditor->CreateNode(curParent->NodeInfo()->NameAtom(),
|
||||
curPositionedDiv, -1);
|
||||
NS_ENSURE_STATE(curList);
|
||||
// curList is now the correct thing to put curNode in
|
||||
// remember our new block for postprocessing
|
||||
// mNewBlock = curList;
|
||||
// curList is now the correct thing to put curNode in. Remember our
|
||||
// new block for postprocessing.
|
||||
}
|
||||
// tuck the node into the end of the active list
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
// Tuck the node into the end of the active list
|
||||
res = mHTMLEditor->MoveNode(curNode, curList, -1);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
// forget curPositionedDiv, if any
|
||||
// curPositionedDiv = nullptr;
|
||||
}
|
||||
|
||||
else // not a list item, use blockquote?
|
||||
{
|
||||
// if we are inside a list item, we don't want to blockquote, we want
|
||||
// to sublist the list item. We may have several nodes listed in the
|
||||
// array of nodes to act on, that are in the same list item. Since
|
||||
// we only want to indent that li once, we must keep track of the most
|
||||
// recent indented list item, and not indent it if we find another node
|
||||
// to act on that is still inside the same li.
|
||||
} else {
|
||||
// Not a list item, use blockquote? If we are inside a list item, we
|
||||
// don't want to blockquote, we want to sublist the list item. We may
|
||||
// have several nodes listed in the array of nodes to act on, that are in
|
||||
// the same list item. Since we only want to indent that li once, we
|
||||
// must keep track of the most recent indented list item, and not indent
|
||||
// it if we find another node to act on that is still inside the same li.
|
||||
nsCOMPtr<Element> listItem = IsInListItem(curNode);
|
||||
if (listItem) {
|
||||
if (indentedLI == GetAsDOMNode(listItem)) {
|
||||
// already indented this list item
|
||||
if (indentedLI == listItem) {
|
||||
// Already indented this list item
|
||||
continue;
|
||||
}
|
||||
curParent = listItem->GetParentNode();
|
||||
offset = curParent ? curParent->IndexOf(listItem) : -1;
|
||||
// check to see if curList is still appropriate. Which it is if
|
||||
// Check to see if curList is still appropriate. Which it is if
|
||||
// curNode is still right after it in the same list.
|
||||
if (curList)
|
||||
{
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
sibling = GetAsDOMNode(mHTMLEditor->GetPriorHTMLSibling(curNode));
|
||||
if (curList) {
|
||||
sibling = mHTMLEditor->GetPriorHTMLSibling(curNode);
|
||||
}
|
||||
|
||||
if (!curList || (sibling && sibling != GetAsDOMNode(curList))) {
|
||||
// create a new nested list of correct type
|
||||
if (!curList || (sibling && sibling != curList)) {
|
||||
// Create a new nested list of correct type
|
||||
res = SplitAsNeeded(*curParent->NodeInfo()->NameAtom(), curParent,
|
||||
offset);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
@ -8643,30 +8619,23 @@ nsHTMLEditRules::WillAbsolutePosition(Selection* aSelection,
|
||||
nsCOMPtr<nsINode> curParentParent = curParent->GetParentNode();
|
||||
int32_t parentOffset = curParentParent ?
|
||||
curParentParent->IndexOf(curParent) : -1;
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
curPositionedDiv = mHTMLEditor->CreateNode(nsGkAtoms::div,
|
||||
curParentParent,
|
||||
parentOffset);
|
||||
mNewBlock = GetAsDOMNode(curPositionedDiv);
|
||||
}
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
curList = mHTMLEditor->CreateNode(curParent->NodeInfo()->NameAtom(),
|
||||
curPositionedDiv, -1);
|
||||
NS_ENSURE_STATE(curList);
|
||||
}
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
res = mHTMLEditor->MoveNode(listItem, curList, -1);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
// remember we indented this li
|
||||
indentedLI = GetAsDOMNode(listItem);
|
||||
}
|
||||
// Remember we indented this li
|
||||
indentedLI = listItem;
|
||||
} else {
|
||||
// Need to make a div to put things in if we haven't already
|
||||
|
||||
else
|
||||
{
|
||||
// need to make a div to put things in if we haven't already
|
||||
|
||||
if (!curPositionedDiv)
|
||||
{
|
||||
if (!curPositionedDiv) {
|
||||
if (curNode->IsHTMLElement(nsGkAtoms::div)) {
|
||||
curPositionedDiv = curNode->AsElement();
|
||||
mNewBlock = GetAsDOMNode(curPositionedDiv);
|
||||
@ -8675,25 +8644,23 @@ nsHTMLEditRules::WillAbsolutePosition(Selection* aSelection,
|
||||
}
|
||||
res = SplitAsNeeded(*nsGkAtoms::div, curParent, offset);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
curPositionedDiv = mHTMLEditor->CreateNode(nsGkAtoms::div, curParent,
|
||||
offset);
|
||||
NS_ENSURE_STATE(curPositionedDiv);
|
||||
// remember our new block for postprocessing
|
||||
// Remember our new block for postprocessing
|
||||
mNewBlock = GetAsDOMNode(curPositionedDiv);
|
||||
// curPositionedDiv is now the correct thing to put curNode in
|
||||
}
|
||||
|
||||
// tuck the node into the end of the active blockquote
|
||||
NS_ENSURE_STATE(mHTMLEditor);
|
||||
// Tuck the node into the end of the active blockquote
|
||||
res = mHTMLEditor->MoveNode(curNode, curPositionedDiv, -1);
|
||||
NS_ENSURE_SUCCESS(res, res);
|
||||
// forget curList, if any
|
||||
// Forget curList, if any
|
||||
curList = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
|
@ -169,7 +169,7 @@ protected:
|
||||
nsresult WillOutdent(Selection& aSelection, bool* aCancel, bool* aHandled);
|
||||
nsresult WillAlign(Selection& aSelection, const nsAString& aAlignType,
|
||||
bool* aCancel, bool* aHandled);
|
||||
nsresult WillAbsolutePosition(Selection* aSelection, bool* aCancel,
|
||||
nsresult WillAbsolutePosition(Selection& aSelection, bool* aCancel,
|
||||
bool* aHandled);
|
||||
nsresult WillRemoveAbsolutePosition(Selection* aSelection, bool* aCancel,
|
||||
bool* aHandled);
|
||||
|
Loading…
Reference in New Issue
Block a user