LibreOffice Module svtools (master) 1
htmlsupp.cxx
Go to the documentation of this file.
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
28{
33 { nullptr, HTMLScriptLanguage(0) }
34};
35
36void HTMLParser::ParseScriptOptions( OUString& rLangString, std::u16string_view rBaseURL,
37 HTMLScriptLanguage& rLang,
38 OUString& rSrc,
39 OUString& rLibrary,
40 OUString& rModule )
41{
42 const HTMLOptions& aScriptOptions = GetOptions();
43
44 rLangString.clear();
46 rSrc.clear();
47 rLibrary.clear();
48 rModule.clear();
49
50 for( size_t i = aScriptOptions.size(); i; )
51 {
52 const HTMLOption& aOption = aScriptOptions[--i];
53 switch( aOption.GetToken() )
54 {
56 {
57 rLangString = aOption.GetString();
59 if( aOption.GetEnum( nLang, aScriptLangOptEnums ) )
60 rLang = nLang;
61 else
63 }
64 break;
65
67 rSrc = INetURLObject::GetAbsURL( rBaseURL, aOption.GetString() );
68 break;
70 rLibrary = aOption.GetString();
71 break;
72
74 rModule = aOption.GetString();
75 break;
76 default: break;
77 }
78 }
79}
80
81void HTMLParser::RemoveSGMLComment( OUString &rString )
82{
83 sal_Unicode c = 0;
84 sal_Int32 idx = 0;
85 while (idx < rString.getLength())
86 {
87 c = rString[idx];
88 if (!( c==' ' || c=='\t' || c=='\r' || c=='\n' ) )
89 break;
90 idx++;
91 }
92 if (idx)
93 rString = rString.copy( idx );
94
95 idx = rString.getLength() - 1;
96 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 {
100 c = rString[idx];
101 if (!( c==' ' || c=='\t' || c=='\r' || c=='\n' ) )
102 break;
103 idx--;
104 }
105 if (idx != rString.getLength() - 1)
106 rString = rString.copy( 0, idx + 1 );
107
108 // remove SGML comments
109 if( rString.startsWith( "<!--" ) )
110 {
111 // the whole line
112 sal_Int32 nPos = 4;
113 while( nPos < rString.getLength() )
114 {
115 c = rString[nPos];
116 if (c == '\r' || c == '\n')
117 break;
118 ++nPos;
119 }
120 if( c == '\r' && nPos+1 < rString.getLength() &&
121 '\n' == rString[nPos+1] )
122 ++nPos;
123 else if( c != '\n' )
124 nPos = 3;
125 ++nPos;
126 rString = rString.copy( nPos );
127 }
128
129 if( !rString.endsWith("-->") )
130 return;
131
132 rString = rString.copy( 0, rString.getLength()-3 );
133 // "//" or "'", maybe preceding CR/LF
134 rString = comphelper::string::stripEnd(rString, ' ');
135 sal_Int32 nDel = 0, nLen = rString.getLength();
136 if( nLen >= 2 &&
137 rString.endsWith("//") )
138 {
139 nDel = 2;
140 }
141 else if( nLen && '\'' == rString[nLen-1] )
142 {
143 nDel = 1;
144 }
145 if( nDel && nLen >= nDel+1 )
146 {
147 c = rString[nLen-(nDel+1)];
148 if( '\r'==c || '\n'==c )
149 {
150 nDel++;
151 if( '\n'==c && nLen >= nDel+1 &&
152 '\r'==rString[nLen-(nDel+1)] )
153 nDel++;
154 }
155 }
156 rString = rString.copy( 0, nLen-nDel );
157}
158
159/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Representation of an HTML option (=attribute in a start tag).
Definition: parhtml.hxx:88
HtmlOptionId GetToken() const
Definition: parhtml.hxx:98
const OUString & GetString() const
Definition: parhtml.hxx:102
EnumT GetEnum(const HTMLOptionEnum< EnumT > *pOptEnums, EnumT nDflt=static_cast< EnumT >(0)) const
Definition: parhtml.hxx:110
static void RemoveSGMLComment(OUString &rString)
Definition: htmlsupp.cxx:81
void ParseScriptOptions(OUString &rLangString, std::u16string_view rBaseURL, HTMLScriptLanguage &rLang, OUString &rSrc, OUString &rLibrary, OUString &rModule)
Definition: htmlsupp.cxx:36
const HTMLOptions & GetOptions(HtmlOptionId const *pNoConvertToken=nullptr)
Definition: parhtml.cxx:1446
static OUString GetAbsURL(std::u16string_view rTheBaseURIRef, OUString const &rTheRelURIRef, EncodeMechanism eEncodeMechanism=EncodeMechanism::WasEncoded, DecodeMechanism eDecodeMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
#define OOO_STRING_SVTOOLS_HTML_LG_javascript
Definition: htmlkywd.hxx:621
#define OOO_STRING_SVTOOLS_HTML_LG_livescript
Definition: htmlkywd.hxx:623
#define OOO_STRING_SVTOOLS_HTML_LG_starbasic
Definition: htmlkywd.hxx:620
#define OOO_STRING_SVTOOLS_HTML_LG_javascript11
Definition: htmlkywd.hxx:622
HTMLOptionEnum< HTMLScriptLanguage > const aScriptLangOptEnums[]
Definition: htmlsupp.cxx:27
const sal_uInt16 idx[]
sal_uInt16 nPos
OString stripEnd(const OString &rIn, char c)
int i
HTMLScriptLanguage
Definition: parhtml.hxx:69
::std::vector< HTMLOption > HTMLOptions
Definition: parhtml.hxx:144
sal_uInt16 sal_Unicode