Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svtools/source/svhtml/htmlsupp.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
#include <comphelper/string.hxx>
21
#include <svtools/parhtml.hxx>
22
#include <svtools/htmltokn.h>
23
#include <svtools/htmlkywd.hxx>
24
#include <tools/urlobj.hxx>
25
26
// Table for converting option values into strings
27
HTMLOptionEnum<HTMLScriptLanguage> const aScriptLangOptEnums[] =
28
{
29
    { OOO_STRING_SVTOOLS_HTML_LG_starbasic,    HTMLScriptLanguage::StarBasic     },
30
    { OOO_STRING_SVTOOLS_HTML_LG_javascript,   HTMLScriptLanguage::JavaScript    },
31
    { OOO_STRING_SVTOOLS_HTML_LG_javascript11, HTMLScriptLanguage::JavaScript    },
32
    { OOO_STRING_SVTOOLS_HTML_LG_livescript,   HTMLScriptLanguage::JavaScript    },
33
    { nullptr,                                 HTMLScriptLanguage(0) }
34
};
35
36
void HTMLParser::ParseScriptOptions( OUString& rLangString, std::u16string_view rBaseURL,
37
                                     HTMLScriptLanguage& rLang,
38
                                     OUString& rSrc,
39
                                     OUString& rLibrary,
40
                                     OUString& rModule )
41
1.68k
{
42
1.68k
    const HTMLOptions& aScriptOptions = GetOptions();
43
44
1.68k
    rLangString.clear();
45
1.68k
    rLang = HTMLScriptLanguage::JavaScript;
46
1.68k
    rSrc.clear();
47
1.68k
    rLibrary.clear();
48
1.68k
    rModule.clear();
49
50
3.47k
    for( size_t i = aScriptOptions.size(); i; )
51
1.79k
    {
52
1.79k
        const HTMLOption& aOption = aScriptOptions[--i];
53
1.79k
        switch( aOption.GetToken() )
54
1.79k
        {
55
330
        case HtmlOptionId::LANGUAGE:
56
330
            {
57
330
                rLangString = aOption.GetString();
58
330
                HTMLScriptLanguage nLang;
59
330
                if( aOption.GetEnum( nLang, aScriptLangOptEnums ) )
60
284
                    rLang = nLang;
61
46
                else
62
46
                    rLang = HTMLScriptLanguage::Unknown;
63
330
            }
64
330
            break;
65
66
570
        case HtmlOptionId::SRC:
67
570
            rSrc = INetURLObject::GetAbsURL( rBaseURL, aOption.GetString() );
68
570
            break;
69
0
        case HtmlOptionId::SDLIBRARY:
70
0
            rLibrary = aOption.GetString();
71
0
            break;
72
73
0
        case HtmlOptionId::SDMODULE:
74
0
            rModule = aOption.GetString();
75
0
            break;
76
893
        default: break;
77
1.79k
        }
78
1.79k
    }
79
1.68k
}
80
81
void HTMLParser::RemoveSGMLComment( OUString &rString )
82
0
{
83
0
    sal_Unicode c = 0;
84
0
    sal_Int32 idx = 0;
85
0
    while (idx < rString.getLength())
86
0
    {
87
0
        c = rString[idx];
88
0
        if (!( c==' ' || c=='\t' || c=='\r' || c=='\n' ) )
89
0
            break;
90
0
        idx++;
91
0
    }
92
0
    if (idx)
93
0
        rString = rString.copy( idx );
94
95
0
    idx = rString.getLength() - 1;
96
0
    while (idx > 0)
97
        // Can never get to 0 because that would mean the string contains only whitespace, and the first
98
        // loop would already have removed all of those.
99
0
    {
100
0
        c = rString[idx];
101
0
        if (!( c==' ' || c=='\t' || c=='\r' || c=='\n' ) )
102
0
            break;
103
0
        idx--;
104
0
    }
105
0
    if (idx != rString.getLength() - 1)
106
0
        rString = rString.copy( 0, idx + 1 );
107
108
    // remove SGML comments
109
0
    if( rString.startsWith( "<!--" ) )
110
0
    {
111
        // the whole line
112
0
        sal_Int32 nPos = 4;
113
0
        while( nPos < rString.getLength() )
114
0
        {
115
0
            c = rString[nPos];
116
0
            if (c == '\r' || c == '\n')
117
0
                break;
118
0
            ++nPos;
119
0
        }
120
0
        if( c == '\r' && nPos+1 < rString.getLength() &&
121
0
            '\n' == rString[nPos+1] )
122
0
            ++nPos;
123
0
        else if( c != '\n' )
124
0
            nPos = 3;
125
0
        ++nPos;
126
0
        rString = rString.copy( nPos );
127
0
    }
128
129
0
    if( !rString.endsWith("-->") )
130
0
        return;
131
132
0
    rString = rString.copy( 0, rString.getLength()-3 );
133
    // "//" or "'", maybe preceding CR/LF
134
0
    rString = comphelper::string::stripEnd(rString, ' ');
135
0
    sal_Int32 nDel = 0, nLen = rString.getLength();
136
0
    if( nLen >= 2 &&
137
0
        rString.endsWith("//") )
138
0
    {
139
0
        nDel = 2;
140
0
    }
141
0
    else if( nLen && '\'' == rString[nLen-1] )
142
0
    {
143
0
        nDel = 1;
144
0
    }
145
0
    if( nDel && nLen >= nDel+1 )
146
0
    {
147
0
        c = rString[nLen-(nDel+1)];
148
0
        if( '\r'==c || '\n'==c )
149
0
        {
150
0
            nDel++;
151
0
            if( '\n'==c && nLen >= nDel+1 &&
152
0
                '\r'==rString[nLen-(nDel+1)] )
153
0
                nDel++;
154
0
        }
155
0
    }
156
0
    rString = rString.copy( 0, nLen-nDel );
157
0
}
158
159
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */