1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
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>
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>
39 #include <IDocumentFieldsAccess.hxx>
41 #include <docufld.hxx>
42 #include "wrthtml.hxx"
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::container
;
49 HTMLOutEvent
const aBodyEventTable
[] =
51 { OOO_STRING_SVTOOLS_HTML_O_SDonload
, OOO_STRING_SVTOOLS_HTML_O_onload
, SvMacroItemId::OpenDoc
},
52 { OOO_STRING_SVTOOLS_HTML_O_SDonunload
, OOO_STRING_SVTOOLS_HTML_O_onunload
, SvMacroItemId::PrepareCloseDoc
},
53 { OOO_STRING_SVTOOLS_HTML_O_SDonfocus
, OOO_STRING_SVTOOLS_HTML_O_onfocus
, SvMacroItemId::ActivateDoc
},
54 { OOO_STRING_SVTOOLS_HTML_O_SDonblur
, OOO_STRING_SVTOOLS_HTML_O_onblur
, SvMacroItemId::DeactivateDoc
},
55 { nullptr, nullptr, SvMacroItemId::NONE
}
58 void SwHTMLParser::NewScript()
60 ParseScriptOptions( m_aScriptType
, m_sBaseURL
, m_eScriptLang
, m_aScriptURL
,
61 m_aBasicLib
, m_aBasicModule
);
63 if( !m_aScriptURL
.isEmpty() )
65 // Ignore the script tag
66 m_bIgnoreRawData
= true;
70 void SwHTMLParser::EndScript()
72 bool bInsIntoBasic
= false,
73 bInsSrcIntoField
= false;
75 switch( m_eScriptLang
)
77 case HTMLScriptLanguage::StarBasic
:
81 bInsSrcIntoField
= true;
85 m_bIgnoreRawData
= false;
86 m_aScriptSource
= convertLineEnd(m_aScriptSource
, GetSystemLineEnd());
88 // Except for StarBasic and unused JavaScript, save each script or module name in a field
89 if( bInsSrcIntoField
&& !m_bIgnoreHTMLComments
)
91 SwScriptFieldType
*pType
=
92 static_cast<SwScriptFieldType
*>(m_xDoc
->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Script
));
94 SwScriptField
aField( pType
, m_aScriptType
,
95 !m_aScriptURL
.isEmpty() ? m_aScriptURL
: m_aScriptSource
,
96 !m_aScriptURL
.isEmpty() );
97 InsertAttr( SwFormatField( aField
), false );
100 SwDocShell
*pDocSh
= m_xDoc
->GetDocShell();
101 if( !m_aScriptSource
.isEmpty() && pDocSh
&&
102 bInsIntoBasic
&& IsNewDoc() )
104 // Create a Basic module for javascript and StarBasic.
106 // The Basic does still not remove SGML comments
107 RemoveSGMLComment( m_aScriptSource
);
111 if( !m_aBasicLib
.isEmpty() )
112 aLibName
= m_aBasicLib
;
114 aLibName
= "Standard";
116 // get module library container
117 Reference
< script::XLibraryContainer
> xModLibContainer
= pDocSh
->GetBasicContainer();
119 if ( xModLibContainer
.is() )
121 Reference
< container::XNameContainer
> xModLib
;
122 if ( xModLibContainer
->hasByName( aLibName
) )
124 // get module library
125 Any aElement
= xModLibContainer
->getByName( aLibName
);
126 aElement
>>= xModLib
;
130 // create module library
131 xModLib
= xModLibContainer
->createLibrary( aLibName
);
136 if( m_aBasicModule
.isEmpty() )
138 // create module name
142 m_aBasicModule
= "Modul" + OUString::number( static_cast<sal_Int32
>(++m_nSBModuleCnt
) );
143 bFound
= xModLib
->hasByName( m_aBasicModule
);
148 OUString
aModName( m_aBasicModule
);
149 if ( !xModLib
->hasByName( aModName
) )
152 aElement
<<= m_aScriptSource
;
153 xModLib
->insertByName( aModName
, aElement
);
158 // get dialog library container
159 Reference
< script::XLibraryContainer
> xDlgLibContainer
= pDocSh
->GetDialogContainer();
161 if ( xDlgLibContainer
.is() )
163 if ( !xDlgLibContainer
->hasByName( aLibName
) )
165 // create dialog library
166 xDlgLibContainer
->createLibrary( aLibName
);
171 m_aScriptSource
.clear();
172 m_aScriptType
.clear();
173 m_aScriptURL
.clear();
176 m_aBasicModule
.clear();
179 void SwHTMLParser::AddScriptSource()
181 // We'll just remember a few strings here
182 if( aToken
.getLength() > 2 &&
183 (HTMLScriptLanguage::StarBasic
==m_eScriptLang
&& aToken
[ 0 ] == '\'') )
186 if( m_aBasicLib
.isEmpty() )
188 nPos
= aToken
.indexOf( OOO_STRING_SVTOOLS_HTML_SB_library
);
192 aToken
.subView( nPos
+ sizeof(OOO_STRING_SVTOOLS_HTML_SB_library
) - 1 );
193 m_aBasicLib
= comphelper::string::strip(m_aBasicLib
, ' ');
197 if( m_aBasicModule
.isEmpty() && nPos
== -1 )
199 nPos
= aToken
.indexOf( OOO_STRING_SVTOOLS_HTML_SB_module
);
203 aToken
.subView( nPos
+ sizeof(OOO_STRING_SVTOOLS_HTML_SB_module
) - 1 );
204 m_aBasicModule
= comphelper::string::strip(m_aBasicModule
, ' ');
210 if( !m_aScriptSource
.isEmpty() )
211 m_aScriptSource
+= "\n";
212 m_aScriptSource
+= aToken
;
215 else if( !m_aScriptSource
.isEmpty() || !aToken
.isEmpty() )
217 // Empty lines are ignored on the beginning
218 if( !m_aScriptSource
.isEmpty() )
220 m_aScriptSource
+= "\n";
222 m_aScriptSource
+= aToken
;
226 void SwHTMLParser::InsertBasicDocEvent( const OUString
& aEvent
, const OUString
& rName
,
228 const OUString
& rScrType
)
230 OSL_ENSURE( !rName
.isEmpty(), "InsertBasicDocEvent() called without macro" );
231 if( rName
.isEmpty() )
234 SwDocShell
*pDocSh
= m_xDoc
->GetDocShell();
235 OSL_ENSURE( pDocSh
, "Where is the DocShell?" );
239 OUString
sEvent(convertLineEnd(rName
, GetSystemLineEnd()));
240 OUString sScriptType
;
241 if( EXTENDED_STYPE
== eScrType
)
242 sScriptType
= rScrType
;
244 SfxEventConfiguration::ConfigureEvent( aEvent
, SvxMacro( sEvent
, sScriptType
, eScrType
),
248 void SwHTMLWriter::OutBasic(const SwHTMLWriter
& rHTMLWrt
)
250 #if !HAVE_FEATURE_SCRIPTING
253 if( !m_bCfgStarBasic
)
256 BasicManager
*pBasicMan
= m_pDoc
->GetDocShell()->GetBasicManager();
257 OSL_ENSURE( pBasicMan
, "Where is the Basic-Manager?" );
258 // Only write DocumentBasic
259 if( !pBasicMan
|| pBasicMan
== SfxApplication::GetBasicManager() )
265 // Now write all StarBasic and unused Javascript modules
266 for( sal_uInt16 i
=0; i
<pBasicMan
->GetLibCount(); i
++ )
268 StarBASIC
*pBasic
= pBasicMan
->GetLib( i
);
269 const OUString
& rLibName
= pBasic
->GetName();
270 for( const auto& pModule
: pBasic
->GetModules() )
272 OUString
sLang(SVX_MACRO_LANGUAGE_STARBASIC
);
279 "<" + rHTMLWrt
.GetNamespace() + OOO_STRING_SVTOOLS_HTML_meta
280 " " OOO_STRING_SVTOOLS_HTML_O_httpequiv
282 OOO_STRING_SVTOOLS_HTML_META_content_script_type
283 "\" " OOO_STRING_SVTOOLS_HTML_O_content
285 Strm().WriteOString( sOut
);
286 // Entities aren't welcome here
287 Strm().WriteOString( OUStringToOString(sLang
, RTL_TEXTENCODING_UTF8
) )
288 .WriteOString( "\">" );
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
);
301 static const char* aEventNames
[] =
303 "OnLoad", "OnPrepareUnload", "OnFocus", "OnUnfocus"
306 void SwHTMLWriter::OutBasicBodyEvents()
308 SwDocShell
*pDocSh
= m_pDoc
->GetDocShell();
312 SvxMacroTableDtor aDocTable
;
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
++ )
318 std::unique_ptr
<SvxMacro
> pMacro
= SfxEventConfiguration::ConvertToMacro( xEvents
->getByName( OUString::createFromAscii(aEventNames
[i
]) ), pDocSh
);
321 aDocTable
.Insert( aBodyEventTable
[i
].nEvent
, *pMacro
);
325 if( !aDocTable
.empty() )
326 HTMLOutFuncs::Out_Events( Strm(), aDocTable
, aBodyEventTable
,
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */