annotate src/luan/modules/parsers/Css.java @ 1562:b89212fd04b5

remove table.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 08 Nov 2020 16:50:59 -0700
parents 27efb1fcbcb5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package luan.modules.parsers;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
3 import luan.Luan;
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import luan.LuanTable;
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
5 import luan.LuanException;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
6 import goodjava.parser.Parser;
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 public final class Css {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
11 public static LuanTable style(String text) {
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
12 try {
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
13 return new Css(text).parseStyle();
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
14 } catch(LuanException e) {
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
15 throw new RuntimeException(e);
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
16 }
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 private final Parser parser;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
21 private Css(String text) {
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 this.parser = new Parser(text);
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
25 private LuanTable parseStyle() throws LuanException {
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
26 LuanTable tbl = new LuanTable();
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 while( !parser.endOfInput() ) {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 int start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 if( !matchPropertyChar() )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 while( matchPropertyChar() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 String prop = parser.textFrom(start).toLowerCase();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 if( !parser.match(':') )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 while( !parser.endOfInput() && parser.noneOf(";") );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 String val = parser.textFrom(start).trim();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
1562
b89212fd04b5 remove table.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
43 tbl.rawPut(prop,val);
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 parser.match(';');
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return tbl;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 private boolean matchPropertyChar() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 return parser.inCharRange('a','z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 || parser.inCharRange('A','Z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 || parser.inCharRange('0','9')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 || parser.anyOf("_-")
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 ;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 private boolean matchSpace() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 return parser.anyOf(" \t\r\n");
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 }