| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.rendering.internal.macro.html; |
| 21 |
|
|
| 22 |
|
import java.io.StringReader; |
| 23 |
|
import java.util.Arrays; |
| 24 |
|
import java.util.Collections; |
| 25 |
|
import java.util.HashMap; |
| 26 |
|
import java.util.List; |
| 27 |
|
import java.util.Map; |
| 28 |
|
|
| 29 |
|
import javax.inject.Inject; |
| 30 |
|
import javax.inject.Named; |
| 31 |
|
import javax.inject.Singleton; |
| 32 |
|
|
| 33 |
|
import org.apache.commons.lang3.StringUtils; |
| 34 |
|
import org.w3c.dom.Document; |
| 35 |
|
import org.w3c.dom.Element; |
| 36 |
|
import org.w3c.dom.Node; |
| 37 |
|
import org.xwiki.component.annotation.Component; |
| 38 |
|
import org.xwiki.rendering.block.Block; |
| 39 |
|
import org.xwiki.rendering.block.Block.Axes; |
| 40 |
|
import org.xwiki.rendering.block.MacroBlock; |
| 41 |
|
import org.xwiki.rendering.block.MacroMarkerBlock; |
| 42 |
|
import org.xwiki.rendering.block.RawBlock; |
| 43 |
|
import org.xwiki.rendering.block.XDOM; |
| 44 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
| 45 |
|
import org.xwiki.rendering.internal.transformation.MutableRenderingContext; |
| 46 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
| 47 |
|
import org.xwiki.rendering.macro.MacroContentParser; |
| 48 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
| 49 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
| 50 |
|
import org.xwiki.rendering.macro.html.HTMLMacroParameters; |
| 51 |
|
import org.xwiki.rendering.renderer.PrintRenderer; |
| 52 |
|
import org.xwiki.rendering.renderer.PrintRendererFactory; |
| 53 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
| 54 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
| 55 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 56 |
|
import org.xwiki.rendering.syntax.SyntaxType; |
| 57 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
| 58 |
|
import org.xwiki.rendering.transformation.RenderingContext; |
| 59 |
|
import org.xwiki.rendering.transformation.Transformation; |
| 60 |
|
import org.xwiki.xml.html.HTMLCleaner; |
| 61 |
|
import org.xwiki.xml.html.HTMLCleanerConfiguration; |
| 62 |
|
import org.xwiki.xml.html.HTMLConstants; |
| 63 |
|
import org.xwiki.xml.html.HTMLUtils; |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
@version |
| 71 |
|
@since |
| 72 |
|
|
| 73 |
|
@Component |
| 74 |
|
@Named("html") |
| 75 |
|
@Singleton |
| |
|
| 96.1% |
Uncovered Elements: 3 (76) |
Complexity: 17 |
Complexity Density: 0.31 |
|
| 76 |
|
public class HTMLMacro extends AbstractMacro<HTMLMacroParameters> |
| 77 |
|
{ |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
private static final String DESCRIPTION = "Inserts HTML or XHTML code into the page."; |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
private static final String CONTENT_DESCRIPTION = "The HTML content to insert in the page."; |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
private static final Syntax XHTML_SYNTAX = new Syntax(SyntaxType.XHTML, "1.0"); |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
private static final ClassBlockMatcher MACROBLOCKMATCHER = new ClassBlockMatcher(MacroBlock.class); |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
@Inject |
| 102 |
|
private HTMLCleaner htmlCleaner; |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
@Inject |
| 110 |
|
@Named("xhtmlmacro/1.0") |
| 111 |
|
private PrintRendererFactory xhtmlRendererFactory; |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
@Inject |
| 117 |
|
private MacroContentParser contentParser; |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
@Inject |
| 123 |
|
private RenderingContext renderingContext; |
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 128 |
50 |
public HTMLMacro()... |
| 129 |
|
{ |
| 130 |
50 |
super("HTML", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), HTMLMacroParameters.class); |
| 131 |
50 |
setDefaultCategory(DEFAULT_CATEGORY_DEVELOPMENT); |
| 132 |
|
} |
| 133 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 134 |
4181 |
@Override... |
| 135 |
|
public boolean supportsInlineMode() |
| 136 |
|
{ |
| 137 |
4181 |
return true; |
| 138 |
|
} |
| 139 |
|
|
| |
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
| 140 |
6687 |
@Override... |
| 141 |
|
public List<Block> execute(HTMLMacroParameters parameters, String content, MacroTransformationContext context) |
| 142 |
|
throws MacroExecutionException |
| 143 |
|
{ |
| 144 |
6687 |
List<Block> blocks; |
| 145 |
|
|
| 146 |
6687 |
if (!StringUtils.isEmpty(content)) { |
| 147 |
|
|
| 148 |
6561 |
String normalizedContent = content; |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
6561 |
if (parameters.getWiki()) { |
| 154 |
165 |
normalizedContent = renderWikiSyntax(normalizedContent, context.getTransformation(), context); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
|
| 158 |
6561 |
if (parameters.getClean()) { |
| 159 |
4657 |
normalizedContent = cleanHTML(normalizedContent, context); |
| 160 |
1904 |
} else if (context.getTransformationContext().isRestricted()) { |
| 161 |
0 |
throw new MacroExecutionException( |
| 162 |
|
"The HTML macro may not be used with clean=\"false\" in this context."); |
| 163 |
|
} |
| 164 |
|
|
| 165 |
6560 |
blocks = Arrays.asList((Block) new RawBlock(normalizedContent, XHTML_SYNTAX)); |
| 166 |
|
} else { |
| 167 |
126 |
blocks = Collections.emptyList(); |
| 168 |
|
} |
| 169 |
|
|
| 170 |
6686 |
return blocks; |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
@param |
| 177 |
|
@param |
| 178 |
|
@return |
| 179 |
|
@throws |
| 180 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
| 181 |
4657 |
private String cleanHTML(String content, MacroTransformationContext context) throws MacroExecutionException... |
| 182 |
|
{ |
| 183 |
4657 |
String cleanedContent = content; |
| 184 |
|
|
| 185 |
4657 |
HTMLCleanerConfiguration cleanerConfiguration = getCleanerConfiguration(context); |
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
4657 |
Document document = this.htmlCleaner.clean(new StringReader(cleanedContent), cleanerConfiguration); |
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
4657 |
HTMLUtils.stripHTMLEnvelope(document); |
| 204 |
|
|
| 205 |
|
|
| 206 |
4657 |
if (context.isInline()) { |
| 207 |
|
|
| 208 |
3981 |
Element root = document.getDocumentElement(); |
| 209 |
3981 |
if (root.getChildNodes().getLength() == 1 && root.getFirstChild().getNodeType() == Node.ELEMENT_NODE |
| 210 |
|
&& root.getFirstChild().getNodeName().equalsIgnoreCase("p")) { |
| 211 |
3980 |
HTMLUtils.stripFirstElementInside(document, HTMLConstants.TAG_HTML, HTMLConstants.TAG_P); |
| 212 |
|
} else { |
| 213 |
1 |
throw new MacroExecutionException( |
| 214 |
|
"When using the HTML macro inline, you can only use inline HTML content." |
| 215 |
|
+ " Block HTML content (such as tables) cannot be displayed." |
| 216 |
|
+ " Try leaving an empty line before and after the HTML macro."); |
| 217 |
|
} |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
|
| 221 |
4656 |
cleanedContent = HTMLUtils.toString(document, true, true); |
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
4656 |
cleanedContent = cleanedContent.substring(7, cleanedContent.length() - 8); |
| 229 |
|
|
| 230 |
4656 |
return cleanedContent; |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
@param |
| 237 |
|
@param |
| 238 |
|
@param |
| 239 |
|
@return |
| 240 |
|
@throws |
| 241 |
|
|
| |
|
| 95.5% |
Uncovered Elements: 1 (22) |
Complexity: 3 |
Complexity Density: 0.15 |
|
| 242 |
165 |
private String renderWikiSyntax(String content, Transformation transformation, MacroTransformationContext context)... |
| 243 |
|
throws MacroExecutionException |
| 244 |
|
{ |
| 245 |
165 |
String xhtml; |
| 246 |
|
|
| 247 |
165 |
try { |
| 248 |
|
|
| 249 |
165 |
XDOM xdom = this.contentParser.parse(content, context, false, false); |
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
|
|
| 255 |
|
|
| 256 |
165 |
List<MacroBlock> macros = xdom.getBlocks(MACROBLOCKMATCHER, Axes.DESCENDANT); |
| 257 |
165 |
for (MacroBlock macro : macros) { |
| 258 |
202 |
if (macro.getId().equals("html")) { |
| 259 |
139 |
macro.setParameter("clean", "false"); |
| 260 |
|
} |
| 261 |
|
} |
| 262 |
|
|
| 263 |
165 |
MacroBlock htmlMacroBlock = context.getCurrentMacroBlock(); |
| 264 |
|
|
| 265 |
165 |
MacroMarkerBlock htmlMacroMarker = |
| 266 |
|
new MacroMarkerBlock(htmlMacroBlock.getId(), htmlMacroBlock.getParameters(), |
| 267 |
|
htmlMacroBlock.getContent(), xdom.getChildren(), htmlMacroBlock.isInline()); |
| 268 |
|
|
| 269 |
|
|
| 270 |
165 |
htmlMacroBlock.getParent().replaceChild(htmlMacroMarker, htmlMacroBlock); |
| 271 |
|
|
| 272 |
165 |
try { |
| 273 |
|
|
| 274 |
165 |
((MutableRenderingContext) this.renderingContext).transformInContext(transformation, |
| 275 |
|
context.getTransformationContext(), htmlMacroMarker); |
| 276 |
|
} finally { |
| 277 |
|
|
| 278 |
165 |
htmlMacroMarker.getParent().replaceChild(htmlMacroBlock, htmlMacroMarker); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
|
| 282 |
165 |
WikiPrinter printer = new DefaultWikiPrinter(); |
| 283 |
165 |
PrintRenderer renderer = this.xhtmlRendererFactory.createRenderer(printer); |
| 284 |
165 |
for (Block block : htmlMacroMarker.getChildren()) { |
| 285 |
309 |
block.traverse(renderer); |
| 286 |
|
} |
| 287 |
|
|
| 288 |
165 |
xhtml = printer.toString(); |
| 289 |
|
|
| 290 |
|
} catch (Exception e) { |
| 291 |
0 |
throw new MacroExecutionException("Failed to parse content [" + content + "].", e); |
| 292 |
|
} |
| 293 |
|
|
| 294 |
165 |
return xhtml; |
| 295 |
|
} |
| 296 |
|
|
| 297 |
|
|
| 298 |
|
@param |
| 299 |
|
@return |
| 300 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 301 |
4657 |
private HTMLCleanerConfiguration getCleanerConfiguration(MacroTransformationContext context)... |
| 302 |
|
{ |
| 303 |
4657 |
HTMLCleanerConfiguration cleanerConfiguration = this.htmlCleaner.getDefaultConfiguration(); |
| 304 |
|
|
| 305 |
4657 |
if (context.getTransformationContext().isRestricted()) { |
| 306 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
| 307 |
1 |
parameters.putAll(cleanerConfiguration.getParameters()); |
| 308 |
1 |
parameters.put(HTMLCleanerConfiguration.RESTRICTED, "true"); |
| 309 |
1 |
cleanerConfiguration.setParameters(parameters); |
| 310 |
|
} |
| 311 |
|
|
| 312 |
4657 |
return cleanerConfiguration; |
| 313 |
|
} |
| 314 |
|
} |