Mercurial Hosting > luan
annotate src/goodjava/html/Css.java @ 1827:50e570b598b2
security
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 15 Sep 2024 10:36:46 -0600 |
parents | 31a82b0d0a87 |
children |
rev | line source |
---|---|
1714
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
1 package goodjava.html; |
1289 | 2 |
1714
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
3 import java.util.Map; |
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
4 import java.util.LinkedHashMap; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1333
diff
changeset
|
5 import goodjava.parser.Parser; |
1289 | 6 |
7 | |
8 public final class Css { | |
9 | |
1714
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
10 public static Map<String,String> style(String text) { |
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
11 return new Css(text).parseStyle(); |
1289 | 12 } |
13 | |
14 private final Parser parser; | |
15 | |
1562 | 16 private Css(String text) { |
1289 | 17 this.parser = new Parser(text); |
18 } | |
19 | |
1714
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
20 private Map<String,String> parseStyle() { |
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
21 Map<String,String> map = new LinkedHashMap<String,String>(); |
1289 | 22 while( matchSpace() ); |
23 while( !parser.endOfInput() ) { | |
24 int start = parser.currentIndex(); | |
25 if( !matchPropertyChar() ) | |
26 return null; | |
27 while( matchPropertyChar() ); | |
28 String prop = parser.textFrom(start).toLowerCase(); | |
29 | |
30 while( matchSpace() ); | |
31 if( !parser.match(':') ) | |
32 return null; | |
33 | |
34 start = parser.currentIndex(); | |
35 while( !parser.endOfInput() && parser.noneOf(";") ); | |
36 String val = parser.textFrom(start).trim(); | |
37 | |
1714
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
38 map.put(prop,val); |
1289 | 39 parser.match(';'); |
40 while( matchSpace() ); | |
41 } | |
1714
31a82b0d0a87
bbcode and html work
Franklin Schmidt <fschmidt@gmail.com>
parents:
1562
diff
changeset
|
42 return map; |
1289 | 43 } |
44 | |
45 private boolean matchPropertyChar() { | |
46 return parser.inCharRange('a','z') | |
47 || parser.inCharRange('A','Z') | |
48 || parser.inCharRange('0','9') | |
49 || parser.anyOf("_-") | |
50 ; | |
51 } | |
52 | |
53 private boolean matchSpace() { | |
54 return parser.anyOf(" \t\r\n"); | |
55 } | |
56 | |
57 } |