1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.wikimodel.xhtml.handler

File AbstractFormatTagHandler.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

24
50
6
1
185
122
21
0.42
8.33
6
3.5

Classes

Class Line # Actions
AbstractFormatTagHandler 38 50 0% 21 6
0.92592.5%
 

Contributing tests

This file is covered by 351 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.wikimodel.xhtml.handler;
21   
22    import java.io.StringReader;
23   
24    import org.w3c.css.sac.InputSource;
25    import org.xwiki.rendering.wikimodel.WikiParameter;
26    import org.xwiki.rendering.wikimodel.WikiParameters;
27    import org.xwiki.rendering.wikimodel.WikiStyle;
28    import org.xwiki.rendering.wikimodel.xhtml.impl.TagContext;
29   
30    import com.steadystate.css.dom.CSSStyleDeclarationImpl;
31    import com.steadystate.css.parser.CSSOMParser;
32    import com.steadystate.css.parser.SACParserCSS21;
33   
34    /**
35    * @version $Id: 351692379675fcaf0a43a204a1747371b166b075 $
36    * @since 4.0M1
37    */
 
38    public abstract class AbstractFormatTagHandler extends TagHandler
39    {
40    public static final String FORMATWIKISTYLE = "formatWikiStyle";
41   
42    public static final String FORMATPARAMETERS = "formatParameters";
43   
44    public static final String FORMATSTYLEPARAMETER = "formatStyleParameter";
45   
46    private final WikiStyle style;
47   
48    /**
49    * The object used to parse the style attribute. Explicitly specify the
50    * parser to use, since otherwise cssparser overrides the default parser
51    * used in the JVM, breaking css4j.
52    */
53    private final CSSOMParser cssParser = new CSSOMParser(new SACParserCSS21());
54   
 
55  767 toggle public AbstractFormatTagHandler()
56    {
57  767 this(null);
58    }
59   
 
60  3798 toggle protected AbstractFormatTagHandler(WikiStyle style)
61    {
62  3798 super(true);
63   
64  3798 this.style = style;
65    }
66   
 
67  204 toggle @Override
68    protected void begin(TagContext context)
69    {
70    // parameters
71  204 WikiParameters currentParameters = (WikiParameters) context
72    .getTagStack().getStackParameter(FORMATPARAMETERS);
73  204 CSSStyleDeclarationImpl currentStyle = (CSSStyleDeclarationImpl) context
74    .getTagStack().getStackParameter(FORMATSTYLEPARAMETER);
75   
76  204 if (currentParameters != null) {
77  19 for (WikiParameter parameter : context.getParams()) {
78  13 WikiParameter currentParameter = currentParameters
79    .getParameter(parameter.getKey());
80   
81  13 String value = parameter.getValue();
82   
83  13 if (currentParameter != null) {
84  5 if ("style".equals(parameter.getKey())) {
85  3 CSSStyleDeclarationImpl mergedStyle = mergeStyle(
86    currentStyle, currentParameter.getValue(),
87    parameter.getValue());
88   
89  3 if (mergedStyle != currentStyle) {
90  3 value = mergedStyle.getCssText();
91  3 currentStyle = mergedStyle;
92    }
93  2 } else if ("class".equals(parameter.getKey())) {
94  2 value = mergeClass(currentParameter.getValue(),
95    parameter.getValue());
96    }
97    }
98   
99  13 currentParameters = currentParameters.setParameter(
100    parameter.getKey(), value);
101    }
102    } else {
103  185 currentParameters = new WikiParameters(context.getParams());
104    }
105  204 context.getTagStack().pushStackParameter(FORMATPARAMETERS,
106    currentParameters);
107  204 context.getTagStack().pushStackParameter(FORMATSTYLEPARAMETER,
108    currentStyle);
109   
110  204 if (currentParameters.getSize() > 0) {
111  59 context.getScannerContext().beginFormat(currentParameters);
112    }
113   
114    // style
115  204 if (this.style != null) {
116  110 context.getScannerContext().beginFormat(this.style);
117  110 context.getTagStack().pushStackParameter(FORMATWIKISTYLE,
118    this.style);
119    }
120    }
121   
 
122  3 toggle private CSSStyleDeclarationImpl mergeStyle(
123    CSSStyleDeclarationImpl parentStyle, String parentStyleValue,
124    String styleValue)
125    {
126  3 CSSStyleDeclarationImpl currentStyle = new CSSStyleDeclarationImpl();
127   
128  3 if (parentStyle == null) {
129  3 try {
130  3 this.cssParser.parseStyleDeclaration(currentStyle,
131    new InputSource(new StringReader(parentStyleValue)));
132    } catch (Exception e) {
133  0 return parentStyle;
134    }
135    } else {
136  0 currentStyle.setProperties(parentStyle.getProperties());
137    }
138   
139  3 try {
140  3 this.cssParser.parseStyleDeclaration(currentStyle, new InputSource(
141    new StringReader(styleValue)));
142    } catch (Exception e) {
143  0 return parentStyle;
144    }
145   
146  3 return currentStyle;
147    }
148   
 
149  2 toggle private String mergeClass(String value1, String value2)
150    {
151  2 return value1 + " " + value2;
152    }
153   
 
154  204 toggle @Override
155    protected void end(TagContext context)
156    {
157    // style
158  204 if (this.style != null) {
159  110 context.getScannerContext().endFormat(this.style);
160  110 context.getTagStack().popStackParameter(FORMATWIKISTYLE);
161    }
162   
163    // parameters
164  204 if (context.getParams().getSize() > 0) {
165  59 context.getScannerContext().endFormat(WikiParameters.EMPTY);
166    }
167   
168  204 context.getTagStack().popStackParameter(FORMATPARAMETERS);
169  204 context.getTagStack().popStackParameter(FORMATSTYLEPARAMETER);
170   
171    // reopen
172   
173  204 WikiStyle currentWikiStyle = (WikiStyle) context.getTagStack()
174    .getStackParameter(FORMATWIKISTYLE);
175  204 WikiParameters currentParameters = (WikiParameters) context
176    .getTagStack().getStackParameter(FORMATPARAMETERS);
177   
178  204 if (currentParameters != null && currentParameters.getSize() > 0) {
179  10 context.getScannerContext().beginFormat(currentParameters);
180  10 if (currentWikiStyle != null) {
181  1 context.getScannerContext().beginFormat(currentWikiStyle);
182    }
183    }
184    }
185    }