forked from jslint-org/jslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
193 lines (163 loc) · 5.65 KB
/
browser.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// browser.js
// 2017-12-26
// Copyright (c) 2015 Douglas Crockford (www.JSLint.com)
/*jslint
browser, for
*/
/*global
ADSAFE, REPORT, jslint
*/
/*property
___nodes___, check, create, each, enable, error, focus, function, getCheck,
getName, getTitle, getValue, innerHTML, isFinite, length, lib, on,
onscroll, property, q, scrollTop, select, split, style, value
*/
// This is the web script companion file for JSLint. It includes code for
// interacting with the browser and generating the reports.
ADSAFE.lib("browser_ui", function () {
"use strict";
var rx_crlf = /\n|\r\n?/;
var rx_separator = /[\s,;'"]+/;
function setHTML(bunch, html) {
bunch.___nodes___[0].innerHTML = html;
}
function setScrollTop(bunch, number) {
bunch.___nodes___[0].scrollTop = number;
}
function getScrollTop(bunch) {
return bunch.___nodes___[0].scrollTop;
}
function onscroll(bunch, handler) {
bunch.___nodes___[0].onscroll = handler;
}
return function (dom) {
// This function is the entry point to this web module.
// First get handles to some of the page features.
var warnings = dom.q("#JSLINT_WARNINGS");
var warnings_div = warnings.q(">div");
var options = dom.q("#JSLINT_OPTIONS");
var global = dom.q("#JSLINT_GLOBAL");
var property_fieldset = dom.q("#JSLINT_PROPERTYFIELDSET");
var property = dom.q("#JSLINT_PROPERTY");
var report_field = dom.q("#JSLINT_REPORT");
var report_div = report_field.q(">div");
var select = dom.q("#JSLINT_SELECT");
var source = dom.q("#JSLINT_SOURCE");
var number = dom.q("#JSLINT_NUMBER");
var fudge = dom.q("#JSLINT_FUDGE");
var aux = dom.q("#JSLINT_AUX");
function clear() {
warnings.style("display", "none");
report_field.style("display", "none");
property_fieldset.style("display", "none");
aux.style("display", "none");
number.value("");
property.value("");
report_div.value("");
source.value("");
source.focus();
warnings_div.value("");
}
function clear_options() {
options.q("input_checkbox").check(false);
options.q("input_text").value("");
global.value("");
}
function show_numbers() {
var f = Number(fudge.getCheck());
var n = source.getValue().split(rx_crlf).length + f;
var text = "";
var i;
for (i = f; i <= n; i += 1) {
text += i + "\n";
}
number.value(text);
}
function mark_scroll() {
var ss = getScrollTop(source);
setScrollTop(number, ss);
var sn = getScrollTop(number);
if (ss > sn) {
show_numbers();
setScrollTop(number, ss);
}
}
function call_jslint() {
// First build the option object.
var option = Object.create(null);
options.q("input_checkbox:checked").each(function (node) {
option[node.getTitle()] = true;
});
options.q("input_text").each(function (node) {
var value = Number(node.getValue());
if (Number.isFinite(value) && value > 0) {
option[node.getTitle()] = value;
}
});
// Call JSLint with the source text, the options, and the predefined globals.
var global_string = global.getValue();
var result = jslint(
source.getValue(),
option,
(global_string === "")
? undefined
: global_string.split(rx_separator)
);
// Generate the reports.
var error_html = REPORT.error(result);
var function_html = REPORT.function(result);
var property_text = REPORT.property(result);
// Display the reports.
setHTML(warnings_div, error_html);
warnings.style(
"display",
(error_html.length === 0)
? "none"
: "block"
);
setHTML(report_div, function_html);
report_field.style("display", "block");
if (property_text) {
property.value(property_text);
property_fieldset.style("display", "block");
setScrollTop(property, 0);
select.enable(true);
} else {
property_fieldset.style("display", "none");
select.enable(false);
}
aux.style("display", "block");
source.select();
}
function select_property_directive() {
property.select();
}
// Lay in the click handlers.
dom.q("button").each(function (button) {
switch (button.getName()) {
case "JSLint":
button.on("click", call_jslint);
break;
case "clear":
button.on("click", clear);
break;
case "options":
button.on("click", clear_options);
break;
case "select":
button.on("click", select_property_directive);
break;
}
});
fudge.on("change", function (ignore) {
show_numbers();
});
source.on("change", function (ignore) {
show_numbers();
});
onscroll(source, function (ignore) {
mark_scroll();
});
source.select();
};
});