Skip to content

Commit 2178414

Browse files
committed
XRENDERING-799: Macro inside link isn't parsed as inline macro (#360)
* Transform macros to inline in InlineFilterListener. * Add a test for InlineFilterListener. * Add an integration test for the XWiki 2.0 parser. (cherry picked from commit 4c7e0f4)
1 parent ebc77fa commit 2178414

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

xwiki-rendering-api/src/main/java/org/xwiki/rendering/listener/InlineFilterListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,11 @@ public void endParagraph(Map<String, String> parameters)
6464
{
6565
// Disable this event
6666
}
67+
68+
@Override
69+
public void onMacro(String id, Map<String, String> parameters, String content, boolean inline)
70+
{
71+
// Force inline to true.
72+
super.onMacro(id, parameters, content, true);
73+
}
6774
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
21+
package org.xwiki.rendering.listener;
22+
23+
import java.util.List;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.xwiki.rendering.block.Block;
27+
import org.xwiki.rendering.block.MacroBlock;
28+
import org.xwiki.rendering.block.SpaceBlock;
29+
import org.xwiki.rendering.block.WordBlock;
30+
import org.xwiki.rendering.internal.parser.XDOMGeneratorListener;
31+
32+
import static org.junit.jupiter.api.Assertions.*;
33+
34+
/**
35+
* Unit tests for {@link InlineFilterListener}.
36+
*
37+
* @version $Id$
38+
*/
39+
class InlineFilterListenerTest
40+
{
41+
@Test
42+
void beginParagraph()
43+
{
44+
InlineFilterListener listener = new InlineFilterListener();
45+
XDOMGeneratorListener xdomGeneratorListener = new XDOMGeneratorListener();
46+
listener.setWrappedListener(xdomGeneratorListener);
47+
48+
listener.beginDocument(new MetaData());
49+
listener.beginParagraph(Listener.EMPTY_PARAMETERS);
50+
listener.onWord("This");
51+
listener.onSpace();
52+
listener.onWord("is");
53+
listener.onSpace();
54+
listener.onWord("inline");
55+
listener.endParagraph(Listener.EMPTY_PARAMETERS);
56+
listener.endDocument(new MetaData());
57+
58+
List<Block> children = xdomGeneratorListener.getXDOM().getChildren();
59+
List<Block> expectedChildren = List.of(new WordBlock("This"), new SpaceBlock(), new WordBlock("is"),
60+
new SpaceBlock(), new WordBlock("inline"));
61+
assertEquals(expectedChildren, children);
62+
}
63+
64+
@Test
65+
void onMacro()
66+
{
67+
InlineFilterListener listener = new InlineFilterListener();
68+
XDOMGeneratorListener xdomGeneratorListener = new XDOMGeneratorListener();
69+
listener.setWrappedListener(xdomGeneratorListener);
70+
71+
listener.beginDocument(new MetaData());
72+
listener.onMacro("testMacro", Listener.EMPTY_PARAMETERS, null, false);
73+
listener.endDocument(new MetaData());
74+
75+
List<Block> children = xdomGeneratorListener.getXDOM().getChildren();
76+
List<Block> expectedChildren = List.of(new MacroBlock("testMacro", Listener.EMPTY_PARAMETERS, null, true));
77+
assertEquals(expectedChildren, children);
78+
}
79+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.#-------------------------------------------------------------------------------------------------------
2+
.input|xwiki/2.0
3+
.# Test a standalone macro nested inside a link
4+
.#-------------------------------------------------------------------------------------------------------
5+
[[
6+
7+
{{info}}Type your information message here.{{/info}}>>https://www.xwiki.org/xwiki/bin/view/Main/]]
8+
.#-----------------------------------------------------
9+
.expect|event/1.0
10+
.#-----------------------------------------------------
11+
beginDocument
12+
beginParagraph
13+
beginLink [Typed = [false] Type = [url] Reference = [https://www.xwiki.org/xwiki/bin/view/Main/]] [false]
14+
onMacroInline [info] [] [Type your information message here.]
15+
endLink [Typed = [false] Type = [url] Reference = [https://www.xwiki.org/xwiki/bin/view/Main/]] [false]
16+
endParagraph
17+
endDocument
18+
.#-----------------------------------------------------
19+
.expect|xwiki/2.0
20+
.#-----------------------------------------------------
21+
[[{{info}}Type your information message here.{{/info}}>>https://www.xwiki.org/xwiki/bin/view/Main/]]

0 commit comments

Comments
 (0)