Bug 1147412 part 2 - Clean up nsHTMLEditor::SetInlinePropertyOnTextNode; r=ehsan

This commit is contained in:
Aryeh Gregor 2015-04-19 15:28:50 +03:00
parent 5599bf4096
commit 65f2d842a0
2 changed files with 80 additions and 108 deletions

View File

@ -646,12 +646,12 @@ protected:
nsresult RelativeFontChangeHelper(int32_t aSizeChange, nsINode* aNode);
/* helper routines for inline style */
nsresult SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
int32_t aStartOffset,
int32_t aEndOffset,
nsIAtom *aProperty,
const nsAString *aAttribute,
const nsAString *aValue);
nsresult SetInlinePropertyOnTextNode(mozilla::dom::Text& aData,
int32_t aStartOffset,
int32_t aEndOffset,
nsIAtom& aProperty,
const nsAString* aAttribute,
const nsAString& aValue);
nsresult SetInlinePropertyOnNode( nsIDOMNode *aNode,
nsIAtom *aProperty,
const nsAString *aAttribute,

View File

@ -154,18 +154,13 @@ nsHTMLEditor::SetInlineProperty(nsIAtom *aProperty,
NS_ENSURE_SUCCESS(res, res);
// check for easy case: both range endpoints in same text node
nsCOMPtr<nsIDOMNode> startNode, endNode;
res = range->GetStartContainer(getter_AddRefs(startNode));
NS_ENSURE_SUCCESS(res, res);
res = range->GetEndContainer(getter_AddRefs(endNode));
NS_ENSURE_SUCCESS(res, res);
if (startNode == endNode && IsTextNode(startNode)) {
int32_t startOffset, endOffset;
range->GetStartOffset(&startOffset);
range->GetEndOffset(&endOffset);
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(startNode);
res = SetInlinePropertyOnTextNode(nodeAsText, startOffset, endOffset,
aProperty, &aAttribute, &aValue);
nsCOMPtr<nsINode> startNode = range->GetStartParent();
nsCOMPtr<nsINode> endNode = range->GetEndParent();
if (startNode && startNode == endNode && startNode->GetAsText()) {
res = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
range->StartOffset(),
range->EndOffset(),
*aProperty, &aAttribute, aValue);
NS_ENSURE_SUCCESS(res, res);
continue;
}
@ -207,14 +202,11 @@ nsHTMLEditor::SetInlineProperty(nsIAtom *aProperty,
// first check the start parent of the range to see if it needs to
// be separately handled (it does if it's a text node, due to how the
// subtree iterator works - it will not have reported it).
if (IsTextNode(startNode) && IsEditable(startNode)) {
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(startNode);
int32_t startOffset;
uint32_t textLen;
range->GetStartOffset(&startOffset);
nodeAsText->GetLength(&textLen);
res = SetInlinePropertyOnTextNode(nodeAsText, startOffset, textLen,
aProperty, &aAttribute, &aValue);
if (startNode && startNode->GetAsText() && IsEditable(startNode)) {
res = SetInlinePropertyOnTextNode(*startNode->GetAsText(),
range->StartOffset(),
startNode->Length(), *aProperty,
&aAttribute, aValue);
NS_ENSURE_SUCCESS(res, res);
}
@ -230,12 +222,10 @@ nsHTMLEditor::SetInlineProperty(nsIAtom *aProperty,
// last check the end parent of the range to see if it needs to
// be separately handled (it does if it's a text node, due to how the
// subtree iterator works - it will not have reported it).
if (IsTextNode(endNode) && IsEditable(endNode)) {
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(endNode);
int32_t endOffset;
range->GetEndOffset(&endOffset);
res = SetInlinePropertyOnTextNode(nodeAsText, 0, endOffset,
aProperty, &aAttribute, &aValue);
if (endNode && endNode->GetAsText() && IsEditable(endNode)) {
res = SetInlinePropertyOnTextNode(*endNode->GetAsText(), 0,
range->EndOffset(), *aProperty,
&aAttribute, aValue);
NS_ENSURE_SUCCESS(res, res);
}
}
@ -326,81 +316,67 @@ nsHTMLEditor::IsSimpleModifiableNode(nsIContent* aContent,
nsresult
nsHTMLEditor::SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
int32_t aStartOffset,
int32_t aEndOffset,
nsIAtom *aProperty,
const nsAString *aAttribute,
const nsAString *aValue)
nsHTMLEditor::SetInlinePropertyOnTextNode(Text& aText,
int32_t aStartOffset,
int32_t aEndOffset,
nsIAtom& aProperty,
const nsAString* aAttribute,
const nsAString& aValue)
{
MOZ_ASSERT(aValue);
nsCOMPtr<nsIContent> textNode = do_QueryInterface(aTextNode);
NS_ENSURE_TRUE(textNode, NS_ERROR_NULL_POINTER);
if (!textNode->GetParentNode() ||
!CanContainTag(*textNode->GetParentNode(), *aProperty)) {
if (!aText.GetParentNode() ||
!CanContainTag(*aText.GetParentNode(), aProperty)) {
return NS_OK;
}
// don't need to do anything if no characters actually selected
if (aStartOffset == aEndOffset) return NS_OK;
nsCOMPtr<nsIDOMNode> node = aTextNode;
// don't need to do anything if property already set on node
bool bHasProp;
if (mHTMLCSSUtils->IsCSSEditableProperty(node, aProperty, aAttribute)) {
// the HTML styles defined by aProperty/aAttribute has a CSS equivalence
// in this implementation for node; let's check if it carries those css styles
nsAutoString value(*aValue);
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(node, aProperty, aAttribute,
bHasProp, value,
nsHTMLCSSUtils::eComputed);
} else {
IsTextPropertySetByContent(node, aProperty, aAttribute, aValue, bHasProp);
// Don't need to do anything if no characters actually selected
if (aStartOffset == aEndOffset) {
return NS_OK;
}
if (bHasProp) return NS_OK;
// do we need to split the text node?
uint32_t textLen;
aTextNode->GetLength(&textLen);
// Don't need to do anything if property already set on node
if (mHTMLCSSUtils->IsCSSEditableProperty(&aText, &aProperty, aAttribute)) {
// The HTML styles defined by aProperty/aAttribute have a CSS equivalence
// for node; let's check if it carries those CSS styles
if (mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(&aText, &aProperty,
aAttribute, aValue, nsHTMLCSSUtils::eComputed)) {
return NS_OK;
}
} else if (IsTextPropertySetByContent(&aText, &aProperty, aAttribute,
&aValue)) {
return NS_OK;
}
nsresult res;
if (uint32_t(aEndOffset) != textLen) {
// we need to split off back of text node
nsCOMPtr<nsIDOMNode> tmp;
res = SplitNode(node, aEndOffset, getter_AddRefs(tmp));
NS_ENSURE_SUCCESS(res, res);
node = tmp; // remember left node
// Do we need to split the text node?
ErrorResult rv;
nsRefPtr<Text> text = &aText;
if (uint32_t(aEndOffset) != aText.Length()) {
// We need to split off back of text node
text = SplitNode(aText, aEndOffset, rv)->GetAsText();
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
}
if (aStartOffset) {
// we need to split off front of text node
nsCOMPtr<nsIDOMNode> tmp;
res = SplitNode(node, aStartOffset, getter_AddRefs(tmp));
NS_ENSURE_SUCCESS(res, res);
// We need to split off front of text node
SplitNode(*text, aStartOffset, rv);
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
}
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
NS_ENSURE_STATE(content);
if (aAttribute) {
// look for siblings that are correct type of node
nsIContent* sibling = GetPriorHTMLSibling(content);
if (IsSimpleModifiableNode(sibling, aProperty, aAttribute, aValue)) {
// previous sib is already right kind of inline node; slide this over into it
return MoveNode(content, sibling, -1);
// Look for siblings that are correct type of node
nsIContent* sibling = GetPriorHTMLSibling(text);
if (IsSimpleModifiableNode(sibling, &aProperty, aAttribute, &aValue)) {
// Previous sib is already right kind of inline node; slide this over
return MoveNode(text, sibling, -1);
}
sibling = GetNextHTMLSibling(content);
if (IsSimpleModifiableNode(sibling, aProperty, aAttribute, aValue)) {
// following sib is already right kind of inline node; slide this over into it
return MoveNode(content, sibling, 0);
sibling = GetNextHTMLSibling(text);
if (IsSimpleModifiableNode(sibling, &aProperty, aAttribute, &aValue)) {
// Following sib is already right kind of inline node; slide this over
return MoveNode(text, sibling, 0);
}
}
// reparent the node inside inline node with appropriate {attribute,value}
return SetInlinePropertyOnNode(node, aProperty, aAttribute, aValue);
// Reparent the node inside inline node with appropriate {attribute,value}
return SetInlinePropertyOnNode(text, &aProperty, aAttribute, &aValue);
}
@ -1394,23 +1370,18 @@ nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAStr
NS_ENSURE_SUCCESS(res, res);
// check for easy case: both range endpoints in same text node
nsCOMPtr<nsIDOMNode> startNode, endNode;
res = range->GetStartContainer(getter_AddRefs(startNode));
NS_ENSURE_SUCCESS(res, res);
res = range->GetEndContainer(getter_AddRefs(endNode));
NS_ENSURE_SUCCESS(res, res);
if ((startNode == endNode) && IsTextNode(startNode))
{
nsCOMPtr<nsINode> startNode = range->GetStartParent();
nsCOMPtr<nsINode> endNode = range->GetEndParent();
if (startNode && startNode == endNode && startNode->GetAsText()) {
// we're done with this range!
if (useCSS && mHTMLCSSUtils->IsCSSEditableProperty(startNode, aProperty, aAttribute)) {
if (useCSS && mHTMLCSSUtils->IsCSSEditableProperty(startNode,
aProperty,
aAttribute)) {
// the HTML style defined by aProperty/aAttribute has a CSS equivalence
// in this implementation for startNode
nsAutoString cssValue;
bool isSet = false;
mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(startNode,
aProperty, aAttribute, isSet , cssValue,
nsHTMLCSSUtils::eComputed);
if (isSet) {
if (mHTMLCSSUtils->IsCSSEquivalentToHTMLInlineStyleSet(startNode,
aProperty, aAttribute, EmptyString(),
nsHTMLCSSUtils::eComputed)) {
// startNode's computed style indicates the CSS equivalence to the HTML style to
// remove is applied; but we found no element in the ancestors of startNode
// carrying specified styles; assume it comes from a rule and let's try to
@ -1419,9 +1390,10 @@ nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAStr
int32_t startOffset, endOffset;
range->GetStartOffset(&startOffset);
range->GetEndOffset(&endOffset);
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(startNode);
if (mHTMLCSSUtils->IsCSSInvertable(aProperty, aAttribute)) {
SetInlinePropertyOnTextNode(nodeAsText, startOffset, endOffset, aProperty, aAttribute, &value);
SetInlinePropertyOnTextNode(*startNode->GetAsText(), startOffset,
endOffset, *aProperty, aAttribute,
value);
}
}
}