diff src/goodjava/html/Css.java @ 1714:31a82b0d0a87

bbcode and html work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 Jul 2022 23:49:47 -0600
parents src/luan/modules/parsers/Css.java@b89212fd04b5
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/html/Css.java	Mon Jul 18 23:49:47 2022 -0600
@@ -0,0 +1,57 @@
+package goodjava.html;
+
+import java.util.Map;
+import java.util.LinkedHashMap;
+import goodjava.parser.Parser;
+
+
+public final class Css {
+
+	public static Map<String,String> style(String text) {
+		return new Css(text).parseStyle();
+	}
+
+	private final Parser parser;
+
+	private Css(String text) {
+		this.parser = new Parser(text);
+	}
+
+	private Map<String,String> parseStyle() {
+		Map<String,String> map = new LinkedHashMap<String,String>();
+		while( matchSpace() );
+		while( !parser.endOfInput() ) {
+			int start = parser.currentIndex();
+			if( !matchPropertyChar() )
+				return null;
+			while( matchPropertyChar() );
+			String prop = parser.textFrom(start).toLowerCase();
+
+			while( matchSpace() );
+			if( !parser.match(':') )
+				return null;
+
+			start = parser.currentIndex();
+			while( !parser.endOfInput() && parser.noneOf(";") );
+			String val = parser.textFrom(start).trim();
+
+			map.put(prop,val);
+			parser.match(';');
+			while( matchSpace() );
+		}
+		return map;
+	}
+
+	private boolean matchPropertyChar() {
+		return parser.inCharRange('a','z')
+			|| parser.inCharRange('A','Z')
+			|| parser.inCharRange('0','9')
+			|| parser.anyOf("_-")
+		;
+	}
+
+	private boolean matchSpace() {
+		return parser.anyOf(" \t\r\n");
+	}
+
+}