Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81144 views
1
/*
2
* Copyright (C) 2007-2015 Diego Perini
3
* All rights reserved.
4
*
5
* this is just a small example to show
6
* how an extension for NWMatcher could be
7
* adapted to handle special jQuery selectors
8
*
9
* Child Selectors
10
* :even, :odd, :eq, :lt, :gt, :first, :last, :nth
11
*
12
* Pseudo Selectors
13
* :has, :button, :header, :input, :checkbox, :radio, :file, :image
14
* :password, :reset, :submit, :text, :hidden, :visible, :parent
15
*
16
*/
17
18
// for structural pseudo-classes extensions
19
NW.Dom.registerSelector(
20
'jquery:child',
21
/^\:((?:(nth|eq|lt|gt)\(([^()]*)\))|(?:even|odd|first|last))(.*)/i,
22
(function(global) {
23
24
return function(match, source, selector) {
25
26
var status = true, ACCEPT_NODE = NW.Dom.ACCEPT_NODE;
27
28
switch (match[1].toLowerCase()) {
29
case 'odd':
30
source = source.replace(ACCEPT_NODE, 'if((x=x^1)==0){' + ACCEPT_NODE + '}');
31
break;
32
case 'even':
33
source = source.replace(ACCEPT_NODE, 'if((x=x^1)==1){' + ACCEPT_NODE + '}');
34
break;
35
case 'first':
36
source = 'n=h.getElementsByTagName(e.nodeName);if(n.length&&n[0]===e){' + source + '}';
37
break;
38
case 'last':
39
source = 'n=h.getElementsByTagName(e.nodeName);if(n.length&&n[n.length-1]===e){' + source + '}';
40
break;
41
default:
42
switch (match[2].toLowerCase()) {
43
case 'nth':
44
source = 'n=h.getElementsByTagName(e.nodeName);if(n.length&&n[' + match[3] + ']===e){' + source + '}';
45
break;
46
case 'eq':
47
source = source.replace(ACCEPT_NODE, 'if(x++==' + match[3] + '){' + ACCEPT_NODE + '}');
48
break;
49
case 'lt':
50
source = source.replace(ACCEPT_NODE, 'if(x++<' + match[3] + '){' + ACCEPT_NODE + '}');
51
break;
52
case 'gt':
53
source = source.replace(ACCEPT_NODE, 'if(x++>' + match[3] + '){' + ACCEPT_NODE + '}');
54
break;
55
default:
56
status = false;
57
break;
58
}
59
break;
60
}
61
62
// compiler will add this to "source"
63
return global.Object({
64
'source': source,
65
'status': status
66
});
67
68
};
69
70
})(this));
71
72
// for element pseudo-classes extensions
73
NW.Dom.registerSelector(
74
'jquery:pseudo',
75
/^\:(has|checkbox|file|image|password|radio|reset|submit|text|button|input|header|hidden|visible|parent)(?:\(\s*(["']*)?([^'"()]*)\2\s*\))?(.*)/i,
76
(function(global) {
77
78
return function(match, source) {
79
80
var status = true, ACCEPT_NODE = NW.Dom.ACCEPT_NODE;
81
82
switch(match[1].toLowerCase()) {
83
case 'has':
84
source = source.replace(ACCEPT_NODE, 'if(e.getElementsByTagName("' + match[3].replace(/^\s|\s$/g, '') + '")[0]){' + ACCEPT_NODE + '}');
85
break;
86
case 'checkbox':
87
case 'file':
88
case 'image':
89
case 'password':
90
case 'radio':
91
case 'reset':
92
case 'submit':
93
case 'text':
94
// :checkbox, :file, :image, :password, :radio, :reset, :submit, :text
95
source = 'if(/^' + match[1] + '$/i.test(e.type)){' + source + '}';
96
break;
97
case 'button':
98
case 'input':
99
source = 'if(e.type||/button/i.test(e.nodeName)){' + source + '}';
100
break;
101
case 'header':
102
source = 'if(/h[1-6]/i.test(e.nodeName)){' + source + '}';
103
break;
104
case 'hidden':
105
source = 'if(!e.offsetWidth&&!e.offsetHeight){' + source + '}';
106
break;
107
case 'visible':
108
source = 'if(e.offsetWidth||e.offsetHeight){' + source + '}';
109
break;
110
case 'parent':
111
source += 'if(e.firstChild){' + source + '}';
112
break;
113
default:
114
status = false;
115
break;
116
}
117
118
// compiler will add this to "source"
119
return global.Object({
120
'source': source,
121
'status': status
122
});
123
124
};
125
126
})(this));
127
128