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); nsresult RelativeFontChangeHelper(int32_t aSizeChange, nsINode* aNode);
/* helper routines for inline style */ /* helper routines for inline style */
nsresult SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode, nsresult SetInlinePropertyOnTextNode(mozilla::dom::Text& aData,
int32_t aStartOffset, int32_t aStartOffset,
int32_t aEndOffset, int32_t aEndOffset,
nsIAtom *aProperty, nsIAtom& aProperty,
const nsAString *aAttribute, const nsAString* aAttribute,
const nsAString *aValue); const nsAString& aValue);
nsresult SetInlinePropertyOnNode( nsIDOMNode *aNode, nsresult SetInlinePropertyOnNode( nsIDOMNode *aNode,
nsIAtom *aProperty, nsIAtom *aProperty,
const nsAString *aAttribute, const nsAString *aAttribute,

View File

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