Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/drawingml/hyperlinkcontext.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 "hyperlinkcontext.hxx"
21
22
#include <com/sun/star/xml/sax/XFastContextHandler.hpp>
23
24
#include <oox/helper/attributelist.hxx>
25
#include <oox/helper/propertymap.hxx>
26
#include <oox/core/relations.hxx>
27
#include <oox/core/xmlfilterbase.hxx>
28
#include <oox/token/namespaces.hxx>
29
#include <oox/token/properties.hxx>
30
#include <oox/token/tokens.hxx>
31
#include <o3tl/string_view.hxx>
32
#include <ooxresid.hxx>
33
#include <strings.hrc>
34
35
using namespace ::oox::core;
36
37
namespace oox::drawingml {
38
39
HyperLinkContext::HyperLinkContext( ContextHandler2Helper const & rParent,
40
        const AttributeList& rAttribs, PropertyMap& aProperties )
41
300
    : ContextHandler2( rParent )
42
300
    , maProperties(aProperties)
43
300
{
44
300
    OUString sURL, sHref;
45
300
    OUString aRelId = rAttribs.getStringDefaulted( R_TOKEN( id ) );
46
300
    if ( !aRelId.isEmpty() )
47
300
    {
48
300
        sHref = getRelations().getExternalTargetFromRelId( aRelId );
49
300
        if( !sHref.isEmpty() )
50
242
        {
51
242
            sURL = getFilter().getAbsoluteUrl( sHref );
52
242
        }
53
58
        else
54
58
        {
55
            // not sure if we also need to set sHref to the internal target
56
58
            sURL = getRelations().getInternalTargetFromRelId( aRelId );
57
58
        }
58
300
    }
59
300
    OUString sTooltip = rAttribs.getStringDefaulted( XML_tooltip );
60
300
    if ( !sTooltip.isEmpty() )
61
2
        maProperties.setProperty(PROP_Representation, sTooltip);
62
63
300
    OUString sFrame = rAttribs.getStringDefaulted( XML_tgtFrame );
64
300
    if( !sFrame.isEmpty() )
65
0
        maProperties.setProperty(PROP_TargetFrame, sFrame);
66
67
300
    OUString aAction = rAttribs.getStringDefaulted( XML_action );
68
300
    if ( !aAction.isEmpty() )
69
0
    {
70
        // reserved values of the unrestricted string aAction:
71
        // ppaction://customshow?id=SHOW_ID             // custom presentation
72
        // ppaction://hlinkfile                         // external file via r:id
73
        // ppaction://hlinkpres?slideindex=SLIDE_NUM    // external presentation via r:id
74
        // ppaction://hlinkshowjump?jump=endshow
75
        // ppaction://hlinkshowjump?jump=firstslide
76
        // ppaction://hlinkshowjump?jump=lastslide
77
        // ppaction://hlinkshowjump?jump=lastslideviewed
78
        // ppaction://hlinkshowjump?jump=nextslide
79
        // ppaction://hlinkshowjump?jump=previousslide
80
        // ppaction://hlinksldjump
81
        // ppaction://macro?name=MACRO_NAME
82
        // ppaction://program
83
84
0
        static constexpr OUString sPPAction( u"ppaction://"_ustr );
85
0
        if ( aAction.matchIgnoreAsciiCase( sPPAction ) )
86
0
        {
87
0
            OUString aPPAct( aAction.copy( sPPAction.getLength() ) );
88
0
            sal_Int32 nIndex = aPPAct.indexOf( '?' );
89
0
            OUString aPPAction( nIndex > 0 ? aPPAct.copy( 0, nIndex ) : aPPAct );
90
91
0
            if ( aPPAction.match( "hlinkshowjump" ) )
92
0
            {
93
0
                static constexpr OUString sJump( u"jump="_ustr );
94
0
                if ( aPPAct.match( sJump, nIndex + 1 ) )
95
0
                {
96
0
                    std::u16string_view aDestination( aPPAct.subView( nIndex + 1 + sJump.getLength() ) );
97
0
                    sURL += OUString::Concat("#action?jump=") + aDestination;
98
0
                }
99
0
            }
100
0
            else if ( aPPAction.match( "hlinksldjump" ) )
101
0
            {
102
0
                sHref = sURL;
103
104
0
                sal_Int32 nIndex2 = 0;
105
0
                while ( nIndex2 < sHref.getLength() )
106
0
                {
107
0
                    sal_Unicode nChar = sHref[ nIndex2 ];
108
0
                    if ( ( nChar >= '0' ) && ( nChar <= '9' ) )
109
0
                        break;
110
0
                    nIndex2++;
111
0
                }
112
0
                if ( nIndex2 && ( nIndex2 != sHref.getLength() ) )
113
0
                {
114
0
                    sal_Int32 nLength = 1;
115
0
                    while( ( nIndex2 + nLength ) < sHref.getLength() )
116
0
                    {
117
0
                        sal_Unicode nChar = sHref[ nIndex2 + nLength ];
118
0
                        if ( ( nChar < '0' ) || ( nChar > '9' ) )
119
0
                            break;
120
0
                        nLength++;
121
0
                    }
122
0
                    sal_Int32 nPageNumber = o3tl::toInt32(sHref.subView( nIndex2, nLength ));
123
0
                    if ( nPageNumber )
124
0
                    {
125
0
                        const OUString aSlideType( sHref.copy( 0, nIndex2 ) );
126
0
                        if ( aSlideType.match( "slide" ) )
127
0
                            sURL = "#" + URLResId(STR_SLIDE_NAME) + " " + OUString::number( nPageNumber );
128
0
                        else if ( aSlideType.match( "notesSlide" ) )
129
0
                            sURL = "#Notes " + OUString::number( nPageNumber );
130
//                      else: todo for other types such as notesMaster or slideMaster as they can't be referenced easily
131
0
                    }
132
0
                }
133
0
            }
134
0
        }
135
0
        maProperties.setProperty(PROP_Action, aAction);
136
0
    }
137
138
300
    if ( !sURL.isEmpty() )
139
244
        maProperties.setProperty(PROP_URL, sURL);
140
141
300
    OUString sInvalidUrl = rAttribs.getStringDefaulted(XML_invalidUrl);
142
300
    if (!sInvalidUrl.isEmpty())
143
0
        maProperties.setProperty(PROP_InvalidUrl, sInvalidUrl);
144
145
300
    bool bHistory = rAttribs.getBool(XML_history, true); // default="true"
146
300
    if (!bHistory) // set only if it is false
147
0
        maProperties.setProperty(PROP_History, bHistory);
148
149
300
    bool bHighlightClick = rAttribs.getBool(XML_highlightClick, false);
150
300
    if (bHighlightClick)
151
0
        maProperties.setProperty(PROP_HighlightClick, bHighlightClick);
152
153
300
    bool bEndSnd = rAttribs.getBool(XML_endSnd, false);
154
300
    if (bEndSnd)
155
0
        maProperties.setProperty(PROP_EndSnd, bEndSnd);
156
300
}
157
158
HyperLinkContext::~HyperLinkContext()
159
300
{
160
300
}
161
162
ContextHandlerRef HyperLinkContext::onCreateContext(
163
        ::sal_Int32 aElement, const AttributeList&  )
164
0
{
165
0
    switch( aElement )
166
0
    {
167
0
    case A_TOKEN( extLst ):
168
0
        maProperties.setProperty(PROP_CharColor, XML_fillcolor);
169
0
        break;
170
0
    case A_TOKEN( snd ):
171
        // TODO use getEmbeddedWAVAudioFile() here
172
0
        break;
173
0
    }
174
175
0
    return this;
176
0
}
177
178
} // namespace oox::drawingml
179
180
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */