2015-05-03 19:32:37 +00: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 11:12:37 +00: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 05:10:32 +00:00
|
|
|
|
2006-03-31 08:00:42 +00: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 05:10:32 +00:00
|
|
|
#ifndef nsPlainTextSerializer_h__
|
|
|
|
#define nsPlainTextSerializer_h__
|
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2000-09-11 05:10:32 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAtom.h"
|
2013-03-22 00:05:20 +00:00
|
|
|
#include "nsIContentSerializer.h"
|
2001-01-12 07:26:39 +00:00
|
|
|
#include "nsIDocumentEncoder.h"
|
2013-03-22 00:05:20 +00:00
|
|
|
#include "nsILineBreaker.h"
|
|
|
|
#include "nsString.h"
|
2009-03-20 08:15:35 +00:00
|
|
|
#include "nsTArray.h"
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2014-12-19 17:45:50 +00:00
|
|
|
#include <stack>
|
|
|
|
|
2013-03-22 00:05:20 +00:00
|
|
|
class nsIContent;
|
|
|
|
|
2010-10-22 21:25:22 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class Element;
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class nsPlainTextSerializer final : public nsIContentSerializer
|
2000-09-11 05:10:32 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsPlainTextSerializer();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIContentSerializer
|
2015-01-13 18:50:12 +00:00
|
|
|
NS_IMETHOD Init(uint32_t flags, uint32_t aWrapColumn,
|
|
|
|
const char* aCharSet, bool aIsCopying,
|
2015-03-21 16:28:04 +00:00
|
|
|
bool aIsWholeDocument) override;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHOD AppendText(nsIContent* aText, int32_t aStartOffset,
|
2015-03-21 16:28:04 +00:00
|
|
|
int32_t aEndOffset, nsAString& aStr) override;
|
2010-05-04 13:19:54 +00:00
|
|
|
NS_IMETHOD AppendCDATASection(nsIContent* aCDATASection,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aStartOffset, int32_t aEndOffset,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAString& aStr) override;
|
2010-05-04 13:19:54 +00:00
|
|
|
NS_IMETHOD AppendProcessingInstruction(nsIContent* aPI,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aStartOffset,
|
|
|
|
int32_t aEndOffset,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAString& aStr) override { return NS_OK; }
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHOD AppendComment(nsIContent* aComment, int32_t aStartOffset,
|
2015-03-21 16:28:04 +00:00
|
|
|
int32_t aEndOffset, nsAString& aStr) override { return NS_OK; }
|
2010-05-04 13:19:54 +00:00
|
|
|
NS_IMETHOD AppendDoctype(nsIContent *aDoctype,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAString& aStr) override { return NS_OK; }
|
2010-10-22 21:25:22 +00:00
|
|
|
NS_IMETHOD AppendElementStart(mozilla::dom::Element* aElement,
|
|
|
|
mozilla::dom::Element* aOriginalElement,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAString& aStr) override;
|
2010-10-22 21:25:22 +00:00
|
|
|
NS_IMETHOD AppendElementEnd(mozilla::dom::Element* aElement,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAString& aStr) override;
|
|
|
|
NS_IMETHOD Flush(nsAString& aStr) override;
|
2002-08-13 18:41:16 +00:00
|
|
|
|
2010-05-04 13:19:54 +00:00
|
|
|
NS_IMETHOD AppendDocumentStart(nsIDocument *aDocument,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAString& aStr) override;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2015-01-17 19:16:11 +00:00
|
|
|
private:
|
|
|
|
~nsPlainTextSerializer();
|
2014-06-25 02:09:15 +00:00
|
|
|
|
2012-03-08 14:42:27 +00:00
|
|
|
nsresult GetAttributeValue(nsIAtom* aName, nsString& aValueRet);
|
2014-01-04 15:02:17 +00:00
|
|
|
void AddToLine(const char16_t* aStringToAdd, int32_t aLength);
|
2011-09-29 06:19:26 +00:00
|
|
|
void EndLine(bool softlinebreak, bool aBreakBySpace = false);
|
2012-08-22 15:56:38 +00:00
|
|
|
void EnsureVerticalSpace(int32_t noOfRows);
|
2000-09-11 05:10:32 +00:00
|
|
|
void FlushLine();
|
2011-09-29 06:19:26 +00:00
|
|
|
void OutputQuotesAndIndent(bool stripTrailingSpaces=false);
|
2001-01-12 07:26:39 +00:00
|
|
|
void Output(nsString& aString);
|
2002-03-23 23:54:46 +00:00
|
|
|
void Write(const nsAString& aString);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsInPre();
|
|
|
|
bool IsInOL();
|
2012-03-08 14:42:27 +00:00
|
|
|
bool IsCurrentNodeConverted();
|
|
|
|
bool MustSuppressLeaf();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the local name of the element as an atom if the element is an
|
2012-07-30 14:20:58 +00:00
|
|
|
* HTML element and the atom is a static atom. Otherwise, nullptr is returned.
|
2012-03-08 14:42:27 +00:00
|
|
|
*/
|
|
|
|
static nsIAtom* GetIdForContent(nsIContent* aContent);
|
|
|
|
nsresult DoOpenContainer(nsIAtom* aTag);
|
|
|
|
nsresult DoCloseContainer(nsIAtom* aTag);
|
|
|
|
nsresult DoAddLeaf(nsIAtom* aTag);
|
|
|
|
void DoAddText(bool aIsWhitespace, const nsAString& aText);
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2001-01-12 07:26:39 +00:00
|
|
|
// Inlined functions
|
2011-09-29 06:19:26 +00:00
|
|
|
inline bool MayWrap()
|
2001-01-12 07:26:39 +00:00
|
|
|
{
|
|
|
|
return mWrapColumn &&
|
|
|
|
((mFlags & nsIDocumentEncoder::OutputFormatted) ||
|
|
|
|
(mFlags & nsIDocumentEncoder::OutputWrap));
|
|
|
|
}
|
2015-11-27 14:27:00 +00:00
|
|
|
inline bool MayBreakLines()
|
|
|
|
{
|
|
|
|
return !(mFlags & nsIDocumentEncoder::OutputDisallowLineBreaking);
|
|
|
|
}
|
2001-01-12 07:26:39 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
inline bool DoOutput()
|
2001-01-12 07:26:39 +00:00
|
|
|
{
|
2008-02-19 13:17:29 +00:00
|
|
|
return mHeadLevel == 0;
|
2001-01-12 07:26:39 +00:00
|
|
|
}
|
2002-12-19 06:22:55 +00:00
|
|
|
|
|
|
|
// Stack handling functions
|
2011-09-29 06:19:26 +00:00
|
|
|
bool GetLastBool(const nsTArray<bool>& aStack);
|
|
|
|
void SetLastBool(nsTArray<bool>& aStack, bool aValue);
|
|
|
|
void PushBool(nsTArray<bool>& aStack, bool aValue);
|
|
|
|
bool PopBool(nsTArray<bool>& aStack);
|
2013-08-06 14:40:38 +00:00
|
|
|
|
|
|
|
bool ShouldReplaceContainerWithPlaceholder(nsIAtom* aTag);
|
2015-03-10 06:11:55 +00:00
|
|
|
bool IsIgnorableRubyAnnotation(nsIAtom* aTag);
|
2013-08-06 14:40:38 +00:00
|
|
|
|
2014-12-19 17:45:50 +00:00
|
|
|
bool IsElementPreformatted(mozilla::dom::Element* aElement);
|
2015-01-17 22:30:21 +00:00
|
|
|
bool IsElementBlock(mozilla::dom::Element* aElement);
|
2014-12-19 17:45:50 +00:00
|
|
|
|
2015-01-17 19:16:11 +00:00
|
|
|
private:
|
2000-09-11 05:10:32 +00:00
|
|
|
nsString mCurrentLine;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mHeadLevel;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mAtFirstColumn;
|
2002-04-16 23:11:33 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mStructs; // Output structs (pref)
|
2001-12-14 23:10:42 +00:00
|
|
|
|
2005-06-01 19:31:47 +00:00
|
|
|
// If we've just written out a cite blockquote, we need to remember it
|
|
|
|
// so we don't duplicate spaces before a <pre wrap> (which mail uses to quote
|
|
|
|
// old messages).
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mHasWrittenCiteBlockquote;
|
2005-06-01 19:31:47 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mIndent;
|
2000-09-11 05:10:32 +00:00
|
|
|
// mInIndentString keeps a header that has to be written in the indent.
|
|
|
|
// That could be, for instance, the bullet in a bulleted list.
|
|
|
|
nsString mInIndentString;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mCiteQuoteLevel;
|
|
|
|
int32_t mFlags;
|
|
|
|
int32_t mFloatingLines; // To store the number of lazy line breaks
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
// The wrap column is how many standard sized chars (western languages)
|
|
|
|
// should be allowed on a line. There could be less chars if the chars
|
|
|
|
// are wider than latin chars of more if the chars are more narrow.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mWrapColumn;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
|
|
|
// The width of the line as it will appear on the screen (approx.)
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mCurrentLineWidth;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2001-07-13 18:45:53 +00:00
|
|
|
// Treat quoted text as though it's preformatted -- don't wrap it.
|
|
|
|
// Having it on a pref is a temporary measure, See bug 69638.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mSpanLevel;
|
2001-07-13 18:45:53 +00:00
|
|
|
|
2001-12-14 23:10:42 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mEmptyLines; // Will be the number of empty lines before
|
2000-09-11 05:10:32 +00:00
|
|
|
// the current. 0 if we are starting a new
|
|
|
|
// line and -1 if we are in a line.
|
2001-12-14 23:10:42 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mInWhitespace;
|
2015-03-04 05:54:40 +00:00
|
|
|
bool mPreFormattedMail; // we're dealing with special DOM
|
|
|
|
// used by Thunderbird code.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mStartedOutput; // we've produced at least a character
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2002-03-04 10:47:19 +00:00
|
|
|
// While handling a new tag, this variable should remind if any line break
|
|
|
|
// is due because of a closing tag. Setting it to "TRUE" while closing the tags.
|
|
|
|
// Hence opening tags are guaranteed to start with appropriate line breaks.
|
2015-01-17 22:31:59 +00:00
|
|
|
bool mLineBreakDue;
|
|
|
|
|
|
|
|
bool mPreformattedBlockBoundary;
|
2002-03-04 10:47:19 +00:00
|
|
|
|
2015-03-10 06:11:55 +00:00
|
|
|
// Whether the output should include ruby annotations.
|
|
|
|
bool mWithRubyAnnotation;
|
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
nsString mURL;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mHeaderStrategy; /* Header strategy (pref)
|
2000-09-11 05:10:32 +00:00
|
|
|
0 = no indention
|
|
|
|
1 = indention, increased with
|
|
|
|
header level (default)
|
|
|
|
2 = numbering and slight indention */
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mHeaderCounter[7]; /* For header-numbering:
|
2000-09-11 05:10:32 +00:00
|
|
|
Number of previous headers of
|
|
|
|
the same depth and in the same
|
|
|
|
section.
|
|
|
|
mHeaderCounter[1] for <h1> etc. */
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<mozilla::dom::Element> mElement;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2002-12-19 06:22:55 +00:00
|
|
|
// For handling table rows
|
2016-02-02 15:36:30 +00:00
|
|
|
AutoTArray<bool, 8> mHasWrittenCellsForRow;
|
2002-12-19 06:22:55 +00:00
|
|
|
|
2002-12-14 06:16:56 +00:00
|
|
|
// Values gotten in OpenContainer that is (also) needed in CloseContainer
|
2016-02-02 15:36:30 +00:00
|
|
|
AutoTArray<bool, 8> mIsInCiteBlockquote;
|
2002-12-14 06:16:56 +00:00
|
|
|
|
|
|
|
// The output data
|
2002-03-23 23:54:46 +00:00
|
|
|
nsAString* mOutputString;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2012-03-08 14:42:27 +00:00
|
|
|
// The tag stack: the stack of tags we're operating on, so we can nest.
|
|
|
|
// The stack only ever points to static atoms, so they don't need to be
|
|
|
|
// refcounted.
|
|
|
|
nsIAtom** mTagStack;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mTagStackIndex;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2014-12-19 17:45:50 +00:00
|
|
|
// The stack indicating whether the elements we've been operating on are
|
|
|
|
// CSS preformatted elements, so that we can tell if the text inside them
|
|
|
|
// should be formatted.
|
|
|
|
std::stack<bool> mPreformatStack;
|
|
|
|
|
2001-11-30 09:19:33 +00:00
|
|
|
// Content in the stack above this index should be ignored:
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mIgnoreAboveIndex;
|
2001-11-30 09:19:33 +00:00
|
|
|
|
2010-09-28 22:37:30 +00:00
|
|
|
// The stack for ordered lists
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t *mOLStack;
|
|
|
|
uint32_t mOLStackIndex;
|
2000-09-11 05:10:32 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mULCount;
|
2001-04-10 01:11:39 +00:00
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
nsString mLineBreak;
|
|
|
|
nsCOMPtr<nsILineBreaker> mLineBreaker;
|
2001-01-12 07:26:39 +00:00
|
|
|
|
|
|
|
// Conveniance constant. It would be nice to have it as a const static
|
|
|
|
// variable, but that causes issues with OpenBSD and module unloading.
|
|
|
|
const nsString kSpace;
|
2013-08-06 14:40:38 +00:00
|
|
|
|
|
|
|
// If nsIDocumentEncoder::OutputNonTextContentAsPlaceholder is set, the child
|
|
|
|
// nodes of specific nodes - <iframe>, <canvas>, etc. should be ignored.
|
|
|
|
// mIgnoredChildNodeLevel is used to tell if current node is an ignorable
|
|
|
|
// child node. The initial value of mIgnoredChildNodeLevel is 0. When
|
|
|
|
// serializer enters those specific nodes, mIgnoredChildNodeLevel increases
|
|
|
|
// and is greater than 0. Otherwise when serializer leaves those nodes,
|
|
|
|
// mIgnoredChildNodeLevel decreases.
|
|
|
|
uint32_t mIgnoredChildNodeLevel;
|
2000-09-11 05:10:32 +00:00
|
|
|
};
|
|
|
|
|
2003-03-05 15:08:41 +00: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 10:57:30 +00:00
|
|
|
|
2000-09-11 05:10:32 +00:00
|
|
|
#endif
|