view src/goodjava/html/Css.java @ 1821:d28f5ed56454

fix stringify empty list
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 09 Jul 2024 07:17:41 -0600
parents 31a82b0d0a87
children
line wrap: on
line source

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");
	}

}