annotate src/luan/modules/parsers/Css.java @ 1330:f41919741100

fix security
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 Feb 2019 01:38:55 -0700
parents 8d54bcc0b6d3
children 25746915a241
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
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import luan.LuanState;
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;
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import luan.lib.parser.Parser;
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
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 public static LuanTable style(LuanState luan,String text) {
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
12 try {
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
13 return new Css(luan,text).parseStyle();
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 LuanState luan;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 private final Parser parser;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 private Css(LuanState luan,String text) {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 this.luan = luan;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 this.parser = new Parser(text);
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
27 private LuanTable parseStyle() throws LuanException {
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 LuanTable tbl = new LuanTable(luan);
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 while( !parser.endOfInput() ) {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 int start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 if( !matchPropertyChar() )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 while( matchPropertyChar() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 String prop = parser.textFrom(start).toLowerCase();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 if( !parser.match(':') )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 while( !parser.endOfInput() && parser.noneOf(";") );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 String val = parser.textFrom(start).trim();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1289
diff changeset
45 tbl.put(prop,val);
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 parser.match(';');
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 while( matchSpace() );
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 return tbl;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 private boolean matchPropertyChar() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 return parser.inCharRange('a','z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 || parser.inCharRange('A','Z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 || parser.inCharRange('0','9')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 || parser.anyOf("_-")
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 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 private boolean matchSpace() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 return parser.anyOf(" \t\r\n");
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }