490: infrastructure for inserting formatting whitespace. Not turned on yet except for DEBUG_akkana

This commit is contained in:
akkana%netscape.com 1999-09-10 18:54:13 +00:00
parent 09bb112935
commit 332f71e329
9 changed files with 209 additions and 6 deletions

View File

@ -74,6 +74,11 @@ NS_IMETHODIMP SplitElementTxn::Do(void)
result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
if (NS_SUCCEEDED(result) && mNewLeftNode)
{
#ifdef DEBUG_akkana
// Insert formatting whitespace for the new node:
mEditor->InsertFormattingForNode(mExistingRightNode);
#endif
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;

View File

@ -862,6 +862,91 @@ nsEditor::SplitNode(nsIDOMNode * aNode,
}
//
// Insert a noneditable text node, e.g. formatting whitespace
//
nsresult
nsEditor::InsertNoneditableTextNode(nsIDOMNode* parent, PRInt32 offset,
nsString& aStr)
{
nsAutoString textNodeTag;
nsresult res = GetTextNodeTag(textNodeTag);
if (NS_FAILED(res))
return res;
// Can't call CreateNode, because that will call us recursively.
// So duplicate what it does:
CreateElementTxn *txn;
res = CreateTxnForCreateElement(textNodeTag, parent, offset, &txn);
if (NS_FAILED(res))
return res;
res = Do(txn);
if (NS_FAILED(res))
return res;
// Now get the pointer to the node we just created ...
nsCOMPtr<nsIDOMNode> newNode;
res = txn->GetNewNode(getter_AddRefs(newNode));
if (NS_FAILED(res))
return res;
nsCOMPtr<nsIDOMCharacterData> newTextNode;
newTextNode = do_QueryInterface(newNode);
if (!newTextNode)
return NS_ERROR_UNEXPECTED;
// ... and set its text.
return newTextNode->SetData(aStr);
}
//
// Figure out what formatting needs to go with this node, and insert it.
//
NS_IMETHODIMP
nsEditor::InsertFormattingForNode(nsIDOMNode* aNode)
{
nsresult res;
// Don't insert any formatting unless it's an element node
PRUint16 nodeType;
res = aNode->GetNodeType(&nodeType);
if (NS_FAILED(res))
return res;
if (nodeType != nsIDOMNode::ELEMENT_NODE)
return NS_OK;
nsCOMPtr<nsIDOMNode> parent;
res = aNode->GetParentNode(getter_AddRefs(parent));
if (NS_FAILED(res))
return res;
PRInt32 offset = GetIndexOf(parent, aNode);
#ifdef DEBUG_akkana
DumpContentTree();
nsString namestr;
aNode->GetNodeName(namestr);
char* nodename = namestr.ToNewCString();
printf("Inserting formatting for node <%s> at offset %d\n",
nodename, offset);
delete[] nodename;
#endif /* DEBUG_akkana */
//
// XXX We don't yet have a real formatter. As a cheap stopgap,
// XXX just insert a newline before and after each newly inserted tag.
//
nsAutoString str (NS_LINEBREAK);
// After the close tag
//res = InsertNoneditableTextNode(parent, offset+1, str);
// Before the open tag
res = InsertNoneditableTextNode(parent, offset, str);
return res;
}
NS_IMETHODIMP
nsEditor::JoinNodes(nsIDOMNode * aLeftNode,
nsIDOMNode * aRightNode,

View File

@ -172,6 +172,12 @@ public:
NS_IMETHOD DeleteNode(nsIDOMNode * aChild);
/* formatting within the dom tree */
NS_IMETHOD InsertNoneditableTextNode(nsIDOMNode* aParent,
PRInt32 aOffset,
nsString& aStr);
NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode);
/* output */
NS_IMETHOD OutputToString(nsString& aOutputString,

View File

@ -2594,9 +2594,6 @@ NS_IMETHODIMP nsHTMLEditor::GetBodyWrapWidth(PRInt32 *aWrapColumn)
//
NS_IMETHODIMP nsHTMLEditor::SetBodyWrapWidth(PRInt32 aWrapColumn)
{
#ifdef DEBUG_akkana
printf("SetBodyWrapWidth(%d)\n", aWrapColumn);
#endif
nsresult res;
// Ought to set a style sheet here ...
@ -5877,3 +5874,4 @@ nsHTMLEditor::CollapseAdjacentTextNodes(nsIDOMSelection *aInSelection)
return result;
}

View File

@ -74,6 +74,11 @@ NS_IMETHODIMP SplitElementTxn::Do(void)
result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
if (NS_SUCCEEDED(result) && mNewLeftNode)
{
#ifdef DEBUG_akkana
// Insert formatting whitespace for the new node:
mEditor->InsertFormattingForNode(mExistingRightNode);
#endif
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;

View File

@ -862,6 +862,91 @@ nsEditor::SplitNode(nsIDOMNode * aNode,
}
//
// Insert a noneditable text node, e.g. formatting whitespace
//
nsresult
nsEditor::InsertNoneditableTextNode(nsIDOMNode* parent, PRInt32 offset,
nsString& aStr)
{
nsAutoString textNodeTag;
nsresult res = GetTextNodeTag(textNodeTag);
if (NS_FAILED(res))
return res;
// Can't call CreateNode, because that will call us recursively.
// So duplicate what it does:
CreateElementTxn *txn;
res = CreateTxnForCreateElement(textNodeTag, parent, offset, &txn);
if (NS_FAILED(res))
return res;
res = Do(txn);
if (NS_FAILED(res))
return res;
// Now get the pointer to the node we just created ...
nsCOMPtr<nsIDOMNode> newNode;
res = txn->GetNewNode(getter_AddRefs(newNode));
if (NS_FAILED(res))
return res;
nsCOMPtr<nsIDOMCharacterData> newTextNode;
newTextNode = do_QueryInterface(newNode);
if (!newTextNode)
return NS_ERROR_UNEXPECTED;
// ... and set its text.
return newTextNode->SetData(aStr);
}
//
// Figure out what formatting needs to go with this node, and insert it.
//
NS_IMETHODIMP
nsEditor::InsertFormattingForNode(nsIDOMNode* aNode)
{
nsresult res;
// Don't insert any formatting unless it's an element node
PRUint16 nodeType;
res = aNode->GetNodeType(&nodeType);
if (NS_FAILED(res))
return res;
if (nodeType != nsIDOMNode::ELEMENT_NODE)
return NS_OK;
nsCOMPtr<nsIDOMNode> parent;
res = aNode->GetParentNode(getter_AddRefs(parent));
if (NS_FAILED(res))
return res;
PRInt32 offset = GetIndexOf(parent, aNode);
#ifdef DEBUG_akkana
DumpContentTree();
nsString namestr;
aNode->GetNodeName(namestr);
char* nodename = namestr.ToNewCString();
printf("Inserting formatting for node <%s> at offset %d\n",
nodename, offset);
delete[] nodename;
#endif /* DEBUG_akkana */
//
// XXX We don't yet have a real formatter. As a cheap stopgap,
// XXX just insert a newline before and after each newly inserted tag.
//
nsAutoString str (NS_LINEBREAK);
// After the close tag
//res = InsertNoneditableTextNode(parent, offset+1, str);
// Before the open tag
res = InsertNoneditableTextNode(parent, offset, str);
return res;
}
NS_IMETHODIMP
nsEditor::JoinNodes(nsIDOMNode * aLeftNode,
nsIDOMNode * aRightNode,

View File

@ -172,6 +172,12 @@ public:
NS_IMETHOD DeleteNode(nsIDOMNode * aChild);
/* formatting within the dom tree */
NS_IMETHOD InsertNoneditableTextNode(nsIDOMNode* aParent,
PRInt32 aOffset,
nsString& aStr);
NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode);
/* output */
NS_IMETHOD OutputToString(nsString& aOutputString,

View File

@ -2594,9 +2594,6 @@ NS_IMETHODIMP nsHTMLEditor::GetBodyWrapWidth(PRInt32 *aWrapColumn)
//
NS_IMETHODIMP nsHTMLEditor::SetBodyWrapWidth(PRInt32 aWrapColumn)
{
#ifdef DEBUG_akkana
printf("SetBodyWrapWidth(%d)\n", aWrapColumn);
#endif
nsresult res;
// Ought to set a style sheet here ...
@ -5877,3 +5874,4 @@ nsHTMLEditor::CollapseAdjacentTextNodes(nsIDOMSelection *aInSelection)
return result;
}

View File

@ -352,6 +352,21 @@ public:
*/
NS_IMETHOD DeleteNode(nsIDOMNode * aChild)=0;
/**
* InsertNoneditableTextNode() inserts a noneditable text node, e.g. for formatting.
* @param aParent The parent of the newly created node
* @param aOffset The offset in the parent for the new node
* @param aStr The string contents to be placed in the node
*/
NS_IMETHOD InsertNoneditableTextNode(nsIDOMNode* aParent, PRInt32 aOffset,
nsString& aStr) = 0;
/**
* InsertFormattingForNode() inserts formatting before and/or after a node.
* Usually this will be called immediately after creating a new node.
* @param aNode The node for which to insert formatting.
*/
NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode) = 0;
/* ------------ Output methods -------------- */
/**