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