LibreOffice Module sw (master) 1
htmlbas.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 <config_features.h>
21
22#include <comphelper/string.hxx>
23#include <osl/diagnose.h>
24#include <basic/basmgr.hxx>
25#include <basic/sbmod.hxx>
26#include <sfx2/evntconf.hxx>
27#include <sfx2/app.hxx>
28#include <svtools/htmlout.hxx>
29#include <svtools/htmlkywd.hxx>
30
31#include <com/sun/star/document/XEventsSupplier.hpp>
32#include <com/sun/star/uno/Reference.hxx>
33#include <com/sun/star/script/XLibraryContainer.hpp>
34#include <com/sun/star/container/XNameContainer.hpp>
35
36#include <fmtfld.hxx>
37
38#include <doc.hxx>
40#include <docsh.hxx>
41#include <docufld.hxx>
42#include "wrthtml.hxx"
43#include "swhtml.hxx"
44
45using namespace ::com::sun::star;
46using namespace ::com::sun::star::uno;
47using namespace ::com::sun::star::container;
48
50{
55 { nullptr, nullptr, SvMacroItemId::NONE }
56};
57
59{
62
63 if( !m_aScriptURL.isEmpty() )
64 {
65 // Ignore the script tag
66 m_bIgnoreRawData = true;
67 }
68}
69
71{
72 bool bInsIntoBasic = false,
73 bInsSrcIntoField = false;
74
75 switch( m_eScriptLang )
76 {
77 case HTMLScriptLanguage::StarBasic:
78 bInsIntoBasic = true;
79 break;
80 default:
81 bInsSrcIntoField = true;
82 break;
83 }
84
85 m_bIgnoreRawData = false;
87
88 // Except for StarBasic and unused JavaScript, save each script or module name in a field
89 if( bInsSrcIntoField && !m_bIgnoreHTMLComments )
90 {
91 SwScriptFieldType *pType =
92 static_cast<SwScriptFieldType*>(m_xDoc->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Script ));
93
94 SwScriptField aField( pType, m_aScriptType,
96 !m_aScriptURL.isEmpty() );
97 InsertAttr( SwFormatField( aField ), false );
98 }
99
100 SwDocShell *pDocSh = m_xDoc->GetDocShell();
101 if( !m_aScriptSource.isEmpty() && pDocSh &&
102 bInsIntoBasic && IsNewDoc() )
103 {
104 // Create a Basic module for javascript and StarBasic.
105
106 // The Basic does still not remove SGML comments
107 RemoveSGMLComment( m_aScriptSource );
108
109 // get library name
110 OUString aLibName;
111 if( !m_aBasicLib.isEmpty() )
112 aLibName = m_aBasicLib;
113 else
114 aLibName = "Standard";
115
116 // get module library container
117 Reference< script::XLibraryContainer > xModLibContainer = pDocSh->GetBasicContainer();
118
119 if ( xModLibContainer.is() )
120 {
121 Reference< container::XNameContainer > xModLib;
122 if ( xModLibContainer->hasByName( aLibName ) )
123 {
124 // get module library
125 Any aElement = xModLibContainer->getByName( aLibName );
126 aElement >>= xModLib;
127 }
128 else
129 {
130 // create module library
131 xModLib = xModLibContainer->createLibrary( aLibName );
132 }
133
134 if ( xModLib.is() )
135 {
136 if( m_aBasicModule.isEmpty() )
137 {
138 // create module name
139 bool bFound = true;
140 while( bFound )
141 {
142 m_aBasicModule = "Modul" + OUString::number( static_cast<sal_Int32>(++m_nSBModuleCnt) );
143 bFound = xModLib->hasByName( m_aBasicModule );
144 }
145 }
146
147 // create module
148 OUString aModName( m_aBasicModule );
149 if ( !xModLib->hasByName( aModName ) )
150 {
151 Any aElement;
152 aElement <<= m_aScriptSource;
153 xModLib->insertByName( aModName , aElement );
154 }
155 }
156 }
157
158 // get dialog library container
159 Reference< script::XLibraryContainer > xDlgLibContainer = pDocSh->GetDialogContainer();
160
161 if ( xDlgLibContainer.is() )
162 {
163 if ( !xDlgLibContainer->hasByName( aLibName ) )
164 {
165 // create dialog library
166 xDlgLibContainer->createLibrary( aLibName );
167 }
168 }
169 }
170
171 m_aScriptSource.clear();
172 m_aScriptType.clear();
173 m_aScriptURL.clear();
174
175 m_aBasicLib.clear();
176 m_aBasicModule.clear();
177}
178
180{
181 // We'll just remember a few strings here
182 if( aToken.getLength() > 2 &&
183 (HTMLScriptLanguage::StarBasic==m_eScriptLang && aToken[ 0 ] == '\'') )
184 {
185 sal_Int32 nPos = -1;
186 if( m_aBasicLib.isEmpty() )
187 {
188 nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_library );
189 if( nPos != -1 )
190 {
192 aToken.subView( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_library) - 1 );
194 }
195 }
196
197 if( m_aBasicModule.isEmpty() && nPos == -1 )
198 {
199 nPos = aToken.indexOf( OOO_STRING_SVTOOLS_HTML_SB_module );
200 if( nPos != -1 )
201 {
203 aToken.subView( nPos + sizeof(OOO_STRING_SVTOOLS_HTML_SB_module) - 1 );
205 }
206 }
207
208 if( nPos == -1 )
209 {
210 if( !m_aScriptSource.isEmpty() )
211 m_aScriptSource += "\n";
212 m_aScriptSource += aToken;
213 }
214 }
215 else if( !m_aScriptSource.isEmpty() || !aToken.isEmpty() )
216 {
217 // Empty lines are ignored on the beginning
218 if( !m_aScriptSource.isEmpty() )
219 {
220 m_aScriptSource += "\n";
221 }
222 m_aScriptSource += aToken;
223 }
224}
225
226void SwHTMLParser::InsertBasicDocEvent( const OUString& aEvent, const OUString& rName,
227 ScriptType eScrType,
228 const OUString& rScrType )
229{
230 OSL_ENSURE( !rName.isEmpty(), "InsertBasicDocEvent() called without macro" );
231 if( rName.isEmpty() )
232 return;
233
234 SwDocShell *pDocSh = m_xDoc->GetDocShell();
235 OSL_ENSURE( pDocSh, "Where is the DocShell?" );
236 if( !pDocSh )
237 return;
238
239 OUString sEvent(convertLineEnd(rName, GetSystemLineEnd()));
240 OUString sScriptType;
241 if( EXTENDED_STYPE == eScrType )
242 sScriptType = rScrType;
243
244 SfxEventConfiguration::ConfigureEvent( aEvent, SvxMacro( sEvent, sScriptType, eScrType ),
245 pDocSh );
246}
247
249{
250#if !HAVE_FEATURE_SCRIPTING
251 (void) rHTMLWrt;
252#else
253 if( !m_bCfgStarBasic )
254 return;
255
257 OSL_ENSURE( pBasicMan, "Where is the Basic-Manager?" );
258 // Only write DocumentBasic
259 if( !pBasicMan || pBasicMan == SfxApplication::GetBasicManager() )
260 {
261 return;
262 }
263
264 bool bFirst=true;
265 // Now write all StarBasic and unused Javascript modules
266 for( sal_uInt16 i=0; i<pBasicMan->GetLibCount(); i++ )
267 {
268 StarBASIC *pBasic = pBasicMan->GetLib( i );
269 const OUString& rLibName = pBasic->GetName();
270 for( const auto& pModule: pBasic->GetModules() )
271 {
272 OUString sLang(SVX_MACRO_LANGUAGE_STARBASIC);
273
274 if( bFirst )
275 {
276 bFirst = false;
277 OutNewLine();
278 OString sOut =
281 "=\""
284 "=\"text/x-";
285 Strm().WriteOString( sOut );
286 // Entities aren't welcome here
287 Strm().WriteOString( OUStringToOString(sLang, RTL_TEXTENCODING_UTF8) )
288 .WriteOString( "\">" );
289 }
290
291 const OUString& rModName = pModule->GetName();
292 Strm().WriteOString( SAL_NEWLINE_STRING ); // don't indent!
293 HTMLOutFuncs::OutScript( Strm(), GetBaseURL(), pModule->GetSource32(),
294 sLang, STARBASIC, OUString(),
295 &rLibName, &rModName );
296 }
297 }
298#endif
299}
300
301static const char* aEventNames[] =
302{
303 "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
304};
305
307{
308 SwDocShell *pDocSh = m_pDoc->GetDocShell();
309 if( !pDocSh )
310 return;
311
312 SvxMacroTableDtor aDocTable;
313
314 uno::Reference< document::XEventsSupplier > xSup( pDocSh->GetModel(), uno::UNO_QUERY );
315 uno::Reference < container::XNameReplace > xEvents = xSup->getEvents();
316 for ( sal_Int32 i=0; i<4; i++ )
317 {
318 std::unique_ptr<SvxMacro> pMacro = SfxEventConfiguration::ConvertToMacro( xEvents->getByName( OUString::createFromAscii(aEventNames[i]) ), pDocSh );
319 if ( pMacro )
320 {
321 aDocTable.Insert( aBodyEventTable[i].nEvent, *pMacro );
322 }
323 }
324
325 if( !aDocTable.empty() )
328}
329
330/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
StarBASIC * GetLib(sal_uInt16 nLib) const
sal_uInt16 GetLibCount() const
const OUString & GetName(SbxNameType=SbxNameType::NONE) const
static BasicManager * GetBasicManager()
static void ConfigureEvent(const OUString &aName, const SvxMacro &, SfxObjectShell const *pObjSh)
static std::unique_ptr< SvxMacro > ConvertToMacro(const css::uno::Any &rElement, SfxObjectShell *pDoc)
css::uno::Reference< css::script::XLibraryContainer > GetBasicContainer()
BasicManager * GetBasicManager() const
css::uno::Reference< css::script::XLibraryContainer > GetDialogContainer()
css::uno::Reference< css::frame::XModel3 > GetModel() const
SbModules & GetModules()
SvStream & WriteOString(std::string_view rStr)
SvxMacro & Insert(SvMacroItemId nEvent, const SvxMacro &rMacro)
bool empty() const
SwDocShell * GetDocShell()
Definition: doc.hxx:1370
OUString m_aBasicModule
Definition: swhtml.hxx:365
void InsertAttr(const SfxPoolItem &rItem, bool bInsAtStart)
Definition: swhtml.cxx:3474
OUString m_aScriptSource
Definition: swhtml.hxx:366
sal_uInt16 m_nSBModuleCnt
Definition: swhtml.hxx:421
OUString m_aScriptType
Definition: swhtml.hxx:367
bool m_bIgnoreHTMLComments
Definition: swhtml.hxx:472
OUString m_aBasicLib
Definition: swhtml.hxx:364
rtl::Reference< SwDoc > m_xDoc
Definition: swhtml.hxx:398
HTMLScriptLanguage m_eScriptLang
Definition: swhtml.hxx:437
void EndScript()
Definition: htmlbas.cxx:70
void AddScriptSource()
Definition: htmlbas.cxx:179
void NewScript()
Definition: htmlbas.cxx:58
bool m_bIgnoreRawData
Definition: swhtml.hxx:451
OUString m_sBaseURL
Definition: swhtml.hxx:363
OUString m_aScriptURL
Definition: swhtml.hxx:368
void InsertBasicDocEvent(const OUString &aEventName, const OUString &rName, ScriptType eScrType, const OUString &rScrType)
Definition: htmlbas.cxx:226
void OutBasic(const SwHTMLWriter &rHTMLWrt)
Definition: htmlbas.cxx:248
OString GetNamespace() const
Determines the prefix string needed to respect the requested namespace alias.
Definition: wrthtml.cxx:1586
void OutBasicBodyEvents()
Definition: htmlbas.cxx:306
void OutNewLine(bool bCheck=false)
Definition: wrthtml.cxx:1537
bool m_bCfgStarBasic
Definition: wrthtml.hxx:358
SvStream & Strm()
Definition: writer.cxx:193
SwDoc * m_pDoc
Definition: shellio.hxx:407
const OUString & GetBaseURL() const
Definition: shellio.hxx:443
#define SAL_NEWLINE_STRING
HTMLOutEvent const aBodyEventTable[]
Definition: htmlbas.cxx:49
static const char * aEventNames[]
Definition: htmlbas.cxx:301
#define OOO_STRING_SVTOOLS_HTML_O_httpequiv
#define OOO_STRING_SVTOOLS_HTML_meta
#define OOO_STRING_SVTOOLS_HTML_O_SDonunload
#define OOO_STRING_SVTOOLS_HTML_O_onunload
#define OOO_STRING_SVTOOLS_HTML_O_onfocus
#define OOO_STRING_SVTOOLS_HTML_O_SDonfocus
#define OOO_STRING_SVTOOLS_HTML_O_SDonblur
#define OOO_STRING_SVTOOLS_HTML_O_SDonload
#define OOO_STRING_SVTOOLS_HTML_META_content_script_type
#define OOO_STRING_SVTOOLS_HTML_O_content
#define OOO_STRING_SVTOOLS_HTML_O_onblur
#define OOO_STRING_SVTOOLS_HTML_SB_module
#define OOO_STRING_SVTOOLS_HTML_O_onload
#define OOO_STRING_SVTOOLS_HTML_SB_library
LineEnd GetSystemLineEnd()
TOOLS_DLLPUBLIC OString convertLineEnd(const OString &rIn, LineEnd eLineEnd)
sal_uInt16 nPos
ScriptType
EXTENDED_STYPE
STARBASIC
constexpr OUStringLiteral SVX_MACRO_LANGUAGE_STARBASIC
OString strip(const OString &rIn, char c)
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
static SVT_DLLPUBLIC SvStream & OutScript(SvStream &rStrm, const OUString &rBaseURL, std::u16string_view rSource, const OUString &rLanguage, ScriptType eScriptType, const OUString &rSrc, const OUString *pSBLibrary, const OUString *pSBModule)
static SVT_DLLPUBLIC SvStream & Out_Events(SvStream &, const SvxMacroTableDtor &, const HTMLOutEvent *, bool bOutStarBasic, OUString *pNonConvertableChars=nullptr)