xmlsecurity: fix --without-system-nss build
[LibreOffice.git] / svgio / source / svgreader / svgstylenode.cxx
blob322cdce2e4ffd65b63654257fa2ec8071e95b961
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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,
29 SvgNode* pParent)
30 : SvgNode(SVGToken::Style, rDocument, pParent),
31 mbTextCss(true)
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
39 if(isTextCss())
41 return false;
44 // call parent
45 return SvgNode::supportsParentStyle();
48 void SvgStyleNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent)
50 // call parent
51 SvgNode::parseAttribute(aSVGToken, aContent);
53 // parse own
54 switch(aSVGToken)
56 case SVGToken::Type:
58 if(!aContent.isEmpty())
60 if(!o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent), u"text/css"))
62 setTextCss(false);
65 break;
67 default:
69 break;
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, ' '))
81 return;
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())
115 return;
117 // comma-separated split (Css abbreviation for same style for multiple selectors)
118 const sal_Int32 nLen(aSelectors.size());
119 sal_Int32 nPos(0);
120 OUStringBuffer aToken;
122 while(nPos < nLen)
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));
129 aToken.setLength(0);
131 // add the current css class only if wasn't previously added
132 auto [aIterator, bIsNew] = maSvgStyleAttributes.try_emplace(aSingleName);
133 if (bIsNew)
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;
141 // fill with content
142 pCurrentStyle->readCssStyle(aContent);
144 if(aSingleName.getLength())
146 addCssStyleSheet(aSingleName, *pCurrentStyle);
149 if(nInitPos == nPos)
151 OSL_ENSURE(false, "Could not interpret on current position (!)");
152 nPos++;
157 void SvgStyleNode::addCssStyleSheet(std::u16string_view aSelectorsAndContent)
159 const sal_Int32 nLen(aSelectorsAndContent.size());
161 if(!nLen)
162 return;
164 sal_Int32 nPos(0);
165 OUStringBuffer aToken;
167 while(nPos < nLen)
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));
176 aToken.setLength(0);
177 OUString aContent;
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);
186 aToken.setLength(0);
189 if(!aSelectors.isEmpty() && !aContent.isEmpty())
191 addCssStyleSheet(aSelectors, aContent);
194 if(nInitPos == nPos)
196 OSL_ENSURE(false, "Could not interpret on current position (!)");
197 nPos++;
202 } // end of namespace svgio::svgreader
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */