view src/goodjava/html/Css.java @ 1741:623d9f62ab92

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Nov 2022 10:03:12 -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");
	}

}