nwmatcher-webforms.js
3.61 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
/*
* Copyright (C) 2007-2018 Diego Perini
* All rights reserved.
*
* this is just a small example to show
* how an extension for NWMatcher could be
* adapted to handle WebForms/HTML5 selectors
*
* Pseudo Selectors
* :default, :indeterminate, :optional, :required,
* :valid, :invalid, :in-range, :out-of-range,
* :read-only, :read-write
* :has, :matches (not yet in a defined specification)
*
*/
// for UI pseudo-classes extensions (WebForms/HTML5)
NW.Dom.registerSelector(
'html5:pseudos',
/^\:(default|indeterminate|optional|required|valid|invalid|in-range|out-of-range|read-only|read-write)(.*)/,
(function(global) {
return function(match, source) {
var status = true,
HTML5PseudoClasses = {
'default': 4, 'indeterminate': 4, 'invalid': 4, 'valid': 4,
'optional': 4, 'required': 4, 'read-write': 4, 'read-only': 4
};
switch (match[1]) {
// HTML5 UI element states (form controls)
case 'default':
// only radio buttons, check boxes and option elements
source = 'if(((typeof e.form!=="undefined"&&(/radio|checkbox/i).test(e.type))||/option/i.test(e.nodeName))&&(e.defaultChecked||e.defaultSelected)){' + source + '}';
break;
case 'indeterminate':
// only radio buttons, check boxes and option elements
source = 'if(typeof e.form!=="undefined"&&(/radio|checkbox/i).test(e.type)&&s.select("[checked]",e.form).length===0){' + source + '}';
break;
case 'optional':
// only fields for which "required" applies
source = 'if(typeof e.form!=="undefined"&&typeof e.required!="undefined"&&!e.required){' + source + '}';
break;
case 'required':
// only fields for which "required" applies
source = 'if(typeof e.form!=="undefined"&&typeof e.required!="undefined"&&e.required){' + source + '}';
break;
case 'read-write':
// only fields for which "readOnly" applies
source = 'if(typeof e.form!=="undefined"&&typeof e.readOnly!="undefined"&&!e.readOnly){' + source + '}';
break;
case 'read-only':
// only fields for which "readOnly" applies
source = 'if(typeof e.form!=="undefined"&&typeof e.readOnly!="undefined"&&e.readOnly){' + source + '}';
break;
case 'invalid':
// only fields for which validity applies
source = 'if(typeof e.form!=="undefined"&&typeof e.validity=="object"&&!e.validity.valid){' + source + '}';
break;
case 'valid':
// only fields for which validity applies
source = 'if(typeof e.form!=="undefined"&&typeof e.validity=="object"&&e.validity.valid){' + source + '}';
break;
case 'in-range':
// only fields for which validity applies
source = 'if(typeof e.form!=="undefined"&&' +
'(s.getAttribute(e,"min")||s.getAttribute(e,"max"))&&' +
'typeof e.validity=="object"&&!e.validity.typeMismatch&&' +
'!e.validity.rangeUnderflow&&!e.validity.rangeOverflow){' + source + '}';
break;
case 'out-of-range':
// only fields for which validity applies
source = 'if(typeof e.form!=="undefined"&&' +
'(s.getAttribute(e,"min")||s.getAttribute(e,"max"))&&' +
'typeof e.validity=="object"&&(e.validity.rangeUnderflow||e.validity.rangeOverflow)){' + source + '}';
break;
default:
status = false;
break;
}
// compiler will add this to "source"
return {
'source': source,
'status': status
};
};
})(this));