/
types.js
89 lines (86 loc) · 2.76 KB
/
types.js
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
var Keyword = require("../tree/keyword"),
DetachedRuleset = require("../tree/detached-ruleset"),
Dimension = require("../tree/dimension"),
Color = require("../tree/color"),
Quoted = require("../tree/quoted"),
Anonymous = require("../tree/anonymous"),
URL = require("../tree/url"),
Operation = require("../tree/operation"),
functionRegistry = require("./function-registry");
var isa = function (n, Type) {
return (n instanceof Type) ? Keyword.True : Keyword.False;
},
isunit = function (n, unit) {
if (unit === undefined) {
throw { type: "Argument", message: "missing the required second argument to isunit." };
}
unit = typeof unit.value === "string" ? unit.value : unit;
if (typeof unit !== "string") {
throw { type: "Argument", message: "Second argument to isunit should be a unit or a string." };
}
return (n instanceof Dimension) && n.unit.is(unit) ? Keyword.True : Keyword.False;
},
getItemsFromNode = function(node) {
// handle non-array values as an array of length 1
// return 'undefined' if index is invalid
var items = Array.isArray(node.value) ?
node.value : Array(node);
return items;
};
functionRegistry.addMultiple({
isruleset: function (n) {
return isa(n, DetachedRuleset);
},
iscolor: function (n) {
return isa(n, Color);
},
isnumber: function (n) {
return isa(n, Dimension);
},
isstring: function (n) {
return isa(n, Quoted);
},
iskeyword: function (n) {
return isa(n, Keyword);
},
isurl: function (n) {
return isa(n, URL);
},
ispixel: function (n) {
return isunit(n, 'px');
},
ispercentage: function (n) {
return isunit(n, '%');
},
isem: function (n) {
return isunit(n, 'em');
},
isunit: isunit,
unit: function (val, unit) {
if (!(val instanceof Dimension)) {
throw { type: "Argument",
message: "the first argument to unit must be a number" +
(val instanceof Operation ? ". Have you forgotten parenthesis?" : "") };
}
if (unit) {
if (unit instanceof Keyword) {
unit = unit.value;
} else {
unit = unit.toCSS();
}
} else {
unit = "";
}
return new Dimension(val.value, unit);
},
"get-unit": function (n) {
return new Anonymous(n.unit);
},
extract: function(values, index) {
index = index.value - 1; // (1-based index)
return getItemsFromNode(values)[index];
},
length: function(values) {
return new Dimension(getItemsFromNode(values).length);
}
});