forked from reflex/reflex-framework
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSSSelector.as
More file actions
140 lines (122 loc) · 3.41 KB
/
CSSSelector.as
File metadata and controls
140 lines (122 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package mx.styles
{
import flash.display.DisplayObject;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public class CSSSelector
{
protected var _property:String;
protected var _type:String;
protected var _conditions:Array;
protected var _ancestor:CSSSelector;
protected var _specificity:uint;
public function CSSSelector(type:String, conditions:Array = null, ancestor:CSSSelector = null)
{
if (!type || type == "global") type = "*";
_type = type;
_conditions = conditions;
_ancestor = ancestor;
_specificity = (type == "*" ? 1 : 0);
if (type != "*" && type.substr(0, 1).toLowerCase() == type.substr(0, 1)) {
_property = type; // if this isn't capitalized, it may be a property.
if (ancestor.property) {
_property + ancestor.property + "." + _property;
}
}
init();
}
protected function init():void
{
if (_conditions) {
for each (var condition:CSSCondition in _conditions) {
_specificity += condition.specificity;
}
_conditions.sortOn("value");
_conditions.sortOn("kind");
_conditions.sortOn("specificity", Array.NUMERIC);
}
}
public function get property():String
{
return _property;
}
public function get type():String
{
return _type;
}
public function get conditions():Array
{
return _conditions;
}
public function get ancestor():CSSSelector
{
return _ancestor;
}
public function get specificity():uint
{
return _specificity;
}
public function match(obj:Object, matchAncestors:Boolean = true, matchPseudos:Boolean = false):Boolean
{
var match:Boolean = false;
var condition:CSSCondition;
var parent:DisplayObject = obj is DisplayObject ? DisplayObject(obj).parent : null;
// if this is a property
if (ancestor && matchAncestors && _property) {
return ancestor.match(obj, matchAncestors, matchPseudos);
} else if (_type != "*" && getTypes(obj).indexOf(_type) == -1) {
return false;
}
if (conditions) {
// must be IStylable to match any of the conditions
for each (condition in conditions) {
if (!matchPseudos && condition.kind == "pseudo") break;
if (!condition.match(obj)) return false;
}
}
if (ancestor && matchAncestors && obj is DisplayObject) {
var selector:CSSSelector = ancestor;
while (selector != null) {
while (parent != null) {
if (selector.match(parent, false)) {
selector = selector.ancestor;
break;
}
parent = parent.parent;
}
// if we didn't match all the ancestors
if (parent == null) return false;
}
}
return true;
}
public function getTypes(obj:Object):Array
{
var className:String = getQualifiedClassName(obj);
var types:Array = [className.split("::").pop()];
while (className != "Object") {
className = getQualifiedSuperclassName(obj);
types.push(className.split("::").pop());
obj = getDefinitionByName(className);
}
return types;
}
public function toString():String
{
var selectors:Array = [];
var selector:CSSSelector = this;
while (selector) {
var str:String = selector.type;
if (conditions) {
for each (var condition:CSSCondition in conditions) {
str += condition;
}
}
selectors.unshift(str);
selector = selector.ancestor;
}
return selectors.join(" ");
}
}
}