Add a working implementation for nsDOMSelection::ContainsNode, and use it in nsDocument::IsInSelection for XIF conversion

git-svn-id: svn://10.0.0.236/trunk@40518 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
akkana%netscape.com
1999-07-21 21:29:29 +00:00
parent 38cb00fc9b
commit cf7c8e5dac
9 changed files with 170 additions and 154 deletions

View File

@@ -2627,10 +2627,10 @@ void nsDocument::ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode)
nsIContent* content = nsnull;
nsresult isContent = aNode->QueryInterface(kIContentIID, (void**)&content);
if (isContent != nsnull)
if (NS_SUCCEEDED(isContent) && content)
{
PRBool isInSelection = IsInSelection(sel,content);
if (isInSelection == PR_TRUE)
{
BeginConvertToXIF(aConverter,aNode);
@@ -2654,8 +2654,9 @@ void nsDocument::ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode)
void nsDocument::CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection)
{
nsXIFConverter converter(aBuffer);
// call the function
printf("nsDocument::CreateXIF\n");
nsXIFConverter converter(aBuffer);
converter.SetSelection(aSelection);
@@ -2667,17 +2668,50 @@ void nsDocument::CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection)
converter.BeginStartTag("document_info");
converter.AddAttribute(nsString("charset"),charset);
converter.FinishStartTag("document_info",PR_TRUE,PR_TRUE);
converter.AddEndTag("section_head");
converter.AddStartTag("section_body");
nsIDOMElement* root = nsnull;
if (NS_OK == GetDocumentElement(&root))
{
#if 1
ToXIF(converter,root);
#else
// Make a content iterator over the selection:
nsCOMPtr<nsIContentIterator> iter;
nsresult res = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull,
nsIContentIterator::GetIID(),
getter_AddRefs(iter));
if ((NS_SUCCEEDED(res)) && iter)
{
nsCOMPtr<nsIContent> rootContent (do_QueryInterface(root));
if (rootContent)
{
iter->Init(rootContent);
// loop through the content iterator for each content node
while (NS_COMFALSE == iter->IsDone())
{
nsCOMPtr<nsIContent> content;
res = iter->CurrentNode(getter_AddRefs(content));
if (NS_FAILED(res))
break;
//content->BeginConvertToXIF(converter);
content->ConvertContentToXIF(converter);
//content->FinishConvertToXIF(converter);
#if 0
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(content));
if (node)
ToXIF(converter, node);
#endif
iter->Next();
}
}
}
#endif
NS_RELEASE(root);
}
converter.AddEndTag("section_body");
converter.AddEndTag("section");
@@ -2904,31 +2938,13 @@ nsIContent* nsDocument::FindContent(const nsIContent* aStartNode,
* @param param -- description
* @return PR_TRUE if the content is found within the selection
*/
PRBool nsDocument::IsInSelection(nsIDOMSelection* aSelection, const nsIContent* aContent) const
PRBool
nsDocument::IsInSelection(nsIDOMSelection* aSelection, const nsIContent* aContent) const
{
PRBool result = PR_FALSE;
if (aSelection != nsnull) {
//traverses through an iterator to see if the acontent is in the ranges
nsIEnumerator *enumerator;
if (NS_SUCCEEDED(aSelection->QueryInterface(kIEnumeratorIID, (void **)&enumerator))
&& enumerator)
{
for (enumerator->First();NS_OK != enumerator->IsDone() ; enumerator->Next()) {
nsIDOMRange* range = nsnull;
if (NS_SUCCEEDED(enumerator->CurrentItem((nsISupports**)&range)))
{
// VC build won't cast away const automatically:
nsIContent* nonConstContent = (nsIContent*)aContent;
return IsNodeIntersectsRange(nonConstContent, range);
NS_RELEASE(range);
}
if (result) break;
}
NS_IF_RELEASE(enumerator);
}
}
return result;
PRBool aYes = PR_FALSE;
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(aContent));
aSelection->ContainsNode(node, PR_FALSE, &aYes);
return aYes;
}
nsIContent* nsDocument::GetPrevContent(const nsIContent *aContent) const

View File

@@ -82,6 +82,7 @@ nsTextEncoder::nsTextEncoder() : mMimeType("text/plain")
NS_INIT_REFCNT();
mDocument = 0;
mSelection = 0;
mAddHeader = PR_FALSE;
}
nsTextEncoder::~nsTextEncoder()
@@ -208,7 +209,8 @@ nsTextEncoder::EncodeToString(nsString& aOutputString)
if (mMimeType == "text/html")
rv = NS_New_HTML_ContentSinkStream(&sink, &aOutputString,
PR_FALSE, mAddHeader);
PR_FALSE,
mSelection ? PR_FALSE : mAddHeader );
else // default to text/plain
rv = NS_New_HTMLToTXT_SinkStream(&sink, &aOutputString,
@@ -272,7 +274,8 @@ nsTextEncoder::EncodeToStream(nsIOutputStream* aStream)
if (mMimeType == "text/html")
rv = NS_New_HTML_ContentSinkStream(&sink, aStream, charset,
PR_FALSE, mAddHeader);
PR_FALSE,
mSelection ? PR_FALSE : mAddHeader);
else
rv = NS_New_HTMLToTXT_SinkStream(&sink, aStream, charset,

View File

@@ -158,9 +158,9 @@ void nsMarkupDocument::StyleSheetsToXIF(nsXIFConverter& aConverter)
PRInt32 count = GetNumberOfStyleSheets();
for (PRInt32 index = 0; index < count; index++)
for (PRInt32 indx = 0; indx < count; indx++)
{
nsIStyleSheet* sheet = GetStyleSheetAt(index);
nsIStyleSheet* sheet = GetStyleSheetAt(indx);
nsICSSStyleSheet* cssSheet = nsnull;
if (sheet != nsnull)
@@ -258,42 +258,4 @@ void nsMarkupDocument::FinishConvertToXIF(nsXIFConverter& aConverter, nsIDOMNode
nsDocument::FinishConvertToXIF(aConverter,aNode);
}
void nsMarkupDocument::ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode)
{
nsIDOMSelection* sel = aConverter.GetSelection();
if (sel != nsnull)
{
nsIContent* content = nsnull;
if (NS_SUCCEEDED(aNode->QueryInterface(kIContentIID, (void**)&content)))
{
PRBool isInSelection = IsInSelection(sel,content);
if (isInSelection == PR_TRUE)
{
BeginConvertToXIF(aConverter,aNode);
ConvertChildrenToXIF(aConverter,aNode);
FinishConvertToXIF(aConverter,aNode);
}
else
{
ConvertChildrenToXIF(aConverter,aNode);
}
NS_RELEASE(content);
}
}
else
{
BeginConvertToXIF(aConverter,aNode);
ConvertChildrenToXIF(aConverter,aNode);
FinishConvertToXIF(aConverter,aNode);
}
}
void nsMarkupDocument::CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection)
{
nsDocument::CreateXIF(aBuffer,aSelection);
}

View File

@@ -49,8 +49,6 @@ public:
* NOTE: we may way to place the result in a stream,
* but we will use a string for now -- gpk
*/
virtual void CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection);
virtual void ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode);
virtual void FinishConvertToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode);
// XXX Temp hack: moved from nsDocument

View File

@@ -2627,10 +2627,10 @@ void nsDocument::ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode)
nsIContent* content = nsnull;
nsresult isContent = aNode->QueryInterface(kIContentIID, (void**)&content);
if (isContent != nsnull)
if (NS_SUCCEEDED(isContent) && content)
{
PRBool isInSelection = IsInSelection(sel,content);
if (isInSelection == PR_TRUE)
{
BeginConvertToXIF(aConverter,aNode);
@@ -2654,8 +2654,9 @@ void nsDocument::ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode)
void nsDocument::CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection)
{
nsXIFConverter converter(aBuffer);
// call the function
printf("nsDocument::CreateXIF\n");
nsXIFConverter converter(aBuffer);
converter.SetSelection(aSelection);
@@ -2667,17 +2668,50 @@ void nsDocument::CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection)
converter.BeginStartTag("document_info");
converter.AddAttribute(nsString("charset"),charset);
converter.FinishStartTag("document_info",PR_TRUE,PR_TRUE);
converter.AddEndTag("section_head");
converter.AddStartTag("section_body");
nsIDOMElement* root = nsnull;
if (NS_OK == GetDocumentElement(&root))
{
#if 1
ToXIF(converter,root);
#else
// Make a content iterator over the selection:
nsCOMPtr<nsIContentIterator> iter;
nsresult res = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull,
nsIContentIterator::GetIID(),
getter_AddRefs(iter));
if ((NS_SUCCEEDED(res)) && iter)
{
nsCOMPtr<nsIContent> rootContent (do_QueryInterface(root));
if (rootContent)
{
iter->Init(rootContent);
// loop through the content iterator for each content node
while (NS_COMFALSE == iter->IsDone())
{
nsCOMPtr<nsIContent> content;
res = iter->CurrentNode(getter_AddRefs(content));
if (NS_FAILED(res))
break;
//content->BeginConvertToXIF(converter);
content->ConvertContentToXIF(converter);
//content->FinishConvertToXIF(converter);
#if 0
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(content));
if (node)
ToXIF(converter, node);
#endif
iter->Next();
}
}
}
#endif
NS_RELEASE(root);
}
converter.AddEndTag("section_body");
converter.AddEndTag("section");
@@ -2904,31 +2938,13 @@ nsIContent* nsDocument::FindContent(const nsIContent* aStartNode,
* @param param -- description
* @return PR_TRUE if the content is found within the selection
*/
PRBool nsDocument::IsInSelection(nsIDOMSelection* aSelection, const nsIContent* aContent) const
PRBool
nsDocument::IsInSelection(nsIDOMSelection* aSelection, const nsIContent* aContent) const
{
PRBool result = PR_FALSE;
if (aSelection != nsnull) {
//traverses through an iterator to see if the acontent is in the ranges
nsIEnumerator *enumerator;
if (NS_SUCCEEDED(aSelection->QueryInterface(kIEnumeratorIID, (void **)&enumerator))
&& enumerator)
{
for (enumerator->First();NS_OK != enumerator->IsDone() ; enumerator->Next()) {
nsIDOMRange* range = nsnull;
if (NS_SUCCEEDED(enumerator->CurrentItem((nsISupports**)&range)))
{
// VC build won't cast away const automatically:
nsIContent* nonConstContent = (nsIContent*)aContent;
return IsNodeIntersectsRange(nonConstContent, range);
NS_RELEASE(range);
}
if (result) break;
}
NS_IF_RELEASE(enumerator);
}
}
return result;
PRBool aYes = PR_FALSE;
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(aContent));
aSelection->ContainsNode(node, PR_FALSE, &aYes);
return aYes;
}
nsIContent* nsDocument::GetPrevContent(const nsIContent *aContent) const

View File

@@ -82,6 +82,7 @@ nsTextEncoder::nsTextEncoder() : mMimeType("text/plain")
NS_INIT_REFCNT();
mDocument = 0;
mSelection = 0;
mAddHeader = PR_FALSE;
}
nsTextEncoder::~nsTextEncoder()
@@ -208,7 +209,8 @@ nsTextEncoder::EncodeToString(nsString& aOutputString)
if (mMimeType == "text/html")
rv = NS_New_HTML_ContentSinkStream(&sink, &aOutputString,
PR_FALSE, mAddHeader);
PR_FALSE,
mSelection ? PR_FALSE : mAddHeader );
else // default to text/plain
rv = NS_New_HTMLToTXT_SinkStream(&sink, &aOutputString,
@@ -272,7 +274,8 @@ nsTextEncoder::EncodeToStream(nsIOutputStream* aStream)
if (mMimeType == "text/html")
rv = NS_New_HTML_ContentSinkStream(&sink, aStream, charset,
PR_FALSE, mAddHeader);
PR_FALSE,
mSelection ? PR_FALSE : mAddHeader);
else
rv = NS_New_HTMLToTXT_SinkStream(&sink, aStream, charset,

View File

@@ -77,7 +77,7 @@ class nsDOMSelection : public nsIDOMSelection , public nsIScriptObjectOwner
{
public:
nsDOMSelection(nsRangeList *aList);
~nsDOMSelection();
virtual ~nsDOMSelection();
NS_DECL_ISUPPORTS
@@ -1168,7 +1168,7 @@ nsDOMSelection::nsDOMSelection(nsRangeList *aList)
{
mRangeList = aList;
mFixupState = PR_FALSE;
nsresult result = NS_NewISupportsArray(getter_AddRefs(mRangeArray));
NS_NewISupportsArray(getter_AddRefs(mRangeArray));
mScriptObject = nsnull;
NS_INIT_REFCNT();
}
@@ -1461,7 +1461,7 @@ nsDOMSelection::RemoveItem(nsIDOMRange *aItem)
{
nsCOMPtr<nsISupports> indexIsupports = dont_AddRef(mRangeArray->ElementAt(i));
nsCOMPtr<nsISupports> isupp;
nsresult result = aItem->QueryInterface(nsCOMTypeInfo<nsISupports>::GetIID(),getter_AddRefs(isupp));
aItem->QueryInterface(nsCOMTypeInfo<nsISupports>::GetIID(),getter_AddRefs(isupp));
if (isupp.get() == indexIsupports.get())
{
mRangeArray->RemoveElementAt(i);
@@ -1891,7 +1891,6 @@ nsDOMSelection::GetIsCollapsed(PRBool* aIsCollapsed)
return (range->GetIsCollapsed(aIsCollapsed));
}
NS_IMETHODIMP
nsDOMSelection::GetRangeCount(PRInt32* aRangeCount)
{
@@ -2474,11 +2473,70 @@ nsDOMSelection::Extend(nsIDOMNode* aParentNode, PRInt32 aOffset)
}
NS_IMETHODIMP
nsDOMSelection::ContainsNode(nsIDOMNode* aNode, PRBool aRecursive, PRBool* aAYes)
nsDOMSelection::ContainsNode(nsIDOMNode* aNode, PRBool aRecursive, PRBool* aYes)
{
return NS_ERROR_NOT_IMPLEMENTED;
if (!aYes)
return NS_ERROR_NULL_POINTER;
*aYes = PR_FALSE;
// Iterate over the ranges in the selection checking for the content:
if (!mRangeArray)
return NS_OK;
PRUint32 cnt;
nsresult rv = mRangeArray->Count(&cnt);
if (NS_FAILED(rv))
return rv;
for (PRUint32 i=0; i < cnt; ++i)
{
nsISupports* element = mRangeArray->ElementAt(i);
nsCOMPtr<nsIDOMRange> range = do_QueryInterface(element);
if (!range)
return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIContent> content (do_QueryInterface(aNode));
if (content)
{
if (IsNodeIntersectsRange(content, range))
{
// If recursive, then we're done -- IsNodeIntersectsRange does the right thing
if (aRecursive)
{
*aYes = PR_TRUE;
return NS_OK;
}
// else not recursive -- node itself must be contained,
// so we need to do more checking
PRBool nodeStartsBeforeRange, nodeEndsAfterRange;
if (NS_SUCCEEDED(CompareNodeToRange(content, range,
&nodeStartsBeforeRange,
&nodeEndsAfterRange)))
{
#ifdef DEBUG_ContainsNode
nsAutoString name, value;
aNode->GetNodeName(name);
aNode->GetNodeValue(value);
printf("%s [%s]: %d, %d\n", name.ToNewCString(), value.ToNewCString(),
nodeStartsBeforeRange, nodeEndsAfterRange);
#endif
PRUint16 nodeType;
aNode->GetNodeType(&nodeType);
if ((!nodeStartsBeforeRange && !nodeEndsAfterRange)
|| (nodeType == nsIDOMNode::TEXT_NODE
&& (!nodeStartsBeforeRange || !nodeEndsAfterRange)))
{
*aYes = PR_TRUE;
return NS_OK;
}
}
}
}
}
return NS_OK;
}
NS_IMETHODIMP
nsDOMSelection::ScrollIntoView()
{

View File

@@ -158,9 +158,9 @@ void nsMarkupDocument::StyleSheetsToXIF(nsXIFConverter& aConverter)
PRInt32 count = GetNumberOfStyleSheets();
for (PRInt32 index = 0; index < count; index++)
for (PRInt32 indx = 0; indx < count; indx++)
{
nsIStyleSheet* sheet = GetStyleSheetAt(index);
nsIStyleSheet* sheet = GetStyleSheetAt(indx);
nsICSSStyleSheet* cssSheet = nsnull;
if (sheet != nsnull)
@@ -258,42 +258,4 @@ void nsMarkupDocument::FinishConvertToXIF(nsXIFConverter& aConverter, nsIDOMNode
nsDocument::FinishConvertToXIF(aConverter,aNode);
}
void nsMarkupDocument::ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode)
{
nsIDOMSelection* sel = aConverter.GetSelection();
if (sel != nsnull)
{
nsIContent* content = nsnull;
if (NS_SUCCEEDED(aNode->QueryInterface(kIContentIID, (void**)&content)))
{
PRBool isInSelection = IsInSelection(sel,content);
if (isInSelection == PR_TRUE)
{
BeginConvertToXIF(aConverter,aNode);
ConvertChildrenToXIF(aConverter,aNode);
FinishConvertToXIF(aConverter,aNode);
}
else
{
ConvertChildrenToXIF(aConverter,aNode);
}
NS_RELEASE(content);
}
}
else
{
BeginConvertToXIF(aConverter,aNode);
ConvertChildrenToXIF(aConverter,aNode);
FinishConvertToXIF(aConverter,aNode);
}
}
void nsMarkupDocument::CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection)
{
nsDocument::CreateXIF(aBuffer,aSelection);
}

View File

@@ -49,8 +49,6 @@ public:
* NOTE: we may way to place the result in a stream,
* but we will use a string for now -- gpk
*/
virtual void CreateXIF(nsString & aBuffer, nsIDOMSelection* aSelection);
virtual void ToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode);
virtual void FinishConvertToXIF(nsXIFConverter& aConverter, nsIDOMNode* aNode);
// XXX Temp hack: moved from nsDocument