@Override void closeAt(int end) { if (!isClosed()) { super.end = end; } }
protected <T extends Appendable & CharSequence> void processCharacter( @NonNull T output, @NonNull Token.Character character) { // there are tags: BUTTON, INPUT, SELECT, SCRIPT, TEXTAREA, STYLE // that might have character data that we do not want to display if (isInsidePreTag) { appendQuietly(output, character.getData()); } else { ensureNewLineIfPreviousWasBlock(output); trimmingAppender.append(output, character.getData()); } }
protected <T extends Appendable & CharSequence> void processInlineTagStart( @NonNull T output, @NonNull Token.StartTag startTag) { final String name = startTag.normalName; final HtmlTagImpl.InlineImpl inline = new HtmlTagImpl.InlineImpl(name, output.length(), extractAttributes(startTag)); ensureNewLineIfPreviousWasBlock(output); if (isVoidTag(name) || startTag.selfClosing) { final String replacement = emptyTagReplacement.replace(inline); if (replacement != null && replacement.length() > 0) { appendQuietly(output, replacement); } // the thing is: we will keep this inline tag in the list, // but in case of void-tag that has no replacement, there will be no // possibility to set a span (requires at least one char) inline.closeAt(output.length()); } inlineTags.add(inline); }
@Test public void resetClearsBothInlinesAndBlocks() { final MarkwonHtmlParserImpl impl = MarkwonHtmlParserImpl.create(); final StringBuilder output = new StringBuilder(); impl.processFragment(output, "<p>paragraph <i>italic</i></p><div>div</div>"); impl.reset(); final CaptureInlineTagsAction inlineTagsAction = new CaptureInlineTagsAction(); final CaptureBlockTagsAction blockTagsAction = new CaptureBlockTagsAction(); impl.flushInlineTags(output.length(), inlineTagsAction); impl.flushBlockTags(output.length(), blockTagsAction); assertTrue(inlineTagsAction.called); assertTrue(blockTagsAction.called); assertEquals(0, inlineTagsAction.tags.size()); assertEquals(0, blockTagsAction.tags.size()); }
@Test public void imageReplacementNoAlt() { final HtmlTag.Inline img = new InlineImpl("img", -1, Collections.<String, String>emptyMap()); assertEquals("\uFFFC", replacement.replace(img)); }
protected <T extends Appendable & CharSequence> void processInlineTagEnd( @NonNull T output, @NonNull Token.EndTag endTag) { // try to find it, if none found -> ignore final HtmlTagImpl.InlineImpl openInline = findOpenInlineTag(endTag.normalName); if (openInline != null) { // okay, if this tag is empty -> call replacement if (isEmpty(output, openInline)) { appendEmptyTagReplacement(output, openInline); } // close open inline tag openInline.closeAt(output.length()); } }
protected <T extends Appendable & CharSequence> void appendEmptyTagReplacement( @NonNull T output, @NonNull HtmlTagImpl tag) { final String replacement = emptyTagReplacement.replace(tag); if (replacement != null) { appendQuietly(output, replacement); } } }
@NonNull static TrimmingAppender create() { return new Impl(); }
@NonNull public static MarkwonHtmlParserImpl create(@NonNull HtmlEmptyTagReplacement inlineTagReplacement) { return new MarkwonHtmlParserImpl(inlineTagReplacement, TrimmingAppender.create()); }
@NonNull public static HtmlEmptyTagReplacement create() { return new HtmlEmptyTagReplacement(); }
@Before public void before() { replacement = HtmlEmptyTagReplacement.create(); }
@Override public void reset() { inlineTags.clear(); currentBlock = HtmlTagImpl.BlockImpl.root(); }
protected <T extends Appendable & CharSequence> void ensureNewLineIfPreviousWasBlock(@NonNull T output) { if (previousIsBlock) { ensureNewLine(output); previousIsBlock = false; } }
protected static <T extends Appendable & CharSequence> void ensureNewLine(@NonNull T output) { final int length = output.length(); if (length > 0 && '\n' != output.charAt(length - 1)) { appendQuietly(output, '\n'); } }
private static <T> void with(@NonNull T t, @NonNull Action<T> action) { action.apply(t); } }
@Test public void imageReplacementAlt() { final HtmlTag.Inline img = new InlineImpl( "img", -1, Collections.singletonMap("alt", "alternative27") ); assertEquals("alternative27", replacement.replace(img)); }
@Before public void before() { impl = new TrimmingAppender.Impl(); }
@Test public void brAddsNewLine() { final HtmlTag.Inline br = new InlineImpl( "br", -1, Collections.<String, String>emptyMap() ); assertEquals("\n", replacement.replace(br)); } }