Search in sources :

Example 1 with HTMLCleaner

use of org.xwiki.wysiwyg.cleaner.HTMLCleaner in project xwiki-platform by xwiki.

the class DefaultHTMLConverterTest method parseAndRender.

/**
 * Unit test for {@link DefaultHTMLConverter#parseAndRender(String, String)}.
 */
@Test
public void parseAndRender() throws Exception {
    String html = "some HTML";
    String syntaxId = "syntax/x.y";
    // Verify the HTML is cleaned.
    HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
    when(cleaner.clean(html)).thenReturn(html);
    // Verify the HTML is parsed into XDOM.
    XDOM xdom = new XDOM(Collections.<Block>emptyList());
    Parser xhtmlParser = mocker.getInstance(Parser.class, "xhtml/1.0");
    when(xhtmlParser.parse(any(StringReader.class))).thenReturn(xdom);
    Assert.assertEquals("", mocker.getComponentUnderTest().parseAndRender(html, syntaxId));
    // Verify that the macro transformations have been executed.
    Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro");
    RenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
    // It's very important to verify that a transformation context id is set as otherwise if the content being
    // edited has different velocity macros executing, they'll be executed in isolation and thus what's defined in
    // one won't be visible from the other ones (For example see https://jira.xwiki.org/browse/XWIKI-11695).
    ArgumentCaptor<TransformationContext> txContextArgument = ArgumentCaptor.forClass(TransformationContext.class);
    verify((MutableRenderingContext) renderingContext).transformInContext(same(macroTransformation), txContextArgument.capture(), same(xdom));
    assertEquals("wysiwygtxid", txContextArgument.getValue().getId());
    // Verify the XDOM is rendered to Annotated XHTML.
    BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
    verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class));
    // Verify that the syntax meta data has been set.
    Assert.assertEquals(Syntax.valueOf("syntax/x.y"), xdom.getMetaData().getMetaData(MetaData.SYNTAX));
}
Also used : RenderingContext(org.xwiki.rendering.transformation.RenderingContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) HTMLCleaner(org.xwiki.wysiwyg.cleaner.HTMLCleaner) Parser(org.xwiki.rendering.parser.Parser) StreamParser(org.xwiki.rendering.parser.StreamParser) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 2 with HTMLCleaner

use of org.xwiki.wysiwyg.cleaner.HTMLCleaner in project xwiki-platform by xwiki.

the class DefaultHTMLConverterTest method fromHTML.

/**
 * Unit test for {@link DefaultHTMLConverter#fromHTML(String, String)}.
 */
@Test
public void fromHTML() throws Exception {
    String html = "some HTML";
    String syntaxId = "syntax/x.y";
    // Verify the HTML is cleaned.
    HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
    when(cleaner.clean(html)).thenReturn(html);
    PrintRendererFactory printRendererFactory = this.mocker.registerMockComponent(PrintRendererFactory.class, syntaxId);
    PrintRenderer printRenderer = mock(PrintRenderer.class);
    when(printRendererFactory.createRenderer(any(WikiPrinter.class))).thenReturn(printRenderer);
    Assert.assertEquals("", mocker.getComponentUnderTest().fromHTML(html, syntaxId));
    // Verify the HTML is converted to the specified syntax.
    StreamParser xhtmlStreamParser = mocker.getInstance(StreamParser.class, "xhtml/1.0");
    verify(xhtmlStreamParser).parse(any(StringReader.class), same(printRenderer));
}
Also used : PrintRenderer(org.xwiki.rendering.renderer.PrintRenderer) StreamParser(org.xwiki.rendering.parser.StreamParser) StringReader(java.io.StringReader) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) HTMLCleaner(org.xwiki.wysiwyg.cleaner.HTMLCleaner) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)2 Test (org.junit.Test)2 StreamParser (org.xwiki.rendering.parser.StreamParser)2 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)2 HTMLCleaner (org.xwiki.wysiwyg.cleaner.HTMLCleaner)2 XDOM (org.xwiki.rendering.block.XDOM)1 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)1 Parser (org.xwiki.rendering.parser.Parser)1 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)1 PrintRenderer (org.xwiki.rendering.renderer.PrintRenderer)1 PrintRendererFactory (org.xwiki.rendering.renderer.PrintRendererFactory)1 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)1 Transformation (org.xwiki.rendering.transformation.Transformation)1 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)1