summaryrefslogtreecommitdiffstats
path: root/include/svtools/parhtml.hxx
blob: 1c9392d2d0b3e75a5b8469c999ce74a7f553970b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_SVTOOLS_PARHTML_HXX
#define INCLUDED_SVTOOLS_PARHTML_HXX

#include <svtools/svtdllapi.h>
#include <svtools/svparser.hxx>
#include <svtools/htmltokn.h>

#include <vector>

namespace com :: sun :: star :: uno { template <class interface_type> class Reference; }

namespace com::sun::star {
    namespace document {
        class XDocumentProperties;
    }
}

class Color;
enum class HtmlOptionId;

#define HTMLFONTSZ1_DFLT 7
#define HTMLFONTSZ2_DFLT 10
#define HTMLFONTSZ3_DFLT 12
#define HTMLFONTSZ4_DFLT 14
#define HTMLFONTSZ5_DFLT 18
#define HTMLFONTSZ6_DFLT 24
#define HTMLFONTSZ7_DFLT 36

enum class HTMLTableFrame { Void, Above, Below, HSides, LHS, RHS, VSides, Box };

enum class HTMLTableRules { NONE, Groups, Rows, Cols, All };

enum class HTMLInputType
{
    Text =      1,
    Password,
    Checkbox,
    Radio,
    Range,
    Scribble,
    File,
    Hidden,
    Submit,
    Image,
    Reset,
    Button
};

enum class HTMLScriptLanguage
{
    StarBasic,
    JavaScript,
    Unknown
};

template<typename EnumT>
struct HTMLOptionEnum
{
    const char *pName;  // value of an HTML option
    EnumT       nValue; // and corresponding value of an enum
};

/** Representation of an HTML option (=attribute in a start tag).
 * The values of the options are always stored as strings.
 * The methods GetNumber,... may only be called if the option
 * is actually numerical,...
 */
class SVT_DLLPUBLIC HTMLOption
{
    OUString aValue;          // value of the option (always as string)
    OUString aToken;          // name of the option as string
    HtmlOptionId nToken;        // and respective token

public:

    HTMLOption( HtmlOptionId nTyp, const OUString& rToken, const OUString& rValue );

    // name of the option...
    HtmlOptionId GetToken() const { return nToken; }  // ... as enum
    const OUString& GetTokenString() const { return aToken; } // ... as string

    // value of the option ...
    const OUString& GetString() const { return aValue; }  // ... as string

    sal_uInt32 GetNumber() const;                           // ... as number
    sal_Int32 GetSNumber() const;                           // ... as number
    void GetNumbers( std::vector<sal_uInt32> &rNumbers ) const; // ... as numbers
    void GetColor( Color& ) const;                      // ... as color

    template<typename EnumT>
    EnumT GetEnum( const HTMLOptionEnum<EnumT> *pOptEnums,
                        EnumT nDflt = static_cast<EnumT>(0) ) const
    {
        while( pOptEnums->pName )
        {
            if( aValue.equalsIgnoreAsciiCaseAscii( pOptEnums->pName ) )
                return pOptEnums->nValue;
            pOptEnums++;
        }
        return nDflt;
    }

    template<typename EnumT>
    bool GetEnum( EnumT &rEnum, const HTMLOptionEnum<EnumT> *pOptEnums ) const
    {
        while( pOptEnums->pName )
        {
            if( aValue.equalsIgnoreAsciiCaseAscii( pOptEnums->pName ) )
            {
                rEnum = pOptEnums->nValue;
                return true;
            }
            pOptEnums++;
        }
        return false;
    }

    // ... and as a few special enums
    HTMLInputType GetInputType() const;                 // <INPUT TYPE=...>
    HTMLTableFrame GetTableFrame() const;               // <TABLE FRAME=...>
    HTMLTableRules GetTableRules() const;               // <TABLE RULES=...>
    //SvxAdjust GetAdjust() const;                      // <P,TH,TD ALIGN=>
};

typedef ::std::vector<HTMLOption> HTMLOptions;

class SVT_DLLPUBLIC HTMLParser : public SvParser<HtmlTokenId>
{
private:
    mutable HTMLOptions maOptions; // options of the start tag

    bool bNewDoc        : 1;        // read new Doc?
    bool bIsInHeader    : 1;        // scan header section
    bool bReadListing   : 1;        // read listings
    bool bReadXMP       : 1;        // read XMP
    bool bReadPRE       : 1;        // read preformatted text
    bool bReadTextArea  : 1;        // read TEXTAREA
    bool bReadScript    : 1;        // read <SCRIPT>
    bool bReadStyle     : 1;        // read <STYLE>
    bool bEndTokenFound : 1;        // found </SCRIPT> or </STYLE>

    bool bPre_IgnoreNewPara : 1;    // flags for reading of PRE paragraphs
    bool bReadNextChar : 1;         // true: read NextChar again(JavaScript!)
    bool bReadComment : 1;          // true: read NextChar again (JavaScript!)

    sal_uInt32 nPre_LinePos;            // Pos in the line in the PRE-Tag

    HtmlTokenId mnPendingOffToken;          ///< OFF token pending for a <XX.../> ON/OFF ON token

    OUString aEndToken;

    /// XML namespace, in case of XHTML.
    OUString maNamespace;

protected:
    OUString sSaveToken;             // the read tag as string

    HtmlTokenId ScanText( const sal_Unicode cBreak = 0U );

    HtmlTokenId GetNextRawToken();

    // scan next token
    virtual HtmlTokenId GetNextToken_() override;

    virtual ~HTMLParser() override;

    void FinishHeader() { bIsInHeader = false; }

    void SetNamespace(const OUString& rNamespace);

public:
    HTMLParser( SvStream& rIn, bool bReadNewDoc = true );

    virtual SvParserState CallParser() override;

    bool IsNewDoc() const       { return bNewDoc; }
    bool IsInHeader() const     { return bIsInHeader; }
    bool IsReadListing() const  { return bReadListing; }
    bool IsReadXMP() const      { return bReadXMP; }
    bool IsReadPRE() const      { return bReadPRE; }
    bool IsReadScript() const   { return bReadScript; }
    bool IsReadStyle() const    { return bReadStyle; }

    // start PRE-/LISTING or XMP mode or filter tags respectively
    inline void StartPRE();
    void FinishPRE() { bReadPRE = false; }
    HtmlTokenId FilterPRE( HtmlTokenId nToken );

    inline void StartListing();
    void FinishListing() { bReadListing = false; }
    HtmlTokenId FilterListing( HtmlTokenId nToken );

    inline void StartXMP();
    void FinishXMP() { bReadXMP = false; }
    HtmlTokenId FilterXMP( HtmlTokenId nToken );

    void FinishTextArea() { bReadTextArea = false; }

    // finish PRE-/LISTING- and XMP mode
    void FinishPREListingXMP() { bReadPRE = bReadListing = bReadXMP = false; }

    // Filter the current token according to the current mode
    // (PRE, XMP, ...) and set the flags. Is called by Continue before
    // NextToken is called. If you implement own loops or call
    // NextToken yourself, you should call this method beforehand.
    HtmlTokenId FilterToken( HtmlTokenId nToken );

    void ReadRawData( const OUString &rEndToken ) { aEndToken = rEndToken; }

    // Token without \-sequences
    void UnescapeToken();

    // Determine the options. pNoConvertToken is the optional token
    // of an option, for which the CR/LFs are not deleted from the value
    // of the option.
    const HTMLOptions& GetOptions( HtmlOptionId const *pNoConvertToken=nullptr );

    // for asynchronous reading from the SvStream
    virtual void Continue( HtmlTokenId nToken ) override;


protected:

    static rtl_TextEncoding GetEncodingByMIME( const OUString& rMime );

    /// template method: called when ParseMetaOptions adds a user-defined meta
    virtual void AddMetaUserDefined( OUString const & i_rMetaName );

private:
    /// parse meta options into XDocumentProperties and encoding
    bool ParseMetaOptionsImpl( const css::uno::Reference< css::document::XDocumentProperties>&,
            SvKeyValueIterator*,
            const HTMLOptions&,
            rtl_TextEncoding& rEnc );

public:
    /// overriding method must call this implementation!
    virtual bool ParseMetaOptions( const css::uno::Reference< css::document::XDocumentProperties>&,
            SvKeyValueIterator* );

    void ParseScriptOptions( OUString& rLangString, const OUString&, HTMLScriptLanguage& rLang,
                             OUString& rSrc, OUString& rLibrary, OUString& rModule );

    // Remove a comment around the content of <SCRIPT> or <STYLE>.
    // The whole line behind a "<!--" might be deleted (for JavaScript).
    static void RemoveSGMLComment( OUString &rString );

    static bool InternalImgToPrivateURL( OUString& rURL );
    static rtl_TextEncoding GetEncodingByHttpHeader( SvKeyValueIterator *pHTTPHeader );
    bool SetEncodingByHTTPHeader( SvKeyValueIterator *pHTTPHeader );
};

inline void HTMLParser::StartPRE()
{
    bReadPRE = true;
    bPre_IgnoreNewPara = true;
    nPre_LinePos = 0;
}

inline void HTMLParser::StartListing()
{
    bReadListing = true;
    bPre_IgnoreNewPara = true;
    nPre_LinePos = 0;
}

inline void HTMLParser::StartXMP()
{
    bReadXMP = true;
    bPre_IgnoreNewPara = true;
    nPre_LinePos = 0;
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */