76 * @param attrName name of the attribute |
76 * @param attrName name of the attribute |
77 * @param attrValue value of the attribute |
77 * @param attrValue value of the attribute |
78 */ |
78 */ |
79 public void addAttr(HtmlAttr attrName, String attrValue) { |
79 public void addAttr(HtmlAttr attrName, String attrValue) { |
80 if (attrs.isEmpty()) |
80 if (attrs.isEmpty()) |
81 attrs = new LinkedHashMap<HtmlAttr,String>(); |
81 attrs = new LinkedHashMap<HtmlAttr,String>(3); |
82 attrs.put(nullCheck(attrName), nullCheck(attrValue)); |
82 attrs.put(nullCheck(attrName), escapeHtmlChars(attrValue)); |
83 } |
83 } |
84 |
84 |
85 /** |
85 /** |
86 * Adds a style for the HTML tag. |
86 * Adds a style for the HTML tag. |
87 * |
87 * |
129 n += c.charCount(); |
129 n += c.charCount(); |
130 return n; |
130 return n; |
131 } |
131 } |
132 |
132 |
133 /** |
133 /** |
|
134 * Given a string, escape all special html characters and |
|
135 * return the result. |
|
136 * |
|
137 * @param s The string to check. |
|
138 * @return the original string with all of the HTML characters escaped. |
|
139 */ |
|
140 private static String escapeHtmlChars(String s) { |
|
141 for (int i = 0; i < s.length(); i++) { |
|
142 char ch = s.charAt(i); |
|
143 switch (ch) { |
|
144 // only start building a new string if we need to |
|
145 case '<': case '>': case '&': |
|
146 StringBuilder sb = new StringBuilder(s.substring(0, i)); |
|
147 for ( ; i < s.length(); i++) { |
|
148 ch = s.charAt(i); |
|
149 switch (ch) { |
|
150 case '<': sb.append("<"); break; |
|
151 case '>': sb.append(">"); break; |
|
152 case '&': sb.append("&"); break; |
|
153 default: sb.append(ch); break; |
|
154 } |
|
155 } |
|
156 return sb.toString(); |
|
157 } |
|
158 } |
|
159 return s; |
|
160 } |
|
161 |
|
162 /** |
134 * Generates an HTML anchor tag. |
163 * Generates an HTML anchor tag. |
135 * |
164 * |
136 * @param ref reference url for the anchor tag |
165 * @param ref reference url for the anchor tag |
137 * @param body content for the anchor tag |
166 * @param body content for the anchor tag |
138 * @return an HtmlTree object |
167 * @return an HtmlTree object |
139 */ |
168 */ |
140 public static HtmlTree A(String ref, Content body) { |
169 public static HtmlTree A(String ref, Content body) { |
141 HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body)); |
170 HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body)); |
142 htmltree.addAttr(HtmlAttr.HREF, Util.escapeHtmlChars(nullCheck(ref))); |
171 htmltree.addAttr(HtmlAttr.HREF, ref); |
143 return htmltree; |
172 return htmltree; |
144 } |
173 } |
145 |
174 |
146 /** |
175 /** |
147 * Generates an HTML anchor tag with name attribute and content. |
176 * Generates an HTML anchor tag with name attribute and content. |
322 */ |
351 */ |
323 public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle, |
352 public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle, |
324 HtmlStyle styleClass, Content body) { |
353 HtmlStyle styleClass, Content body) { |
325 HtmlTree htmltree = new HtmlTree(headingTag, nullCheck(body)); |
354 HtmlTree htmltree = new HtmlTree(headingTag, nullCheck(body)); |
326 if (printTitle) |
355 if (printTitle) |
327 htmltree.addAttr(HtmlAttr.TITLE, Util.stripHtml(body.toString())); |
356 htmltree.addAttr(HtmlAttr.TITLE, stripHtml(body)); |
328 if (styleClass != null) |
357 if (styleClass != null) |
329 htmltree.addStyle(styleClass); |
358 htmltree.addStyle(styleClass); |
330 return htmltree; |
359 return htmltree; |
331 } |
360 } |
332 |
361 |
835 return true; |
864 return true; |
836 } else { |
865 } else { |
837 return false; |
866 return false; |
838 } |
867 } |
839 } |
868 } |
|
869 |
|
870 /** |
|
871 * Given a Content node, strips all html characters and |
|
872 * return the result. |
|
873 * |
|
874 * @param body The content node to check. |
|
875 * @return the plain text from the content node |
|
876 * |
|
877 */ |
|
878 private static String stripHtml(Content body) { |
|
879 String rawString = body.toString(); |
|
880 // remove HTML tags |
|
881 rawString = rawString.replaceAll("\\<.*?>", " "); |
|
882 // consolidate multiple spaces between a word to a single space |
|
883 rawString = rawString.replaceAll("\\b\\s{2,}\\b", " "); |
|
884 // remove extra whitespaces |
|
885 return rawString.trim(); |
|
886 } |
840 } |
887 } |