2000-09-11 05:10:32 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
* Daniel Bratell <bratell@lysator.liu.se>
|
|
|
|
* Ben Bucksch <mozilla@bucksch.org>
|
2000-09-11 05:10:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsPlainTextSerializer.h"
|
|
|
|
#include "nsILineBreakerFactory.h"
|
|
|
|
#include "nsLWBrkCIID.h"
|
|
|
|
#include "nsIPref.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsHTMLAtoms.h"
|
|
|
|
#include "nsIDOMText.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsIHTMLContent.h"
|
|
|
|
#include "nsITextContent.h"
|
|
|
|
#include "nsTextFragment.h"
|
|
|
|
#include "nsParserCIID.h"
|
2001-02-19 12:55:42 +00:00
|
|
|
#include "nsContentUtils.h"
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kLWBrkCID, NS_LWBRK_CID);
|
|
|
|
static NS_DEFINE_CID(kPrefServiceCID, NS_PREF_CID);
|
|
|
|
static NS_DEFINE_CID(kParserServiceCID, NS_PARSERSERVICE_CID);
|
|
|
|
|
|
|
|
#define PREF_STRUCTS "converter.html2txt.structs"
|
|
|
|
#define PREF_HEADER_STRATEGY "converter.html2txt.header_strategy"
|
|
|
|
|
|
|
|
static const PRInt32 kTabSize=4;
|
|
|
|
static const PRInt32 kOLNumberWidth = 3;
|
|
|
|
static const PRInt32 kIndentSizeHeaders = 2; /* Indention of h1, if
|
|
|
|
mHeaderStrategy = 1 or = 2.
|
|
|
|
Indention of other headers
|
|
|
|
is derived from that.
|
|
|
|
XXX center h1? */
|
|
|
|
static const PRInt32 kIndentIncrementHeaders = 2; /* If mHeaderStrategy = 1,
|
|
|
|
indent h(x+1) this many
|
|
|
|
columns more than h(x) */
|
|
|
|
static const PRInt32 kIndentSizeList = (kTabSize > kOLNumberWidth+3) ? kTabSize: kOLNumberWidth+3;
|
|
|
|
// Indention of non-first lines of ul and ol
|
|
|
|
static const PRInt32 kIndentSizeDD = kTabSize; // Indention of <dd>
|
|
|
|
|
|
|
|
static PRInt32 HeaderLevel(eHTMLTags aTag);
|
2001-01-12 07:26:39 +00:00
|
|
|
static PRInt32 GetUnicharWidth(PRUnichar ucs);
|
|
|
|
static PRInt32 GetUnicharStringWidth(const PRUnichar* pwcs, PRInt32 n);
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
// Someday may want to make this non-const:
|
|
|
|
static const PRUint32 TagStackSize = 500;
|
|
|
|
static const PRUint32 OLStackSize = 100;
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
nsresult NS_NewPlainTextSerializer(nsIContentSerializer** aSerializer)
|
|
|
|
{
|
|
|
|
nsPlainTextSerializer* it = new nsPlainTextSerializer();
|
|
|
|
if (!it) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it->QueryInterface(NS_GET_IID(nsIContentSerializer),
|
|
|
|
(void**)aSerializer);
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
nsPlainTextSerializer::nsPlainTextSerializer()
|
2001-01-12 07:26:39 +00:00
|
|
|
: kSpace(NS_LITERAL_STRING(" ")) // Init of "constant"
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
|
|
|
|
|
|
|
mOutputString = nsnull;
|
|
|
|
mInHead = PR_FALSE;
|
2001-01-12 07:26:39 +00:00
|
|
|
mAtFirstColumn = PR_TRUE;
|
2000-09-11 05:10:32 +00:00
|
|
|
mIndent = 0;
|
|
|
|
mCiteQuoteLevel = 0;
|
|
|
|
mStructs = PR_TRUE; // will be read from prefs later
|
|
|
|
mHeaderStrategy = 1 /*indent increasingly*/; // ditto
|
2001-07-13 18:45:53 +00:00
|
|
|
mQuotesPreformatted = PR_FALSE; // ditto
|
|
|
|
mSpanLevel = 0;
|
2000-09-11 05:10:32 +00:00
|
|
|
for (PRInt32 i = 0; i <= 6; i++) {
|
|
|
|
mHeaderCounter[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line breaker
|
|
|
|
mWrapColumn = 72; // XXX magic number, we expect someone to reset this
|
|
|
|
mCurrentLineWidth = 0;
|
|
|
|
|
|
|
|
// Flow
|
|
|
|
mEmptyLines = 1; // The start of the document is an "empty line" in itself,
|
|
|
|
mInWhitespace = PR_TRUE;
|
|
|
|
mPreFormatted = PR_FALSE;
|
|
|
|
mStartedOutput = PR_FALSE;
|
|
|
|
|
|
|
|
// initialize the tag stack to zero:
|
|
|
|
mTagStack = new nsHTMLTag[TagStackSize];
|
|
|
|
mTagStackIndex = 0;
|
|
|
|
|
|
|
|
// initialize the OL stack, where numbers for ordered lists are kept:
|
|
|
|
mOLStack = new PRInt32[OLStackSize];
|
|
|
|
mOLStackIndex = 0;
|
2001-04-10 01:11:39 +00:00
|
|
|
|
|
|
|
mULCount = 0;
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPlainTextSerializer::~nsPlainTextSerializer()
|
|
|
|
{
|
|
|
|
delete[] mTagStack;
|
|
|
|
delete[] mOLStack;
|
|
|
|
}
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
NS_IMPL_ISUPPORTS4(nsPlainTextSerializer,
|
2000-09-11 05:10:32 +00:00
|
|
|
nsIContentSerializer,
|
|
|
|
nsIContentSink,
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
nsIHTMLContentSink,
|
|
|
|
nsIHTMLToTextSink)
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-13 01:00:14 +00:00
|
|
|
nsPlainTextSerializer::Init(PRUint32 aFlags, PRUint32 aWrapColumn,
|
|
|
|
nsIAtom* aCharSet)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
2001-01-12 07:26:39 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Check if the major control flags are set correctly.
|
|
|
|
if(aFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
|
|
|
NS_ASSERTION(aFlags & nsIDocumentEncoder::OutputFormatted,
|
|
|
|
"If you want format=flowed, you must combine it with "
|
|
|
|
"nsIDocumentEncoder::OutputFormatted");
|
|
|
|
}
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
if(aFlags & nsIDocumentEncoder::OutputFormatted) {
|
|
|
|
NS_ASSERTION(!(aFlags & nsIDocumentEncoder::OutputPreformatted),
|
|
|
|
"Can't do formatted and preformatted output at the same time!");
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mFlags = aFlags;
|
|
|
|
mWrapColumn = aWrapColumn;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
// Only create a linebreaker if we will handle wrapping.
|
|
|
|
if(MayWrap()) {
|
|
|
|
NS_WITH_SERVICE(nsILineBreakerFactory, lf, kLWBrkCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsAutoString lbarg;
|
|
|
|
rv = lf->GetBreaker(lbarg, getter_AddRefs(mLineBreaker));
|
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
|
|
}
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the line break character:
|
|
|
|
if ((mFlags & nsIDocumentEncoder::OutputCRLineBreak)
|
2001-06-15 19:52:50 +00:00
|
|
|
&& (mFlags & nsIDocumentEncoder::OutputLFLineBreak)) // Windows
|
2001-04-26 19:33:12 +00:00
|
|
|
mLineBreak.AssignWithConversion("\r\n");
|
2000-09-11 05:10:32 +00:00
|
|
|
else if (mFlags & nsIDocumentEncoder::OutputCRLineBreak) // Mac
|
2001-01-12 07:26:39 +00:00
|
|
|
mLineBreak.Assign(PRUnichar('\r'));
|
2000-09-11 05:10:32 +00:00
|
|
|
else if (mFlags & nsIDocumentEncoder::OutputLFLineBreak) // Unix/DOM
|
2001-01-12 07:26:39 +00:00
|
|
|
mLineBreak.Assign(PRUnichar('\n'));
|
2000-09-11 05:10:32 +00:00
|
|
|
else
|
|
|
|
mLineBreak.AssignWithConversion(NS_LINEBREAK); // Platform/default
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
|
|
|
|
if(mFlags & nsIDocumentEncoder::OutputFormatted) {
|
|
|
|
// Get some prefs that controls how we do formatted output
|
|
|
|
NS_WITH_SERVICE(nsIPref, prefs, NS_PREF_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && prefs) {
|
|
|
|
prefs->GetBoolPref(PREF_STRUCTS, &mStructs);
|
|
|
|
prefs->GetIntPref(PREF_HEADER_STRATEGY, &mHeaderStrategy);
|
2001-07-13 18:45:53 +00:00
|
|
|
// The quotesPreformatted pref is a temporary measure. See bug 69638.
|
|
|
|
prefs->GetBoolPref("editor.quotesPreformatted", &mQuotesPreformatted);
|
2001-01-12 07:26:39 +00:00
|
|
|
}
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::Initialize(nsAWritableString* aOutString,
|
|
|
|
PRUint32 aFlags, PRUint32 aWrapCol)
|
|
|
|
{
|
2001-02-13 01:00:14 +00:00
|
|
|
nsresult rv = Init(aFlags, aWrapCol, nsnull);
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// XXX This is wrong. It violates XPCOM string ownership rules.
|
|
|
|
// We're only getting away with this because instances of this
|
|
|
|
// class are restricted to single function scope.
|
|
|
|
mOutputString = aOutString;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::AppendText(nsIDOMText* aText,
|
|
|
|
PRInt32 aStartOffset,
|
|
|
|
PRInt32 aEndOffset,
|
|
|
|
nsAWritableString& aStr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aText);
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
nsresult rv = NS_OK;
|
2000-09-11 05:10:32 +00:00
|
|
|
PRInt32 length = 0;
|
|
|
|
nsAutoString textstr;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITextContent> content = do_QueryInterface(aText);
|
|
|
|
if (!content) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
const nsTextFragment* frag;
|
|
|
|
content->GetText(&frag);
|
|
|
|
|
|
|
|
if (frag) {
|
|
|
|
length = ((aEndOffset == -1) ? frag->GetLength() : aEndOffset) - aStartOffset;
|
|
|
|
if (frag->Is2b()) {
|
|
|
|
textstr.Assign(frag->Get2b() + aStartOffset, length);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
textstr.AssignWithConversion(frag->Get1b()+aStartOffset, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
mOutputString = &aStr;
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
nsAutoString linebuffer;
|
|
|
|
|
|
|
|
// We have to split the string across newlines
|
|
|
|
// to match parser behavior
|
|
|
|
PRInt32 start = 0;
|
|
|
|
PRInt32 offset = textstr.FindCharInSet("\n\r");
|
|
|
|
while (offset != kNotFound) {
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
if(offset>start) {
|
|
|
|
// Pass in the line
|
|
|
|
textstr.Mid(linebuffer, start, offset-start);
|
|
|
|
rv = DoAddLeaf(eHTMLTag_text, linebuffer);
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
}
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
// Pass in a newline
|
|
|
|
rv = DoAddLeaf(eHTMLTag_newline, mLineBreak);
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
if (NS_FAILED(rv)) break;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
start = offset+1;
|
|
|
|
offset = textstr.FindCharInSet("\n\r", start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the last bit of the string if there's any left
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
if (NS_SUCCEEDED(rv) & (start < length)) {
|
2000-09-11 05:10:32 +00:00
|
|
|
if (start) {
|
|
|
|
textstr.Mid(linebuffer, start, offset-start);
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
rv = DoAddLeaf(eHTMLTag_text, linebuffer);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else {
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
rv = DoAddLeaf(eHTMLTag_text, textstr);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
mOutputString = nsnull;
|
|
|
|
|
|
|
|
return rv;
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::AppendElementStart(nsIDOMElement *aElement,
|
|
|
|
nsAWritableString& aStr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aElement);
|
|
|
|
|
|
|
|
mContent = do_QueryInterface(aElement);
|
|
|
|
if (!mContent) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
PRInt32 id;
|
|
|
|
rv = GetIdForContent(mContent, &id);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
PRBool isContainer = IsContainer(id);
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
mOutputString = &aStr;
|
|
|
|
|
|
|
|
if (isContainer) {
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
rv = DoOpenContainer(id);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsAutoString empty;
|
|
|
|
rv = DoAddLeaf(id, empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
mContent = 0;
|
|
|
|
mOutputString = nsnull;
|
|
|
|
|
2000-10-11 22:50:14 +00:00
|
|
|
if (!mInHead && id == eHTMLTag_head)
|
|
|
|
mInHead = PR_TRUE;
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::AppendElementEnd(nsIDOMElement *aElement,
|
|
|
|
nsAWritableString& aStr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aElement);
|
|
|
|
|
|
|
|
mContent = do_QueryInterface(aElement);
|
|
|
|
if (!mContent) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
PRInt32 id;
|
|
|
|
rv = GetIdForContent(mContent, &id);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
PRBool isContainer = IsContainer(id);
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
mOutputString = &aStr;
|
|
|
|
|
|
|
|
rv = NS_OK;
|
|
|
|
if (isContainer) {
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
rv = DoCloseContainer(id);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mContent = 0;
|
|
|
|
mOutputString = nsnull;
|
|
|
|
|
2000-10-11 22:50:14 +00:00
|
|
|
if (mInHead && id == eHTMLTag_head)
|
|
|
|
mInHead = PR_FALSE;
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::Flush(nsAWritableString& aStr)
|
|
|
|
{
|
|
|
|
mOutputString = &aStr;
|
|
|
|
FlushLine();
|
|
|
|
mOutputString = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::OpenContainer(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
PRInt32 type = aNode.GetNodeType();
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
mParserNode = NS_CONST_CAST(nsIParserNode *, &aNode);
|
|
|
|
return DoOpenContainer(type);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::CloseContainer(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
PRInt32 type = aNode.GetNodeType();
|
2000-12-12 21:58:13 +00:00
|
|
|
const nsAReadableString& namestr = aNode.GetText();
|
2000-09-11 05:10:32 +00:00
|
|
|
nsCOMPtr<nsIAtom> name = dont_AddRef(NS_NewAtom(namestr));
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
mParserNode = NS_CONST_CAST(nsIParserNode *, &aNode);
|
|
|
|
return DoCloseContainer(type);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::AddLeaf(const nsIParserNode& aNode)
|
|
|
|
{
|
2001-01-22 23:11:02 +00:00
|
|
|
eHTMLTags type = (eHTMLTags)aNode.GetNodeType();
|
2000-12-12 21:58:13 +00:00
|
|
|
const nsAReadableString& text = aNode.GetText();
|
2000-09-11 05:10:32 +00:00
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
mParserNode = NS_CONST_CAST(nsIParserNode *, &aNode);
|
2001-01-22 23:11:02 +00:00
|
|
|
if ((type == eHTMLTag_text) ||
|
|
|
|
(type == eHTMLTag_whitespace) ||
|
|
|
|
(type == eHTMLTag_newline)) {
|
|
|
|
// Copy the text out, stripping out CRs
|
|
|
|
nsAutoString str;
|
|
|
|
PRUint32 length;
|
|
|
|
str.SetCapacity(text.Length());
|
|
|
|
nsReadingIterator<PRUnichar> srcStart, srcEnd;
|
2001-02-19 12:55:42 +00:00
|
|
|
length = nsContentUtils::CopyNewlineNormalizedUnicodeTo(text.BeginReading(srcStart), text.EndReading(srcEnd), str);
|
2001-01-22 23:11:02 +00:00
|
|
|
str.SetLength(length);
|
|
|
|
return DoAddLeaf(type, str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return DoAddLeaf(type, text);
|
|
|
|
}
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::OpenHTML(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return OpenContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::CloseHTML(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return CloseContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::OpenHead(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
mInHead = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::CloseHead(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
mInHead = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::OpenBody(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return OpenContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::CloseBody(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return CloseContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::OpenForm(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return OpenContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::CloseForm(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return CloseContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::OpenMap(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return OpenContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::CloseMap(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return CloseContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::OpenFrameset(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return OpenContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::CloseFrameset(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
return CloseContainer(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::DoFragment(PRBool aFlag)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
nsPlainTextSerializer::DoOpenContainer(PRInt32 aTag)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
eHTMLTags type = (eHTMLTags)aTag;
|
|
|
|
|
|
|
|
if (mTagStackIndex < TagStackSize) {
|
|
|
|
mTagStack[mTagStackIndex++] = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eHTMLTag_body) {
|
|
|
|
// Try to figure out here whether we have a
|
|
|
|
// preformatted style attribute.
|
|
|
|
//
|
|
|
|
// Trigger on the presence of a "-moz-pre-wrap" in the
|
|
|
|
// style attribute. That's a very simplistic way to do
|
|
|
|
// it, but better than nothing.
|
|
|
|
// Also set mWrapColumn to the value given there
|
|
|
|
// (which arguably we should only do if told to do so).
|
|
|
|
nsAutoString style;
|
|
|
|
PRInt32 whitespace;
|
|
|
|
if(NS_SUCCEEDED(GetAttributeValue(nsHTMLAtoms::style, style)) &&
|
2001-01-12 07:26:39 +00:00
|
|
|
(kNotFound != (whitespace = style.Find("white-space:")))) {
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
if (kNotFound != style.Find("-moz-pre-wrap", PR_TRUE, whitespace)) {
|
2000-09-11 05:10:32 +00:00
|
|
|
#ifdef DEBUG_preformatted
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("Set mPreFormatted based on style moz-pre-wrap\n");
|
2000-09-11 05:10:32 +00:00
|
|
|
#endif
|
|
|
|
mPreFormatted = PR_TRUE;
|
|
|
|
PRInt32 widthOffset = style.Find("width:");
|
|
|
|
if (widthOffset >= 0) {
|
|
|
|
// We have to search for the ch before the semicolon,
|
|
|
|
// not for the semicolon itself, because nsString::ToInteger()
|
|
|
|
// considers 'c' to be a valid numeric char (even if radix=10)
|
|
|
|
// but then gets confused if it sees it next to the number
|
|
|
|
// when the radix specified was 10, and returns an error code.
|
|
|
|
PRInt32 semiOffset = style.Find("ch", widthOffset+6);
|
|
|
|
PRInt32 length = (semiOffset > 0 ? semiOffset - widthOffset - 6
|
|
|
|
: style.Length() - widthOffset);
|
|
|
|
nsAutoString widthstr;
|
|
|
|
style.Mid(widthstr, widthOffset+6, length);
|
|
|
|
PRInt32 err;
|
|
|
|
PRInt32 col = widthstr.ToInteger(&err);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(err)) {
|
|
|
|
mWrapColumn = (PRUint32)col;
|
|
|
|
#ifdef DEBUG_preformatted
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("Set wrap column to %d based on style\n", mWrapColumn);
|
2000-09-11 05:10:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
else if (kNotFound != style.Find("pre", PR_TRUE, whitespace)) {
|
2000-09-11 05:10:32 +00:00
|
|
|
#ifdef DEBUG_preformatted
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("Set mPreFormatted based on style pre\n");
|
2000-09-11 05:10:32 +00:00
|
|
|
#endif
|
|
|
|
mPreFormatted = PR_TRUE;
|
|
|
|
mWrapColumn = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mPreFormatted = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoOutput()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == eHTMLTag_p || type == eHTMLTag_pre) {
|
|
|
|
EnsureVerticalSpace(1); // Should this be 0 in unformatted case?
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_td || type == eHTMLTag_th) {
|
|
|
|
// We must make sure that the content of two table cells get a
|
|
|
|
// space between them.
|
|
|
|
|
|
|
|
// Fow now, I will only add a SPACE. Could be a TAB or something
|
|
|
|
// else but I'm not sure everything can handle the TAB so SPACE
|
|
|
|
// seems like a better solution.
|
|
|
|
if(!mInWhitespace) {
|
|
|
|
// Maybe add something else? Several spaces? A TAB? SPACE+TAB?
|
2001-06-30 11:02:25 +00:00
|
|
|
AddToLine(kSpace.get(), 1);
|
2000-09-11 05:10:32 +00:00
|
|
|
mInWhitespace = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2001-01-10 22:08:08 +00:00
|
|
|
else if (type == eHTMLTag_ul) {
|
2001-04-10 01:11:39 +00:00
|
|
|
// Indent here to support nested lists, which aren't included in li :-(
|
|
|
|
EnsureVerticalSpace(mULCount + mOLStackIndex == 0 ? 1 : 0);
|
|
|
|
// Must end the current line before we change indention
|
2001-01-10 22:08:08 +00:00
|
|
|
mIndent += kIndentSizeList;
|
2001-04-10 01:11:39 +00:00
|
|
|
mULCount++;
|
2001-01-10 22:08:08 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_ol) {
|
2001-04-10 01:11:39 +00:00
|
|
|
EnsureVerticalSpace(mULCount + mOLStackIndex == 0 ? 1 : 0);
|
|
|
|
// Must end the current line before we change indention
|
2001-01-10 22:08:08 +00:00
|
|
|
if (mOLStackIndex < OLStackSize) {
|
|
|
|
mOLStack[mOLStackIndex++] = 1; // XXX should get it from the node!
|
|
|
|
}
|
|
|
|
mIndent += kIndentSizeList; // see ul
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_li) {
|
|
|
|
if (mTagStackIndex > 1 && mTagStack[mTagStackIndex-2] == eHTMLTag_ol) {
|
|
|
|
if (mOLStackIndex > 0) {
|
|
|
|
// This is what nsBulletFrame does for OLs:
|
|
|
|
mInIndentString.AppendInt(mOLStack[mOLStackIndex-1]++, 10);
|
|
|
|
}
|
|
|
|
else {
|
2001-01-12 07:26:39 +00:00
|
|
|
mInIndentString.Append(PRUnichar('#'));
|
2001-01-10 22:08:08 +00:00
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
mInIndentString.Append(PRUnichar('.'));
|
2001-01-10 22:08:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2001-01-12 07:26:39 +00:00
|
|
|
mInIndentString.Append(PRUnichar('*'));
|
2001-01-10 22:08:08 +00:00
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
mInIndentString.Append(PRUnichar(' '));
|
2001-01-10 22:08:08 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_dl) {
|
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_dd) {
|
|
|
|
mIndent += kIndentSizeDD;
|
|
|
|
}
|
2001-07-13 18:45:53 +00:00
|
|
|
else if (type == eHTMLTag_span) {
|
|
|
|
++mSpanLevel;
|
|
|
|
}
|
2001-01-10 22:08:08 +00:00
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
// Else make sure we'll separate block level tags,
|
|
|
|
// even if we're about to leave, before doing any other formatting.
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
else if (IsBlockLevel(aTag)) {
|
2000-09-11 05:10:32 +00:00
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
}
|
|
|
|
|
2001-01-10 22:08:08 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 05:10:32 +00:00
|
|
|
if (!(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-10 22:08:08 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// The rest of this routine is formatted output stuff,
|
|
|
|
// which we should skip if we're not formatted:
|
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
if (type == eHTMLTag_h1 || type == eHTMLTag_h2 ||
|
|
|
|
type == eHTMLTag_h3 || type == eHTMLTag_h4 ||
|
|
|
|
type == eHTMLTag_h5 || type == eHTMLTag_h6)
|
|
|
|
{
|
|
|
|
EnsureVerticalSpace(2);
|
|
|
|
if (mHeaderStrategy == 2) { // numbered
|
|
|
|
mIndent += kIndentSizeHeaders;
|
|
|
|
// Caching
|
|
|
|
nsCAutoString leadup;
|
|
|
|
PRInt32 level = HeaderLevel(type);
|
|
|
|
// Increase counter for current level
|
|
|
|
mHeaderCounter[level]++;
|
|
|
|
// Reset all lower levels
|
|
|
|
PRInt32 i;
|
|
|
|
|
|
|
|
for (i = level + 1; i <= 6; i++) {
|
|
|
|
mHeaderCounter[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct numbers
|
|
|
|
for (i = 1; i <= level; i++) {
|
|
|
|
leadup.AppendInt(mHeaderCounter[i]);
|
|
|
|
leadup += ".";
|
|
|
|
}
|
|
|
|
leadup += " ";
|
2001-03-02 09:26:57 +00:00
|
|
|
Write(NS_ConvertASCIItoUCS2(leadup.get()));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (mHeaderStrategy == 1) { // indent increasingly
|
|
|
|
mIndent += kIndentSizeHeaders;
|
|
|
|
for (PRInt32 i = HeaderLevel(type); i > 1; i--) {
|
|
|
|
// for h(x), run x-1 times
|
|
|
|
mIndent += kIndentIncrementHeaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_blockquote) {
|
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
nsresult rv = GetAttributeValue(nsHTMLAtoms::type, value);
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && value.EqualsIgnoreCase("cite")) {
|
2000-09-11 05:10:32 +00:00
|
|
|
mCiteQuoteLevel++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mIndent += kTabSize; // Check for some maximum value?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_a && !IsCurrentNodeConverted()) {
|
|
|
|
nsAutoString url;
|
|
|
|
if (NS_SUCCEEDED(GetAttributeValue(nsHTMLAtoms::href, url))
|
|
|
|
&& !url.IsEmpty()) {
|
|
|
|
mURL = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_q) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("\""));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_sup && mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("^"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_sub && mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("_"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_code && mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("|"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if ((type == eHTMLTag_strong || type == eHTMLTag_b)
|
|
|
|
&& mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("*"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if ((type == eHTMLTag_em || type == eHTMLTag_i)
|
|
|
|
&& mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("/"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_u && mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("_"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
nsPlainTextSerializer::DoCloseContainer(PRInt32 aTag)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
eHTMLTags type = (eHTMLTags)aTag;
|
|
|
|
|
|
|
|
if (mTagStackIndex > 0) {
|
|
|
|
--mTagStackIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End current line if we're ending a block level tag
|
|
|
|
if((type == eHTMLTag_body) || (type == eHTMLTag_html)) {
|
|
|
|
// We want the output to end with a new line,
|
|
|
|
// but in preformatted areas like text fields,
|
|
|
|
// we can't emit newlines that weren't there.
|
|
|
|
// So add the newline only in the case of formatted output.
|
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatted) {
|
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FlushLine();
|
|
|
|
}
|
|
|
|
// We won't want to do anything with these in formatted mode either,
|
|
|
|
// so just return now:
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if ((type == eHTMLTag_tr) ||
|
|
|
|
(type == eHTMLTag_li) ||
|
|
|
|
(type == eHTMLTag_dt)) {
|
|
|
|
// Items that should always end a line, but get no more whitespace
|
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
}
|
2001-04-10 01:11:39 +00:00
|
|
|
else if (type == eHTMLTag_pre) {
|
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
}
|
2001-01-10 22:08:08 +00:00
|
|
|
else if (type == eHTMLTag_ul) {
|
|
|
|
mIndent -= kIndentSizeList;
|
2001-04-10 01:11:39 +00:00
|
|
|
if (--mULCount + mOLStackIndex == 0)
|
|
|
|
EnsureVerticalSpace(1);
|
2001-01-10 22:08:08 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_ol) {
|
|
|
|
FlushLine(); // Doing this after decreasing OLStackIndex would be wrong.
|
|
|
|
mIndent -= kIndentSizeList;
|
2001-04-10 01:11:39 +00:00
|
|
|
mOLStackIndex--;
|
|
|
|
if (mULCount + mOLStackIndex == 0)
|
|
|
|
EnsureVerticalSpace(1);
|
2001-01-10 22:08:08 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_dd) {
|
|
|
|
mIndent -= kIndentSizeDD;
|
|
|
|
}
|
2001-07-13 18:45:53 +00:00
|
|
|
else if (type == eHTMLTag_span) {
|
|
|
|
--mSpanLevel;
|
|
|
|
}
|
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
else if (IsBlockLevel(aTag)
|
2000-09-11 05:10:32 +00:00
|
|
|
&& type != eHTMLTag_blockquote
|
|
|
|
&& type != eHTMLTag_script
|
2001-02-03 01:13:55 +00:00
|
|
|
&& type != eHTMLTag_doctypeDecl
|
2000-09-11 05:10:32 +00:00
|
|
|
&& type != eHTMLTag_markupDecl) {
|
|
|
|
// All other blocks get 1 vertical space after them
|
|
|
|
// in formatted mode, otherwise 0.
|
|
|
|
// This is hard. Sometimes 0 is a better number, but
|
|
|
|
// how to know?
|
|
|
|
EnsureVerticalSpace((mFlags & nsIDocumentEncoder::OutputFormatted)
|
|
|
|
? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2001-01-10 22:08:08 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 05:10:32 +00:00
|
|
|
if (!(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-10 22:08:08 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// The rest of this routine is formatted output stuff,
|
|
|
|
// which we should skip if we're not formatted:
|
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
if (type == eHTMLTag_h1 || type == eHTMLTag_h2 ||
|
|
|
|
type == eHTMLTag_h3 || type == eHTMLTag_h4 ||
|
|
|
|
type == eHTMLTag_h5 || type == eHTMLTag_h6) {
|
|
|
|
|
|
|
|
if (mHeaderStrategy) { /*numbered or indent increasingly*/
|
|
|
|
mIndent -= kIndentSizeHeaders;
|
|
|
|
}
|
|
|
|
if (mHeaderStrategy == 1 /*indent increasingly*/ ) {
|
|
|
|
for (PRInt32 i = HeaderLevel(type); i > 1; i--) {
|
|
|
|
// for h(x), run x-1 times
|
|
|
|
mIndent -= kIndentIncrementHeaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_blockquote) {
|
|
|
|
FlushLine(); // Is this needed?
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
nsresult rv = GetAttributeValue(nsHTMLAtoms::type, value);
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && value.EqualsIgnoreCase("cite")) {
|
2000-09-11 05:10:32 +00:00
|
|
|
mCiteQuoteLevel--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mIndent -= kTabSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_a && !IsCurrentNodeConverted() && !mURL.IsEmpty()) {
|
|
|
|
nsAutoString temp;
|
2001-04-26 19:33:12 +00:00
|
|
|
temp.AssignWithConversion(" <");
|
2000-09-11 05:10:32 +00:00
|
|
|
temp += mURL;
|
2001-01-12 07:26:39 +00:00
|
|
|
temp.Append(PRUnichar('>'));
|
2000-09-11 05:10:32 +00:00
|
|
|
Write(temp);
|
|
|
|
mURL.Truncate();
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_q) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("\""));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if ((type == eHTMLTag_sup || type == eHTMLTag_sub)
|
|
|
|
&& mStructs && !IsCurrentNodeConverted()) {
|
2001-01-12 07:26:39 +00:00
|
|
|
Write(kSpace);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_code && mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("|"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if ((type == eHTMLTag_strong || type == eHTMLTag_b)
|
|
|
|
&& mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("*"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if ((type == eHTMLTag_em || type == eHTMLTag_i)
|
|
|
|
&& mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("/"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_u && mStructs && !IsCurrentNodeConverted()) {
|
2001-04-26 19:33:12 +00:00
|
|
|
Write(NS_LITERAL_STRING("_"));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPlainTextSerializer::DoAddLeaf(PRInt32 aTag,
|
2000-12-12 21:58:13 +00:00
|
|
|
const nsAReadableString& aText)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
// If we don't want any output, just return
|
|
|
|
if (!DoOutput()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
eHTMLTags type = (eHTMLTags)aTag;
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
if ((mTagStackIndex > 1 &&
|
|
|
|
mTagStack[mTagStackIndex-2] == eHTMLTag_select) ||
|
|
|
|
(mTagStackIndex > 0 &&
|
|
|
|
mTagStack[mTagStackIndex-1] == eHTMLTag_select)) {
|
2000-09-11 05:10:32 +00:00
|
|
|
// Don't output the contents of SELECT elements;
|
|
|
|
// Might be nice, eventually, to output just the selected element.
|
2001-01-12 07:26:39 +00:00
|
|
|
// Read more in bug 31994.
|
2000-09-11 05:10:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if (mTagStackIndex > 0 && mTagStack[mTagStackIndex-1] == eHTMLTag_script) {
|
|
|
|
// Don't output the contents of <script> tags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_text) {
|
2001-01-12 07:26:39 +00:00
|
|
|
/* Check, if we are in a link (symbolized with mURL containing the URL)
|
|
|
|
and the text is equal to the URL. In that case we don't want to output
|
|
|
|
the URL twice so we scrap the text in mURL. */
|
2000-09-11 05:10:32 +00:00
|
|
|
if (!mURL.IsEmpty() && mURL.Equals(aText)) {
|
|
|
|
mURL.Truncate();
|
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
Write(aText);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_entity) {
|
|
|
|
nsCOMPtr<nsIParserService> parserService;
|
|
|
|
GetParserService(getter_AddRefs(parserService));
|
|
|
|
if (parserService) {
|
|
|
|
nsAutoString str(aText);
|
|
|
|
PRInt32 entity;
|
|
|
|
parserService->HTMLConvertEntityToUnicode(str, &entity);
|
|
|
|
nsAutoString temp(entity);
|
|
|
|
Write(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_br) {
|
|
|
|
// Another egregious editor workaround, see bug 38194:
|
|
|
|
// ignore the bogus br tags that the editor sticks here and there.
|
|
|
|
nsAutoString typeAttr;
|
|
|
|
if (NS_FAILED(GetAttributeValue(nsHTMLAtoms::type, typeAttr))
|
|
|
|
|| !typeAttr.EqualsWithConversion("_moz")) {
|
|
|
|
EnsureVerticalSpace(mEmptyLines+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_whitespace) {
|
|
|
|
// The only times we want to pass along whitespace from the original
|
|
|
|
// html source are if we're forced into preformatted mode via flags,
|
|
|
|
// or if we're prettyprinting and we're inside a <pre>.
|
|
|
|
// Otherwise, either we're collapsing to minimal text, or we're
|
|
|
|
// prettyprinting to mimic the html format, and in neither case
|
|
|
|
// does the formatting of the html source help us.
|
|
|
|
// One exception: at the very beginning of a selection,
|
|
|
|
// we want to preserve whitespace.
|
|
|
|
if (mFlags & nsIDocumentEncoder::OutputPreformatted ||
|
2000-11-04 20:51:33 +00:00
|
|
|
(mPreFormatted && !mWrapColumn) ||
|
2000-09-11 05:10:32 +00:00
|
|
|
((mFlags & nsIDocumentEncoder::OutputFormatted)
|
2000-11-04 20:51:33 +00:00
|
|
|
&& IsInPre())) {
|
2001-01-12 07:26:39 +00:00
|
|
|
Write(aText);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if(!mInWhitespace ||
|
|
|
|
(!mStartedOutput
|
|
|
|
&& mFlags | nsIDocumentEncoder::OutputSelectionOnly)) {
|
|
|
|
mInWhitespace = PR_FALSE;
|
2001-01-12 07:26:39 +00:00
|
|
|
Write(kSpace);
|
2000-09-11 05:10:32 +00:00
|
|
|
mInWhitespace = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_newline) {
|
|
|
|
if (mFlags & nsIDocumentEncoder::OutputPreformatted ||
|
2000-11-04 20:51:33 +00:00
|
|
|
(mPreFormatted && !mWrapColumn) ||
|
|
|
|
IsInPre()) {
|
2000-09-11 05:10:32 +00:00
|
|
|
EnsureVerticalSpace(mEmptyLines+1);
|
|
|
|
}
|
|
|
|
else {
|
2001-01-12 07:26:39 +00:00
|
|
|
Write(kSpace);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_hr &&
|
|
|
|
(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
|
|
|
|
// Make a line of dashes as wide as the wrap width
|
|
|
|
// XXX honoring percentage would be nice
|
|
|
|
nsAutoString line;
|
|
|
|
PRUint32 width = (mWrapColumn > 0 ? mWrapColumn : 25);
|
|
|
|
while (line.Length() < width) {
|
2001-01-12 07:26:39 +00:00
|
|
|
line.Append(PRUnichar('-'));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
Write(line);
|
|
|
|
|
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
}
|
|
|
|
else if (type == eHTMLTag_img) {
|
|
|
|
/* Output (in decreasing order of preference)
|
|
|
|
alt, title or src (URI) attribute */
|
|
|
|
// See <http://www.w3.org/TR/REC-html40/struct/objects.html#edef-IMG>
|
|
|
|
nsAutoString desc, temp;
|
|
|
|
if (NS_SUCCEEDED(GetAttributeValue(nsHTMLAtoms::alt, desc))) {
|
|
|
|
if (!desc.IsEmpty()) {
|
2001-01-12 07:26:39 +00:00
|
|
|
temp.Append(NS_LITERAL_STRING(" [")); // Should we output chars at all here?
|
2000-09-11 05:10:32 +00:00
|
|
|
desc.StripChars("\"");
|
|
|
|
temp += desc;
|
2001-01-12 07:26:39 +00:00
|
|
|
temp.Append(NS_LITERAL_STRING("] "));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
// If the alt attribute has an empty value (|alt=""|), output nothing
|
|
|
|
}
|
|
|
|
else if (NS_SUCCEEDED(GetAttributeValue(nsHTMLAtoms::title, desc))
|
|
|
|
&& !desc.IsEmpty()) {
|
2001-01-12 07:26:39 +00:00
|
|
|
temp.Append(NS_LITERAL_STRING(" ["));
|
2000-09-11 05:10:32 +00:00
|
|
|
desc.StripChars("\"");
|
|
|
|
temp += desc;
|
2001-01-12 07:26:39 +00:00
|
|
|
temp.Append(NS_LITERAL_STRING("] "));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else if (NS_SUCCEEDED(GetAttributeValue(nsHTMLAtoms::src, desc))
|
|
|
|
&& !desc.IsEmpty()) {
|
2001-01-12 07:26:39 +00:00
|
|
|
temp.Append(NS_LITERAL_STRING(" <"));
|
2000-09-11 05:10:32 +00:00
|
|
|
desc.StripChars("\"");
|
|
|
|
temp += desc;
|
2001-01-12 07:26:39 +00:00
|
|
|
temp.Append(NS_LITERAL_STRING("> "));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
if (!temp.IsEmpty()) {
|
|
|
|
Write(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Adds as many newline as necessary to get |noOfRows| empty lines
|
|
|
|
*
|
|
|
|
* noOfRows = -1 : Being in the middle of some line of text
|
|
|
|
* noOfRows = 0 : Being at the start of a line
|
|
|
|
* noOfRows = n>0 : Having n empty lines before the current line.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
void
|
|
|
|
nsPlainTextSerializer::EnsureVerticalSpace(PRInt32 noOfRows)
|
|
|
|
{
|
2001-04-10 01:11:39 +00:00
|
|
|
// If we have something in the indent we probably want to output
|
|
|
|
// it and it's not included in the count for empty lines so we don't
|
|
|
|
// realize that we should start a new line.
|
|
|
|
if(noOfRows >= 0 && mInIndentString.Length() > 0) {
|
|
|
|
EndLine(PR_FALSE);
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
while(mEmptyLines < noOfRows) {
|
|
|
|
EndLine(PR_FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* This empties the current line cache without adding a NEWLINE.
|
|
|
|
* Should not be used if line wrapping is of importance since
|
|
|
|
* this function destroys the cache information.
|
|
|
|
*
|
|
|
|
* It will also write indentation and quotes if we believe us to be
|
|
|
|
* at the start of the line.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
void
|
|
|
|
nsPlainTextSerializer::FlushLine()
|
|
|
|
{
|
|
|
|
if(mCurrentLine.Length()>0) {
|
2001-01-12 07:26:39 +00:00
|
|
|
if(mAtFirstColumn) {
|
|
|
|
OutputQuotesAndIndent(); // XXX: Should we always do this? Bug?
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
Output(mCurrentLine);
|
|
|
|
mAtFirstColumn = mAtFirstColumn && mCurrentLine.Length() == 0;
|
2000-09-11 05:10:32 +00:00
|
|
|
mCurrentLine.Truncate();
|
|
|
|
mCurrentLineWidth = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Prints the text to output to our current output device (the string mOutputString).
|
|
|
|
* The only logic here is to replace non breaking spaces with a normal space since
|
|
|
|
* most (all?) receivers of the result won't understand the nbsp and even be
|
|
|
|
* confused by it.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
void
|
2001-01-12 07:26:39 +00:00
|
|
|
nsPlainTextSerializer::Output(nsString& aString)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
if (aString.Length() > 0) {
|
|
|
|
mStartedOutput = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, replace all nbsp characters with spaces,
|
|
|
|
// which the unicode encoder won't do for us.
|
|
|
|
static PRUnichar nbsp = 160;
|
|
|
|
static PRUnichar space = ' ';
|
|
|
|
aString.ReplaceChar(nbsp, space);
|
|
|
|
|
|
|
|
mOutputString->Append(aString);
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* This function adds a piece of text to the current stored line. If we are
|
|
|
|
* wrapping text and the stored line will become too long, a suitable
|
|
|
|
* location to wrap will be found and the line that's complete will be
|
|
|
|
* output.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
void
|
|
|
|
nsPlainTextSerializer::AddToLine(const PRUnichar * aLineFragment,
|
|
|
|
PRInt32 aLineFragmentLength)
|
|
|
|
{
|
|
|
|
PRUint32 prefixwidth = (mCiteQuoteLevel > 0 ? mCiteQuoteLevel + 1:0)+mIndent;
|
|
|
|
|
|
|
|
PRInt32 linelength = mCurrentLine.Length();
|
|
|
|
if(0 == linelength) {
|
|
|
|
if(0 == aLineFragmentLength) {
|
|
|
|
// Nothing at all. Are you kidding me?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
2001-04-10 01:11:39 +00:00
|
|
|
if(
|
|
|
|
(
|
|
|
|
'>' == aLineFragment[0] ||
|
|
|
|
' ' == aLineFragment[0] ||
|
|
|
|
!nsCRT::strncmp(aLineFragment, "From ", 5)
|
|
|
|
)
|
|
|
|
&& mCiteQuoteLevel == 0 // We space-stuff quoted lines anyway
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Space stuffing a la RFC 2646 (format=flowed).
|
|
|
|
mCurrentLine.Append(PRUnichar(' '));
|
|
|
|
|
|
|
|
if(MayWrap()) {
|
|
|
|
mCurrentLineWidth += GetUnicharWidth(' ');
|
2000-09-11 05:10:32 +00:00
|
|
|
#ifdef DEBUG_wrapping
|
2001-06-30 11:02:25 +00:00
|
|
|
NS_ASSERTION(GetUnicharStringWidth(mCurrentLine.get(),
|
2001-04-10 01:11:39 +00:00
|
|
|
mCurrentLine.Length()) ==
|
|
|
|
(PRInt32)mCurrentLineWidth,
|
|
|
|
"mCurrentLineWidth and reality out of sync!");
|
2000-09-11 05:10:32 +00:00
|
|
|
#endif
|
2001-04-10 01:11:39 +00:00
|
|
|
}
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mEmptyLines=-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentLine.Append(aLineFragment, aLineFragmentLength);
|
|
|
|
if(MayWrap()) {
|
2001-01-12 07:26:39 +00:00
|
|
|
mCurrentLineWidth += GetUnicharStringWidth(aLineFragment,
|
|
|
|
aLineFragmentLength);
|
2000-09-11 05:10:32 +00:00
|
|
|
#ifdef DEBUG_wrapping
|
2001-06-30 11:02:25 +00:00
|
|
|
NS_ASSERTION(GetUnicharstringWidth(mCurrentLine.get(),
|
2001-01-12 07:26:39 +00:00
|
|
|
mCurrentLine.Length()) ==
|
2000-09-11 05:10:32 +00:00
|
|
|
(PRInt32)mCurrentLineWidth,
|
|
|
|
"mCurrentLineWidth and reality out of sync!");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
linelength = mCurrentLine.Length();
|
|
|
|
|
|
|
|
// Wrap?
|
|
|
|
if(MayWrap())
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_wrapping
|
2001-06-30 11:02:25 +00:00
|
|
|
NS_ASSERTION(GetUnicharstringWidth(mCurrentLine.get(),
|
2000-09-11 05:10:32 +00:00
|
|
|
mCurrentLine.Length()) ==
|
|
|
|
(PRInt32)mCurrentLineWidth,
|
|
|
|
"mCurrentLineWidth and reality out of sync!");
|
|
|
|
#endif
|
|
|
|
// Yes, wrap!
|
|
|
|
// The "+4" is to avoid wrap lines that only would be a couple
|
|
|
|
// of letters too long. We give this bonus only if the
|
|
|
|
// wrapcolumn is more than 20.
|
|
|
|
PRUint32 bonuswidth = (mWrapColumn > 20) ? 4 : 0;
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
// XXX: Should calculate prefixwidth with GetUnicharStringWidth
|
2000-09-11 05:10:32 +00:00
|
|
|
while(mCurrentLineWidth+prefixwidth > mWrapColumn+bonuswidth) {
|
|
|
|
// Must wrap. Let's find a good place to do that.
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
// We go from the end removing one letter at a time until
|
|
|
|
// we have a reasonable width
|
|
|
|
PRInt32 goodSpace = mCurrentLine.Length();
|
|
|
|
PRUint32 width = mCurrentLineWidth;
|
|
|
|
while(goodSpace > 0 && (width+prefixwidth > mWrapColumn)) {
|
|
|
|
goodSpace--;
|
2001-01-12 07:26:39 +00:00
|
|
|
width -= GetUnicharWidth(mCurrentLine[goodSpace]);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goodSpace++;
|
|
|
|
|
|
|
|
PRBool oNeedMoreText;
|
|
|
|
if (nsnull != mLineBreaker) {
|
2001-06-30 11:02:25 +00:00
|
|
|
result = mLineBreaker->Prev(mCurrentLine.get(),
|
2000-09-11 05:10:32 +00:00
|
|
|
mCurrentLine.Length(), goodSpace,
|
|
|
|
(PRUint32 *) &goodSpace, &oNeedMoreText);
|
|
|
|
if (oNeedMoreText) {
|
|
|
|
goodSpace = -1;
|
|
|
|
}
|
|
|
|
else if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace-1))) {
|
|
|
|
--goodSpace; // adjust the position since line breaker returns a position next to space
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fallback if the line breaker is unavailable or failed
|
|
|
|
if (nsnull == mLineBreaker || NS_FAILED(result)) {
|
|
|
|
goodSpace = mWrapColumn-prefixwidth;
|
|
|
|
while (goodSpace >= 0 &&
|
|
|
|
!nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
|
|
|
|
goodSpace--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString restOfLine;
|
|
|
|
if (goodSpace < 0) {
|
|
|
|
// If we don't found a good place to break, accept long line and
|
|
|
|
// try to find another place to break
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
goodSpace=(prefixwidth>mWrapColumn+1)?1:mWrapColumn-prefixwidth+1;
|
2000-09-11 05:10:32 +00:00
|
|
|
result = NS_OK;
|
|
|
|
if (nsnull != mLineBreaker) {
|
2001-06-30 11:02:25 +00:00
|
|
|
result = mLineBreaker->Next(mCurrentLine.get(),
|
2000-09-11 05:10:32 +00:00
|
|
|
mCurrentLine.Length(), goodSpace,
|
|
|
|
(PRUint32 *) &goodSpace, &oNeedMoreText);
|
|
|
|
}
|
|
|
|
// fallback if the line breaker is unavailable or failed
|
|
|
|
if (nsnull == mLineBreaker || NS_FAILED(result)) {
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
goodSpace=(prefixwidth>mWrapColumn)?1:mWrapColumn-prefixwidth;
|
2000-09-11 05:10:32 +00:00
|
|
|
while (goodSpace < linelength &&
|
|
|
|
!nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
|
|
|
|
goodSpace++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((goodSpace < linelength) && (goodSpace > 0)) {
|
|
|
|
// Found a place to break
|
|
|
|
|
|
|
|
// -1 (trim a char at the break position)
|
|
|
|
// only if the line break was a space.
|
|
|
|
if (nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
|
|
|
|
mCurrentLine.Right(restOfLine, linelength-goodSpace-1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mCurrentLine.Right(restOfLine, linelength-goodSpace);
|
|
|
|
}
|
|
|
|
mCurrentLine.Truncate(goodSpace);
|
|
|
|
EndLine(PR_TRUE);
|
|
|
|
mCurrentLine.Truncate();
|
|
|
|
// Space stuff new line?
|
|
|
|
if(mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
2001-04-10 01:11:39 +00:00
|
|
|
if(
|
|
|
|
restOfLine.Length() > 0
|
|
|
|
&&
|
|
|
|
(
|
|
|
|
restOfLine[0] == '>' ||
|
|
|
|
restOfLine[0] == ' ' ||
|
|
|
|
restOfLine.EqualsWithConversion("From ",PR_FALSE,5)
|
|
|
|
)
|
|
|
|
&& mCiteQuoteLevel == 0 // We space-stuff quoted lines anyway
|
|
|
|
)
|
|
|
|
{
|
2000-09-11 05:10:32 +00:00
|
|
|
// Space stuffing a la RFC 2646 (format=flowed).
|
2001-01-12 07:26:39 +00:00
|
|
|
mCurrentLine.Append(PRUnichar(' '));
|
2001-04-10 01:11:39 +00:00
|
|
|
//XXX doesn't seem to work correctly for ' '
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mCurrentLine.Append(restOfLine);
|
2001-06-30 11:02:25 +00:00
|
|
|
mCurrentLineWidth = GetUnicharStringWidth(mCurrentLine.get(),
|
2001-01-12 07:26:39 +00:00
|
|
|
mCurrentLine.Length());
|
2000-09-11 05:10:32 +00:00
|
|
|
linelength = mCurrentLine.Length();
|
|
|
|
mEmptyLines = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Nothing to do. Hopefully we get more data later
|
|
|
|
// to use for a place to break line
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No wrapping.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Outputs the contents of mCurrentLine, and resets line specific
|
|
|
|
* variables. Also adds an indentation and prefix if there is
|
|
|
|
* one specified. Strips ending spaces from the line if it isn't
|
|
|
|
* preformatted.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
void
|
2001-01-12 07:26:39 +00:00
|
|
|
nsPlainTextSerializer::EndLine(PRBool aSoftlinebreak)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
2001-01-12 07:26:39 +00:00
|
|
|
PRUint32 currentlinelength = mCurrentLine.Length();
|
|
|
|
|
|
|
|
if(aSoftlinebreak && 0 == currentlinelength) {
|
|
|
|
// No meaning
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In non-preformatted mode, remove SPACE from the end
|
|
|
|
// of the line, unless we got "-- " in a format=flowed
|
|
|
|
// output. "-- " is the sig delimiter by convention and
|
|
|
|
// shouldn't be touched even in format=flowed
|
|
|
|
// (see RFC 2646). We only check for "-- " when it's a hard line
|
|
|
|
// break for obvious reasons.
|
|
|
|
if(!(mFlags & nsIDocumentEncoder::OutputPreformatted) &&
|
|
|
|
(aSoftlinebreak || !mCurrentLine.EqualsWithConversion("-- "))) {
|
|
|
|
// Remove SPACE:s from the end of the line.
|
|
|
|
while(currentlinelength > 0 &&
|
2001-04-10 01:11:39 +00:00
|
|
|
mCurrentLine[currentlinelength-1] == ' ') {
|
2001-01-12 07:26:39 +00:00
|
|
|
--currentlinelength;
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
mCurrentLine.SetLength(currentlinelength);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(aSoftlinebreak &&
|
|
|
|
(mFlags & nsIDocumentEncoder::OutputFormatFlowed) &&
|
|
|
|
(mIndent == 0)) {
|
|
|
|
// Add the soft part of the soft linebreak (RFC 2646 4.1)
|
|
|
|
// We only do this when there is no indentation since format=flowed
|
|
|
|
// lines and indentation doesn't work well together.
|
|
|
|
mCurrentLine.Append(PRUnichar(' '));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(aSoftlinebreak) {
|
2000-09-11 05:10:32 +00:00
|
|
|
mEmptyLines=0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Hard break
|
2001-04-10 01:11:39 +00:00
|
|
|
if(mCurrentLine.Length()>0 || mInIndentString.Length()>0) {
|
2000-09-11 05:10:32 +00:00
|
|
|
mEmptyLines=-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mEmptyLines++;
|
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
|
2001-04-10 01:11:39 +00:00
|
|
|
if(mAtFirstColumn) {
|
|
|
|
// If we don't have anything "real" to output we have to
|
|
|
|
// make sure the indent doesn't end in a space since that
|
|
|
|
// would trick a format=flowed-aware receiver.
|
|
|
|
PRBool stripTrailingSpaces = (mCurrentLine.Length() == 0);
|
|
|
|
OutputQuotesAndIndent(stripTrailingSpaces);
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
mCurrentLine.Append(mLineBreak);
|
|
|
|
Output(mCurrentLine);
|
|
|
|
mCurrentLine.Truncate();
|
|
|
|
mCurrentLineWidth = 0;
|
|
|
|
mAtFirstColumn=PR_TRUE;
|
|
|
|
mInWhitespace=PR_TRUE;
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the calculated and stored indent and text in the indentation. That is
|
|
|
|
* quote chars and numbers for numbered lists and such. It will also reset any
|
|
|
|
* stored text to put in the indentation after using it.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
void
|
2001-04-10 01:11:39 +00:00
|
|
|
nsPlainTextSerializer::OutputQuotesAndIndent(PRBool stripTrailingSpaces /* = PR_FALSE */)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
2001-04-10 01:11:39 +00:00
|
|
|
nsAutoString stringToOutput;
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
// Put the mail quote "> " chars in, if appropriate:
|
|
|
|
if (mCiteQuoteLevel > 0) {
|
|
|
|
nsAutoString quotes;
|
|
|
|
for(int i=0; i < mCiteQuoteLevel; i++) {
|
2001-01-12 07:26:39 +00:00
|
|
|
quotes.Append(PRUnichar('>'));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
2001-04-10 01:11:39 +00:00
|
|
|
if (mCurrentLine.Length() > 0) {
|
|
|
|
/* Better don't output a space here, if the line is empty,
|
|
|
|
in case a recieving f=f-aware UA thinks, this were a flowed line,
|
|
|
|
which it isn't - it's just empty.
|
|
|
|
(Flowed lines may be joined with the following one,
|
|
|
|
so the empty line may be lost completely.) */
|
|
|
|
quotes.Append(PRUnichar(' '));
|
|
|
|
}
|
|
|
|
stringToOutput = quotes;
|
2001-01-12 07:26:39 +00:00
|
|
|
mAtFirstColumn = PR_FALSE;
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Indent if necessary
|
|
|
|
PRInt32 indentwidth = mIndent - mInIndentString.Length();
|
2001-04-10 01:11:39 +00:00
|
|
|
if (indentwidth > 0
|
|
|
|
&& (mCurrentLine.Length() > 0 || mInIndentString.Length() > 0)
|
|
|
|
// Don't make empty lines look flowed
|
|
|
|
) {
|
2000-09-11 05:10:32 +00:00
|
|
|
nsAutoString spaces;
|
|
|
|
for (int i=0; i < indentwidth; ++i)
|
2001-01-12 07:26:39 +00:00
|
|
|
spaces.Append(PRUnichar(' '));
|
2001-04-10 01:11:39 +00:00
|
|
|
stringToOutput += spaces;
|
2001-01-12 07:26:39 +00:00
|
|
|
mAtFirstColumn = PR_FALSE;
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mInIndentString.Length() > 0) {
|
2001-04-10 01:11:39 +00:00
|
|
|
stringToOutput += mInIndentString;
|
2001-01-12 07:26:39 +00:00
|
|
|
mAtFirstColumn = PR_FALSE;
|
2000-09-11 05:10:32 +00:00
|
|
|
mInIndentString.Truncate();
|
|
|
|
}
|
2001-04-10 01:11:39 +00:00
|
|
|
|
|
|
|
if(stripTrailingSpaces) {
|
|
|
|
PRInt32 lineLength = stringToOutput.Length();
|
|
|
|
while(lineLength > 0 &&
|
|
|
|
' ' == stringToOutput[lineLength-1]) {
|
|
|
|
--lineLength;
|
|
|
|
}
|
|
|
|
stringToOutput.SetLength(lineLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stringToOutput.Length() > 0) {
|
|
|
|
Output(stringToOutput);
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Write a string. This is the highlevel function to use to get text output.
|
|
|
|
* By using AddToLine, Output, EndLine and other functions it handles quotation,
|
|
|
|
* line wrapping, indentation, whitespace compression and other things.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
void
|
2000-12-12 21:58:13 +00:00
|
|
|
nsPlainTextSerializer::Write(const nsAReadableString& aString)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_wrapping
|
2001-07-13 18:45:53 +00:00
|
|
|
printf("Write(%s): wrap col = %d\n",
|
|
|
|
NS_ConvertUCS2toUTF8(aString).get(), mWrapColumn);
|
2000-09-11 05:10:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
PRInt32 bol = 0;
|
|
|
|
PRInt32 newline;
|
|
|
|
|
|
|
|
PRInt32 totLen = aString.Length();
|
2001-01-12 07:26:39 +00:00
|
|
|
|
2001-07-13 18:45:53 +00:00
|
|
|
// If the string is empty, do nothing:
|
|
|
|
if (totLen <= 0) return;
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
// We have two major codepaths here. One that does preformatted text and one
|
|
|
|
// that does normal formatted text. The one for preformatted text calls
|
|
|
|
// Output directly while the other code path goes through AddToLine.
|
2001-07-13 18:45:53 +00:00
|
|
|
if ((mPreFormatted && !mWrapColumn) || IsInPre()
|
|
|
|
|| (!mQuotesPreformatted && mSpanLevel > 0
|
|
|
|
//&& Substring(aString, 0, 1) == NS_LITERAL_STRING(">"))) {
|
|
|
|
&& aString.First() == PRUnichar('>'))) {
|
2001-01-12 07:26:39 +00:00
|
|
|
// No intelligent wrapping.
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
// This mustn't be mixed with intelligent wrapping without clearing
|
|
|
|
// the mCurrentLine buffer before!!!
|
2000-09-11 05:10:32 +00:00
|
|
|
NS_WARN_IF_FALSE(mCurrentLine.Length() == 0,
|
|
|
|
"Mixed wrapping data and nonwrapping data on the same line");
|
|
|
|
if (mCurrentLine.Length() > 0) {
|
|
|
|
FlushLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the mail quote "> " chars in, if appropriate.
|
|
|
|
// Have to put it in before every line.
|
|
|
|
while(bol<totLen) {
|
2001-01-12 07:26:39 +00:00
|
|
|
if(mAtFirstColumn) {
|
|
|
|
OutputQuotesAndIndent();
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
|
|
|
|
// Find one of '\n' or '\r' using iterators since nsAReadableString
|
|
|
|
// doesn't have the old FindCharInSet function.
|
2001-04-03 03:29:49 +00:00
|
|
|
nsAString::const_iterator iter; aString.BeginReading(iter);
|
|
|
|
nsAString::const_iterator done_searching; aString.EndReading(done_searching);
|
2001-03-06 08:33:00 +00:00
|
|
|
iter.advance(bol);
|
2001-01-12 07:26:39 +00:00
|
|
|
PRInt32 new_newline = bol;
|
|
|
|
newline = kNotFound;
|
|
|
|
while(iter != done_searching) {
|
|
|
|
if('\n' == *iter || '\r' == *iter) {
|
|
|
|
newline = new_newline;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++new_newline;
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done searching
|
|
|
|
if(newline == kNotFound) {
|
2000-09-11 05:10:32 +00:00
|
|
|
// No new lines.
|
|
|
|
nsAutoString stringpart;
|
|
|
|
aString.Right(stringpart, totLen-bol);
|
|
|
|
if(stringpart.Length() > 0) {
|
|
|
|
PRUnichar lastchar = stringpart[stringpart.Length()-1];
|
|
|
|
if((lastchar == '\t') || (lastchar == ' ') ||
|
|
|
|
(lastchar == '\r') ||(lastchar == '\n')) {
|
|
|
|
mInWhitespace = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mInWhitespace = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
Output(stringpart);
|
2000-09-11 05:10:32 +00:00
|
|
|
mEmptyLines=-1;
|
2001-01-12 07:26:39 +00:00
|
|
|
mAtFirstColumn = mAtFirstColumn && (totLen-bol)==0;
|
2000-09-11 05:10:32 +00:00
|
|
|
bol = totLen;
|
|
|
|
}
|
|
|
|
else {
|
2000-10-20 23:05:14 +00:00
|
|
|
// There is a newline
|
2000-09-11 05:10:32 +00:00
|
|
|
nsAutoString stringpart;
|
2000-10-20 23:05:14 +00:00
|
|
|
aString.Mid(stringpart, bol, newline-bol);
|
2000-09-11 05:10:32 +00:00
|
|
|
mInWhitespace = PR_TRUE;
|
2001-01-12 07:26:39 +00:00
|
|
|
Output(stringpart);
|
2000-10-20 23:05:14 +00:00
|
|
|
// and write the newline
|
2001-01-12 07:26:39 +00:00
|
|
|
Output(mLineBreak);
|
2000-09-11 05:10:32 +00:00
|
|
|
mEmptyLines=0;
|
2001-01-12 07:26:39 +00:00
|
|
|
mAtFirstColumn = PR_TRUE;
|
2000-09-11 05:10:32 +00:00
|
|
|
bol = newline+1;
|
2001-03-08 08:05:05 +00:00
|
|
|
if('\r' == *iter && bol < totLen && '\n' == *++iter) {
|
2001-01-12 07:26:39 +00:00
|
|
|
// There was a CRLF in the input. This used to be illegal and
|
|
|
|
// stripped by the parser. Apparently not anymore. Let's skip
|
|
|
|
// over the LF.
|
|
|
|
bol++;
|
|
|
|
}
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_wrapping
|
2001-01-12 07:26:39 +00:00
|
|
|
printf("No wrapping: newline is %d, totLen is %d\n",
|
|
|
|
newline, totLen);
|
2000-09-11 05:10:32 +00:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-12-12 21:58:13 +00:00
|
|
|
// XXX Copy necessary to use nsString methods and gain
|
|
|
|
// access to underlying buffer
|
|
|
|
nsAutoString str(aString);
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
// Intelligent handling of text
|
|
|
|
// If needed, strip out all "end of lines"
|
|
|
|
// and multiple whitespace between words
|
|
|
|
PRInt32 nextpos;
|
|
|
|
nsAutoString tempstr;
|
|
|
|
const PRUnichar * offsetIntoBuffer = nsnull;
|
|
|
|
|
|
|
|
while (bol < totLen) { // Loop over lines
|
|
|
|
// Find a place where we may have to do whitespace compression
|
2000-12-12 21:58:13 +00:00
|
|
|
nextpos = str.FindCharInSet(" \t\n\r", bol);
|
2000-09-11 05:10:32 +00:00
|
|
|
#ifdef DEBUG_wrapping
|
2000-12-12 21:58:13 +00:00
|
|
|
nsAutoString remaining;
|
|
|
|
str.Right(remaining, totLen - bol);
|
2000-09-11 05:10:32 +00:00
|
|
|
foo = remaining.ToNewCString();
|
2000-10-28 22:17:53 +00:00
|
|
|
// printf("Next line: bol = %d, newlinepos = %d, totLen = %d, string = '%s'\n",
|
|
|
|
// bol, nextpos, totLen, foo);
|
2000-09-11 05:10:32 +00:00
|
|
|
nsMemory::Free(foo);
|
|
|
|
#endif
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
if(nextpos == kNotFound) {
|
2000-09-11 05:10:32 +00:00
|
|
|
// The rest of the string
|
2001-06-30 11:02:25 +00:00
|
|
|
offsetIntoBuffer = str.get() + bol;
|
2001-01-12 07:26:39 +00:00
|
|
|
AddToLine(offsetIntoBuffer, totLen-bol);
|
2000-09-11 05:10:32 +00:00
|
|
|
bol=totLen;
|
|
|
|
mInWhitespace=PR_FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// There's still whitespace left in the string
|
|
|
|
|
|
|
|
// If we're already in whitespace and not preformatted, just skip it:
|
|
|
|
if (mInWhitespace && (nextpos == bol) && !mPreFormatted &&
|
|
|
|
!(mFlags & nsIDocumentEncoder::OutputPreformatted)) {
|
|
|
|
// Skip whitespace
|
|
|
|
bol++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nextpos == bol) {
|
|
|
|
// Note that we are in whitespace.
|
|
|
|
mInWhitespace = PR_TRUE;
|
2001-06-30 11:02:25 +00:00
|
|
|
offsetIntoBuffer = str.get() + nextpos;
|
2001-01-12 07:26:39 +00:00
|
|
|
AddToLine(offsetIntoBuffer, 1);
|
2000-09-11 05:10:32 +00:00
|
|
|
bol++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
mInWhitespace = PR_TRUE;
|
|
|
|
|
2001-06-30 11:02:25 +00:00
|
|
|
offsetIntoBuffer = str.get() + bol;
|
2001-01-12 07:26:39 +00:00
|
|
|
if(mPreFormatted || (mFlags & nsIDocumentEncoder::OutputPreformatted)) {
|
|
|
|
// Preserve the real whitespace character
|
|
|
|
nextpos++;
|
|
|
|
AddToLine(offsetIntoBuffer, nextpos-bol);
|
|
|
|
bol = nextpos;
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
else {
|
2001-01-12 07:26:39 +00:00
|
|
|
// Replace the whitespace with a space
|
|
|
|
AddToLine(offsetIntoBuffer, nextpos-bol);
|
2001-06-30 11:02:25 +00:00
|
|
|
AddToLine(kSpace.get(),1);
|
2001-01-12 07:26:39 +00:00
|
|
|
bol = nextpos + 1; // Let's eat the whitespace
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
2000-10-28 22:17:53 +00:00
|
|
|
} // Continue looping over the string
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Gets the value of an attribute in a string. If the function returns
|
|
|
|
* NS_ERROR_NOT_AVAILABLE, there was none such attribute specified.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
nsresult
|
|
|
|
nsPlainTextSerializer::GetAttributeValue(nsIAtom* aName,
|
|
|
|
nsString& aValueRet)
|
|
|
|
{
|
|
|
|
if (mContent) {
|
|
|
|
if (NS_CONTENT_ATTR_NOT_THERE != mContent->GetAttribute(kNameSpaceID_None,
|
|
|
|
aName, aValueRet)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mParserNode) {
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
const PRUnichar *name;
|
|
|
|
aName->GetUnicode(&name);
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
PRInt32 count = mParserNode->GetAttributeCount();
|
|
|
|
for (PRInt32 i=0;i<count;i++) {
|
2000-12-12 21:58:13 +00:00
|
|
|
const nsAReadableString& key = mParserNode->GetKeyAt(i);
|
2000-09-11 05:10:32 +00:00
|
|
|
if (key.Equals(name)) {
|
|
|
|
aValueRet = mParserNode->GetValueAt(i);
|
|
|
|
aValueRet.StripChars("\"");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true, if the element was inserted by Moz' TXT->HTML converter.
|
|
|
|
* In this case, we should ignore it.
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
nsPlainTextSerializer::IsCurrentNodeConverted()
|
|
|
|
{
|
|
|
|
nsAutoString value;
|
|
|
|
nsresult rv = GetAttributeValue(nsHTMLAtoms::kClass, value);
|
|
|
|
return (NS_SUCCEEDED(rv) &&
|
2001-01-12 07:26:39 +00:00
|
|
|
(value.EqualsIgnoreCase("txt", 3) ||
|
|
|
|
value.EqualsIgnoreCase("\"txt", 4)));
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPlainTextSerializer::GetIdForContent(nsIContent* aContent,
|
|
|
|
PRInt32* aID)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIHTMLContent> htmlcontent = do_QueryInterface(aContent);
|
|
|
|
if (!htmlcontent) {
|
|
|
|
*aID = eHTMLTag_unknown;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> tagname;
|
|
|
|
mContent->GetTag(*getter_AddRefs(tagname));
|
|
|
|
if (!tagname) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsAutoString namestr;
|
|
|
|
tagname->ToString(namestr);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIParserService> parserService;
|
|
|
|
rv = GetParserService(getter_AddRefs(parserService));
|
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = parserService->HTMLStringTagToId(namestr, aID);
|
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPlainTextSerializer::GetParserService(nsIParserService** aParserService)
|
|
|
|
{
|
|
|
|
if (!mParserService) {
|
|
|
|
nsresult rv;
|
|
|
|
mParserService = do_GetService(kParserServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CallQueryInterface(mParserService.get(), aParserService);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the id represents an element of block type.
|
|
|
|
* Can be used to determine if a new paragraph should be started.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
PRBool
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
nsPlainTextSerializer::IsBlockLevel(PRInt32 aId)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
PRBool isBlock = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIParserService> parserService;
|
|
|
|
GetParserService(getter_AddRefs(parserService));
|
|
|
|
if (parserService) {
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
parserService->IsBlock(aId, isBlock);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return isBlock;
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the id represents a container.
|
|
|
|
*/
|
2000-09-11 05:10:32 +00:00
|
|
|
PRBool
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
nsPlainTextSerializer::IsContainer(PRInt32 aId)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
PRBool isContainer = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIParserService> parserService;
|
|
|
|
GetParserService(getter_AddRefs(parserService));
|
|
|
|
if (parserService) {
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
parserService->IsContainer(aId, isContainer);
|
2000-09-11 05:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return isContainer;
|
|
|
|
}
|
|
|
|
|
2000-11-04 20:51:33 +00:00
|
|
|
/**
|
|
|
|
* Returns true if we currently are inside a <pre>. The check is done
|
|
|
|
* by traversing the tag stack looking for <pre> until we hit a block
|
|
|
|
* level tag which is assumed to override any <pre>:s below it in
|
|
|
|
* the stack. To do this correctly to a 100% would require access
|
|
|
|
* to style which we don't support in this converter.
|
|
|
|
*/
|
|
|
|
PRBool
|
|
|
|
nsPlainTextSerializer::IsInPre()
|
|
|
|
{
|
|
|
|
PRInt32 i = mTagStackIndex;
|
|
|
|
while(i > 0) {
|
|
|
|
if(mTagStack[i-1] == eHTMLTag_pre)
|
|
|
|
return PR_TRUE;
|
|
|
|
if(IsBlockLevel(mTagStack[i-1])) {
|
|
|
|
// We assume that every other block overrides a <pre>
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a <pre> in the whole stack
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
/*
|
|
|
|
@return 0 = no header, 1 = h1, ..., 6 = h6
|
|
|
|
*/
|
|
|
|
PRInt32 HeaderLevel(eHTMLTags aTag)
|
|
|
|
{
|
|
|
|
PRInt32 result;
|
|
|
|
switch (aTag)
|
|
|
|
{
|
|
|
|
case eHTMLTag_h1:
|
|
|
|
result = 1; break;
|
|
|
|
case eHTMLTag_h2:
|
|
|
|
result = 2; break;
|
|
|
|
case eHTMLTag_h3:
|
|
|
|
result = 3; break;
|
|
|
|
case eHTMLTag_h4:
|
|
|
|
result = 4; break;
|
|
|
|
case eHTMLTag_h5:
|
|
|
|
result = 5; break;
|
|
|
|
case eHTMLTag_h6:
|
|
|
|
result = 6; break;
|
|
|
|
default:
|
|
|
|
result = 0; break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-01-12 07:26:39 +00:00
|
|
|
* This is an implementation of GetUnicharWidth() and
|
|
|
|
* GetUnicharStringWidth() as defined in
|
2000-09-11 05:10:32 +00:00
|
|
|
* "The Single UNIX Specification, Version 2, The Open Group, 1997"
|
|
|
|
* <http://www.UNIX-systems.org/online.html>
|
|
|
|
*
|
|
|
|
* Markus Kuhn -- 2000-02-08 -- public domain
|
|
|
|
*
|
|
|
|
* Minor alterations to fit Mozilla's data types by Daniel Bratell
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* These functions define the column width of an ISO 10646 character
|
|
|
|
* as follows:
|
|
|
|
*
|
|
|
|
* - The null character (U+0000) has a column width of 0.
|
|
|
|
*
|
|
|
|
* - Other C0/C1 control characters and DEL will lead to a return
|
|
|
|
* value of -1.
|
|
|
|
*
|
|
|
|
* - Non-spacing and enclosing combining characters (general
|
|
|
|
* category code Mn or Me in the Unicode database) have a
|
|
|
|
* column width of 0.
|
|
|
|
*
|
|
|
|
* - Spacing characters in the East Asian Wide (W) or East Asian
|
|
|
|
* FullWidth (F) category as defined in Unicode Technical
|
|
|
|
* Report #11 have a column width of 2.
|
|
|
|
*
|
|
|
|
* - All remaining characters (including all printable
|
|
|
|
* ISO 8859-1 and WGL4 characters, Unicode control characters,
|
|
|
|
* etc.) have a column width of 1.
|
|
|
|
*
|
|
|
|
* This implementation assumes that wchar_t characters are encoded
|
|
|
|
* in ISO 10646.
|
|
|
|
*/
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
PRInt32 GetUnicharWidth(PRUnichar ucs)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
/* sorted list of non-overlapping intervals of non-spacing characters */
|
|
|
|
static const struct interval {
|
|
|
|
PRUint16 first;
|
|
|
|
PRUint16 last;
|
|
|
|
} combining[] = {
|
|
|
|
{ 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
|
|
|
|
{ 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
|
|
|
|
{ 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
|
|
|
|
{ 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
|
|
|
|
{ 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
|
|
|
|
{ 0x0711, 0x0711 }, { 0x0730, 0x074A }, { 0x07A6, 0x07B0 },
|
|
|
|
{ 0x0901, 0x0902 }, { 0x093C, 0x093C }, { 0x0941, 0x0948 },
|
|
|
|
{ 0x094D, 0x094D }, { 0x0951, 0x0954 }, { 0x0962, 0x0963 },
|
|
|
|
{ 0x0981, 0x0981 }, { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 },
|
|
|
|
{ 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 }, { 0x0A02, 0x0A02 },
|
|
|
|
{ 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 },
|
|
|
|
{ 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 },
|
|
|
|
{ 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 },
|
|
|
|
{ 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
|
|
|
|
{ 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
|
|
|
|
{ 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
|
|
|
|
{ 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
|
|
|
|
{ 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBF, 0x0CBF },
|
|
|
|
{ 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD }, { 0x0D41, 0x0D43 },
|
|
|
|
{ 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 },
|
|
|
|
{ 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A },
|
|
|
|
{ 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 },
|
|
|
|
{ 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 },
|
|
|
|
{ 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 },
|
|
|
|
{ 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 },
|
|
|
|
{ 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 },
|
|
|
|
{ 0x102D, 0x1030 }, { 0x1032, 0x1032 }, { 0x1036, 0x1037 },
|
|
|
|
{ 0x1039, 0x1039 }, { 0x1058, 0x1059 }, { 0x17B7, 0x17BD },
|
|
|
|
{ 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x18A9, 0x18A9 },
|
|
|
|
{ 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
|
|
|
|
{ 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }
|
|
|
|
};
|
|
|
|
PRInt32 min = 0;
|
|
|
|
PRInt32 max = sizeof(combining) / sizeof(struct interval) - 1;
|
|
|
|
PRInt32 mid;
|
|
|
|
|
|
|
|
/* test for 8-bit control characters */
|
|
|
|
if (ucs == 0)
|
|
|
|
return 0;
|
|
|
|
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* first quick check for Latin-1 etc. characters */
|
|
|
|
if (ucs < combining[0].first)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* binary search in table of non-spacing characters */
|
|
|
|
while (max >= min) {
|
|
|
|
mid = (min + max) / 2;
|
|
|
|
if (combining[mid].last < ucs)
|
|
|
|
min = mid + 1;
|
|
|
|
else if (combining[mid].first > ucs)
|
|
|
|
max = mid - 1;
|
|
|
|
else if (combining[mid].first <= ucs && combining[mid].last >= ucs)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we arrive here, ucs is not a combining or C0/C1 control character */
|
|
|
|
|
|
|
|
/* fast test for majority of non-wide scripts */
|
|
|
|
if (ucs < 0x1100)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 1 +
|
|
|
|
((ucs >= 0x1100 && ucs <= 0x115f) || /* Hangul Jamo */
|
|
|
|
(ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
|
|
|
|
ucs != 0x303f) || /* CJK ... Yi */
|
|
|
|
(ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
|
|
|
|
(ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
|
|
|
|
(ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
|
|
|
|
(ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
|
|
|
|
(ucs >= 0xffe0 && ucs <= 0xffe6));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
PRInt32 GetUnicharStringWidth(const PRUnichar* pwcs, PRInt32 n)
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
PRInt32 w, width = 0;
|
|
|
|
|
|
|
|
for (;*pwcs && n-- > 0; pwcs++)
|
2001-01-12 07:26:39 +00:00
|
|
|
if ((w = GetUnicharWidth(*pwcs)) < 0)
|
2000-09-11 05:10:32 +00:00
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
width += w;
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|