2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2006-03-31 12:00:42 +04:00
|
|
|
/*
|
|
|
|
* nsIContentSerializer implementation that can be used with an
|
|
|
|
* nsIDocumentEncoder to convert a DOM into plaintext in a nice way
|
|
|
|
* (eg for copy/paste as plaintext).
|
|
|
|
*/
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
#include "nsPlainTextSerializer.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2000-09-11 09:10:32 +04:00
|
|
|
#include "nsTextFragment.h"
|
2001-02-19 15:55:42 +03:00
|
|
|
#include "nsContentUtils.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2002-04-29 10:20:58 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
2002-05-02 00:52:29 +04:00
|
|
|
#include "nsCRT.h"
|
2010-10-23 01:25:22 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-09-17 17:46:24 +04:00
|
|
|
#include "mozilla/BinarySearch.h"
|
2014-12-19 20:45:50 +03:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2010-10-23 01:25:22 +04:00
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
namespace mozilla {
|
|
|
|
class Encoding;
|
|
|
|
}
|
|
|
|
|
2011-05-25 10:31:59 +04:00
|
|
|
using namespace mozilla;
|
2010-10-23 01:25:22 +04:00
|
|
|
using namespace mozilla::dom;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
#define PREF_STRUCTS "converter.html2txt.structs"
|
|
|
|
#define PREF_HEADER_STRATEGY "converter.html2txt.header_strategy"
|
2015-03-10 09:11:55 +03:00
|
|
|
#define PREF_ALWAYS_INCLUDE_RUBY "converter.html2txt.always_include_ruby"
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kTabSize=4;
|
|
|
|
static const int32_t kIndentSizeHeaders = 2; /* Indention of h1, if
|
2000-09-11 09:10:32 +04:00
|
|
|
mHeaderStrategy = 1 or = 2.
|
|
|
|
Indention of other headers
|
|
|
|
is derived from that.
|
|
|
|
XXX center h1? */
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kIndentIncrementHeaders = 2; /* If mHeaderStrategy = 1,
|
2000-09-11 09:10:32 +04:00
|
|
|
indent h(x+1) this many
|
|
|
|
columns more than h(x) */
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kIndentSizeList = kTabSize;
|
2000-09-11 09:10:32 +04:00
|
|
|
// Indention of non-first lines of ul and ol
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kIndentSizeDD = kTabSize; // Indention of <dd>
|
2014-01-04 19:02:17 +04:00
|
|
|
static const char16_t kNBSP = 160;
|
|
|
|
static const char16_t kSPACE = ' ';
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
static int32_t HeaderLevel(nsAtom* aTag);
|
2014-01-04 19:02:17 +04:00
|
|
|
static int32_t GetUnicharWidth(char16_t ucs);
|
|
|
|
static int32_t GetUnicharStringWidth(const char16_t* pwcs, int32_t n);
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
// Someday may want to make this non-const:
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t TagStackSize = 500;
|
|
|
|
static const uint32_t OLStackSize = 100;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2017-04-27 06:44:24 +03:00
|
|
|
static bool gPreferenceInitialized = false;
|
|
|
|
static bool gAlwaysIncludeRuby = false;
|
|
|
|
|
2017-04-24 13:17:17 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPlainTextSerializer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPlainTextSerializer)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPlainTextSerializer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentSerializer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsPlainTextSerializer,
|
|
|
|
mElement)
|
|
|
|
|
2015-03-12 20:20:29 +03:00
|
|
|
nsresult
|
|
|
|
NS_NewPlainTextSerializer(nsIContentSerializer** aSerializer)
|
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 14:57:30 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPlainTextSerializer> it = new nsPlainTextSerializer();
|
2015-03-12 20:20:29 +03:00
|
|
|
it.forget(aSerializer);
|
|
|
|
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 14:57:30 +04:00
|
|
|
}
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
nsPlainTextSerializer::nsPlainTextSerializer()
|
2018-06-16 17:21:46 +03:00
|
|
|
: mFlags(0)
|
|
|
|
, mFloatingLines(-1)
|
|
|
|
, mLineBreakDue(false)
|
|
|
|
, kSpace(NS_LITERAL_STRING(" ")) // Init of "constant"
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mOutputString = nullptr;
|
2008-02-19 16:17:29 +03:00
|
|
|
mHeadLevel = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mAtFirstColumn = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
mIndent = 0;
|
|
|
|
mCiteQuoteLevel = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mStructs = true; // will be read from prefs later
|
2000-09-11 09:10:32 +04:00
|
|
|
mHeaderStrategy = 1 /*indent increasingly*/; // ditto
|
2011-10-17 18:59:28 +04:00
|
|
|
mHasWrittenCiteBlockquote = false;
|
2001-07-13 22:45:53 +04:00
|
|
|
mSpanLevel = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i <= 6; i++) {
|
2000-09-11 09:10:32 +04:00
|
|
|
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,
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = false;
|
2015-03-04 08:54:40 +03:00
|
|
|
mPreFormattedMail = false;
|
2011-10-17 18:59:28 +04:00
|
|
|
mStartedOutput = false;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2015-01-18 01:31:59 +03:00
|
|
|
mPreformattedBlockBoundary = false;
|
2015-03-10 09:11:55 +03:00
|
|
|
mWithRubyAnnotation = false; // will be read from pref and flag later
|
2015-01-18 01:31:59 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
// initialize the tag stack to zero:
|
2012-03-08 18:42:27 +04:00
|
|
|
// The stack only ever contains pointers to static atoms, so they don't
|
|
|
|
// need refcounting.
|
2017-10-03 01:05:19 +03:00
|
|
|
mTagStack = new nsAtom*[TagStackSize];
|
2000-09-11 09:10:32 +04:00
|
|
|
mTagStackIndex = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
mIgnoreAboveIndex = (uint32_t)kNotFound;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2010-09-29 02:37:30 +04:00
|
|
|
// initialize the OL stack, where numbers for ordered lists are kept
|
2012-08-22 19:56:38 +04:00
|
|
|
mOLStack = new int32_t[OLStackSize];
|
2000-09-11 09:10:32 +04:00
|
|
|
mOLStackIndex = 0;
|
2001-04-10 05:11:39 +04:00
|
|
|
|
|
|
|
mULCount = 0;
|
2013-08-06 18:40:38 +04:00
|
|
|
|
|
|
|
mIgnoredChildNodeLevel = 0;
|
2017-04-27 06:44:24 +03:00
|
|
|
|
|
|
|
if (!gPreferenceInitialized) {
|
|
|
|
Preferences::AddBoolVarCache(&gAlwaysIncludeRuby, PREF_ALWAYS_INCLUDE_RUBY,
|
|
|
|
true);
|
|
|
|
gPreferenceInitialized = true;
|
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPlainTextSerializer::~nsPlainTextSerializer()
|
|
|
|
{
|
|
|
|
delete[] mTagStack;
|
2010-09-29 02:37:30 +04:00
|
|
|
delete[] mOLStack;
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(mHeadLevel == 0, "Wrong head level!");
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::Init(uint32_t aFlags,
|
|
|
|
uint32_t aWrapColumn,
|
|
|
|
const Encoding* aEncoding,
|
|
|
|
bool aIsCopying,
|
2017-06-12 22:20:44 +03:00
|
|
|
bool aIsWholeDocument,
|
|
|
|
bool* aNeedsPreformatScanning)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2001-01-12 10:26:39 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Check if the major control flags are set correctly.
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
2001-01-12 10:26:39 +03:00
|
|
|
NS_ASSERTION(aFlags & nsIDocumentEncoder::OutputFormatted,
|
|
|
|
"If you want format=flowed, you must combine it with "
|
|
|
|
"nsIDocumentEncoder::OutputFormatted");
|
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aFlags & nsIDocumentEncoder::OutputFormatted) {
|
2001-01-12 10:26:39 +03:00
|
|
|
NS_ASSERTION(!(aFlags & nsIDocumentEncoder::OutputPreformatted),
|
|
|
|
"Can't do formatted and preformatted output at the same time!");
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2001-01-12 10:26:39 +03:00
|
|
|
#endif
|
2003-11-19 04:20:56 +03:00
|
|
|
|
2017-06-12 22:20:44 +03:00
|
|
|
*aNeedsPreformatScanning = true;
|
2001-01-12 10:26:39 +03:00
|
|
|
mFlags = aFlags;
|
|
|
|
mWrapColumn = aWrapColumn;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2001-01-12 10:26:39 +03:00
|
|
|
// Only create a linebreaker if we will handle wrapping.
|
2015-11-27 17:27:00 +03:00
|
|
|
if (MayWrap() && MayBreakLines()) {
|
2005-09-11 14:08:43 +04:00
|
|
|
mLineBreaker = nsContentUtils::LineBreaker();
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the line break character:
|
|
|
|
if ((mFlags & nsIDocumentEncoder::OutputCRLineBreak)
|
2002-12-11 02:44:03 +03:00
|
|
|
&& (mFlags & nsIDocumentEncoder::OutputLFLineBreak)) {
|
|
|
|
// Windows
|
2004-06-17 04:13:25 +04:00
|
|
|
mLineBreak.AssignLiteral("\r\n");
|
2002-12-11 02:44:03 +03:00
|
|
|
}
|
|
|
|
else if (mFlags & nsIDocumentEncoder::OutputCRLineBreak) {
|
|
|
|
// Mac
|
2014-01-04 19:02:17 +04:00
|
|
|
mLineBreak.Assign(char16_t('\r'));
|
2002-12-11 02:44:03 +03:00
|
|
|
}
|
|
|
|
else if (mFlags & nsIDocumentEncoder::OutputLFLineBreak) {
|
|
|
|
// Unix/DOM
|
2014-01-04 19:02:17 +04:00
|
|
|
mLineBreak.Assign(char16_t('\n'));
|
2002-12-11 02:44:03 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Platform/default
|
2004-08-04 21:33:30 +04:00
|
|
|
mLineBreak.AssignLiteral(NS_LINEBREAK);
|
2002-12-11 02:44:03 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = false;
|
2002-12-11 02:44:03 +03:00
|
|
|
mFloatingLines = -1;
|
|
|
|
|
2015-01-18 01:31:59 +03:00
|
|
|
mPreformattedBlockBoundary = false;
|
|
|
|
|
2002-12-11 02:44:03 +03:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatted) {
|
|
|
|
// Get some prefs that controls how we do formatted output
|
2011-05-25 10:31:59 +04:00
|
|
|
mStructs = Preferences::GetBool(PREF_STRUCTS, mStructs);
|
2004-04-30 03:34:19 +04:00
|
|
|
|
|
|
|
mHeaderStrategy =
|
2011-05-25 10:32:00 +04:00
|
|
|
Preferences::GetInt(PREF_HEADER_STRATEGY, mHeaderStrategy);
|
2002-12-11 02:44:03 +03:00
|
|
|
}
|
2001-11-30 12:19:33 +03:00
|
|
|
|
2015-03-10 09:11:55 +03:00
|
|
|
// The pref is default inited to false in libpref, but we use true
|
|
|
|
// as fallback value because we don't want to affect behavior in
|
|
|
|
// other places which use this serializer currently.
|
|
|
|
mWithRubyAnnotation =
|
2017-04-27 06:44:24 +03:00
|
|
|
gAlwaysIncludeRuby ||
|
2015-03-10 09:11:55 +03:00
|
|
|
(mFlags & nsIDocumentEncoder::OutputRubyAnnotation);
|
|
|
|
|
2014-05-23 13:54:58 +04:00
|
|
|
// XXX We should let the caller decide whether to do this or not
|
|
|
|
mFlags &= ~nsIDocumentEncoder::OutputNoFramesContent;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
|
|
|
nsPlainTextSerializer::GetLastBool(const nsTArray<bool>& aStack)
|
2002-12-19 09:22:55 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size = aStack.Length();
|
2002-12-19 09:22:55 +03:00
|
|
|
if (size == 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-12-19 09:22:55 +03:00
|
|
|
}
|
2009-03-20 11:15:35 +03:00
|
|
|
return aStack.ElementAt(size-1);
|
2002-12-19 09:22:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsPlainTextSerializer::SetLastBool(nsTArray<bool>& aStack, bool aValue)
|
2002-12-19 09:22:55 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size = aStack.Length();
|
2002-12-19 09:22:55 +03:00
|
|
|
if (size > 0) {
|
2009-03-20 11:15:35 +03:00
|
|
|
aStack.ElementAt(size-1) = aValue;
|
2002-12-19 09:22:55 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ERROR("There is no \"Last\" value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsPlainTextSerializer::PushBool(nsTArray<bool>& aStack, bool aValue)
|
2002-12-19 09:22:55 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
aStack.AppendElement(bool(aValue));
|
2002-12-19 09:22:55 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
|
|
|
nsPlainTextSerializer::PopBool(nsTArray<bool>& aStack)
|
2002-12-19 09:22:55 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool returnValue = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size = aStack.Length();
|
2002-12-19 09:22:55 +03:00
|
|
|
if (size > 0) {
|
2009-03-20 11:15:35 +03:00
|
|
|
returnValue = aStack.ElementAt(size-1);
|
2002-12-19 09:22:55 +03:00
|
|
|
aStack.RemoveElementAt(size-1);
|
|
|
|
}
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2013-08-06 18:40:38 +04:00
|
|
|
bool
|
2017-10-03 01:05:19 +03:00
|
|
|
nsPlainTextSerializer::ShouldReplaceContainerWithPlaceholder(nsAtom* aTag)
|
2013-08-06 18:40:38 +04:00
|
|
|
{
|
|
|
|
// If nsIDocumentEncoder::OutputNonTextContentAsPlaceholder is set,
|
|
|
|
// non-textual container element should be serialized as placeholder
|
|
|
|
// character and its child nodes should be ignored. See bug 895239.
|
|
|
|
if (!(mFlags & nsIDocumentEncoder::OutputNonTextContentAsPlaceholder)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
(aTag == nsGkAtoms::audio) ||
|
|
|
|
(aTag == nsGkAtoms::canvas) ||
|
|
|
|
(aTag == nsGkAtoms::iframe) ||
|
|
|
|
(aTag == nsGkAtoms::meter) ||
|
|
|
|
(aTag == nsGkAtoms::progress) ||
|
|
|
|
(aTag == nsGkAtoms::object) ||
|
|
|
|
(aTag == nsGkAtoms::svg) ||
|
|
|
|
(aTag == nsGkAtoms::video);
|
|
|
|
}
|
|
|
|
|
2015-03-10 09:11:55 +03:00
|
|
|
bool
|
2017-10-03 01:05:19 +03:00
|
|
|
nsPlainTextSerializer::IsIgnorableRubyAnnotation(nsAtom* aTag)
|
2015-03-10 09:11:55 +03:00
|
|
|
{
|
|
|
|
if (mWithRubyAnnotation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
aTag == nsGkAtoms::rp ||
|
|
|
|
aTag == nsGkAtoms::rt ||
|
|
|
|
aTag == nsGkAtoms::rtc;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2010-05-04 17:19:54 +04:00
|
|
|
nsPlainTextSerializer::AppendText(nsIContent* aText,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aStartOffset,
|
2017-07-06 15:00:35 +03:00
|
|
|
int32_t aEndOffset,
|
2002-03-24 02:54:46 +03:00
|
|
|
nsAString& aStr)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
if (mIgnoreAboveIndex != (uint32_t)kNotFound) {
|
2001-10-02 14:02:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2002-03-22 11:50:35 +03:00
|
|
|
NS_ASSERTION(aStartOffset >= 0, "Negative start offset for text fragment!");
|
|
|
|
if ( aStartOffset < 0 )
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2001-10-02 14:02:07 +04:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
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 14:57:30 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2010-05-04 17:19:54 +04:00
|
|
|
nsIContent* content = aText;
|
2006-07-19 08:36:36 +04:00
|
|
|
const nsTextFragment* frag;
|
|
|
|
if (!content || !(frag = content->GetText())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2014-04-17 02:01:55 +04:00
|
|
|
int32_t fragLength = frag->GetLength();
|
|
|
|
int32_t endoffset = (aEndOffset == -1) ? fragLength : std::min(aEndOffset, fragLength);
|
2006-07-19 08:36:36 +04:00
|
|
|
NS_ASSERTION(aStartOffset <= endoffset, "A start offset is beyond the end of the text fragment!");
|
2002-03-22 11:50:35 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t length = endoffset - aStartOffset;
|
2006-07-19 08:36:36 +04:00
|
|
|
if (length <= 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-03-22 11:50:35 +03:00
|
|
|
|
2011-11-04 12:44:13 +04:00
|
|
|
nsAutoString textstr;
|
2006-07-19 08:36:36 +04:00
|
|
|
if (frag->Is2b()) {
|
|
|
|
textstr.Assign(frag->Get2b() + aStartOffset, length);
|
|
|
|
}
|
|
|
|
else {
|
2011-11-04 12:44:13 +04:00
|
|
|
// AssignASCII is for 7-bit character only, so don't use it
|
|
|
|
const char *data = frag->Get1b();
|
|
|
|
CopyASCIItoUTF16(Substring(data + aStartOffset, data + endoffset), textstr);
|
2000-09-11 09:10:32 +04: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 14:57:30 +04:00
|
|
|
mOutputString = &aStr;
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
// We have to split the string across newlines
|
|
|
|
// to match parser behavior
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t start = 0;
|
|
|
|
int32_t offset = textstr.FindCharInSet("\n\r");
|
2000-09-11 09:10:32 +04:00
|
|
|
while (offset != kNotFound) {
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (offset>start) {
|
2001-01-12 10:26:39 +03:00
|
|
|
// Pass in the line
|
2012-03-08 18:42:27 +04:00
|
|
|
DoAddText(false,
|
|
|
|
Substring(textstr, start, offset-start));
|
2001-01-12 10:26:39 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
// Pass in a newline
|
2012-03-08 18:42:27 +04:00
|
|
|
DoAddText(true, mLineBreak);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
start = offset+1;
|
|
|
|
offset = textstr.FindCharInSet("\n\r", start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the last bit of the string if there's any left
|
2012-03-08 18:42:27 +04:00
|
|
|
if (start < length) {
|
2000-09-11 09:10:32 +04:00
|
|
|
if (start) {
|
2012-03-08 18:42:27 +04:00
|
|
|
DoAddText(false, Substring(textstr, start, length - start));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-03-08 18:42:27 +04:00
|
|
|
DoAddText(false, textstr);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mOutputString = nullptr;
|
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 14:57:30 +04:00
|
|
|
|
|
|
|
return rv;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2004-02-20 03:10:36 +03:00
|
|
|
NS_IMETHODIMP
|
2010-05-04 17:19:54 +04:00
|
|
|
nsPlainTextSerializer::AppendCDATASection(nsIContent* aCDATASection,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset,
|
2004-02-20 03:10:36 +03:00
|
|
|
nsAString& aStr)
|
|
|
|
{
|
|
|
|
return AppendText(aCDATASection, aStartOffset, aEndOffset, aStr);
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:20:44 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::ScanElementForPreformat(Element* aElement)
|
|
|
|
{
|
|
|
|
mPreformatStack.push(IsElementPreformatted(aElement));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlainTextSerializer::ForgetElementForPreformat(Element* aElement)
|
|
|
|
{
|
2017-12-01 18:45:25 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mPreformatStack.empty(), "Tried to pop without previous push.");
|
2017-06-12 22:20:44 +03:00
|
|
|
mPreformatStack.pop();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-02-20 03:10:36 +03:00
|
|
|
NS_IMETHODIMP
|
2010-10-23 01:25:22 +04:00
|
|
|
nsPlainTextSerializer::AppendElementStart(Element* aElement,
|
|
|
|
Element* aOriginalElement,
|
2002-03-24 02:54:46 +03:00
|
|
|
nsAString& aStr)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aElement);
|
|
|
|
|
2011-07-03 13:53:40 +04:00
|
|
|
mElement = aElement;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* id = GetIdForContent(mElement);
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2015-01-19 14:17:22 +03:00
|
|
|
bool isContainer = !FragmentOrElement::IsHTMLVoid(id);
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
mOutputString = &aStr;
|
|
|
|
|
|
|
|
if (isContainer) {
|
2012-03-08 18:42:27 +04:00
|
|
|
rv = DoOpenContainer(id);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-03-08 18:42:27 +04:00
|
|
|
rv = DoAddLeaf(id);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mElement = nullptr;
|
|
|
|
mOutputString = nullptr;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (id == nsGkAtoms::head) {
|
2008-02-19 16:17:29 +03:00
|
|
|
++mHeadLevel;
|
|
|
|
}
|
2000-10-12 02:50:14 +04:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
return rv;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-23 01:25:22 +04:00
|
|
|
nsPlainTextSerializer::AppendElementEnd(Element* aElement,
|
2002-03-24 02:54:46 +03:00
|
|
|
nsAString& aStr)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aElement);
|
|
|
|
|
2011-07-03 13:53:40 +04:00
|
|
|
mElement = aElement;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* id = GetIdForContent(mElement);
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2015-01-19 14:17:22 +03:00
|
|
|
bool isContainer = !FragmentOrElement::IsHTMLVoid(id);
|
2000-09-11 09:10:32 +04: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 14:57:30 +04:00
|
|
|
rv = DoCloseContainer(id);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mElement = nullptr;
|
|
|
|
mOutputString = nullptr;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (id == nsGkAtoms::head) {
|
2012-04-17 20:55:50 +04:00
|
|
|
NS_ASSERTION(mHeadLevel != 0,
|
|
|
|
"mHeadLevel being decremented below 0");
|
2008-02-19 16:17:29 +03:00
|
|
|
--mHeadLevel;
|
|
|
|
}
|
2000-10-12 02:50:14 +04:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 02:54:46 +03:00
|
|
|
nsPlainTextSerializer::Flush(nsAString& aStr)
|
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 14:57:30 +04:00
|
|
|
{
|
|
|
|
mOutputString = &aStr;
|
|
|
|
FlushLine();
|
2012-07-30 18:20:58 +04:00
|
|
|
mOutputString = nullptr;
|
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 14:57:30 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-08-13 22:41:16 +04:00
|
|
|
NS_IMETHODIMP
|
2010-05-04 17:19:54 +04:00
|
|
|
nsPlainTextSerializer::AppendDocumentStart(nsIDocument *aDocument,
|
|
|
|
nsAString& aStr)
|
2002-08-13 22:41:16 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsPlainTextSerializer::DoOpenContainer(nsAtom* aTag)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2013-08-06 18:40:38 +04:00
|
|
|
// Check if we need output current node as placeholder character and ignore
|
|
|
|
// child nodes.
|
2015-03-03 14:09:00 +03:00
|
|
|
if (ShouldReplaceContainerWithPlaceholder(mElement->NodeInfo()->NameAtom())) {
|
2013-08-06 18:40:38 +04:00
|
|
|
if (mIgnoredChildNodeLevel == 0) {
|
|
|
|
// Serialize current node as placeholder character
|
2016-07-20 07:07:53 +03:00
|
|
|
Write(NS_LITERAL_STRING(u"\xFFFC"));
|
2013-08-06 18:40:38 +04:00
|
|
|
}
|
|
|
|
// Ignore child nodes.
|
|
|
|
mIgnoredChildNodeLevel++;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-10 09:11:55 +03:00
|
|
|
if (IsIgnorableRubyAnnotation(aTag)) {
|
|
|
|
// Ignorable ruby annotation shouldn't be replaced by a placeholder
|
|
|
|
// character, neither any of its descendants.
|
|
|
|
mIgnoredChildNodeLevel++;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-08-06 18:40:38 +04:00
|
|
|
|
2015-01-18 01:31:59 +03:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputForPlainTextClipboardCopy) {
|
|
|
|
if (mPreformattedBlockBoundary && DoOutput()) {
|
|
|
|
// Should always end a line, but get no more whitespace
|
|
|
|
if (mFloatingLines < 0)
|
|
|
|
mFloatingLines = 0;
|
|
|
|
mLineBreakDue = true;
|
|
|
|
}
|
|
|
|
mPreformattedBlockBoundary = false;
|
|
|
|
}
|
|
|
|
|
2003-07-03 04:34:34 +04:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputRaw) {
|
|
|
|
// Raw means raw. Don't even think about doing anything fancy
|
|
|
|
// here like indenting, adding line breaks or any other
|
|
|
|
// characters such as list item bullets, quote characters
|
|
|
|
// around <q>, etc. I mean it! Don't make me smack you!
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-11-30 12:19:33 +03:00
|
|
|
if (mTagStackIndex < TagStackSize) {
|
2012-03-08 18:42:27 +04:00
|
|
|
mTagStack[mTagStackIndex++] = aTag;
|
2001-11-30 12:19:33 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if (mIgnoreAboveIndex != (uint32_t)kNotFound) {
|
2001-10-02 14:02:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-06-16 22:20:59 +04:00
|
|
|
// Reset this so that <blockquote type=cite> doesn't affect the whitespace
|
|
|
|
// above random <pre>s below it.
|
2012-03-08 18:42:27 +04:00
|
|
|
mHasWrittenCiteBlockquote = mHasWrittenCiteBlockquote &&
|
|
|
|
aTag == nsGkAtoms::pre;
|
2005-06-16 22:20:59 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isInCiteBlockquote = false;
|
2005-06-01 23:31:47 +04:00
|
|
|
|
|
|
|
// XXX special-case <blockquote type=cite> so that we don't add additional
|
|
|
|
// newlines before the text.
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aTag == nsGkAtoms::blockquote) {
|
2005-06-01 23:31:47 +04:00
|
|
|
nsAutoString value;
|
2012-03-08 18:42:27 +04:00
|
|
|
nsresult rv = GetAttributeValue(nsGkAtoms::type, value);
|
2005-06-01 23:31:47 +04:00
|
|
|
isInCiteBlockquote = NS_SUCCEEDED(rv) && value.EqualsIgnoreCase("cite");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLineBreakDue && !isInCiteBlockquote)
|
2002-03-04 13:47:19 +03:00
|
|
|
EnsureVerticalSpace(mFloatingLines);
|
|
|
|
|
2001-11-30 12:19:33 +03:00
|
|
|
// Check if this tag's content that should not be output
|
2012-03-08 18:42:27 +04:00
|
|
|
if ((aTag == nsGkAtoms::noscript &&
|
2001-11-30 12:19:33 +03:00
|
|
|
!(mFlags & nsIDocumentEncoder::OutputNoScriptContent)) ||
|
2012-03-08 18:42:27 +04:00
|
|
|
((aTag == nsGkAtoms::iframe || aTag == nsGkAtoms::noframes) &&
|
2001-11-30 12:19:33 +03:00
|
|
|
!(mFlags & nsIDocumentEncoder::OutputNoFramesContent))) {
|
2017-07-06 15:00:35 +03:00
|
|
|
// Ignore everything that follows the current tag in
|
2003-01-16 23:24:03 +03:00
|
|
|
// question until a matching end tag is encountered.
|
|
|
|
mIgnoreAboveIndex = mTagStackIndex - 1;
|
2001-11-30 12:19:33 +03:00
|
|
|
return NS_OK;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aTag == nsGkAtoms::body) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// Try to figure out here whether we have a
|
2015-03-04 08:54:40 +03:00
|
|
|
// preformatted style attribute set by Thunderbird.
|
2000-09-11 09:10:32 +04:00
|
|
|
//
|
2008-03-08 12:44:35 +03:00
|
|
|
// Trigger on the presence of a "pre-wrap" in the
|
2000-09-11 09:10:32 +04:00
|
|
|
// 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;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t whitespace;
|
2012-03-08 18:42:27 +04:00
|
|
|
if (NS_SUCCEEDED(GetAttributeValue(nsGkAtoms::style, style)) &&
|
2001-01-12 10:26:39 +03:00
|
|
|
(kNotFound != (whitespace = style.Find("white-space:")))) {
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
if (kNotFound != style.Find("pre-wrap", true, whitespace)) {
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_preformatted
|
2015-03-04 08:54:40 +03:00
|
|
|
printf("Set mPreFormattedMail based on style pre-wrap\n");
|
2000-09-11 09:10:32 +04:00
|
|
|
#endif
|
2015-03-04 08:54:40 +03:00
|
|
|
mPreFormattedMail = true;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t widthOffset = style.Find("width:");
|
2000-09-11 09:10:32 +04:00
|
|
|
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.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t semiOffset = style.Find("ch", false, widthOffset+6);
|
|
|
|
int32_t length = (semiOffset > 0 ? semiOffset - widthOffset - 6
|
2000-09-11 09:10:32 +04:00
|
|
|
: style.Length() - widthOffset);
|
|
|
|
nsAutoString widthstr;
|
|
|
|
style.Mid(widthstr, widthOffset+6, length);
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult err;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t col = widthstr.ToInteger(&err);
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(err)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
mWrapColumn = (uint32_t)col;
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_preformatted
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("Set wrap column to %d based on style\n", mWrapColumn);
|
2000-09-11 09:10:32 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
else if (kNotFound != style.Find("pre", true, whitespace)) {
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_preformatted
|
2015-03-04 08:54:40 +03:00
|
|
|
printf("Set mPreFormattedMail based on style pre\n");
|
2000-09-11 09:10:32 +04:00
|
|
|
#endif
|
2015-03-04 08:54:40 +03:00
|
|
|
mPreFormattedMail = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
mWrapColumn = 0;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
2010-09-21 17:02:55 +04:00
|
|
|
/* See comment at end of function. */
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
2015-03-04 08:54:40 +03:00
|
|
|
mPreFormattedMail = false;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-06 21:38:06 +03:00
|
|
|
// Keep this in sync with DoCloseContainer!
|
2000-09-11 09:10:32 +04:00
|
|
|
if (!DoOutput()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aTag == nsGkAtoms::p)
|
2005-06-01 23:31:47 +04:00
|
|
|
EnsureVerticalSpace(1);
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::pre) {
|
2005-06-01 23:31:47 +04:00
|
|
|
if (GetLastBool(mIsInCiteBlockquote))
|
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
else if (mHasWrittenCiteBlockquote) {
|
|
|
|
EnsureVerticalSpace(0);
|
2011-10-17 18:59:28 +04:00
|
|
|
mHasWrittenCiteBlockquote = false;
|
2005-06-01 23:31:47 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
EnsureVerticalSpace(1);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::tr) {
|
2011-10-17 18:59:28 +04:00
|
|
|
PushBool(mHasWrittenCellsForRow, false);
|
2002-12-19 09:22:55 +03:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::td || aTag == nsGkAtoms::th) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// We must make sure that the content of two table cells get a
|
|
|
|
// space between them.
|
2002-12-19 09:22:55 +03:00
|
|
|
|
2003-03-24 07:15:34 +03:00
|
|
|
// To make the separation between cells most obvious and
|
2002-12-19 09:22:55 +03:00
|
|
|
// importable, we use a TAB.
|
|
|
|
if (GetLastBool(mHasWrittenCellsForRow)) {
|
|
|
|
// Bypass |Write| so that the TAB isn't compressed away.
|
2016-07-21 08:03:25 +03:00
|
|
|
AddToLine(u"\t", 1);
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2009-03-20 11:15:35 +03:00
|
|
|
else if (mHasWrittenCellsForRow.IsEmpty()) {
|
2002-12-19 09:22:55 +03:00
|
|
|
// We don't always see a <tr> (nor a <table>) before the <td> if we're
|
|
|
|
// copying part of a table
|
2011-10-17 18:59:28 +04:00
|
|
|
PushBool(mHasWrittenCellsForRow, true); // will never be popped
|
2002-12-19 09:22:55 +03:00
|
|
|
}
|
|
|
|
else {
|
2011-10-17 18:59:28 +04:00
|
|
|
SetLastBool(mHasWrittenCellsForRow, true);
|
2002-12-19 09:22:55 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::ul) {
|
2001-04-10 05:11:39 +04: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-11 01:08:08 +03:00
|
|
|
mIndent += kIndentSizeList;
|
2001-04-10 05:11:39 +04:00
|
|
|
mULCount++;
|
2001-01-11 01:08:08 +03:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::ol) {
|
2001-04-10 05:11:39 +04:00
|
|
|
EnsureVerticalSpace(mULCount + mOLStackIndex == 0 ? 1 : 0);
|
2010-09-29 02:37:30 +04:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatted) {
|
|
|
|
// Must end the current line before we change indention
|
|
|
|
if (mOLStackIndex < OLStackSize) {
|
|
|
|
nsAutoString startAttr;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startVal = 1;
|
2012-03-08 18:42:27 +04:00
|
|
|
if (NS_SUCCEEDED(GetAttributeValue(nsGkAtoms::start, startAttr))) {
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult rv = NS_OK;
|
2010-09-29 02:37:30 +04:00
|
|
|
startVal = startAttr.ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
startVal = 1;
|
|
|
|
}
|
|
|
|
mOLStack[mOLStackIndex++] = startVal;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mOLStackIndex++;
|
|
|
|
}
|
2001-01-11 01:08:08 +03:00
|
|
|
mIndent += kIndentSizeList; // see ul
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::li &&
|
2010-09-29 02:37:30 +04:00
|
|
|
(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
if (mTagStackIndex > 1 && IsInOL()) {
|
|
|
|
if (mOLStackIndex > 0) {
|
|
|
|
nsAutoString valueAttr;
|
2012-03-08 18:42:27 +04:00
|
|
|
if (NS_SUCCEEDED(GetAttributeValue(nsGkAtoms::value, valueAttr))) {
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult rv = NS_OK;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t valueAttrVal = valueAttr.ToInteger(&rv);
|
2010-09-29 02:37:30 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mOLStack[mOLStackIndex-1] = valueAttrVal;
|
|
|
|
}
|
|
|
|
// This is what nsBulletFrame does for OLs:
|
|
|
|
mInIndentString.AppendInt(mOLStack[mOLStackIndex-1]++, 10);
|
|
|
|
}
|
|
|
|
else {
|
2014-01-04 19:02:17 +04:00
|
|
|
mInIndentString.Append(char16_t('#'));
|
2010-09-29 02:37:30 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
mInIndentString.Append(char16_t('.'));
|
2010-09-29 02:37:30 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2018-02-07 20:49:43 +03:00
|
|
|
static const char bulletCharArray[] = "*o+#";
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t index = mULCount > 0 ? (mULCount - 1) : 3;
|
2010-09-29 02:37:30 +04:00
|
|
|
char bulletChar = bulletCharArray[index % 4];
|
2014-01-04 19:02:17 +04:00
|
|
|
mInIndentString.Append(char16_t(bulletChar));
|
2010-09-29 02:37:30 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
mInIndentString.Append(char16_t(' '));
|
2010-09-29 02:37:30 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::dl) {
|
2001-01-11 01:08:08 +03:00
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::dt) {
|
2002-09-17 09:41:11 +04:00
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::dd) {
|
2002-09-17 09:41:11 +04:00
|
|
|
EnsureVerticalSpace(0);
|
2001-01-11 01:08:08 +03:00
|
|
|
mIndent += kIndentSizeDD;
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::span) {
|
2001-07-13 22:45:53 +04:00
|
|
|
++mSpanLevel;
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::blockquote) {
|
2002-12-14 09:16:56 +03:00
|
|
|
// Push
|
2002-12-19 09:22:55 +03:00
|
|
|
PushBool(mIsInCiteBlockquote, isInCiteBlockquote);
|
2002-12-14 09:16:56 +03:00
|
|
|
if (isInCiteBlockquote) {
|
2005-06-01 23:31:47 +04:00
|
|
|
EnsureVerticalSpace(0);
|
2002-09-10 15:15:43 +04:00
|
|
|
mCiteQuoteLevel++;
|
|
|
|
}
|
|
|
|
else {
|
2005-06-01 23:31:47 +04:00
|
|
|
EnsureVerticalSpace(1);
|
2002-09-10 15:15:43 +04:00
|
|
|
mIndent += kTabSize; // Check for some maximum value?
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::q) {
|
2007-06-05 23:24:55 +04:00
|
|
|
Write(NS_LITERAL_STRING("\""));
|
|
|
|
}
|
2001-01-11 01:08:08 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
// Else make sure we'll separate block level tags,
|
|
|
|
// even if we're about to leave, before doing any other formatting.
|
2015-01-18 01:30:21 +03:00
|
|
|
else if (IsElementBlock(mElement)) {
|
2000-09-11 09:10:32 +04:00
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
}
|
|
|
|
|
2001-01-11 01:08:08 +03:00
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 09:10:32 +04:00
|
|
|
if (!(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-11 01:08:08 +03:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// The rest of this routine is formatted output stuff,
|
|
|
|
// which we should skip if we're not formatted:
|
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2002-12-14 09:16:56 +03:00
|
|
|
// Push on stack
|
2012-03-08 18:42:27 +04:00
|
|
|
bool currentNodeIsConverted = IsCurrentNodeConverted();
|
2002-12-14 09:16:56 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aTag == nsGkAtoms::h1 || aTag == nsGkAtoms::h2 ||
|
|
|
|
aTag == nsGkAtoms::h3 || aTag == nsGkAtoms::h4 ||
|
|
|
|
aTag == nsGkAtoms::h5 || aTag == nsGkAtoms::h6)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
EnsureVerticalSpace(2);
|
|
|
|
if (mHeaderStrategy == 2) { // numbered
|
|
|
|
mIndent += kIndentSizeHeaders;
|
|
|
|
// Caching
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t level = HeaderLevel(aTag);
|
2000-09-11 09:10:32 +04:00
|
|
|
// Increase counter for current level
|
|
|
|
mHeaderCounter[level]++;
|
|
|
|
// Reset all lower levels
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
for (i = level + 1; i <= 6; i++) {
|
|
|
|
mHeaderCounter[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct numbers
|
2003-12-23 18:41:10 +03:00
|
|
|
nsAutoString leadup;
|
2000-09-11 09:10:32 +04:00
|
|
|
for (i = 1; i <= level; i++) {
|
|
|
|
leadup.AppendInt(mHeaderCounter[i]);
|
2014-01-04 19:02:17 +04:00
|
|
|
leadup.Append(char16_t('.'));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
leadup.Append(char16_t(' '));
|
2003-12-23 18:41:10 +03:00
|
|
|
Write(leadup);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
else if (mHeaderStrategy == 1) { // indent increasingly
|
|
|
|
mIndent += kIndentSizeHeaders;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = HeaderLevel(aTag); i > 1; i--) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// for h(x), run x-1 times
|
|
|
|
mIndent += kIndentIncrementHeaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::a && !currentNodeIsConverted) {
|
2000-09-11 09:10:32 +04:00
|
|
|
nsAutoString url;
|
2012-03-08 18:42:27 +04:00
|
|
|
if (NS_SUCCEEDED(GetAttributeValue(nsGkAtoms::href, url))
|
2000-09-11 09:10:32 +04:00
|
|
|
&& !url.IsEmpty()) {
|
|
|
|
mURL = url;
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::sup && mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("^"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::sub && mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("_"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::code && mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("|"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if ((aTag == nsGkAtoms::strong || aTag == nsGkAtoms::b)
|
2002-12-14 09:16:56 +03:00
|
|
|
&& mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("*"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if ((aTag == nsGkAtoms::em || aTag == nsGkAtoms::i)
|
2002-12-14 09:16:56 +03:00
|
|
|
&& mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("/"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::u && mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("_"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2010-09-21 17:02:55 +04:00
|
|
|
/* Container elements are always block elements, so we shouldn't
|
|
|
|
output any whitespace immediately after the container tag even if
|
|
|
|
there's extra whitespace there because the HTML is pretty-printed
|
|
|
|
or something. To ensure that happens, tell the serializer we're
|
|
|
|
already in whitespace so it won't output more. */
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
2010-09-21 17:02:55 +04:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsPlainTextSerializer::DoCloseContainer(nsAtom* aTag)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2015-03-03 14:09:00 +03:00
|
|
|
if (ShouldReplaceContainerWithPlaceholder(mElement->NodeInfo()->NameAtom())) {
|
2013-08-06 18:40:38 +04:00
|
|
|
mIgnoredChildNodeLevel--;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-10 09:11:55 +03:00
|
|
|
if (IsIgnorableRubyAnnotation(aTag)) {
|
|
|
|
mIgnoredChildNodeLevel--;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-08-06 18:40:38 +04:00
|
|
|
|
2015-01-18 01:31:59 +03:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputForPlainTextClipboardCopy) {
|
|
|
|
if (DoOutput() && IsInPre() && IsElementBlock(mElement)) {
|
|
|
|
// If we're closing a preformatted block element, output a line break
|
|
|
|
// when we find a new container.
|
|
|
|
mPreformattedBlockBoundary = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-03 04:34:34 +04:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputRaw) {
|
|
|
|
// Raw means raw. Don't even think about doing anything fancy
|
|
|
|
// here like indenting, adding line breaks or any other
|
|
|
|
// characters such as list item bullets, quote characters
|
|
|
|
// around <q>, etc. I mean it! Don't make me smack you!
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
if (mTagStackIndex > 0) {
|
|
|
|
--mTagStackIndex;
|
|
|
|
}
|
|
|
|
|
2001-11-30 12:19:33 +03:00
|
|
|
if (mTagStackIndex >= mIgnoreAboveIndex) {
|
2003-01-16 23:24:03 +03:00
|
|
|
if (mTagStackIndex == mIgnoreAboveIndex) {
|
|
|
|
// We're dealing with the close tag whose matching
|
|
|
|
// open tag had set the mIgnoreAboveIndex value.
|
|
|
|
// Reset mIgnoreAboveIndex before discarding this tag.
|
2012-08-22 19:56:38 +04:00
|
|
|
mIgnoreAboveIndex = (uint32_t)kNotFound;
|
2003-01-16 23:24:03 +03:00
|
|
|
}
|
2001-11-30 12:19:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
// End current line if we're ending a block level tag
|
2012-03-08 18:42:27 +04:00
|
|
|
if ((aTag == nsGkAtoms::body) || (aTag == nsGkAtoms::html)) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// 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;
|
2002-12-19 09:28:28 +03:00
|
|
|
}
|
2008-01-06 21:38:06 +03:00
|
|
|
|
|
|
|
// Keep this in sync with DoOpenContainer!
|
|
|
|
if (!DoOutput()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aTag == nsGkAtoms::tr) {
|
2002-12-19 09:22:55 +03:00
|
|
|
PopBool(mHasWrittenCellsForRow);
|
|
|
|
// Should always end a line, but get no more whitespace
|
|
|
|
if (mFloatingLines < 0)
|
|
|
|
mFloatingLines = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2010-09-29 02:37:30 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (((aTag == nsGkAtoms::li) ||
|
|
|
|
(aTag == nsGkAtoms::dt)) &&
|
2010-09-29 02:37:30 +04:00
|
|
|
(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
// Items that should always end a line, but get no more whitespace
|
|
|
|
if (mFloatingLines < 0)
|
|
|
|
mFloatingLines = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2010-09-29 02:37:30 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::pre) {
|
2005-06-01 23:31:47 +04:00
|
|
|
mFloatingLines = GetLastBool(mIsInCiteBlockquote) ? 0 : 1;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2001-04-10 05:11:39 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::ul) {
|
2002-10-02 11:12:49 +04:00
|
|
|
FlushLine();
|
2001-01-11 01:08:08 +03:00
|
|
|
mIndent -= kIndentSizeList;
|
2002-03-04 13:47:19 +03:00
|
|
|
if (--mULCount + mOLStackIndex == 0) {
|
|
|
|
mFloatingLines = 1;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2002-03-04 13:47:19 +03:00
|
|
|
}
|
2001-01-11 01:08:08 +03:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::ol) {
|
2001-01-11 01:08:08 +03:00
|
|
|
FlushLine(); // Doing this after decreasing OLStackIndex would be wrong.
|
|
|
|
mIndent -= kIndentSizeList;
|
2008-01-06 21:38:06 +03:00
|
|
|
NS_ASSERTION(mOLStackIndex, "Wrong OLStack level!");
|
2001-04-10 05:11:39 +04:00
|
|
|
mOLStackIndex--;
|
2002-03-04 13:47:19 +03:00
|
|
|
if (mULCount + mOLStackIndex == 0) {
|
|
|
|
mFloatingLines = 1;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2002-03-04 13:47:19 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::dl) {
|
2002-09-17 09:41:11 +04:00
|
|
|
mFloatingLines = 1;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2002-09-17 09:41:11 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::dd) {
|
2002-09-17 09:41:11 +04:00
|
|
|
FlushLine();
|
2001-01-11 01:08:08 +03:00
|
|
|
mIndent -= kIndentSizeDD;
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::span) {
|
2008-01-06 21:38:06 +03:00
|
|
|
NS_ASSERTION(mSpanLevel, "Span level will be negative!");
|
2001-07-13 22:45:53 +04:00
|
|
|
--mSpanLevel;
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::div) {
|
2002-04-02 01:21:14 +04:00
|
|
|
if (mFloatingLines < 0)
|
|
|
|
mFloatingLines = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2002-04-02 01:21:14 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::blockquote) {
|
2002-09-10 15:15:43 +04:00
|
|
|
FlushLine(); // Is this needed?
|
|
|
|
|
2002-12-14 09:16:56 +03:00
|
|
|
// Pop
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isInCiteBlockquote = PopBool(mIsInCiteBlockquote);
|
2002-09-10 15:15:43 +04:00
|
|
|
|
2002-12-14 09:16:56 +03:00
|
|
|
if (isInCiteBlockquote) {
|
2008-01-06 21:38:06 +03:00
|
|
|
NS_ASSERTION(mCiteQuoteLevel, "CiteQuote level will be negative!");
|
2002-09-10 15:15:43 +04:00
|
|
|
mCiteQuoteLevel--;
|
2005-06-01 23:31:47 +04:00
|
|
|
mFloatingLines = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mHasWrittenCiteBlockquote = true;
|
2002-09-10 15:15:43 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mIndent -= kTabSize;
|
2005-06-01 23:31:47 +04:00
|
|
|
mFloatingLines = 1;
|
2002-09-10 15:15:43 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2002-09-10 15:15:43 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::q) {
|
2007-06-05 23:24:55 +04:00
|
|
|
Write(NS_LITERAL_STRING("\""));
|
|
|
|
}
|
2015-01-18 01:30:21 +03:00
|
|
|
else if (IsElementBlock(mElement) && aTag != nsGkAtoms::script) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// 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?
|
2002-03-04 13:47:19 +03:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatted)
|
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
else {
|
|
|
|
if (mFloatingLines < 0)
|
|
|
|
mFloatingLines = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = true;
|
2002-03-04 13:47:19 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2001-01-11 01:08:08 +03:00
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 09:10:32 +04:00
|
|
|
if (!(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-11 01:08:08 +03:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// The rest of this routine is formatted output stuff,
|
|
|
|
// which we should skip if we're not formatted:
|
|
|
|
//////////////////////////////////////////////////////////////
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2002-12-14 09:16:56 +03:00
|
|
|
// Pop the currentConverted stack
|
2012-03-08 18:42:27 +04:00
|
|
|
bool currentNodeIsConverted = IsCurrentNodeConverted();
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aTag == nsGkAtoms::h1 || aTag == nsGkAtoms::h2 ||
|
|
|
|
aTag == nsGkAtoms::h3 || aTag == nsGkAtoms::h4 ||
|
|
|
|
aTag == nsGkAtoms::h5 || aTag == nsGkAtoms::h6) {
|
2017-07-06 15:00:35 +03:00
|
|
|
|
|
|
|
if (mHeaderStrategy) { /*numbered or indent increasingly*/
|
2000-09-11 09:10:32 +04:00
|
|
|
mIndent -= kIndentSizeHeaders;
|
|
|
|
}
|
|
|
|
if (mHeaderStrategy == 1 /*indent increasingly*/ ) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = HeaderLevel(aTag); i > 1; i--) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// for h(x), run x-1 times
|
|
|
|
mIndent -= kIndentIncrementHeaders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EnsureVerticalSpace(1);
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::a && !currentNodeIsConverted && !mURL.IsEmpty()) {
|
2017-07-06 15:00:35 +03:00
|
|
|
nsAutoString temp;
|
2004-06-17 04:13:25 +04:00
|
|
|
temp.AssignLiteral(" <");
|
2000-09-11 09:10:32 +04:00
|
|
|
temp += mURL;
|
2014-01-04 19:02:17 +04:00
|
|
|
temp.Append(char16_t('>'));
|
2000-09-11 09:10:32 +04:00
|
|
|
Write(temp);
|
|
|
|
mURL.Truncate();
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if ((aTag == nsGkAtoms::sup || aTag == nsGkAtoms::sub)
|
2002-12-14 09:16:56 +03:00
|
|
|
&& mStructs && !currentNodeIsConverted) {
|
2001-01-12 10:26:39 +03:00
|
|
|
Write(kSpace);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::code && mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("|"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if ((aTag == nsGkAtoms::strong || aTag == nsGkAtoms::b)
|
2002-12-14 09:16:56 +03:00
|
|
|
&& mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("*"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if ((aTag == nsGkAtoms::em || aTag == nsGkAtoms::i)
|
2002-12-14 09:16:56 +03:00
|
|
|
&& mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("/"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::u && mStructs && !currentNodeIsConverted) {
|
2001-04-26 23:33:12 +04:00
|
|
|
Write(NS_LITERAL_STRING("_"));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
bool
|
|
|
|
nsPlainTextSerializer::MustSuppressLeaf()
|
|
|
|
{
|
2013-08-06 18:40:38 +04:00
|
|
|
if (mIgnoredChildNodeLevel > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if ((mTagStackIndex > 1 &&
|
|
|
|
mTagStack[mTagStackIndex-2] == nsGkAtoms::select) ||
|
|
|
|
(mTagStackIndex > 0 &&
|
|
|
|
mTagStack[mTagStackIndex-1] == nsGkAtoms::select)) {
|
|
|
|
// Don't output the contents of SELECT elements;
|
|
|
|
// Might be nice, eventually, to output just the selected element.
|
|
|
|
// Read more in bug 31994.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTagStackIndex > 0 &&
|
|
|
|
(mTagStack[mTagStackIndex-1] == nsGkAtoms::script ||
|
|
|
|
mTagStack[mTagStackIndex-1] == nsGkAtoms::style)) {
|
|
|
|
// Don't output the contents of <script> or <style> tags;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPlainTextSerializer::DoAddText(bool aIsLineBreak, const nsAString& aText)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
// If we don't want any output, just return
|
|
|
|
if (!DoOutput()) {
|
2012-03-08 18:42:27 +04:00
|
|
|
return;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2005-06-16 22:20:59 +04:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (!aIsLineBreak) {
|
2005-06-16 22:20:59 +04:00
|
|
|
// Make sure to reset this, since it's no longer true.
|
2011-10-17 18:59:28 +04:00
|
|
|
mHasWrittenCiteBlockquote = false;
|
2005-06-16 22:20:59 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
|
2002-03-04 13:47:19 +03:00
|
|
|
if (mLineBreakDue)
|
|
|
|
EnsureVerticalSpace(mFloatingLines);
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (MustSuppressLeaf()) {
|
|
|
|
return;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
|
|
|
|
if (aIsLineBreak) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// 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.
|
2012-03-08 18:42:27 +04:00
|
|
|
if ((mFlags & nsIDocumentEncoder::OutputPreformatted) ||
|
2015-03-04 08:54:40 +03:00
|
|
|
(mPreFormattedMail && !mWrapColumn) ||
|
2002-03-13 09:26:33 +03:00
|
|
|
IsInPre()) {
|
2012-03-08 18:42:27 +04:00
|
|
|
EnsureVerticalSpace(mEmptyLines+1);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (!mInWhitespace) {
|
2001-01-12 10:26:39 +03:00
|
|
|
Write(kSpace);
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (!mURL.IsEmpty() && mURL.Equals(aText)) {
|
|
|
|
mURL.Truncate();
|
|
|
|
}
|
|
|
|
Write(aText);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsPlainTextSerializer::DoAddLeaf(nsAtom* aTag)
|
2012-03-08 18:42:27 +04:00
|
|
|
{
|
2015-01-18 01:31:59 +03:00
|
|
|
mPreformattedBlockBoundary = false;
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
// If we don't want any output, just return
|
|
|
|
if (!DoOutput()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLineBreakDue)
|
|
|
|
EnsureVerticalSpace(mFloatingLines);
|
|
|
|
|
|
|
|
if (MustSuppressLeaf()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTag == nsGkAtoms::br) {
|
|
|
|
// Another egregious editor workaround, see bug 38194:
|
|
|
|
// ignore the bogus br tags that the editor sticks here and there.
|
|
|
|
nsAutoString tagAttr;
|
|
|
|
if (NS_FAILED(GetAttributeValue(nsGkAtoms::type, tagAttr))
|
|
|
|
|| !tagAttr.EqualsLiteral("_moz")) {
|
|
|
|
EnsureVerticalSpace(mEmptyLines+1);
|
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::hr &&
|
2000-09-11 09:10:32 +04:00
|
|
|
(mFlags & nsIDocumentEncoder::OutputFormatted)) {
|
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
|
|
|
|
// Make a line of dashes as wide as the wrap width
|
|
|
|
// XXX honoring percentage would be nice
|
|
|
|
nsAutoString line;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t width = (mWrapColumn > 0 ? mWrapColumn : 25);
|
2000-09-11 09:10:32 +04:00
|
|
|
while (line.Length() < width) {
|
2014-01-04 19:02:17 +04:00
|
|
|
line.Append(char16_t('-'));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
Write(line);
|
|
|
|
|
|
|
|
EnsureVerticalSpace(0);
|
|
|
|
}
|
2013-07-11 19:48:43 +04:00
|
|
|
else if (mFlags & nsIDocumentEncoder::OutputNonTextContentAsPlaceholder) {
|
2016-07-20 07:07:53 +03:00
|
|
|
Write(NS_LITERAL_STRING(u"\xFFFC"));
|
2013-07-11 19:48:43 +04:00
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (aTag == nsGkAtoms::img) {
|
2004-05-06 21:11:27 +04:00
|
|
|
/* Output (in decreasing order of preference)
|
|
|
|
alt, title or nothing */
|
|
|
|
// See <http://www.w3.org/TR/REC-html40/struct/objects.html#edef-IMG>
|
|
|
|
nsAutoString imageDescription;
|
2012-03-08 18:42:27 +04:00
|
|
|
if (NS_SUCCEEDED(GetAttributeValue(nsGkAtoms::alt,
|
2004-05-06 21:11:27 +04:00
|
|
|
imageDescription))) {
|
|
|
|
// If the alt attribute has an empty value (|alt=""|), output nothing
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
else if (NS_SUCCEEDED(GetAttributeValue(nsGkAtoms::title,
|
2004-05-06 21:11:27 +04:00
|
|
|
imageDescription))
|
|
|
|
&& !imageDescription.IsEmpty()) {
|
|
|
|
imageDescription = NS_LITERAL_STRING(" [") +
|
|
|
|
imageDescription +
|
|
|
|
NS_LITERAL_STRING("] ");
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2004-05-06 21:11:27 +04:00
|
|
|
Write(imageDescription);
|
|
|
|
}
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPlainTextSerializer::EnsureVerticalSpace(int32_t noOfRows)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2001-04-10 05:11:39 +04: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.
|
2012-03-08 18:42:27 +04:00
|
|
|
if (noOfRows >= 0 && !mInIndentString.IsEmpty()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
EndLine(false);
|
|
|
|
mInWhitespace = true;
|
2001-04-10 05:11:39 +04:00
|
|
|
}
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
while(mEmptyLines < noOfRows) {
|
2011-10-17 18:59:28 +04:00
|
|
|
EndLine(false);
|
|
|
|
mInWhitespace = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mLineBreakDue = false;
|
2002-03-04 13:47:19 +03:00
|
|
|
mFloatingLines = -1;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
void
|
|
|
|
nsPlainTextSerializer::FlushLine()
|
|
|
|
{
|
2012-03-08 18:42:27 +04:00
|
|
|
if (!mCurrentLine.IsEmpty()) {
|
|
|
|
if (mAtFirstColumn) {
|
2001-01-12 10:26:39 +03:00
|
|
|
OutputQuotesAndIndent(); // XXX: Should we always do this? Bug?
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03:00
|
|
|
Output(mCurrentLine);
|
2002-01-24 05:03:19 +03:00
|
|
|
mAtFirstColumn = mAtFirstColumn && mCurrentLine.IsEmpty();
|
2000-09-11 09:10:32 +04:00
|
|
|
mCurrentLine.Truncate();
|
|
|
|
mCurrentLineWidth = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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.
|
|
|
|
*/
|
2017-07-06 15:00:35 +03:00
|
|
|
void
|
2001-01-12 10:26:39 +03:00
|
|
|
nsPlainTextSerializer::Output(nsString& aString)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2002-01-24 05:03:19 +03:00
|
|
|
if (!aString.IsEmpty()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mStartedOutput = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2006-08-07 07:27:49 +04:00
|
|
|
if (!(mFlags & nsIDocumentEncoder::OutputPersistNBSP)) {
|
|
|
|
// First, replace all nbsp characters with spaces,
|
|
|
|
// which the unicode encoder won't do for us.
|
2007-09-29 11:30:23 +04:00
|
|
|
aString.ReplaceChar(kNBSP, kSPACE);
|
2006-08-07 07:27:49 +04:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
mOutputString->Append(aString);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2014-01-04 19:02:17 +04:00
|
|
|
IsSpaceStuffable(const char16_t *s)
|
2008-01-06 06:54:38 +03:00
|
|
|
{
|
|
|
|
if (s[0] == '>' || s[0] == ' ' || s[0] == kNBSP ||
|
2018-01-09 17:49:37 +03:00
|
|
|
NS_strncmp(s, u"From ", 5) == 0)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-01-06 06:54:38 +03:00
|
|
|
else
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-01-06 06:54:38 +03:00
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
void
|
2017-07-06 15:00:35 +03:00
|
|
|
nsPlainTextSerializer::AddToLine(const char16_t * aLineFragment,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aLineFragmentLength)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t prefixwidth = (mCiteQuoteLevel > 0 ? mCiteQuoteLevel + 1:0)+mIndent;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2002-03-04 13:47:19 +03:00
|
|
|
if (mLineBreakDue)
|
|
|
|
EnsureVerticalSpace(mFloatingLines);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t linelength = mCurrentLine.Length();
|
2012-03-08 18:42:27 +04:00
|
|
|
if (0 == linelength) {
|
|
|
|
if (0 == aLineFragmentLength) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// Nothing at all. Are you kidding me?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
|
|
|
if (IsSpaceStuffable(aLineFragment)
|
2001-04-10 05:11:39 +04:00
|
|
|
&& mCiteQuoteLevel == 0 // We space-stuff quoted lines anyway
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Space stuffing a la RFC 2646 (format=flowed).
|
2014-01-04 19:02:17 +04:00
|
|
|
mCurrentLine.Append(char16_t(' '));
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (MayWrap()) {
|
2001-04-10 05:11:39 +04:00
|
|
|
mCurrentLineWidth += GetUnicharWidth(' ');
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_wrapping
|
2001-06-30 15:02:25 +04:00
|
|
|
NS_ASSERTION(GetUnicharStringWidth(mCurrentLine.get(),
|
2001-04-10 05:11:39 +04:00
|
|
|
mCurrentLine.Length()) ==
|
2012-08-22 19:56:38 +04:00
|
|
|
(int32_t)mCurrentLineWidth,
|
2001-04-10 05:11:39 +04:00
|
|
|
"mCurrentLineWidth and reality out of sync!");
|
2000-09-11 09:10:32 +04:00
|
|
|
#endif
|
2001-04-10 05:11:39 +04:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mEmptyLines=-1;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
mCurrentLine.Append(aLineFragment, aLineFragmentLength);
|
2012-03-08 18:42:27 +04:00
|
|
|
if (MayWrap()) {
|
2001-01-12 10:26:39 +03:00
|
|
|
mCurrentLineWidth += GetUnicharStringWidth(aLineFragment,
|
|
|
|
aLineFragmentLength);
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_wrapping
|
2001-06-30 15:02:25 +04:00
|
|
|
NS_ASSERTION(GetUnicharstringWidth(mCurrentLine.get(),
|
2001-01-12 10:26:39 +03:00
|
|
|
mCurrentLine.Length()) ==
|
2012-08-22 19:56:38 +04:00
|
|
|
(int32_t)mCurrentLineWidth,
|
2000-09-11 09:10:32 +04:00
|
|
|
"mCurrentLineWidth and reality out of sync!");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
linelength = mCurrentLine.Length();
|
|
|
|
|
|
|
|
// Wrap?
|
2012-03-08 18:42:27 +04:00
|
|
|
if (MayWrap())
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_wrapping
|
2001-06-30 15:02:25 +04:00
|
|
|
NS_ASSERTION(GetUnicharstringWidth(mCurrentLine.get(),
|
2000-09-11 09:10:32 +04:00
|
|
|
mCurrentLine.Length()) ==
|
2012-08-22 19:56:38 +04:00
|
|
|
(int32_t)mCurrentLineWidth,
|
2000-09-11 09:10:32 +04:00
|
|
|
"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.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bonuswidth = (mWrapColumn > 20) ? 4 : 0;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2001-01-12 10:26:39 +03:00
|
|
|
// XXX: Should calculate prefixwidth with GetUnicharStringWidth
|
2017-07-06 15:00:35 +03:00
|
|
|
while(mCurrentLineWidth+prefixwidth > mWrapColumn+bonuswidth) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// We go from the end removing one letter at a time until
|
|
|
|
// we have a reasonable width
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t goodSpace = mCurrentLine.Length();
|
|
|
|
uint32_t width = mCurrentLineWidth;
|
2000-09-11 09:10:32 +04:00
|
|
|
while(goodSpace > 0 && (width+prefixwidth > mWrapColumn)) {
|
|
|
|
goodSpace--;
|
2001-01-12 10:26:39 +03:00
|
|
|
width -= GetUnicharWidth(mCurrentLine[goodSpace]);
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
goodSpace++;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-08-22 07:00:06 +04:00
|
|
|
if (mLineBreaker) {
|
2017-07-06 15:00:35 +03:00
|
|
|
goodSpace = mLineBreaker->Prev(mCurrentLine.get(),
|
2005-08-22 07:00:06 +04:00
|
|
|
mCurrentLine.Length(), goodSpace);
|
|
|
|
if (goodSpace != NS_LINEBREAKER_NEED_MORE_TEXT &&
|
|
|
|
nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace-1))) {
|
2000-09-11 09:10:32 +04:00
|
|
|
--goodSpace; // adjust the position since line breaker returns a position next to space
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fallback if the line breaker is unavailable or failed
|
2005-08-22 07:00:06 +04:00
|
|
|
if (!mLineBreaker) {
|
2016-05-23 07:35:00 +03:00
|
|
|
if (mCurrentLine.IsEmpty() || mWrapColumn < prefixwidth) {
|
|
|
|
goodSpace = NS_LINEBREAKER_NEED_MORE_TEXT;
|
|
|
|
} else {
|
|
|
|
goodSpace = std::min(mWrapColumn - prefixwidth, mCurrentLine.Length() - 1);
|
|
|
|
while (goodSpace >= 0 &&
|
|
|
|
!nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
|
|
|
|
goodSpace--;
|
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
nsAutoString restOfLine;
|
2005-08-22 07:00:06 +04:00
|
|
|
if (goodSpace == NS_LINEBREAKER_NEED_MORE_TEXT) {
|
2016-05-23 07:35:00 +03:00
|
|
|
// If we didn't find a good place to break, accept long line and
|
2000-09-11 09:10:32 +04:00
|
|
|
// 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 14:57:30 +04:00
|
|
|
goodSpace=(prefixwidth>mWrapColumn+1)?1:mWrapColumn-prefixwidth+1;
|
2005-08-22 07:00:06 +04:00
|
|
|
if (mLineBreaker) {
|
2012-08-22 19:56:38 +04:00
|
|
|
if ((uint32_t)goodSpace < mCurrentLine.Length())
|
2017-07-06 15:00:35 +03:00
|
|
|
goodSpace = mLineBreaker->Next(mCurrentLine.get(),
|
2009-01-19 12:07:11 +03:00
|
|
|
mCurrentLine.Length(), goodSpace);
|
2005-08-22 07:00:06 +04:00
|
|
|
if (goodSpace == NS_LINEBREAKER_NEED_MORE_TEXT)
|
|
|
|
goodSpace = mCurrentLine.Length();
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
// fallback if the line breaker is unavailable or failed
|
2005-08-22 07:00:06 +04:00
|
|
|
if (!mLineBreaker) {
|
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 14:57:30 +04:00
|
|
|
goodSpace=(prefixwidth>mWrapColumn)?1:mWrapColumn-prefixwidth;
|
2000-09-11 09:10:32 +04:00
|
|
|
while (goodSpace < linelength &&
|
|
|
|
!nsCRT::IsAsciiSpace(mCurrentLine.CharAt(goodSpace))) {
|
|
|
|
goodSpace++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if ((goodSpace < linelength) && (goodSpace > 0)) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// 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);
|
|
|
|
}
|
2010-04-02 13:21:40 +04:00
|
|
|
// if breaker was U+0020, it has to consider for delsp=yes support
|
2011-09-29 10:19:26 +04:00
|
|
|
bool breakBySpace = mCurrentLine.CharAt(goodSpace) == ' ';
|
2017-07-06 15:00:35 +03:00
|
|
|
mCurrentLine.Truncate(goodSpace);
|
2011-10-17 18:59:28 +04:00
|
|
|
EndLine(true, breakBySpace);
|
2000-09-11 09:10:32 +04:00
|
|
|
mCurrentLine.Truncate();
|
|
|
|
// Space stuff new line?
|
2012-03-08 18:42:27 +04:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
|
|
|
if (!restOfLine.IsEmpty() && IsSpaceStuffable(restOfLine.get())
|
2001-04-10 05:11:39 +04:00
|
|
|
&& mCiteQuoteLevel == 0 // We space-stuff quoted lines anyway
|
|
|
|
)
|
|
|
|
{
|
2000-09-11 09:10:32 +04:00
|
|
|
// Space stuffing a la RFC 2646 (format=flowed).
|
2014-01-04 19:02:17 +04:00
|
|
|
mCurrentLine.Append(char16_t(' '));
|
2001-04-10 05:11:39 +04:00
|
|
|
//XXX doesn't seem to work correctly for ' '
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mCurrentLine.Append(restOfLine);
|
2001-06-30 15:02:25 +04:00
|
|
|
mCurrentLineWidth = GetUnicharStringWidth(mCurrentLine.get(),
|
2001-01-12 10:26:39 +03:00
|
|
|
mCurrentLine.Length());
|
2000-09-11 09:10:32 +04:00
|
|
|
linelength = mCurrentLine.Length();
|
|
|
|
mEmptyLines = -1;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
|
|
|
// Nothing to do. Hopefully we get more data later
|
|
|
|
// to use for a place to break line
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
|
|
|
// No wrapping.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsPlainTextSerializer::EndLine(bool aSoftlinebreak, bool aBreakBySpace)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t currentlinelength = mCurrentLine.Length();
|
2001-01-12 10:26:39 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aSoftlinebreak && 0 == currentlinelength) {
|
2001-01-12 10:26:39 +03:00
|
|
|
// No meaning
|
|
|
|
return;
|
|
|
|
}
|
2007-10-03 07:30:17 +04:00
|
|
|
|
|
|
|
/* In non-preformatted mode, remove spaces from the end of the line for
|
|
|
|
* format=flowed compatibility. Don't do this for these special cases:
|
|
|
|
* "-- ", the signature separator (RFC 2646) shouldn't be touched and
|
|
|
|
* "- -- ", the OpenPGP dash-escaped signature separator in inline
|
|
|
|
* signed messages according to the OpenPGP standard (RFC 2440).
|
2017-07-06 15:00:35 +03:00
|
|
|
*/
|
2012-03-08 18:42:27 +04:00
|
|
|
if (!(mFlags & nsIDocumentEncoder::OutputPreformatted) &&
|
2013-09-06 00:10:04 +04:00
|
|
|
!(mFlags & nsIDocumentEncoder::OutputDontRemoveLineEndingSpaces) &&
|
2017-07-06 15:00:35 +03:00
|
|
|
(aSoftlinebreak ||
|
2007-10-03 07:30:17 +04:00
|
|
|
!(mCurrentLine.EqualsLiteral("-- ") || mCurrentLine.EqualsLiteral("- -- ")))) {
|
|
|
|
// Remove spaces from the end of the line.
|
2001-01-12 10:26:39 +03:00
|
|
|
while(currentlinelength > 0 &&
|
2001-04-10 05:11:39 +04:00
|
|
|
mCurrentLine[currentlinelength-1] == ' ') {
|
2001-01-12 10:26:39 +03:00
|
|
|
--currentlinelength;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2001-01-12 10:26:39 +03:00
|
|
|
mCurrentLine.SetLength(currentlinelength);
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aSoftlinebreak &&
|
2001-01-12 10:26:39 +03:00
|
|
|
(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.
|
2010-04-02 13:21:40 +04:00
|
|
|
|
|
|
|
// If breaker character is ASCII space with RFC 3676 support (delsp=yes),
|
|
|
|
// add twice space.
|
2012-03-08 18:42:27 +04:00
|
|
|
if ((mFlags & nsIDocumentEncoder::OutputFormatDelSp) && aBreakBySpace)
|
2014-05-22 07:48:50 +04:00
|
|
|
mCurrentLine.AppendLiteral(" ");
|
2010-04-02 13:21:40 +04:00
|
|
|
else
|
2014-01-04 19:02:17 +04:00
|
|
|
mCurrentLine.Append(char16_t(' '));
|
2001-01-12 10:26:39 +03:00
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aSoftlinebreak) {
|
2000-09-11 09:10:32 +04:00
|
|
|
mEmptyLines=0;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
|
|
|
// Hard break
|
2012-03-08 18:42:27 +04:00
|
|
|
if (!mCurrentLine.IsEmpty() || !mInIndentString.IsEmpty()) {
|
2000-09-11 09:10:32 +04:00
|
|
|
mEmptyLines=-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mEmptyLines++;
|
|
|
|
}
|
2001-01-12 10:26:39 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (mAtFirstColumn) {
|
2001-04-10 05:11:39 +04:00
|
|
|
// 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.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool stripTrailingSpaces = mCurrentLine.IsEmpty();
|
2001-04-10 05:11:39 +04:00
|
|
|
OutputQuotesAndIndent(stripTrailingSpaces);
|
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03:00
|
|
|
mCurrentLine.Append(mLineBreak);
|
|
|
|
Output(mCurrentLine);
|
|
|
|
mCurrentLine.Truncate();
|
|
|
|
mCurrentLineWidth = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mAtFirstColumn=true;
|
|
|
|
mInWhitespace=true;
|
|
|
|
mLineBreakDue = false;
|
2002-03-04 13:47:19 +03:00
|
|
|
mFloatingLines = -1;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsPlainTextSerializer::OutputQuotesAndIndent(bool stripTrailingSpaces /* = false */)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2001-04-10 05:11:39 +04:00
|
|
|
nsAutoString stringToOutput;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
// Put the mail quote "> " chars in, if appropriate:
|
|
|
|
if (mCiteQuoteLevel > 0) {
|
|
|
|
nsAutoString quotes;
|
|
|
|
for(int i=0; i < mCiteQuoteLevel; i++) {
|
2014-01-04 19:02:17 +04:00
|
|
|
quotes.Append(char16_t('>'));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2002-01-24 05:03:19 +03:00
|
|
|
if (!mCurrentLine.IsEmpty()) {
|
2001-04-10 05:11:39 +04:00
|
|
|
/* Better don't output a space here, if the line is empty,
|
2006-08-29 00:12:37 +04:00
|
|
|
in case a receiving f=f-aware UA thinks, this were a flowed line,
|
2001-04-10 05:11:39 +04:00
|
|
|
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.) */
|
2014-01-04 19:02:17 +04:00
|
|
|
quotes.Append(char16_t(' '));
|
2001-04-10 05:11:39 +04:00
|
|
|
}
|
|
|
|
stringToOutput = quotes;
|
2011-10-17 18:59:28 +04:00
|
|
|
mAtFirstColumn = false;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
// Indent if necessary
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t indentwidth = mIndent - mInIndentString.Length();
|
2001-04-10 05:11:39 +04:00
|
|
|
if (indentwidth > 0
|
2002-01-24 05:03:19 +03:00
|
|
|
&& (!mCurrentLine.IsEmpty() || !mInIndentString.IsEmpty())
|
2001-04-10 05:11:39 +04:00
|
|
|
// Don't make empty lines look flowed
|
|
|
|
) {
|
2000-09-11 09:10:32 +04:00
|
|
|
nsAutoString spaces;
|
|
|
|
for (int i=0; i < indentwidth; ++i)
|
2014-01-04 19:02:17 +04:00
|
|
|
spaces.Append(char16_t(' '));
|
2001-04-10 05:11:39 +04:00
|
|
|
stringToOutput += spaces;
|
2011-10-17 18:59:28 +04:00
|
|
|
mAtFirstColumn = false;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (!mInIndentString.IsEmpty()) {
|
2001-04-10 05:11:39 +04:00
|
|
|
stringToOutput += mInIndentString;
|
2011-10-17 18:59:28 +04:00
|
|
|
mAtFirstColumn = false;
|
2000-09-11 09:10:32 +04:00
|
|
|
mInIndentString.Truncate();
|
|
|
|
}
|
2001-04-10 05:11:39 +04:00
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (stripTrailingSpaces) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t lineLength = stringToOutput.Length();
|
2001-04-10 05:11:39 +04:00
|
|
|
while(lineLength > 0 &&
|
|
|
|
' ' == stringToOutput[lineLength-1]) {
|
|
|
|
--lineLength;
|
|
|
|
}
|
|
|
|
stringToOutput.SetLength(lineLength);
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (!stringToOutput.IsEmpty()) {
|
2001-04-10 05:11:39 +04:00
|
|
|
Output(stringToOutput);
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
void
|
2007-10-01 07:29:16 +04:00
|
|
|
nsPlainTextSerializer::Write(const nsAString& aStr)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2007-10-01 07:29:16 +04:00
|
|
|
// XXX Copy necessary to use nsString methods and gain
|
|
|
|
// access to underlying buffer
|
|
|
|
nsAutoString str(aStr);
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_wrapping
|
2001-07-13 22:45:53 +04:00
|
|
|
printf("Write(%s): wrap col = %d\n",
|
2008-01-06 06:54:38 +03:00
|
|
|
NS_ConvertUTF16toUTF8(str).get(), mWrapColumn);
|
2000-09-11 09:10:32 +04:00
|
|
|
#endif
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bol = 0;
|
|
|
|
int32_t newline;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t totLen = str.Length();
|
2001-01-12 10:26:39 +03:00
|
|
|
|
2001-07-13 22:45:53 +04:00
|
|
|
// If the string is empty, do nothing:
|
|
|
|
if (totLen <= 0) return;
|
|
|
|
|
2007-10-01 07:29:16 +04:00
|
|
|
// For Flowed text change nbsp-ses to spaces at end of lines to allow them
|
|
|
|
// to be cut off along with usual spaces if required. (bug #125928)
|
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = totLen-1; i >= 0; i--) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c = str[i];
|
2007-10-01 07:29:16 +04:00
|
|
|
if ('\n' == c || '\r' == c || ' ' == c || '\t' == c)
|
|
|
|
continue;
|
2008-01-06 06:54:38 +03:00
|
|
|
if (kNBSP == c)
|
2007-10-01 07:29:16 +04:00
|
|
|
str.Replace(i, 1, ' ');
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-12 10:26:39 +03: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.
|
2015-03-04 08:54:40 +03:00
|
|
|
if ((mPreFormattedMail && !mWrapColumn) || (IsInPre() && !mPreFormattedMail)
|
2016-09-29 08:10:00 +03:00
|
|
|
|| (mSpanLevel > 0 && mEmptyLines >= 0 && IsQuotedLine(str))) {
|
2001-01-12 10:26:39 +03:00
|
|
|
// No intelligent wrapping.
|
2000-09-11 09:10:32 +04:00
|
|
|
|
2001-01-12 10:26:39 +03:00
|
|
|
// This mustn't be mixed with intelligent wrapping without clearing
|
|
|
|
// the mCurrentLine buffer before!!!
|
2015-03-04 08:54:40 +03:00
|
|
|
NS_ASSERTION(mCurrentLine.IsEmpty() || (IsInPre() && !mPreFormattedMail),
|
2000-09-11 09:10:32 +04:00
|
|
|
"Mixed wrapping data and nonwrapping data on the same line");
|
2002-01-24 05:03:19 +03:00
|
|
|
if (!mCurrentLine.IsEmpty()) {
|
2000-09-11 09:10:32 +04:00
|
|
|
FlushLine();
|
|
|
|
}
|
2005-06-01 23:31:47 +04:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
// Put the mail quote "> " chars in, if appropriate.
|
|
|
|
// Have to put it in before every line.
|
|
|
|
while(bol<totLen) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool outputQuotes = mAtFirstColumn;
|
2018-06-26 23:23:43 +03:00
|
|
|
bool atFirstColumn;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool outputLineBreak = false;
|
|
|
|
bool spacesOnly = true;
|
2001-01-12 10:26:39 +03:00
|
|
|
|
2002-03-24 02:54:46 +03:00
|
|
|
// Find one of '\n' or '\r' using iterators since nsAString
|
2001-01-12 10:26:39 +03:00
|
|
|
// doesn't have the old FindCharInSet function.
|
2007-10-01 07:29:16 +04:00
|
|
|
nsAString::const_iterator iter; str.BeginReading(iter);
|
|
|
|
nsAString::const_iterator done_searching; str.EndReading(done_searching);
|
2017-07-06 15:00:35 +03:00
|
|
|
iter.advance(bol);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t new_newline = bol;
|
2001-01-12 10:26:39 +03:00
|
|
|
newline = kNotFound;
|
|
|
|
while(iter != done_searching) {
|
2012-03-08 18:42:27 +04:00
|
|
|
if ('\n' == *iter || '\r' == *iter) {
|
2001-01-12 10:26:39 +03:00
|
|
|
newline = new_newline;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-08 18:42:27 +04:00
|
|
|
if (' ' != *iter)
|
2011-10-17 18:59:28 +04:00
|
|
|
spacesOnly = false;
|
2001-01-12 10:26:39 +03:00
|
|
|
++new_newline;
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done searching
|
2008-01-06 06:54:38 +03:00
|
|
|
nsAutoString stringpart;
|
2012-03-08 18:42:27 +04:00
|
|
|
if (newline == kNotFound) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// No new lines.
|
2008-01-06 06:54:38 +03:00
|
|
|
stringpart.Assign(Substring(str, bol, totLen - bol));
|
2012-03-08 18:42:27 +04:00
|
|
|
if (!stringpart.IsEmpty()) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t lastchar = stringpart[stringpart.Length()-1];
|
2012-03-08 18:42:27 +04:00
|
|
|
if ((lastchar == '\t') || (lastchar == ' ') ||
|
2000-09-11 09:10:32 +04:00
|
|
|
(lastchar == '\r') ||(lastchar == '\n')) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = false;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mEmptyLines=-1;
|
2005-06-01 23:31:47 +04:00
|
|
|
atFirstColumn = mAtFirstColumn && (totLen-bol)==0;
|
2000-09-11 09:10:32 +04:00
|
|
|
bol = totLen;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
2000-10-21 03:05:14 +04:00
|
|
|
// There is a newline
|
2008-01-06 06:54:38 +03:00
|
|
|
stringpart.Assign(Substring(str, bol, newline-bol));
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
|
|
|
outputLineBreak = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
mEmptyLines=0;
|
2011-10-17 18:59:28 +04:00
|
|
|
atFirstColumn = true;
|
2000-09-11 09:10:32 +04:00
|
|
|
bol = newline+1;
|
2012-03-08 18:42:27 +04:00
|
|
|
if ('\r' == *iter && bol < totLen && '\n' == *++iter) {
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
}
|
2005-06-01 23:31:47 +04:00
|
|
|
|
2014-05-26 22:54:53 +04:00
|
|
|
mCurrentLine.Truncate();
|
2008-01-06 06:54:38 +03:00
|
|
|
if (mFlags & nsIDocumentEncoder::OutputFormatFlowed) {
|
|
|
|
if ((outputLineBreak || !spacesOnly) && // bugs 261467,125928
|
2016-09-29 08:10:00 +03:00
|
|
|
!IsQuotedLine(stringpart) &&
|
2008-01-06 06:54:38 +03:00
|
|
|
!stringpart.EqualsLiteral("-- ") &&
|
|
|
|
!stringpart.EqualsLiteral("- -- "))
|
2011-10-17 18:59:28 +04:00
|
|
|
stringpart.Trim(" ", false, true, true);
|
2016-09-29 08:10:00 +03:00
|
|
|
if (IsSpaceStuffable(stringpart.get()) && !IsQuotedLine(stringpart))
|
2014-01-04 19:02:17 +04:00
|
|
|
mCurrentLine.Append(char16_t(' '));
|
2008-01-06 06:54:38 +03:00
|
|
|
}
|
|
|
|
mCurrentLine.Append(stringpart);
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (outputQuotes) {
|
2005-06-01 23:31:47 +04:00
|
|
|
// Note: this call messes with mAtFirstColumn
|
|
|
|
OutputQuotesAndIndent();
|
|
|
|
}
|
|
|
|
|
|
|
|
Output(mCurrentLine);
|
|
|
|
if (outputLineBreak) {
|
|
|
|
Output(mLineBreak);
|
|
|
|
}
|
|
|
|
mAtFirstColumn = atFirstColumn;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2005-06-01 23:31:47 +04:00
|
|
|
// Reset mCurrentLine.
|
|
|
|
mCurrentLine.Truncate();
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_wrapping
|
2001-01-12 10:26:39 +03:00
|
|
|
printf("No wrapping: newline is %d, totLen is %d\n",
|
|
|
|
newline, totLen);
|
2000-09-11 09:10:32 +04:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intelligent handling of text
|
|
|
|
// If needed, strip out all "end of lines"
|
|
|
|
// and multiple whitespace between words
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nextpos;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t * offsetIntoBuffer = nullptr;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
while (bol < totLen) { // Loop over lines
|
|
|
|
// Find a place where we may have to do whitespace compression
|
2000-12-13 00:58:13 +03:00
|
|
|
nextpos = str.FindCharInSet(" \t\n\r", bol);
|
2000-09-11 09:10:32 +04:00
|
|
|
#ifdef DEBUG_wrapping
|
2000-12-13 00:58:13 +03:00
|
|
|
nsAutoString remaining;
|
|
|
|
str.Right(remaining, totLen - bol);
|
2001-09-29 12:28:41 +04:00
|
|
|
foo = ToNewCString(remaining);
|
2000-10-29 02:17:53 +04:00
|
|
|
// printf("Next line: bol = %d, newlinepos = %d, totLen = %d, string = '%s'\n",
|
|
|
|
// bol, nextpos, totLen, foo);
|
2015-03-27 03:01:12 +03:00
|
|
|
free(foo);
|
2000-09-11 09:10:32 +04:00
|
|
|
#endif
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (nextpos == kNotFound) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// The rest of the string
|
2001-06-30 15:02:25 +04:00
|
|
|
offsetIntoBuffer = str.get() + bol;
|
2001-01-12 10:26:39 +03:00
|
|
|
AddToLine(offsetIntoBuffer, totLen-bol);
|
2000-09-11 09:10:32 +04:00
|
|
|
bol=totLen;
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace=false;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
|
|
|
// There's still whitespace left in the string
|
2002-07-02 00:43:16 +04:00
|
|
|
if (nextpos != 0 && (nextpos + 1) < totLen) {
|
|
|
|
offsetIntoBuffer = str.get() + nextpos;
|
|
|
|
// skip '\n' if it is between CJ chars
|
|
|
|
if (offsetIntoBuffer[0] == '\n' && IS_CJ_CHAR(offsetIntoBuffer[-1]) && IS_CJ_CHAR(offsetIntoBuffer[1])) {
|
|
|
|
offsetIntoBuffer = str.get() + bol;
|
|
|
|
AddToLine(offsetIntoBuffer, nextpos-bol);
|
|
|
|
bol = nextpos + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
// If we're already in whitespace and not preformatted, just skip it:
|
2015-03-04 08:54:40 +03:00
|
|
|
if (mInWhitespace && (nextpos == bol) && !mPreFormattedMail &&
|
2000-09-11 09:10:32 +04:00
|
|
|
!(mFlags & nsIDocumentEncoder::OutputPreformatted)) {
|
|
|
|
// Skip whitespace
|
|
|
|
bol++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:42:27 +04:00
|
|
|
if (nextpos == bol) {
|
2000-09-11 09:10:32 +04:00
|
|
|
// Note that we are in whitespace.
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
2001-06-30 15:02:25 +04:00
|
|
|
offsetIntoBuffer = str.get() + nextpos;
|
2001-01-12 10:26:39 +03:00
|
|
|
AddToLine(offsetIntoBuffer, 1);
|
2000-09-11 09:10:32 +04:00
|
|
|
bol++;
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mInWhitespace = true;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2001-06-30 15:02:25 +04:00
|
|
|
offsetIntoBuffer = str.get() + bol;
|
2015-03-04 08:54:40 +03:00
|
|
|
if (mPreFormattedMail || (mFlags & nsIDocumentEncoder::OutputPreformatted)) {
|
2001-01-12 10:26:39 +03:00
|
|
|
// Preserve the real whitespace character
|
|
|
|
nextpos++;
|
|
|
|
AddToLine(offsetIntoBuffer, nextpos-bol);
|
|
|
|
bol = nextpos;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2000-09-11 09:10:32 +04:00
|
|
|
else {
|
2001-01-12 10:26:39 +03:00
|
|
|
// Replace the whitespace with a space
|
|
|
|
AddToLine(offsetIntoBuffer, nextpos-bol);
|
2001-06-30 15:02:25 +04:00
|
|
|
AddToLine(kSpace.get(),1);
|
2001-01-12 10:26:39 +03:00
|
|
|
bol = nextpos + 1; // Let's eat the whitespace
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
}
|
2000-10-29 02:17:53 +04:00
|
|
|
} // Continue looping over the string
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-12 10:26:39 +03: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 09:10:32 +04:00
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsPlainTextSerializer::GetAttributeValue(nsAtom* aName,
|
2000-09-11 09:10:32 +04:00
|
|
|
nsString& aValueRet)
|
|
|
|
{
|
2011-07-03 13:53:40 +04:00
|
|
|
if (mElement) {
|
|
|
|
if (mElement->GetAttr(kNameSpaceID_None, aName, aValueRet)) {
|
2000-09-11 09:10:32 +04:00
|
|
|
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.
|
|
|
|
*/
|
2017-07-06 15:00:35 +03:00
|
|
|
bool
|
2012-03-08 18:42:27 +04:00
|
|
|
nsPlainTextSerializer::IsCurrentNodeConverted()
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
nsAutoString value;
|
2012-03-08 18:42:27 +04:00
|
|
|
nsresult rv = GetAttributeValue(nsGkAtoms::_class, value);
|
2000-09-11 09:10:32 +04:00
|
|
|
return (NS_SUCCEEDED(rv) &&
|
2001-07-17 04:16:50 +04:00
|
|
|
(value.EqualsIgnoreCase("moz-txt", 7) ||
|
|
|
|
value.EqualsIgnoreCase("\"moz-txt", 8)));
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-19 04:20:56 +03:00
|
|
|
// static
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom*
|
2003-11-19 04:20:56 +03:00
|
|
|
nsPlainTextSerializer::GetIdForContent(nsIContent* aContent)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2015-03-03 14:08:59 +03:00
|
|
|
if (!aContent->IsHTMLElement()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* localName = aContent->NodeInfo()->NameAtom();
|
2018-03-07 07:17:34 +03:00
|
|
|
return localName->IsStatic() ? localName : nullptr;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2000-11-04 23:51:33 +03:00
|
|
|
nsPlainTextSerializer::IsInPre()
|
|
|
|
{
|
2014-12-19 20:45:50 +03:00
|
|
|
return !mPreformatStack.empty() && mPreformatStack.top();
|
|
|
|
}
|
2015-01-13 21:50:12 +03:00
|
|
|
|
2014-12-19 20:45:50 +03:00
|
|
|
bool
|
|
|
|
nsPlainTextSerializer::IsElementPreformatted(Element* aElement)
|
|
|
|
{
|
2018-03-22 16:49:21 +03:00
|
|
|
RefPtr<ComputedStyle> computedStyle =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyleNoFlush(aElement, nullptr);
|
2018-03-22 16:49:21 +03:00
|
|
|
if (computedStyle) {
|
|
|
|
const nsStyleText* textStyle = computedStyle->StyleText();
|
2014-12-19 20:45:50 +03:00
|
|
|
return textStyle->WhiteSpaceOrNewlineIsSignificant();
|
|
|
|
}
|
2015-01-24 02:07:03 +03:00
|
|
|
// Fall back to looking at the tag, in case there is no style information.
|
|
|
|
return GetIdForContent(aElement) == nsGkAtoms::pre;
|
2000-11-04 23:51:33 +03:00
|
|
|
}
|
|
|
|
|
2015-01-18 01:30:21 +03:00
|
|
|
bool
|
|
|
|
nsPlainTextSerializer::IsElementBlock(Element* aElement)
|
|
|
|
{
|
2018-03-22 16:49:21 +03:00
|
|
|
RefPtr<ComputedStyle> computedStyle =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyleNoFlush(aElement, nullptr);
|
2018-03-22 16:49:21 +03:00
|
|
|
if (computedStyle) {
|
|
|
|
const nsStyleDisplay* displayStyle = computedStyle->StyleDisplay();
|
2015-01-18 01:30:21 +03:00
|
|
|
return displayStyle->IsBlockOutsideStyle();
|
|
|
|
}
|
|
|
|
// Fall back to looking at the tag, in case there is no style information.
|
2015-03-03 14:09:00 +03:00
|
|
|
return nsContentUtils::IsHTMLBlock(aElement);
|
2015-01-18 01:30:21 +03:00
|
|
|
}
|
|
|
|
|
2002-09-15 17:14:14 +04:00
|
|
|
/**
|
2006-08-29 00:12:37 +04:00
|
|
|
* This method is required only to identify LI's inside OL.
|
2002-09-15 17:14:14 +04:00
|
|
|
* Returns TRUE if we are inside an OL tag and FALSE otherwise.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2002-09-15 17:14:14 +04:00
|
|
|
nsPlainTextSerializer::IsInOL()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i = mTagStackIndex;
|
2002-09-15 17:14:14 +04:00
|
|
|
while(--i >= 0) {
|
2012-03-08 18:42:27 +04:00
|
|
|
if (mTagStack[i] == nsGkAtoms::ol)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2012-03-08 18:42:27 +04:00
|
|
|
if (mTagStack[i] == nsGkAtoms::ul) {
|
2002-09-15 17:14:14 +04:00
|
|
|
// If a UL is reached first, LI belongs the UL nested in OL.
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-09-15 17:14:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We may reach here for orphan LI's.
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-09-15 17:14:14 +04:00
|
|
|
}
|
|
|
|
|
2000-09-11 09:10:32 +04:00
|
|
|
/*
|
|
|
|
@return 0 = no header, 1 = h1, ..., 6 = h6
|
|
|
|
*/
|
2017-10-03 01:05:19 +03:00
|
|
|
int32_t HeaderLevel(nsAtom* aTag)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2012-03-08 18:42:27 +04:00
|
|
|
if (aTag == nsGkAtoms::h1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (aTag == nsGkAtoms::h2) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (aTag == nsGkAtoms::h3) {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (aTag == nsGkAtoms::h4) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if (aTag == nsGkAtoms::h5) {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
if (aTag == nsGkAtoms::h6) {
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
return 0;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-01-12 10:26:39 +03:00
|
|
|
* This is an implementation of GetUnicharWidth() and
|
|
|
|
* GetUnicharStringWidth() as defined in
|
2000-09-11 09:10:32 +04: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.
|
|
|
|
*/
|
|
|
|
|
2014-09-17 17:46:24 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct interval
|
|
|
|
{
|
|
|
|
uint16_t first;
|
|
|
|
uint16_t last;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CombiningComparator
|
|
|
|
{
|
|
|
|
const char16_t mUcs;
|
2014-09-24 17:16:53 +04:00
|
|
|
explicit CombiningComparator(char16_t aUcs) : mUcs(aUcs) {}
|
2014-09-17 17:46:24 +04:00
|
|
|
int operator()(const interval& combining) const {
|
|
|
|
if (mUcs > combining.last)
|
|
|
|
return 1;
|
|
|
|
if (mUcs < combining.first)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
MOZ_ASSERT(combining.first <= mUcs);
|
|
|
|
MOZ_ASSERT(mUcs <= combining.last);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
int32_t GetUnicharWidth(char16_t ucs)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
|
|
|
/* sorted list of non-overlapping intervals of non-spacing characters */
|
2014-09-17 17:46:24 +04:00
|
|
|
static const interval combining[] = {
|
2000-09-11 09:10:32 +04:00
|
|
|
{ 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 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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 */
|
2014-09-17 17:46:24 +04:00
|
|
|
size_t idx;
|
|
|
|
if (BinarySearchIf(combining, 0, ArrayLength(combining),
|
|
|
|
CombiningComparator(ucs), &idx)) {
|
|
|
|
return 0;
|
2000-09-11 09:10:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
int32_t GetUnicharStringWidth(const char16_t* pwcs, int32_t n)
|
2000-09-11 09:10:32 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t w, width = 0;
|
2000-09-11 09:10:32 +04:00
|
|
|
|
|
|
|
for (;*pwcs && n-- > 0; pwcs++)
|
2001-01-12 10:26:39 +03:00
|
|
|
if ((w = GetUnicharWidth(*pwcs)) < 0)
|
2002-06-20 15:45:09 +04:00
|
|
|
++width; // Taking 1 as the width of non-printable character, for bug# 94475.
|
2000-09-11 09:10:32 +04:00
|
|
|
else
|
|
|
|
width += w;
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|