diff src/goodjava/xml/XmlElement.java @ 1793:a55a891a4f67

xml minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 28 Dec 2023 11:36:24 -0700
parents f8f5c51f5b36
children 3c43b07e12b7
line wrap: on
line diff
--- a/src/goodjava/xml/XmlElement.java	Tue Dec 26 20:57:19 2023 -0700
+++ b/src/goodjava/xml/XmlElement.java	Thu Dec 28 11:36:24 2023 -0700
@@ -58,7 +58,7 @@
 			sb.append( ' ' );
 			sb.append( attribute.getKey() );
 			sb.append( "=\"" );
-			sb.append( encode(attribute.getValue()) );
+			sb.append( encodeAttr(attribute.getValue()) );
 			sb.append( '"' );
 		}
 		if( content == null ) {
@@ -112,6 +112,33 @@
 			case '\'':
 				buf.append("&apos;");
 				break;
+			default:
+				buf.append(c);
+			}
+		}
+		return buf.toString();
+	}
+
+	public static String encodeAttr(String s) {
+		final char[] a = s.toCharArray();
+		StringBuilder buf = new StringBuilder();
+		for( char c : a ) {
+			switch(c) {
+			case '&':
+				buf.append("&amp;");
+				break;
+			case '<':
+				buf.append("&lt;");
+				break;
+			case '>':
+				buf.append("&gt;");
+				break;
+			case '"':
+				buf.append("&quot;");
+				break;
+			case '\'':
+				buf.append("&apos;");
+				break;
 			case '\n':
 				buf.append("&#10;");
 				break;