Bug 1190172 part 7 - Clean up nsHTMLEditor::PromoteInlineRange; r=ehsan

This commit is contained in:
Aryeh Gregor 2016-05-01 16:12:39 +03:00
Родитель 1588157e3f
Коммит ae41a2dd75
2 изменённых файлов: 48 добавлений и 64 удалений

Просмотреть файл

@ -659,7 +659,7 @@ protected:
const nsAString* aAttribute,
const nsAString& aValue);
nsresult PromoteInlineRange(nsRange* aRange);
nsresult PromoteInlineRange(nsRange& aRange);
nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* aRange);
nsresult SplitStyleAboveRange(nsRange* aRange,
nsIAtom *aProperty,
@ -678,8 +678,8 @@ protected:
const nsAString* aAttribute);
bool NodeIsProperty(nsINode& aNode);
bool IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset);
bool IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset);
bool IsAtFrontOfNode(nsINode& aNode, int32_t aOffset);
bool IsAtEndOfNode(nsINode& aNode, int32_t aOffset);
bool IsOnlyAttribute(const nsIContent* aElement, const nsAString& aAttribute);
nsresult RemoveBlockContainer(nsIDOMNode *inNode);

Просмотреть файл

@ -145,7 +145,7 @@ nsHTMLEditor::SetInlineProperty(nsIAtom* aProperty,
// Adjust range to include any ancestors whose children are entirely
// selected
res = PromoteInlineRange(range);
res = PromoteInlineRange(*range);
NS_ENSURE_SUCCESS(res, res);
// Check for easy case: both range endpoints in same text node
@ -899,90 +899,74 @@ nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor(nsRange* inRange)
}
nsresult
nsHTMLEditor::PromoteInlineRange(nsRange* inRange)
nsHTMLEditor::PromoteInlineRange(nsRange& aRange)
{
NS_ENSURE_TRUE(inRange, NS_ERROR_NULL_POINTER);
nsresult res;
nsCOMPtr<nsIDOMNode> startNode, endNode, parent;
int32_t startOffset, endOffset;
nsCOMPtr<nsINode> startNode = aRange.GetStartParent();
int32_t startOffset = aRange.StartOffset();
nsCOMPtr<nsINode> endNode = aRange.GetEndParent();
int32_t endOffset = aRange.EndOffset();
res = inRange->GetStartContainer(getter_AddRefs(startNode));
NS_ENSURE_SUCCESS(res, res);
res = inRange->GetStartOffset(&startOffset);
NS_ENSURE_SUCCESS(res, res);
res = inRange->GetEndContainer(getter_AddRefs(endNode));
NS_ENSURE_SUCCESS(res, res);
res = inRange->GetEndOffset(&endOffset);
NS_ENSURE_SUCCESS(res, res);
while ( startNode &&
!nsTextEditUtils::IsBody(startNode) &&
IsEditable(startNode) &&
IsAtFrontOfNode(startNode, startOffset) )
{
parent = GetNodeLocation(startNode, &startOffset);
while (startNode && !startNode->IsHTMLElement(nsGkAtoms::body) &&
IsEditable(startNode) && IsAtFrontOfNode(*startNode, startOffset)) {
nsCOMPtr<nsINode> parent = startNode->GetParentNode();
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
startOffset = parent->IndexOf(startNode);
startNode = parent;
}
NS_ENSURE_TRUE(startNode, NS_ERROR_NULL_POINTER);
while ( endNode &&
!nsTextEditUtils::IsBody(endNode) &&
IsEditable(endNode) &&
IsAtEndOfNode(endNode, endOffset) )
{
parent = GetNodeLocation(endNode, &endOffset);
while (endNode && !endNode->IsHTMLElement(nsGkAtoms::body) &&
IsEditable(endNode) && IsAtEndOfNode(*endNode, endOffset)) {
nsCOMPtr<nsINode> parent = endNode->GetParentNode();
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
// We are AFTER this node
endOffset = 1 + parent->IndexOf(endNode);
endNode = parent;
endOffset++; // we are AFTER this node
}
NS_ENSURE_TRUE(endNode, NS_ERROR_NULL_POINTER);
res = inRange->SetStart(startNode, startOffset);
nsresult res = aRange.SetStart(startNode, startOffset);
NS_ENSURE_SUCCESS(res, res);
res = inRange->SetEnd(endNode, endOffset);
return res;
res = aRange.SetEnd(endNode, endOffset);
NS_ENSURE_SUCCESS(res, res);
return NS_OK;
}
bool nsHTMLEditor::IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset)
bool
nsHTMLEditor::IsAtFrontOfNode(nsINode& aNode, int32_t aOffset)
{
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
NS_ENSURE_TRUE(node, false);
if (!aOffset) {
return true;
}
if (IsTextNode(aNode))
{
if (IsTextNode(&aNode)) {
return false;
}
else
{
nsCOMPtr<nsIContent> firstNode = GetFirstEditableChild(*node);
nsCOMPtr<nsIContent> firstNode = GetFirstEditableChild(aNode);
NS_ENSURE_TRUE(firstNode, true);
int32_t offset = node->IndexOf(firstNode);
if (offset < aOffset) return false;
if (aNode.IndexOf(firstNode) < aOffset) {
return false;
}
return true;
}
bool
nsHTMLEditor::IsAtEndOfNode(nsINode& aNode, int32_t aOffset)
{
if (aOffset == (int32_t)aNode.Length()) {
return true;
}
bool nsHTMLEditor::IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset)
{
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
NS_ENSURE_TRUE(node, false);
uint32_t len = node->Length();
if (aOffset == (int32_t)len) return true;
if (IsTextNode(aNode))
{
if (IsTextNode(&aNode)) {
return false;
}
else
{
nsCOMPtr<nsIContent> lastNode = GetLastEditableChild(*node);
nsCOMPtr<nsIContent> lastNode = GetLastEditableChild(aNode);
NS_ENSURE_TRUE(lastNode, true);
int32_t offset = node->IndexOf(lastNode);
if (offset < aOffset) return true;
return false;
if (aNode.IndexOf(lastNode) < aOffset) {
return true;
}
return false;
}
@ -1265,7 +1249,7 @@ nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom* aProperty,
} else {
// Adjust range to include any ancestors whose children are entirely
// selected
res = PromoteInlineRange(range);
res = PromoteInlineRange(*range);
}
NS_ENSURE_SUCCESS(res, res);
@ -1402,7 +1386,7 @@ nsHTMLEditor::RelativeFontChange(FontSize aDir)
RefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
// Adjust range to include any ancestors with entirely selected children
nsresult res = PromoteInlineRange(range);
nsresult res = PromoteInlineRange(*range);
NS_ENSURE_SUCCESS(res, res);
// Check for easy case: both range endpoints in same text node