Fix =no MapCSS check. This must be the longest-standing bug in P2...
[potlatch2.git] / net / systemeD / halcyon / styleparser / Condition.as
1 package net.systemeD.halcyon.styleparser {
2
3         /**     A single tag test that forms part of a MapCSS selector.
4                 For example, "highway==primary" or "population>1000".
5                 Conditions are grouped in Rules.
6                 
7                 @see net.systemeD.halcyon.styleparser.Rule */
8
9         public class Condition {
10                 public var type:String;
11                 public var params:Array;
12
13                 /** Create a new Condition.                                                                                                             <p>
14                   
15                         Valid types:                                                                                                                    </p><p>
16                         eq,'highway','trunk'            - simple equality test                                          </p><p>
17                         ne,'highway','trunk'            - not equals                                                            </p><p>
18                         regex,'highway','trunk.+'       - regular expression                                            </p><p>
19                         true,'bridge'                           - value is true/yes/1                                           </p><p>
20                         untrue,'bridge'                         - value is not true/yes/1                                       </p><p>
21                         set,'highway'                           - tag exists and is not ''                                      </p><p>
22                         unset,'highway'                         - tag does not exist, or is ''                          </p><p>
23                         <,'population','5000'           - numeric comparison (also <=, >, >=)           </p>
24                  */
25                 
26                 public function Condition(type:String='', ...params) {
27                         this.type=type; this.params=params;
28                 }
29                 
30                 /** Test a tag hash against the Condition. */
31
32                 public function test(tags:Object):Boolean {
33                         switch (type) {
34                                 case 'eq':              return (tags[params[0]]==params[1]); break;
35                                 case 'ne':              return (tags[params[0]]!=params[1]); break;
36                                 case 'regex':   var r:RegExp=new RegExp(params[1],"i");
37                                                                 return (r.test(tags[params[0]])); break;
38                                 case 'true':    return (tags[params[0]]=='true' || tags[params[0]]=='yes' || tags[params[0]]=='1'); break;
39                                 case 'false':   return (tags[params[0]]!='true' && tags[params[0]]!='yes' && tags[params[0]]!='1' && tags[params[0]]!=undefined && tags[params[0]]!=''); break;
40                                 case 'set':             return (tags[params[0]]!=undefined && tags[params[0]]!=''); break;
41                                 case 'unset':   return (tags[params[0]]==undefined || tags[params[0]]==''); break;
42                                 case '<':               return (Number(tags[params[0]])< Number(params[1])); break;
43                                 case '<=':              return (Number(tags[params[0]])<=Number(params[1])); break;
44                                 case '>':               return (Number(tags[params[0]])> Number(params[1])); break;
45                                 case '>=':              return (Number(tags[params[0]])>=Number(params[1])); break;
46                         }
47                         return false;
48                 }
49
50         public function toString():String {
51             return "Condition("+type+":"+params+")";
52         }
53
54
55         }
56
57 }