|
| 1 | +package org.pegdown; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public class Attributes { |
| 8 | + public final HashMap<String, String> attrMap = new HashMap<String, String>(); |
| 9 | + public final ArrayList<String> attrOrder = new ArrayList<String>(); |
| 10 | + |
| 11 | + public Attributes() { |
| 12 | + } |
| 13 | + |
| 14 | + public Attributes(String name, String value) { |
| 15 | + add(name, value); |
| 16 | + } |
| 17 | + |
| 18 | + public Attributes(List<LinkRenderer.Attribute> attributes) { |
| 19 | + addAll(attributes); |
| 20 | + } |
| 21 | + |
| 22 | + public Attributes add(String attribute, String value) { |
| 23 | + addDelimitedValue(attribute, " ", value); |
| 24 | + return this; |
| 25 | + } |
| 26 | + |
| 27 | + public Attributes addAll(List<LinkRenderer.Attribute> attributes) { |
| 28 | + for (LinkRenderer.Attribute attribute : attributes) { |
| 29 | + add(attribute.name, attribute.value); |
| 30 | + } |
| 31 | + return this; |
| 32 | + } |
| 33 | + |
| 34 | + public Attributes addDelimitedValue(String attribute, String delim, String value) { |
| 35 | + if (attrMap.containsKey(attribute)) { |
| 36 | + attrMap.put(attribute, attrMap.get(attribute) + delim + value); |
| 37 | + } else { |
| 38 | + attrMap.put(attribute, value); |
| 39 | + attrOrder.add(attribute); |
| 40 | + } |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public Attributes replace(String attribute, String value) { |
| 45 | + if (attrMap.containsKey(attribute)) { |
| 46 | + attrMap.put(attribute, value); |
| 47 | + } else { |
| 48 | + attrMap.put(attribute, value); |
| 49 | + attrOrder.add(attribute); |
| 50 | + } |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public Attributes removeDelimitedValue(String name, String delim, String value, boolean skipEmpties) { |
| 55 | + if (attrMap.containsKey(name)) { |
| 56 | + String[] classList = attrMap.get(name).split(delim); |
| 57 | + String classAttr = ""; |
| 58 | + for (String classValue : classList) { |
| 59 | + if ((!skipEmpties || !classValue.isEmpty()) && !classValue.equals(value)) { |
| 60 | + classAttr += delim + classValue; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // vsch: we always put it back so as not to change the order of when it was defined, otherwise tests may fail |
| 65 | + attrMap.put(name, classAttr); |
| 66 | + } |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + // vsch: use these to manipulate classes in preview() |
| 71 | + public Attributes removeClass(String value) { |
| 72 | + return removeDelimitedValue("class", " ", value, true); |
| 73 | + } |
| 74 | + |
| 75 | + public Attributes addClass(String value) { |
| 76 | + return add("class", value); |
| 77 | + } |
| 78 | + |
| 79 | + public Attributes replaceClass(String value) { |
| 80 | + return replace("class", value); |
| 81 | + } |
| 82 | + |
| 83 | + public boolean hasClass(String value) { |
| 84 | + String classAttr = " " + get("class", "") + " "; |
| 85 | + return classAttr.contains(" " + value + " "); |
| 86 | + } |
| 87 | + |
| 88 | + public boolean contains(String attribute) { |
| 89 | + return attrMap.containsKey(attribute); |
| 90 | + } |
| 91 | + |
| 92 | + public String get(String attribute, String valueIfMissing) { |
| 93 | + if (!attrMap.containsKey(attribute)) return valueIfMissing; |
| 94 | + return attrMap.get(attribute); |
| 95 | + } |
| 96 | + |
| 97 | + // vsch: NOTE: only lowercase "class" attribute is trimmed and skipped if it is empty, the rest are output as is, if you want something else take care of it in preview() |
| 98 | + void print(Printer printer) { |
| 99 | + for (String name : attrOrder) { |
| 100 | + assert attrMap.containsKey(name) : "Unexpected, key: " + name + " is not in attrMap, must have been manipulated outside of Attribute class"; |
| 101 | + if (name.equals("class")) { |
| 102 | + String classAttr = attrMap.get(name).trim(); |
| 103 | + if (!classAttr.isEmpty()) { |
| 104 | + printer.print(' ').print(name).print('=').print('"').print(attrMap.get(name).replace("\\", "\\\\").replace("\"", "\\\"")).print('"'); |
| 105 | + } |
| 106 | + } else { |
| 107 | + printer.print(' ').print(name).print('=').print('"').print(attrMap.get(name).replace("\\", "\\\\").replace("\"", "\\\"")).print('"'); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments