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 <svgstylenode.hxx>
21 #include <svgdocument.hxx>
22 #include <o3tl/string_view.hxx>
23 #include <osl/diagnose.h>
25 namespace svgio::svgreader
27 SvgStyleNode::SvgStyleNode(
28 SvgDocument
& rDocument
,
30 : SvgNode(SVGToken::Style
, rDocument
, pParent
),
35 // #i125258# no parent when we are a CssStyle holder to break potential loops because
36 // when using CssStyles we jump uncontrolled inside the node tree hierarchy
37 bool SvgStyleNode::supportsParentStyle() const
45 return SvgNode::supportsParentStyle();
48 void SvgStyleNode::parseAttribute(SVGToken aSVGToken
, const OUString
& aContent
)
51 SvgNode::parseAttribute(aSVGToken
, aContent
);
58 if(!aContent
.isEmpty())
60 if(!o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent
), u
"text/css"))
74 void SvgStyleNode::addCssStyleSheet(std::u16string_view aSelectors
, const SvgStyleAttributes
& rNewStyle
)
76 // aSelectors: CssStyle selectors, any combination, no comma separations, no spaces at start/end
77 // rNewStyle: the already prepared style to register on that name
78 SvgStringVector aSelectorParts
;
80 if(!readSvgStringVector(aSelectors
, aSelectorParts
, ' '))
83 OUStringBuffer aConcatenatedSelector
;
85 // re-combine without spaces, create a unique name (for now)
86 for(const auto &a
: aSelectorParts
)
88 aConcatenatedSelector
.append(a
);
91 // CssStyles in SVG are currently not completely supported; the current idea for
92 // supporting the needed minimal set is to register CssStyles associated to a string
93 // which is just the space-char cleaned, concatenated Selectors. The part to 'match'
94 // these is in fillCssStyleVectorUsingHierarchyAndSelectors. There, the same string is
95 // built up using the priorities of local CssStyle, Id, Class and other info combined
96 // with the existing hierarchy. This creates a specificity and priority-sorted local
97 // list for each node which is then chained using get/setCssStyle.
98 // The current solution is capable of solving space-separated selectors which can be
99 // mixed between Id, Class and type specifiers.
100 // When CssStyles need more specific solving, the start point is here; remember the
101 // needed infos not in maIdStyleTokenMapperList at the document, but select evtl.
102 // more specific infos there in a class capable of handling more complex matchings.
103 // Additionally fillCssStyleVector (or the mechanism above that when a linked list of
104 // SvgStyleAttributes will not do it) will have to be adapted to make use of it.
106 // register new style at document for (evtl. concatenated) stylename
107 const_cast< SvgDocument
& >(getDocument()).addSvgStyleAttributesToMapper(aConcatenatedSelector
.makeStringAndClear(), rNewStyle
);
110 void SvgStyleNode::addCssStyleSheet(std::u16string_view aSelectors
, std::u16string_view aContent
)
112 // aSelectors: possible comma-separated list of CssStyle definitions, no spaces at start/end
113 // aContent: the svg style definitions as string
114 if(aSelectors
.empty() || aContent
.empty())
117 // comma-separated split (Css abbreviation for same style for multiple selectors)
118 const sal_Int32
nLen(aSelectors
.size());
120 OUStringBuffer aToken
;
124 const sal_Int32
nInitPos(nPos
);
125 copyToLimiter(aSelectors
, u
',', nPos
, aToken
, nLen
);
126 skip_char(aSelectors
, u
' ', u
',', nPos
, nLen
);
128 const OUString
aSingleName(o3tl::trim(aToken
));
131 // add the current css class only if wasn't previously added
132 auto [aIterator
, bIsNew
] = maSvgStyleAttributes
.try_emplace(aSingleName
);
135 // create new style and add to local list (for ownership control) and
136 // in case it's written to again in future classes to prevent overwrites
137 aIterator
->second
= std::make_unique
<SvgStyleAttributes
>(*this);
139 const std::unique_ptr
<SvgStyleAttributes
>& pCurrentStyle
= aIterator
->second
;
142 pCurrentStyle
->readCssStyle(aContent
);
144 if(aSingleName
.getLength())
146 addCssStyleSheet(aSingleName
, *pCurrentStyle
);
151 OSL_ENSURE(false, "Could not interpret on current position (!)");
157 void SvgStyleNode::addCssStyleSheet(std::u16string_view aSelectorsAndContent
)
159 const sal_Int32
nLen(aSelectorsAndContent
.size());
165 OUStringBuffer aToken
;
169 // read the full selectors (may be multiple, comma-separated)
170 const sal_Int32
nInitPos(nPos
);
171 skip_char(aSelectorsAndContent
, u
' ', nPos
, nLen
);
172 copyToLimiter(aSelectorsAndContent
, u
'{', nPos
, aToken
, nLen
);
173 skip_char(aSelectorsAndContent
, u
' ', u
'{', nPos
, nLen
);
175 const OUString
aSelectors(o3tl::trim(aToken
));
179 if(!aSelectors
.isEmpty() && nPos
< nLen
)
181 // isolate content as text, embraced by '{' and '}'
182 copyToLimiter(aSelectorsAndContent
, u
'}', nPos
, aToken
, nLen
);
183 skip_char(aSelectorsAndContent
, u
' ', u
'}', nPos
, nLen
);
185 aContent
= o3tl::trim(aToken
);
189 if(!aSelectors
.isEmpty() && !aContent
.isEmpty())
191 addCssStyleSheet(aSelectors
, aContent
);
196 OSL_ENSURE(false, "Could not interpret on current position (!)");
202 } // end of namespace svgio::svgreader
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */