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"));
}
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;
}
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");
}
}
}
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);
}
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);
}
Aggregations