Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef ____nstxttohtmlconv___h___
7 #define ____nstxttohtmlconv___h___
9 #include "nsITXTToHTMLConv.h"
10 #include "nsCOMPtr.h"
11 #include "nsTArray.h"
12 #include "nsString.h"
14 #define NS_NSTXTTOHTMLCONVERTER_CID \
15 { /* 9ef9fa14-1dd1-11b2-9d65-d72d6d1f025e */ \
16 0x9ef9fa14, \
17 0x1dd1, \
18 0x11b2, \
19 {0x9d, 0x65, 0xd7, 0x2d, 0x6d, 0x1f, 0x02, 0x5e} \
20 }
22 // Internal representation of a "token"
23 typedef struct convToken {
24 nsString token; // the actual string (i.e. "http://")
25 nsString modText; // replacement text or href prepend text.
26 bool prepend; // flag indicating how the modText should be used.
27 } convToken;
29 template<class T> class nsAutoPtr;
31 /**
32 * Convert plain text to HTML.
33 *
34 * OVERVIEW OF HOW THIS CLASS WORKS:
35 *
36 * This class stores an array of tokens that should be replaced by something,
37 * or something that should be prepended.
38 * The "token" member of convToken is the text to search for. This is a
39 * substring of the desired token. Tokens are delimited by TOKEN_DELIMITERS.
40 * That entire token will be replaced by modText (if prepend is false); or it
41 * will be linkified and modText will be prepended to the token if prepend is
42 * true.
43 *
44 * Note that all of the text will be in a preformatted block, so there is no
45 * need to emit line-end tags, or set the font face to monospace.
46 *
47 * This works as a stream converter, so data will arrive by
48 * OnStartRequest/OnDataAvailable/OnStopRequest calls.
49 *
50 * OStopR will possibly process a remaining token.
51 *
52 * If the data of one pass contains a part of a token, that part will be stored
53 * in mBuffer. The rest of the data will be sent to the next listener.
54 *
55 * XXX this seems suboptimal. this means that this design will only work for
56 * links. and it is impossible to append anything to the token. this means that,
57 * for example, making *foo* bold is not possible.
58 */
59 class nsTXTToHTMLConv : public nsITXTToHTMLConv {
60 public:
61 NS_DECL_ISUPPORTS
62 NS_DECL_NSISTREAMCONVERTER
63 NS_DECL_NSITXTTOHTMLCONV
64 NS_DECL_NSIREQUESTOBSERVER
65 NS_DECL_NSISTREAMLISTENER
67 nsTXTToHTMLConv();
68 virtual ~nsTXTToHTMLConv();
69 nsresult Init();
71 // For factory creation.
72 static NS_METHOD
73 Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
74 nsresult rv;
75 if (aOuter)
76 return NS_ERROR_NO_AGGREGATION;
78 nsTXTToHTMLConv* _s = new nsTXTToHTMLConv();
79 if (_s == nullptr)
80 return NS_ERROR_OUT_OF_MEMORY;
81 NS_ADDREF(_s);
82 rv = _s->Init();
83 if (NS_FAILED(rv)) {
84 delete _s;
85 return rv;
86 }
87 rv = _s->QueryInterface(aIID, aResult);
88 NS_RELEASE(_s);
89 return rv;
90 }
93 protected:
94 // return the token and it's location in the underlying buffer.
95 int32_t FindToken(int32_t cursor, convToken* *_retval);
97 // return the cursor location after munging HTML into the
98 // underlying buffer, according to mToken
99 int32_t CatHTML(int32_t front, int32_t back);
101 nsCOMPtr<nsIStreamListener> mListener; // final listener (consumer)
102 nsString mBuffer; // any carry over data
103 nsTArray<nsAutoPtr<convToken> > mTokens; // list of tokens to search for
104 convToken *mToken; // current token (if any)
105 nsString mPageTitle; // Page title
106 bool mPreFormatHTML; // Whether to use <pre> tags
107 };
109 #endif // ____nstxttohtmlconv___h___