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); }
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()); } }
@Override public void flushInlineTags(int documentLength, @NonNull FlushAction<Inline> action) { if (inlineTags.size() > 0) { if (documentLength > HtmlTag.NO_END) { for (HtmlTagImpl.InlineImpl inline : inlineTags) { inline.closeAt(documentLength); } } //noinspection unchecked action.apply(Collections.unmodifiableList((List<? extends Inline>) inlineTags)); inlineTags.clear(); } else { action.apply(Collections.<Inline>emptyList()); } }
@Override void closeAt(int end) { if (!isClosed()) { super.end = end; } }
@Test public void imageReplacementNoAlt() { final HtmlTag.Inline img = new InlineImpl("img", -1, Collections.<String, String>emptyMap()); assertEquals("\uFFFC", replacement.replace(img)); }
@Test public void brAddsNewLine() { final HtmlTag.Inline br = new InlineImpl( "br", -1, Collections.<String, String>emptyMap() ); assertEquals("\n", replacement.replace(br)); } }
@Test public void imageReplacementAlt() { final HtmlTag.Inline img = new InlineImpl( "img", -1, Collections.singletonMap("alt", "alternative27") ); assertEquals("alternative27", replacement.replace(img)); }