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 <osl/diagnose.h>
24 namespace svgio::svgreader
26 SvgStyleNode::SvgStyleNode(
27 SvgDocument
& rDocument
,
29 : SvgNode(SVGToken::Style
, rDocument
, pParent
),
30 maSvgStyleAttributes(),
35 SvgStyleNode::~SvgStyleNode()
37 while(!maSvgStyleAttributes
.empty())
39 delete *(maSvgStyleAttributes
.end() - 1);
40 maSvgStyleAttributes
.pop_back();
44 // #i125258# no parent when we are a CssStyle holder to break potential loops because
45 // when using CssStyles we jump uncontrolled inside the node tree hierarchy
46 bool SvgStyleNode::supportsParentStyle() const
54 return SvgNode::supportsParentStyle();
57 void SvgStyleNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
60 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
67 if(!aContent
.isEmpty())
69 if(aContent
.startsWith("text/css"))
83 void SvgStyleNode::addCssStyleSheet(const OUString
& aSelectors
, const SvgStyleAttributes
& rNewStyle
)
85 // aSelectors: CssStyle selectors, any combination, no comma separations, no spaces at start/end
86 // rNewStyle: the already prepared style to register on that name
87 if(aSelectors
.isEmpty())
90 std::vector
< OUString
> aSelectorParts
;
91 const sal_Int32
nLen(aSelectors
.getLength());
93 OUStringBuffer aToken
;
95 // split into single tokens (currently only space separator)
98 const sal_Int32
nInitPos(nPos
);
99 copyToLimiter(aSelectors
, u
' ', nPos
, aToken
, nLen
);
100 skip_char(aSelectors
, u
' ', nPos
, nLen
);
101 const OUString
aSelectorPart(aToken
.makeStringAndClear().trim());
103 if(!aSelectorPart
.isEmpty())
105 aSelectorParts
.push_back(aSelectorPart
);
110 OSL_ENSURE(false, "Could not interpret on current position (!)");
115 if(aSelectorParts
.empty())
118 OUStringBuffer aConcatenatedSelector
;
120 // re-combine without spaces, create a unique name (for now)
121 for(size_t a(0); a
< aSelectorParts
.size(); a
++)
123 aConcatenatedSelector
.append(aSelectorParts
[a
]);
126 // CssStyles in SVG are currently not completely supported; the current idea for
127 // supporting the needed minimal set is to register CssStyles associated to a string
128 // which is just the space-char cleaned, concatenated Selectors. The part to 'match'
129 // these is in fillCssStyleVectorUsingHierarchyAndSelectors. There, the same string is
130 // built up using the priorities of local CssStyle, Id, Class and other info combined
131 // with the existing hierarchy. This creates a specificity and priority-sorted local
132 // list for each node which is then chained using get/setCssStyleParent.
133 // The current solution is capable of solving space-separated selectors which can be
134 // mixed between Id, Class and type specifiers.
135 // When CssStyles need more specific solving, the start point is here; remember the
136 // needed infos not in maIdStyleTokenMapperList at the document, but select evtl.
137 // more specific infos there in a class capable of handling more complex matchings.
138 // Additionally fillCssStyleVector (or the mechanism above that when a linked list of
139 // SvgStyleAttributes will not do it) will have to be adapted to make use of it.
141 // register new style at document for (evtl. concatenated) stylename
142 const_cast< SvgDocument
& >(getDocument()).addSvgStyleAttributesToMapper(aConcatenatedSelector
.makeStringAndClear(), rNewStyle
);
145 void SvgStyleNode::addCssStyleSheet(const OUString
& aSelectors
, const OUString
& aContent
)
147 // aSelectors: possible comma-separated list of CssStyle definitions, no spaces at start/end
148 // aContent: the svg style definitions as string
149 if(aSelectors
.isEmpty() || aContent
.isEmpty())
152 // create new style and add to local list (for ownership control)
153 SvgStyleAttributes
* pNewStyle
= new SvgStyleAttributes(*this);
154 maSvgStyleAttributes
.push_back(pNewStyle
);
157 pNewStyle
->readCssStyle(aContent
);
159 // comma-separated split (Css abbreviation for same style for multiple selectors)
160 const sal_Int32
nLen(aSelectors
.getLength());
162 OUStringBuffer aToken
;
166 const sal_Int32
nInitPos(nPos
);
167 copyToLimiter(aSelectors
, u
',', nPos
, aToken
, nLen
);
168 skip_char(aSelectors
, u
' ', u
',', nPos
, nLen
);
170 const OUString
aSingleName(aToken
.makeStringAndClear().trim());
172 if(aSingleName
.getLength())
174 addCssStyleSheet(aSingleName
, *pNewStyle
);
179 OSL_ENSURE(false, "Could not interpret on current position (!)");
185 void SvgStyleNode::addCssStyleSheet(const OUString
& aSelectorsAndContent
)
187 const sal_Int32
nLen(aSelectorsAndContent
.getLength());
193 OUStringBuffer aToken
;
197 // read the full selectors (may be multiple, comma-separated)
198 const sal_Int32
nInitPos(nPos
);
199 skip_char(aSelectorsAndContent
, u
' ', nPos
, nLen
);
200 copyToLimiter(aSelectorsAndContent
, u
'{', nPos
, aToken
, nLen
);
201 skip_char(aSelectorsAndContent
, u
' ', u
'{', nPos
, nLen
);
203 const OUString
aSelectors(aToken
.makeStringAndClear().trim());
206 if(!aSelectors
.isEmpty() && nPos
< nLen
)
208 // isolate content as text, embraced by '{' and '}'
209 copyToLimiter(aSelectorsAndContent
, u
'}', nPos
, aToken
, nLen
);
210 skip_char(aSelectorsAndContent
, u
' ', u
'}', nPos
, nLen
);
212 aContent
= aToken
.makeStringAndClear().trim();
215 if(!aSelectors
.isEmpty() && !aContent
.isEmpty())
217 addCssStyleSheet(aSelectors
, aContent
);
222 OSL_ENSURE(false, "Could not interpret on current position (!)");
228 } // end of namespace svgio::svgreader
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */