-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
130 lines (119 loc) · 3.07 KB
/
index.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
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
"use strict";
// @TODO text content
var language = require("cssauron")({
tag: "tagName",
class: function (node) {
if (node.className !== undefined) {
return node.className;
}
// html-to-vdom puts the 'class' in attributes.
if (node.properties && node.properties.attributes) {
return node.properties.attributes.class;
}
return undefined;
},
"id" :"id",
children: "children",
parent: "parent",
contents: function(node) {
return node.contents || "";
},
attr: function(node, attr) {
if (node.properties) {
var attrs = node.properties.attributes;
if(attrs && attrs[attr]){
return attrs[attr];
}
return node.properties[attr];
}
},
});
module.exports = function(sel, options) {
options = options || {};
var selector = language(sel);
function match(vtree) {
var node = mapTree(vtree, null, options) || {};
var matched = [];
// Traverse each node in the tree and see if it matches our selector
traverse(node, function(node) {
var result = selector(node);
if (result) {
if ( ! Array.isArray(result)) {
result = [result];
}
matched.push.apply(matched, result);
}
});
var results = mapResult(matched);
if (results.length === 0) {
return null;
}
return results;
}
match.matches = function(vtree) {
var node = mapTree(vtree, null, options);
return !!selector(node);
};
return match;
};
function traverse(vtree, fn) {
fn(vtree);
if (vtree.children) {
vtree.children.forEach(function(vtree) {
traverse(vtree, fn);
});
}
}
function mapResult(result) {
return result
.filter(function(node) {
return !! node.vtree;
})
.map(function(node){
return node.vtree;
});
}
function getNormalizeCaseFn(caseSensitive) {
return caseSensitive ?
function noop(str) {
return str;
} :
function toLowerCase(str) {
return str.toLowerCase();
};
}
// Map a virtual-dom node tree into a data structure that cssauron can use to
// traverse.
function mapTree(vtree, parent, options) {
var normalizeTagCase = getNormalizeCaseFn(options.caseSensitiveTag);
// VText represents text nodes
// See https://github.com/Matt-Esch/virtual-dom/blob/master/docs/vtext.md
if (vtree.type === "VirtualText") {
return {
contents: vtree.text,
parent: parent,
vtree: vtree,
};
}
if (vtree.tagName != null) {
var node = {};
node.parent = parent;
node.vtree = vtree;
node.tagName = normalizeTagCase(vtree.tagName);
if (vtree.properties) {
node.properties = vtree.properties;
if (typeof vtree.properties.className === "string") {
node.className = vtree.properties.className;
}
if (typeof vtree.properties.id === "string") {
node.id = vtree.properties.id;
}
}
if (vtree.children && typeof vtree.children.map === "function") {
node.children = vtree.children.map(function(child) {
return mapTree(child, node, options);
}).filter(Boolean);
}
return node;
}
}