changeset 1793:a55a891a4f67

xml minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 28 Dec 2023 11:36:24 -0700
parents a5f62fe28b3e
children 04ac5f2771c8
files src/goodjava/xml/XmlElement.java
diffstat 1 files changed, 28 insertions(+), 1 deletions(-) [+]
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;