Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/bastyp/sfxhtml.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
21
#include <tools/urlobj.hxx>
22
#include <tools/debug.hxx>
23
24
#include <sfx2/docfile.hxx>
25
#include <sfx2/event.hxx>
26
#include <openflag.hxx>
27
28
#include <svl/numformat.hxx>
29
#include <svtools/htmlkywd.hxx>
30
#include <svtools/htmltokn.h>
31
#include <vcl/imap.hxx>
32
#include <vcl/imapcirc.hxx>
33
#include <vcl/imapobj.hxx>
34
#include <vcl/imappoly.hxx>
35
#include <vcl/imaprect.hxx>
36
#include <svl/zforlist.hxx>
37
38
#include <sfx2/sfxhtml.hxx>
39
40
#include <comphelper/string.hxx>
41
#include <o3tl/string_view.hxx>
42
43
#include <vector>
44
45
46
using namespace ::com::sun::star;
47
48
49
// <INPUT TYPE=xxx>
50
HTMLOptionEnum<IMapObjectType> const aAreaShapeOptEnums[] =
51
{
52
    { OOO_STRING_SVTOOLS_HTML_SH_rect,      IMapObjectType::Rectangle  },
53
    { OOO_STRING_SVTOOLS_HTML_SH_rectangle, IMapObjectType::Rectangle  },
54
    { OOO_STRING_SVTOOLS_HTML_SH_circ,      IMapObjectType::Circle     },
55
    { OOO_STRING_SVTOOLS_HTML_SH_circle,    IMapObjectType::Circle     },
56
    { OOO_STRING_SVTOOLS_HTML_SH_poly,      IMapObjectType::Polygon    },
57
    { OOO_STRING_SVTOOLS_HTML_SH_polygon,   IMapObjectType::Polygon    },
58
    { nullptr,                              IMapObjectType::Rectangle  }
59
};
60
61
SfxHTMLParser::SfxHTMLParser( SvStream& rStream, bool bIsNewDoc,
62
                              SfxMedium *pMed )
63
14.6k
    : HTMLParser(rStream, bIsNewDoc)
64
14.6k
    , pMedium(pMed)
65
14.6k
    , eScriptType(STARBASIC)
66
14.6k
{
67
14.6k
    DBG_ASSERT( RTL_TEXTENCODING_UTF8 == GetSrcEncoding( ),
68
14.6k
                "SfxHTMLParser::SfxHTMLParser: From where comes ZS?" );
69
70
14.6k
    DBG_ASSERT( !IsSwitchToUCS2(),
71
14.6k
                "SfxHTMLParser::SfxHTMLParser: Switch to UCS2?" );
72
73
    // If the file starts with a BOM, switch to UCS2.
74
14.6k
    SetSwitchToUCS2( true );
75
14.6k
}
76
77
SfxHTMLParser::~SfxHTMLParser()
78
14.6k
{
79
14.6k
    DBG_ASSERT( !pDLMedium, "Here is a File Download that has got stuck" );
80
14.6k
}
81
82
bool SfxHTMLParser::ParseMapOptions(
83
    ImageMap* pImageMap, const HTMLOptions& rOptions)
84
2.06k
{
85
2.06k
    DBG_ASSERT( pImageMap, "ParseMapOptions: No Image-Map" );
86
87
2.06k
    OUString aName;
88
89
4.20k
    for (size_t i = rOptions.size(); i; )
90
2.13k
    {
91
2.13k
        const HTMLOption& aOption = rOptions[--i];
92
2.13k
        if ( aOption.GetToken() == HtmlOptionId::NAME )
93
1.50k
            aName = aOption.GetString();
94
2.13k
    }
95
96
2.06k
    if( !aName.isEmpty() )
97
1.25k
        pImageMap->SetName( aName );
98
99
2.06k
    return !aName.isEmpty();
100
2.06k
}
101
102
bool SfxHTMLParser::ParseAreaOptions(ImageMap * pImageMap, std::u16string_view rBaseURL,
103
                                     const HTMLOptions& rOptions,
104
                                     SvMacroItemId nEventMouseOver,
105
                                     SvMacroItemId nEventMouseOut )
106
2.50k
{
107
2.50k
    DBG_ASSERT( pImageMap, "ParseAreaOptions: no Image-Map" );
108
109
2.50k
    IMapObjectType nShape = IMapObjectType::Rectangle;
110
2.50k
    std::vector<sal_uInt32> aCoords;
111
2.50k
    OUString aName, aHRef, aAlt, aTarget;
112
2.50k
    bool bNoHRef = false;
113
2.50k
    SvxMacroTableDtor aMacroTbl;
114
115
10.8k
    for (size_t i = rOptions.size(); i; )
116
8.30k
    {
117
8.30k
        SvMacroItemId nEvent = SvMacroItemId::NONE;
118
8.30k
        ScriptType eScrpType = STARBASIC;
119
8.30k
        const HTMLOption& rOption = rOptions[--i];
120
8.30k
        switch( rOption.GetToken() )
121
8.30k
        {
122
49
        case HtmlOptionId::NAME:
123
49
            aName = rOption.GetString();
124
49
            break;
125
1.80k
        case HtmlOptionId::SHAPE:
126
1.80k
            rOption.GetEnum( nShape, aAreaShapeOptEnums );
127
1.80k
            break;
128
2.07k
        case HtmlOptionId::COORDS:
129
2.07k
            rOption.GetNumbers( aCoords );
130
2.07k
            break;
131
864
        case HtmlOptionId::HREF:
132
864
            aHRef = INetURLObject::GetAbsURL( rBaseURL, rOption.GetString() );
133
864
            break;
134
2
        case HtmlOptionId::NOHREF:
135
2
            bNoHRef = true;
136
2
            break;
137
902
        case HtmlOptionId::ALT:
138
902
            aAlt = rOption.GetString();
139
902
            break;
140
67
        case HtmlOptionId::TARGET:
141
67
            aTarget = rOption.GetString();
142
67
            break;
143
144
21
        case HtmlOptionId::ONMOUSEOVER:
145
21
            eScrpType = JAVASCRIPT;
146
21
            [[fallthrough]];
147
21
        case HtmlOptionId::SDONMOUSEOVER:
148
21
            nEvent = nEventMouseOver;
149
21
            goto IMAPOBJ_SETEVENT;
150
151
51
        case HtmlOptionId::ONMOUSEOUT:
152
51
            eScrpType = JAVASCRIPT;
153
51
            [[fallthrough]];
154
51
        case HtmlOptionId::SDONMOUSEOUT:
155
51
            nEvent = nEventMouseOut;
156
51
            goto IMAPOBJ_SETEVENT;
157
72
IMAPOBJ_SETEVENT:
158
72
            if( nEvent != SvMacroItemId::NONE)
159
72
            {
160
72
                OUString sTmp( rOption.GetString() );
161
72
                if( !sTmp.isEmpty() )
162
72
                {
163
72
                    sTmp = convertLineEnd(sTmp, GetSystemLineEnd());
164
72
                    aMacroTbl.Insert( nEvent, SvxMacro( sTmp, u""_ustr, eScrpType ));
165
72
                }
166
72
            }
167
72
            break;
168
2.46k
        default: break;
169
8.30k
        }
170
8.30k
    }
171
172
2.50k
    if( bNoHRef )
173
2
        aHRef.clear();
174
175
2.50k
    bool bNewArea = true;
176
2.50k
    switch( nShape )
177
2.50k
    {
178
2.38k
    case IMapObjectType::Rectangle:
179
2.38k
        if( aCoords.size() >=4 )
180
1.56k
        {
181
1.56k
            tools::Rectangle aRect( aCoords[0], aCoords[1],
182
1.56k
                             aCoords[2], aCoords[3] );
183
1.56k
            std::unique_ptr<IMapRectangleObject> pMapRObj( new IMapRectangleObject(aRect, aHRef, aAlt, OUString(), aTarget, aName,
184
1.56k
                                          !bNoHRef ));
185
1.56k
            if( !aMacroTbl.empty() )
186
3
                pMapRObj->SetMacroTable( aMacroTbl );
187
1.56k
            pImageMap->InsertIMapObject( std::move(pMapRObj) );
188
1.56k
        }
189
2.38k
        break;
190
58
    case IMapObjectType::Circle:
191
58
        if( aCoords.size() >=3 )
192
57
        {
193
57
            Point aPoint( aCoords[0], aCoords[1] );
194
57
            std::unique_ptr<IMapCircleObject> pMapCObj(new IMapCircleObject(aPoint, aCoords[2],aHRef, aAlt, OUString(),
195
57
                                       aTarget, aName, !bNoHRef ));
196
57
            if( !aMacroTbl.empty() )
197
0
                pMapCObj->SetMacroTable( aMacroTbl );
198
57
            pImageMap->InsertIMapObject( std::move(pMapCObj) );
199
57
        }
200
58
        break;
201
61
    case IMapObjectType::Polygon:
202
61
        if( aCoords.size() >=6 )
203
51
        {
204
51
            sal_uInt16 nCount = aCoords.size() / 2;
205
51
            tools::Polygon aPoly( nCount );
206
306
            for( sal_uInt16 i=0; i<nCount; i++ )
207
255
                aPoly[i] = Point( aCoords[2*i], aCoords[2*i+1] );
208
51
            std::unique_ptr<IMapPolygonObject> pMapPObj(new IMapPolygonObject( aPoly, aHRef, aAlt, OUString(), aTarget, aName,
209
51
                                        !bNoHRef ));
210
51
            if( !aMacroTbl.empty() )
211
1
                pMapPObj->SetMacroTable( aMacroTbl );
212
51
            pImageMap->InsertIMapObject( std::move(pMapPObj) );
213
51
        }
214
61
        break;
215
0
    default:
216
0
        bNewArea = false;
217
2.50k
    }
218
219
2.50k
    return bNewArea;
220
2.50k
}
221
222
void SfxHTMLParser::StartFileDownload(const OUString& rURL)
223
0
{
224
0
    DBG_ASSERT( !pDLMedium, "StartFileDownload when active Download" );
225
0
    if( pDLMedium )
226
0
        return;
227
228
0
    pDLMedium.reset( new SfxMedium( rURL, SFX_STREAM_READONLY ) );
229
0
    pDLMedium->Download();
230
0
}
231
232
bool SfxHTMLParser::FinishFileDownload( OUString& rStr )
233
0
{
234
0
    bool bOK = pDLMedium && pDLMedium->GetErrorCode() == ERRCODE_NONE;
235
0
    if( bOK )
236
0
    {
237
0
        SvStream* pStream = pDLMedium->GetInStream();
238
0
        DBG_ASSERT( pStream, "No In-Stream received from Medium" );
239
240
0
        SvMemoryStream aStream;
241
0
        if( pStream )
242
0
            aStream.WriteStream( *pStream );
243
244
0
        sal_uInt64 const nLen = aStream.TellEnd();
245
0
        aStream.Seek( 0 );
246
0
        rStr = read_uInt8s_ToOUString(aStream, nLen, RTL_TEXTENCODING_UTF8);
247
0
    }
248
249
0
    pDLMedium.reset();
250
251
0
    return bOK;
252
0
}
253
254
void SfxHTMLParser::GetScriptType_Impl( SvKeyValueIterator *pHTTPHeader )
255
7.27k
{
256
7.27k
    aScriptType = SVX_MACRO_LANGUAGE_JAVASCRIPT;
257
7.27k
    eScriptType = JAVASCRIPT;
258
7.27k
    if( !pHTTPHeader )
259
0
        return;
260
261
7.27k
    SvKeyValue aKV;
262
9.35k
    for( bool bCont = pHTTPHeader->GetFirst( aKV ); bCont;
263
7.27k
         bCont = pHTTPHeader->GetNext( aKV ) )
264
2.09k
    {
265
2.09k
        if( aKV.GetKey().equalsIgnoreAsciiCase(
266
2.09k
                                OOO_STRING_SVTOOLS_HTML_META_content_script_type ) )
267
5
        {
268
5
            if( !aKV.GetValue().isEmpty() )
269
4
            {
270
4
                OUString aTmp( aKV.GetValue() );
271
4
                if( aTmp.startsWithIgnoreAsciiCase( "text/" ) )
272
2
                    aTmp = aTmp.copy( 5 );
273
2
                else if( aTmp.startsWithIgnoreAsciiCase( "application/" ) )
274
0
                    aTmp = aTmp.copy( 12 );
275
2
                else
276
2
                    break;
277
278
2
                if( aTmp.startsWithIgnoreAsciiCase( "x-" ) ) // MIME-experimental
279
0
                {
280
0
                    aTmp = aTmp.copy( 2 );
281
0
                }
282
283
2
                if( aTmp.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_LG_starbasic ) )
284
0
                {
285
0
                    eScriptType = STARBASIC;
286
0
                    aScriptType = SVX_MACRO_LANGUAGE_STARBASIC;
287
0
                }
288
2
                if( !aTmp.equalsIgnoreAsciiCase( OOO_STRING_SVTOOLS_HTML_LG_javascript ) )
289
0
                {
290
0
                    eScriptType = EXTENDED_STYPE;
291
0
                    aScriptType = aTmp;
292
0
                }
293
2
            }
294
3
            break;
295
5
        }
296
2.09k
    }
297
7.27k
}
298
299
ScriptType SfxHTMLParser::GetScriptType( SvKeyValueIterator *pHTTPHeader ) const
300
101k
{
301
101k
    if( aScriptType.isEmpty() )
302
7.27k
        const_cast<SfxHTMLParser *>(this)->GetScriptType_Impl( pHTTPHeader );
303
304
101k
    return eScriptType;
305
101k
}
306
307
const OUString& SfxHTMLParser::GetScriptTypeString(
308
                                    SvKeyValueIterator *pHTTPHeader ) const
309
101k
{
310
101k
    if( aScriptType.isEmpty() )
311
0
        const_cast<SfxHTMLParser *>(this)->GetScriptType_Impl( pHTTPHeader );
312
313
101k
    return aScriptType;
314
101k
}
315
316
double SfxHTMLParser::GetTableDataOptionsValNum( sal_uInt32& nNumForm,
317
        LanguageType& eNumLang, const OUString& aValStr, std::u16string_view aNumStr,
318
        SvNumberFormatter& rFormatter )
319
156k
{
320
156k
    LanguageType eParseLang(o3tl::toInt32(aNumStr));
321
156k
    sal_uInt32 nParseForm = rFormatter.GetFormatForLanguageIfBuiltIn( 0, eParseLang );
322
156k
    double fVal;
323
156k
    (void)rFormatter.IsNumberFormat(aValStr, nParseForm, fVal);
324
156k
    if ( comphelper::string::getTokenCount(aNumStr, ';') > 2 )
325
138k
    {
326
138k
        sal_Int32 nIdx {0};
327
138k
        eNumLang = LanguageType(o3tl::toInt32(o3tl::getToken(aNumStr, 1, ';', nIdx )));
328
138k
        OUString aFormat( aNumStr.substr( nIdx ) );
329
138k
        sal_Int32 nCheckPos;
330
138k
        SvNumFormatType nType;
331
138k
        if ( eNumLang != LANGUAGE_SYSTEM )
332
65.8k
            rFormatter.PutEntry( aFormat, nCheckPos, nType, nNumForm, eNumLang );
333
72.8k
        else
334
72.8k
            rFormatter.PutandConvertEntry( aFormat, nCheckPos, nType, nNumForm,
335
72.8k
                                           eParseLang, eNumLang, true);
336
138k
    }
337
18.1k
    else
338
18.1k
    {
339
18.1k
        eNumLang = LANGUAGE_SYSTEM;
340
18.1k
        nNumForm = rFormatter.GetFormatForLanguageIfBuiltIn( 0, eNumLang );
341
18.1k
    }
342
156k
    return fVal;
343
156k
}
344
345
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */