Search in sources :

Example 6 with Attributes

use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.

the class AttributesTest method testBasic.

@Test
public void testBasic() throws Exception {
    Attributes attributes = new Attributes();
    assertEquals("empty no attributes", false, attributes.contains("class"));
    assertEquals("empty no values", false, attributes.containsValue("class", "class1"));
    attributes.addValue("class", "class1");
    assertEquals("add value", "class1", attributes.getValue("class"));
    assertEquals("contains added attribute", true, attributes.contains("class"));
    assertEquals("contains added value", true, attributes.containsValue("class", "class1"));
    attributes.addValue("class", "class2");
    assertEquals("add value", "class1 class2", attributes.getValue("class"));
    assertEquals("contains added attribute", true, attributes.contains("class"));
    assertEquals("contains old value", true, attributes.containsValue("class", "class1"));
    assertEquals("contains added value", true, attributes.containsValue("class", "class2"));
    attributes.addValue("class", "class3");
    assertEquals("add value", "class1 class2 class3", attributes.getValue("class"));
    assertEquals("contains added attribute", true, attributes.contains("class"));
    assertEquals("contains old value", true, attributes.containsValue("class", "class1"));
    assertEquals("contains added value", true, attributes.containsValue("class", "class2"));
    assertEquals("contains added value", true, attributes.containsValue("class", "class3"));
    attributes.removeValue("class", "class2");
    assertEquals("removed value", "class1 class3", attributes.getValue("class"));
    assertEquals("contains removed value attribute", true, attributes.contains("class"));
    assertEquals("contains old value", true, attributes.containsValue("class", "class1"));
    assertEquals("does not contain removed value", false, attributes.containsValue("class", "class2"));
    assertEquals("contains old value", true, attributes.containsValue("class", "class3"));
    attributes.removeValue("class", "class3");
    assertEquals("removed value", "class1", attributes.getValue("class"));
    assertEquals("contains removed value attribute", true, attributes.contains("class"));
    assertEquals("contains old value", true, attributes.containsValue("class", "class1"));
    assertEquals("does not contain removed value", false, attributes.containsValue("class", "class2"));
    assertEquals("does not contain removed value", false, attributes.containsValue("class", "class3"));
    attributes.removeValue("class", "class1");
    assertEquals("removed value", "", attributes.getValue("class"));
    assertEquals("contains removed value attribute", true, attributes.contains("class"));
    assertEquals("does not contain removed value", false, attributes.containsValue("class", "class1"));
    assertEquals("does not contain removed value", false, attributes.containsValue("class", "class2"));
    assertEquals("does not contain removed value", false, attributes.containsValue("class", "class3"));
    attributes.replaceValue("class", "class1 class2 class3");
    assertEquals("replaced value", "class1 class2 class3", attributes.getValue("class"));
    assertEquals("contains value attribute", true, attributes.contains("class"));
    assertEquals("contains added values", true, attributes.containsValue("class", "class1"));
    assertEquals("contains added values", true, attributes.containsValue("class", "class2"));
    assertEquals("contains added values", true, attributes.containsValue("class", "class3"));
    attributes.addValue("id", "id1");
    assertEquals("add value", "id1", attributes.getValue("id"));
    assertEquals("contains added attribute", true, attributes.contains("id"));
    assertEquals("contains added value", true, attributes.containsValue("id", "id1"));
}
Also used : Attributes(com.vladsch.flexmark.util.html.Attributes) Test(org.junit.Test)

Example 7 with Attributes

use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.

the class HtmlWriter method tag.

@Override
public HtmlWriter tag(CharSequence tagName, boolean voidElement) {
    if (useAttributes != null) {
        final Attributes attributes = context.extendRenderingNodeAttributes(useAttributes, getAttributes());
        String sourcePositionAttribute = context.getHtmlOptions().sourcePositionAttribute;
        String attributeValue = attributes.getValue(sourcePositionAttribute);
        if (!attributeValue.isEmpty()) {
            // add to tag ranges
            int pos = attributeValue.indexOf('-');
            int startOffset = -1;
            int endOffset = -1;
            if (pos != -1) {
                try {
                    startOffset = Integer.valueOf(attributeValue.substring(0, pos));
                } catch (Throwable ignored) {
                }
                try {
                    endOffset = Integer.valueOf(attributeValue.substring(pos + 1));
                } catch (Throwable ignored) {
                }
            }
            if (startOffset >= 0 && startOffset < endOffset) {
                ArrayList<TagRange> tagRanges = context.getDocument().get(HtmlRenderer.TAG_RANGES);
                tagRanges.add(new TagRange(tagName, startOffset, endOffset));
            }
        }
        setAttributes(attributes);
        useAttributes = null;
    }
    super.tag(tagName, voidElement);
    return this;
}
Also used : TagRange(com.vladsch.flexmark.util.sequence.TagRange) Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 8 with Attributes

use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.

the class CoreNodeRenderer method render.

private void render(ImageRef node, NodeRendererContext context, HtmlWriter html) {
    ResolvedLink resolvedLink = null;
    if (!node.isDefined() && recheckUndefinedReferences) {
        if (node.getReferenceNode(referenceRepository) != null) {
            node.setDefined(true);
        }
    }
    Reference reference = null;
    if (node.isDefined()) {
        reference = node.getReferenceNode(referenceRepository);
        String url = reference.getUrl().unescape();
        resolvedLink = context.resolveLink(LinkType.IMAGE, url, null, null);
        if (reference.getTitle().isNotNull()) {
            resolvedLink.getNonNullAttributes().replaceValue(Attribute.TITLE_ATTR, reference.getTitle().unescape());
        } else {
            resolvedLink.getNonNullAttributes().remove(Attribute.TITLE_ATTR);
        }
    } else {
        // see if have reference resolver and this is resolved
        String normalizeRef = referenceRepository.normalizeKey(node.getReference());
        resolvedLink = context.resolveLink(LinkType.IMAGE_REF, normalizeRef, null, null);
        if (resolvedLink.getStatus() == UNKNOWN) {
            resolvedLink = null;
        }
    }
    if (resolvedLink == null) {
        // empty ref, we treat it as text
        html.text(node.getChars().unescape());
    } else {
        if (!context.isDoNotRenderLinks()) {
            String altText = new TextCollectingVisitor().collectAndGetText(node);
            Attributes attributes = resolvedLink.getNonNullAttributes();
            html.attr("src", resolvedLink.getUrl());
            html.attr("alt", altText);
            // need to take attributes for reference definition, then overlay them with ours
            if (reference != null) {
                attributes = context.extendRenderingNodeAttributes(reference, AttributablePart.NODE, attributes);
            }
            html.attr(attributes);
            html.srcPos(node.getChars()).withAttr(resolvedLink).tagVoid("img");
        }
    }
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 9 with Attributes

use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.

the class ResolvedLink method withTarget.

public ResolvedLink withTarget(CharSequence target) {
    String haveTarget = myAttributes == null ? null : myAttributes.getValue(Attribute.TARGET_ATTR);
    if (target == haveTarget || haveTarget != null && haveTarget.equals(target))
        return this;
    Attributes attributes = new Attributes(myAttributes);
    if (target == null) {
        attributes.remove(Attribute.TARGET_ATTR);
        if (attributes.isEmpty())
            attributes = null;
    } else {
        attributes.replaceValue(Attribute.TARGET_ATTR, target);
    }
    return new ResolvedLink(myLinkType, myUrl, attributes, myStatus);
}
Also used : Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 10 with Attributes

use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.

the class ResolvedLink method withTitle.

public ResolvedLink withTitle(CharSequence title) {
    String haveTitle = myAttributes == null ? null : myAttributes.getValue(Attribute.TITLE_ATTR);
    if (title == haveTitle || haveTitle != null && haveTitle.equals(title))
        return this;
    Attributes attributes = new Attributes(myAttributes);
    if (title == null) {
        attributes.remove(Attribute.TITLE_ATTR);
        if (attributes.isEmpty())
            attributes = null;
    } else {
        attributes.replaceValue(Attribute.TITLE_ATTR, title);
    }
    return new ResolvedLink(myLinkType, myUrl, attributes, myStatus);
}
Also used : Attributes(com.vladsch.flexmark.util.html.Attributes)

Aggregations

Attributes (com.vladsch.flexmark.util.html.Attributes)19 ResolvedLink (com.vladsch.flexmark.html.renderer.ResolvedLink)5 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)3 EnumRefTextCollectingVisitor (com.vladsch.flexmark.ext.enumerated.reference.internal.EnumRefTextCollectingVisitor)3 Test (org.junit.Test)3 Node (com.vladsch.flexmark.ast.Node)2 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)2 FencedCodeBlock (com.vladsch.flexmark.ast.FencedCodeBlock)1 TextBase (com.vladsch.flexmark.ast.TextBase)1 EmojiResolvedShortcut (com.vladsch.flexmark.ext.emoji.internal.EmojiResolvedShortcut)1 AttributeProvider (com.vladsch.flexmark.html.AttributeProvider)1 AttributeProviderFactory (com.vladsch.flexmark.html.AttributeProviderFactory)1 CustomNodeRenderer (com.vladsch.flexmark.html.CustomNodeRenderer)1 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)1 HtmlWriter (com.vladsch.flexmark.html.HtmlWriter)1 IndependentAttributeProviderFactory (com.vladsch.flexmark.html.IndependentAttributeProviderFactory)1 AttributablePart (com.vladsch.flexmark.html.renderer.AttributablePart)1 LinkResolverContext (com.vladsch.flexmark.html.renderer.LinkResolverContext)1 Attribute (com.vladsch.flexmark.util.html.Attribute)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1