HtmlBBCodeTranslatorImpl implements BBCodeTranslator { @Override public String parse(String text) { StringBundler sb = new StringBundler(); List<BBCodeItem> bbCodeItems = _bbCodeParser.parse(text); Stack<String> tags = new Stack<>(); IntegerWrapper marker = new IntegerWrapper(); for (; marker.getValue() < bbCodeItems.size(); marker.increment()) { BBCodeItem bbCodeItem = bbCodeItems.get(marker.getValue()); int type = bbCodeItem.getType(); if (type == BBCodeParser.TYPE_DATA) { handleData(sb, bbCodeItems, tags, marker, bbCodeItem); } else if (type == BBCodeParser.TYPE_TAG_END) { handleTagEnd(sb, tags); } else if (type == BBCodeParser.TYPE_TAG_START) { handleTagStart(sb, bbCodeItems, tags, marker, bbCodeItem); } } return sb.toString(); } HtmlBBCodeTranslatorImpl(); @Override String[] getEmoticonDescriptions(); @Override String[] getEmoticonFiles(); @Override String[][] getEmoticons(); @Override String[] getEmoticonSymbols(); @Override String getHTML(String bbcode); @Override String parse(String text);  }
@Test public void testAlign() { String expected = "<p style=\"text-align: center\">text</p>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[center]text[/center]"); Assert.assertEquals(expected, actual); }
@Test public void testAsterisk() { String expected = "<strong>type</strong> some <u>text</u><li>this is a test</li>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[b]type[/b] some [u]text[/u][*]this is a test"); Assert.assertEquals(expected, actual); }
@Test public void testAsteriskInBold() { String expected = "<strong>asterisk</strong><li> is inside the bold</li>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[b]asterisk[*][/b] is inside the bold"); Assert.assertEquals(expected, actual); }
@Test public void testBold() { String expected = "<strong>text</strong>"; String actual = _htmlBBCodeTranslatorImpl.parse("[b]text[/b]"); Assert.assertEquals(expected, actual); }
@Test public void testCode() { StringBundler sb = new StringBundler(4); sb.append("<div class=\"lfr-code\"><table><tbody><tr>"); sb.append("<td class=\"line-numbers\" data-line-number=\"1\"></td>"); sb.append("<td class=\"lines\"><div class=\"line\">:)</div>"); sb.append("</td></tr></tbody></table></div>"); Assert.assertEquals( sb.toString(), _htmlBBCodeTranslatorImpl.parse("[code]:)[/code]")); }
@Test public void testColor() { String expected = "<span style=\"color: #ff0000\">text</span>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[color=#ff0000]text[/color]"); Assert.assertEquals(expected, actual); }
@Test public void testEmotion() { String expected = "<img alt=\"emoticon\" " + "src=\"@theme_images_path@/emoticons/happy.gif\" >"; String actual = _htmlBBCodeTranslatorImpl.parse(":)"); Assert.assertEquals(expected, actual); }
@Test public void testFontFamily() { String expected = "<span style=\"font-family: " + HtmlUtil.escapeAttribute("georgia, serif") + "\">text</span>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[font=georgia, serif]text[/font]"); Assert.assertEquals(expected, actual); }
@Test public void testFontSize() { String expected = "<span style=\"font-size: 18px;\">text</span>"; String actual = _htmlBBCodeTranslatorImpl.parse("[size=5]text[/size]"); Assert.assertEquals(expected, actual); }
@Test public void testImgXSS() { String expected = "<img src=\"\" />"; String actual = _htmlBBCodeTranslatorImpl.parse( "[img]asd[font= onerror=alert(/XSS/.source) Assert.assertEquals(expected, actual); }
@Test public void testIncompleteTag() { String expected = "<strong>text</strong>"; String actual = _htmlBBCodeTranslatorImpl.parse("[b]text"); Assert.assertEquals(expected, actual); }
@Test public void testInvalidTag() { String expected = "[x]invalidTag[/x]"; String actual = _htmlBBCodeTranslatorImpl.parse("[x]invalidTag[/x]"); Assert.assertEquals(expected, actual); }
@Test public void testInvalidTagAndValidTag() { String expected = "[x]bbb<u>XXX</u>ddd[/x]"; String actual = _htmlBBCodeTranslatorImpl.parse( "[x]bbb[u]XXX[/u]ddd[/x]"); Assert.assertEquals(expected, actual); }
@Test public void testItalic() { String expected = "<em>text</em>"; String actual = _htmlBBCodeTranslatorImpl.parse("[i]text[/i]"); Assert.assertEquals(expected, actual); }
@Test public void testOrderedList() { String expected = "<ol style=\"list-style: lower-roman outside;\" start=\"2\">" + "<li>line1</li><li>line2</li></ol>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[list type=\"i\" start=\"2\"][*]line1[*]line2[/list]"); Assert.assertEquals(expected, actual); }
@Test public void testQuote() { String expected = "<div class=\"quote-title\">citer:</div><div class=\"quote\">" + "<div class=\"quote-content\">text</div></div>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[quote=citer]text[/quote]"); Assert.assertEquals(expected, actual); }
@Test public void testStrike() { String expected = "<strike>text</strike>"; String actual = _htmlBBCodeTranslatorImpl.parse("[s]text[/s]"); Assert.assertEquals(expected, actual); }
@Test public void testUnderline() { String expected = "<u>text</u>"; String actual = _htmlBBCodeTranslatorImpl.parse("[u]text[/u]"); Assert.assertEquals(expected, actual); }
@Test public void testUnorderedList() { String expected = "<ul style=\"list-style: circle outside;\">" + "<li>line1</li><li>line2</li></ul>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[list type=\"circle\"][*]line1[*]line2[/list]"); Assert.assertEquals(expected, actual); }
@Test public void testURL() { String url = "https: String expected = "<a href=\"" + HtmlUtil.escapeHREF(url) + "\">link</a>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[url=" + url + "]link[/url]"); Assert.assertEquals(expected, actual); }
@Test public void testURLWithAccents() { String urlWithAccents = "https: String expected = "<a href=\"" + HtmlUtil.escapeHREF(urlWithAccents) + "\">link</a>"; String actual = _htmlBBCodeTranslatorImpl.parse( "[url=" + urlWithAccents + "]link[/url]"); Assert.assertEquals(expected, actual); }