diff --git a/editor/base/nsTextEditRules.cpp b/editor/base/nsTextEditRules.cpp index 198b4545bbd..e3e76f2f09d 100644 --- a/editor/base/nsTextEditRules.cpp +++ b/editor/base/nsTextEditRules.cpp @@ -243,7 +243,7 @@ nsTextEditRules::WillInsertText(nsIDOMSelection *aSelection, // if len(doc) is at or over max, cancel the insert // if l(doc) + l(input) > max, set aOutString to subset of inString so length = max PRInt32 docLength; - result = GetLengthOfDocumentInCharacters(docLength); + result = mEditor->GetDocumentLength(&docLength); if (NS_SUCCEEDED(result)) { if (docLength >= aMaxLength) @@ -272,7 +272,7 @@ nsTextEditRules::WillInsertText(nsIDOMSelection *aSelection, { // manage the password buffer PRInt32 start, end; - result = GetTextSelectionOffsets(aSelection, start, end); + result = mEditor->GetTextSelectionOffsets(aSelection, start, end); NS_ASSERTION((NS_SUCCEEDED(result)), "getTextSelectionOffsets failed!"); mPasswordText.Insert(inString, start); @@ -346,7 +346,7 @@ nsTextEditRules::CreateStyleForInsertText(nsIDOMSelection *aSelection, TypeInSta { PRUint32 length; anchorAsText->GetLength(&length); - if (length==offset) + if (length==(PRUint32)offset) { // newTextNode will be the left node result = mEditor->SplitNode(anchorAsText, offset, getter_AddRefs(newTextNode)); @@ -626,7 +626,7 @@ nsTextEditRules::WillDeleteSelection(nsIDOMSelection *aSelection, { // manage the password buffer PRInt32 start, end; - GetTextSelectionOffsets(aSelection, start, end); + mEditor->GetTextSelectionOffsets(aSelection, start, end); if (end==start) { // collapsed selection if (nsIEditor::eDeleteLeft==aCollapsedAction && 0 sel; - mEditor->GetSelection(getter_AddRefs(sel)); - if ((NS_SUCCEEDED(result)) && sel) - { - nsAutoSelectionReset selectionResetter(sel); - result = mEditor->SelectAll(); - if (NS_SUCCEEDED(result)) - { - PRInt32 start, end; - result = GetTextSelectionOffsets(sel, start, end); - if (NS_SUCCEEDED(result)) - { - NS_ASSERTION(0==start, "GetTextSelectionOffsets failed to set start correctly."); - NS_ASSERTION(0<=end, "GetTextSelectionOffsets failed to set end correctly."); - if (0<=end) { - aCount = end; - } - } - } - } - return result; -} - -// this is a complete ripoff from nsTextEditor::GetTextSelectionOffsetsForRange -// the two should use common code, or even just be one method -nsresult nsTextEditRules::GetTextSelectionOffsets(nsIDOMSelection *aSelection, - PRInt32 &aStartOffset, - PRInt32 &aEndOffset) -{ - nsresult result; - // initialize out params - aStartOffset = 0; // default to first char in selection - aEndOffset = -1; // default to total length of text in selection - - nsCOMPtr startNode, endNode, parentNode; - PRInt32 startOffset, endOffset; - aSelection->GetAnchorNode(getter_AddRefs(startNode)); - aSelection->GetAnchorOffset(&startOffset); - aSelection->GetFocusNode(getter_AddRefs(endNode)); - aSelection->GetFocusOffset(&endOffset); - - nsCOMPtr enumerator; - enumerator = do_QueryInterface(aSelection); - if (enumerator) - { - // don't use "result" in this block - enumerator->First(); - nsISupports *currentItem; - nsresult findParentResult = enumerator->CurrentItem(¤tItem); - if ((NS_SUCCEEDED(findParentResult)) && (nsnull!=currentItem)) - { - nsCOMPtr range( do_QueryInterface(currentItem) ); - range->GetCommonParent(getter_AddRefs(parentNode)); - } - else parentNode = do_QueryInterface(startNode); - } - - nsCOMPtr iter; - result = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull, - nsIContentIterator::GetIID(), - getter_AddRefs(iter)); - if ((NS_SUCCEEDED(result)) && iter) - { - PRUint32 totalLength=0; - nsCOMPtrtextNode; - nsCOMPtrblockParentContent = do_QueryInterface(parentNode); - iter->Init(blockParentContent); - // loop through the content iterator for each content node - nsCOMPtr content; - result = iter->CurrentNode(getter_AddRefs(content)); - while (NS_COMFALSE == iter->IsDone()) - { - textNode = do_QueryInterface(content); - if (textNode) - { - nsCOMPtrcurrentNode = do_QueryInterface(textNode); - if (!currentNode) {return NS_ERROR_NO_INTERFACE;} - if (PR_TRUE==mEditor->IsEditable(currentNode)) - { - if (currentNode.get() == startNode.get()) - { - aStartOffset = totalLength + startOffset; - } - if (currentNode.get() == endNode.get()) - { - aEndOffset = totalLength + endOffset; - break; - } - PRUint32 length; - textNode->GetLength(&length); - totalLength += length; - } - } - iter->Next(); - iter->CurrentNode(getter_AddRefs(content)); - } - if (-1==aEndOffset) { - aEndOffset = totalLength; - } - } - return result; -} - - - - diff --git a/editor/base/nsTextEditRules.h b/editor/base/nsTextEditRules.h index 19e0076024b..2cce8e15b45 100644 --- a/editor/base/nsTextEditRules.h +++ b/editor/base/nsTextEditRules.h @@ -150,16 +150,6 @@ protected: /** creates a bogus text node if the document has no editable content */ nsresult CreateBogusNodeIfNeeded(nsIDOMSelection *aSelection); - /** set aCount to the number of characters in the document. */ - nsresult GetLengthOfDocumentInCharacters(PRInt32 &aCount); - - /** returns the absolute position of the end points of aSelection - * in the document as a text stream. - */ - nsresult GetTextSelectionOffsets(nsIDOMSelection *aSelection, - PRInt32 &aStartOffset, - PRInt32 &aEndOffset); - // data nsTextEditor *mEditor; // note that we do not refcount the editor nsString mPasswordText; // a buffer we use to store the real value of password editors diff --git a/editor/base/nsTextEditor.cpp b/editor/base/nsTextEditor.cpp index 16fe3676b1d..85f388f1078 100644 --- a/editor/base/nsTextEditor.cpp +++ b/editor/base/nsTextEditor.cpp @@ -2817,3 +2817,120 @@ nsTextEditor::StopLogging() return nsEditor::StopLogging(); } + +NS_IMETHODIMP +nsTextEditor::GetDocumentLength(PRInt32 *aCount) +{ + if (!aCount) { return NS_ERROR_NULL_POINTER; } + nsresult result; + // initialize out params + *aCount = 0; + + nsCOMPtr sel; + GetSelection(getter_AddRefs(sel)); + if ((NS_SUCCEEDED(result)) && sel) + { + nsAutoSelectionReset selectionResetter(sel); + result = SelectAll(); + if (NS_SUCCEEDED(result)) + { + PRInt32 start, end; + result = GetTextSelectionOffsets(sel, start, end); + if (NS_SUCCEEDED(result)) + { + NS_ASSERTION(0==start, "GetTextSelectionOffsets failed to set start correctly."); + NS_ASSERTION(0<=end, "GetTextSelectionOffsets failed to set end correctly."); + if (0<=end) { + *aCount = end; + } + } + } + } + return result; +} + +// this is a complete ripoff from nsTextEditor::GetTextSelectionOffsetsForRange +// the two should use common code, or even just be one method +nsresult nsTextEditor::GetTextSelectionOffsets(nsIDOMSelection *aSelection, + PRInt32 &aStartOffset, + PRInt32 &aEndOffset) +{ + nsresult result; + // initialize out params + aStartOffset = 0; // default to first char in selection + aEndOffset = -1; // default to total length of text in selection + + nsCOMPtr startNode, endNode, parentNode; + PRInt32 startOffset, endOffset; + aSelection->GetAnchorNode(getter_AddRefs(startNode)); + aSelection->GetAnchorOffset(&startOffset); + aSelection->GetFocusNode(getter_AddRefs(endNode)); + aSelection->GetFocusOffset(&endOffset); + + nsCOMPtr enumerator; + enumerator = do_QueryInterface(aSelection); + if (enumerator) + { + // don't use "result" in this block + enumerator->First(); + nsISupports *currentItem; + nsresult findParentResult = enumerator->CurrentItem(¤tItem); + if ((NS_SUCCEEDED(findParentResult)) && (nsnull!=currentItem)) + { + nsCOMPtr range( do_QueryInterface(currentItem) ); + range->GetCommonParent(getter_AddRefs(parentNode)); + } + else parentNode = do_QueryInterface(startNode); + } + + nsCOMPtr iter; + result = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull, + nsIContentIterator::GetIID(), + getter_AddRefs(iter)); + if ((NS_SUCCEEDED(result)) && iter) + { + PRUint32 totalLength=0; + nsCOMPtrtextNode; + nsCOMPtrblockParentContent = do_QueryInterface(parentNode); + iter->Init(blockParentContent); + // loop through the content iterator for each content node + nsCOMPtr content; + result = iter->CurrentNode(getter_AddRefs(content)); + while (NS_COMFALSE == iter->IsDone()) + { + textNode = do_QueryInterface(content); + if (textNode) + { + nsCOMPtrcurrentNode = do_QueryInterface(textNode); + if (!currentNode) {return NS_ERROR_NO_INTERFACE;} + if (PR_TRUE==IsEditable(currentNode)) + { + if (currentNode.get() == startNode.get()) + { + aStartOffset = totalLength + startOffset; + } + if (currentNode.get() == endNode.get()) + { + aEndOffset = totalLength + endOffset; + break; + } + PRUint32 length; + textNode->GetLength(&length); + totalLength += length; + } + } + iter->Next(); + iter->CurrentNode(getter_AddRefs(content)); + } + if (-1==aEndOffset) { + aEndOffset = totalLength; + } + } + return result; +} + + + + + + diff --git a/editor/base/nsTextEditor.h b/editor/base/nsTextEditor.h index 3e051ba0889..94374ba1c26 100644 --- a/editor/base/nsTextEditor.h +++ b/editor/base/nsTextEditor.h @@ -54,6 +54,8 @@ public: NS_IMETHOD GetFlags(PRUint32 *aFlags); NS_IMETHOD SetFlags(PRUint32 aFlags); + NS_IMETHOD GetDocumentLength(PRInt32 *aCount); + //============================================================================ // Methods that are duplicates of nsEditor -- exposed here for convenience @@ -273,6 +275,13 @@ protected: PRInt32 aStartOffset, PRInt32 aEndOffset, nsIDOMSelection *aSelection); + + /** returns the absolute position of the end points of aSelection + * in the document as a text stream. + */ + nsresult GetTextSelectionOffsets(nsIDOMSelection *aSelection, + PRInt32 &aStartOffset, + PRInt32 &aEndOffset); TypeInState *GetTypeInState(); diff --git a/editor/libeditor/text/nsTextEditRules.cpp b/editor/libeditor/text/nsTextEditRules.cpp index 198b4545bbd..e3e76f2f09d 100644 --- a/editor/libeditor/text/nsTextEditRules.cpp +++ b/editor/libeditor/text/nsTextEditRules.cpp @@ -243,7 +243,7 @@ nsTextEditRules::WillInsertText(nsIDOMSelection *aSelection, // if len(doc) is at or over max, cancel the insert // if l(doc) + l(input) > max, set aOutString to subset of inString so length = max PRInt32 docLength; - result = GetLengthOfDocumentInCharacters(docLength); + result = mEditor->GetDocumentLength(&docLength); if (NS_SUCCEEDED(result)) { if (docLength >= aMaxLength) @@ -272,7 +272,7 @@ nsTextEditRules::WillInsertText(nsIDOMSelection *aSelection, { // manage the password buffer PRInt32 start, end; - result = GetTextSelectionOffsets(aSelection, start, end); + result = mEditor->GetTextSelectionOffsets(aSelection, start, end); NS_ASSERTION((NS_SUCCEEDED(result)), "getTextSelectionOffsets failed!"); mPasswordText.Insert(inString, start); @@ -346,7 +346,7 @@ nsTextEditRules::CreateStyleForInsertText(nsIDOMSelection *aSelection, TypeInSta { PRUint32 length; anchorAsText->GetLength(&length); - if (length==offset) + if (length==(PRUint32)offset) { // newTextNode will be the left node result = mEditor->SplitNode(anchorAsText, offset, getter_AddRefs(newTextNode)); @@ -626,7 +626,7 @@ nsTextEditRules::WillDeleteSelection(nsIDOMSelection *aSelection, { // manage the password buffer PRInt32 start, end; - GetTextSelectionOffsets(aSelection, start, end); + mEditor->GetTextSelectionOffsets(aSelection, start, end); if (end==start) { // collapsed selection if (nsIEditor::eDeleteLeft==aCollapsedAction && 0 sel; - mEditor->GetSelection(getter_AddRefs(sel)); - if ((NS_SUCCEEDED(result)) && sel) - { - nsAutoSelectionReset selectionResetter(sel); - result = mEditor->SelectAll(); - if (NS_SUCCEEDED(result)) - { - PRInt32 start, end; - result = GetTextSelectionOffsets(sel, start, end); - if (NS_SUCCEEDED(result)) - { - NS_ASSERTION(0==start, "GetTextSelectionOffsets failed to set start correctly."); - NS_ASSERTION(0<=end, "GetTextSelectionOffsets failed to set end correctly."); - if (0<=end) { - aCount = end; - } - } - } - } - return result; -} - -// this is a complete ripoff from nsTextEditor::GetTextSelectionOffsetsForRange -// the two should use common code, or even just be one method -nsresult nsTextEditRules::GetTextSelectionOffsets(nsIDOMSelection *aSelection, - PRInt32 &aStartOffset, - PRInt32 &aEndOffset) -{ - nsresult result; - // initialize out params - aStartOffset = 0; // default to first char in selection - aEndOffset = -1; // default to total length of text in selection - - nsCOMPtr startNode, endNode, parentNode; - PRInt32 startOffset, endOffset; - aSelection->GetAnchorNode(getter_AddRefs(startNode)); - aSelection->GetAnchorOffset(&startOffset); - aSelection->GetFocusNode(getter_AddRefs(endNode)); - aSelection->GetFocusOffset(&endOffset); - - nsCOMPtr enumerator; - enumerator = do_QueryInterface(aSelection); - if (enumerator) - { - // don't use "result" in this block - enumerator->First(); - nsISupports *currentItem; - nsresult findParentResult = enumerator->CurrentItem(¤tItem); - if ((NS_SUCCEEDED(findParentResult)) && (nsnull!=currentItem)) - { - nsCOMPtr range( do_QueryInterface(currentItem) ); - range->GetCommonParent(getter_AddRefs(parentNode)); - } - else parentNode = do_QueryInterface(startNode); - } - - nsCOMPtr iter; - result = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull, - nsIContentIterator::GetIID(), - getter_AddRefs(iter)); - if ((NS_SUCCEEDED(result)) && iter) - { - PRUint32 totalLength=0; - nsCOMPtrtextNode; - nsCOMPtrblockParentContent = do_QueryInterface(parentNode); - iter->Init(blockParentContent); - // loop through the content iterator for each content node - nsCOMPtr content; - result = iter->CurrentNode(getter_AddRefs(content)); - while (NS_COMFALSE == iter->IsDone()) - { - textNode = do_QueryInterface(content); - if (textNode) - { - nsCOMPtrcurrentNode = do_QueryInterface(textNode); - if (!currentNode) {return NS_ERROR_NO_INTERFACE;} - if (PR_TRUE==mEditor->IsEditable(currentNode)) - { - if (currentNode.get() == startNode.get()) - { - aStartOffset = totalLength + startOffset; - } - if (currentNode.get() == endNode.get()) - { - aEndOffset = totalLength + endOffset; - break; - } - PRUint32 length; - textNode->GetLength(&length); - totalLength += length; - } - } - iter->Next(); - iter->CurrentNode(getter_AddRefs(content)); - } - if (-1==aEndOffset) { - aEndOffset = totalLength; - } - } - return result; -} - - - - diff --git a/editor/libeditor/text/nsTextEditRules.h b/editor/libeditor/text/nsTextEditRules.h index 19e0076024b..2cce8e15b45 100644 --- a/editor/libeditor/text/nsTextEditRules.h +++ b/editor/libeditor/text/nsTextEditRules.h @@ -150,16 +150,6 @@ protected: /** creates a bogus text node if the document has no editable content */ nsresult CreateBogusNodeIfNeeded(nsIDOMSelection *aSelection); - /** set aCount to the number of characters in the document. */ - nsresult GetLengthOfDocumentInCharacters(PRInt32 &aCount); - - /** returns the absolute position of the end points of aSelection - * in the document as a text stream. - */ - nsresult GetTextSelectionOffsets(nsIDOMSelection *aSelection, - PRInt32 &aStartOffset, - PRInt32 &aEndOffset); - // data nsTextEditor *mEditor; // note that we do not refcount the editor nsString mPasswordText; // a buffer we use to store the real value of password editors diff --git a/editor/public/nsITextEditor.h b/editor/public/nsITextEditor.h index 6aecd160ae5..7bd70bf43a4 100644 --- a/editor/public/nsITextEditor.h +++ b/editor/public/nsITextEditor.h @@ -76,6 +76,9 @@ public: /** set the edit flags for this editor. May be called at any time. */ NS_IMETHOD SetFlags(PRUint32 aFlags)=0; + /** get the length of the document in characters */ + NS_IMETHOD GetDocumentLength(PRInt32 *aCount)=0; + /** * SetTextProperties() sets the aggregate properties on the current selection *