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
var utils = require('./utils'),
browser = require('./browser');
module.exports = function(window, less, options) {
function errorHTML(e, rootHref) {
var id = 'less-error-message:' + utils.extractId(rootHref || '');
var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
var elem = window.document.createElement('div'), timer, content, errors = [];
var filename = e.filename || rootHref;
var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];
elem.id = id;
elem.className = 'less-error-message';
content = '<h3>' + (e.type || 'Syntax') + 'Error: ' + (e.message || 'There is an error in your .less file') +
'</h3>' + '<p>in <a href="' + filename + '">' + filenameNoPath + '</a> ';
var errorline = function (e, i, classname) {
if (e.extract[i] !== undefined) {
errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1))
.replace(/\{class\}/, classname)
.replace(/\{content\}/, e.extract[i]));
}
};
if (e.line) {
errorline(e, 0, '');
errorline(e, 1, 'line');
errorline(e, 2, '');
content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' +
'<ul>' + errors.join('') + '</ul>';
}
if (e.stack && (e.extract || options.logLevel >= 4)) {
content += '<br/>Stack Trace</br />' + e.stack.split('\n').slice(1).join('<br/>');
}
elem.innerHTML = content;
// CSS for error messages
browser.createCSS(window.document, [
'.less-error-message ul, .less-error-message li {',
'list-style-type: none;',
'margin-right: 15px;',
'padding: 4px 0;',
'margin: 0;',
'}',
'.less-error-message label {',
'font-size: 12px;',
'margin-right: 15px;',
'padding: 4px 0;',
'color: #cc7777;',
'}',
'.less-error-message pre {',
'color: #dd6666;',
'padding: 4px 0;',
'margin: 0;',
'display: inline-block;',
'}',
'.less-error-message pre.line {',
'color: #ff0000;',
'}',
'.less-error-message h3 {',
'font-size: 20px;',
'font-weight: bold;',
'padding: 15px 0 5px 0;',
'margin: 0;',
'}',
'.less-error-message a {',
'color: #10a',
'}',
'.less-error-message .error {',
'color: red;',
'font-weight: bold;',
'padding-bottom: 2px;',
'border-bottom: 1px dashed red;',
'}'
].join('\n'), { title: 'error-message' });
elem.style.cssText = [
'font-family: Arial, sans-serif',
'border: 1px solid #e00',
'background-color: #eee',
'border-radius: 5px',
'-webkit-border-radius: 5px',
'-moz-border-radius: 5px',
'color: #e00',
'padding: 15px',
'margin-bottom: 15px'
].join(';');
if (options.env === 'development') {
timer = setInterval(function () {
var document = window.document,
body = document.body;
if (body) {
if (document.getElementById(id)) {
body.replaceChild(elem, document.getElementById(id));
} else {
body.insertBefore(elem, body.firstChild);
}
clearInterval(timer);
}
}, 10);
}
}
function removeErrorHTML(path) {
var node = window.document.getElementById('less-error-message:' + utils.extractId(path));
if (node) {
node.parentNode.removeChild(node);
}
}
function removeErrorConsole(path) {
// no action
}
function removeError(path) {
if (!options.errorReporting || options.errorReporting === 'html') {
removeErrorHTML(path);
} else if (options.errorReporting === 'console') {
removeErrorConsole(path);
} else if (typeof options.errorReporting === 'function') {
options.errorReporting('remove', path);
}
}
function errorConsole(e, rootHref) {
var template = '{line} {content}';
var filename = e.filename || rootHref;
var errors = [];
var content = (e.type || 'Syntax') + 'Error: ' + (e.message || 'There is an error in your .less file') +
' in ' + filename;
var errorline = function (e, i, classname) {
if (e.extract[i] !== undefined) {
errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1))
.replace(/\{class\}/, classname)
.replace(/\{content\}/, e.extract[i]));
}
};
if (e.line) {
errorline(e, 0, '');
errorline(e, 1, 'line');
errorline(e, 2, '');
content += ' on line ' + e.line + ', column ' + (e.column + 1) + ':\n' +
errors.join('\n');
}
if (e.stack && (e.extract || options.logLevel >= 4)) {
content += '\nStack Trace\n' + e.stack;
}
less.logger.error(content);
}
function error(e, rootHref) {
if (!options.errorReporting || options.errorReporting === 'html') {
errorHTML(e, rootHref);
} else if (options.errorReporting === 'console') {
errorConsole(e, rootHref);
} else if (typeof options.errorReporting === 'function') {
options.errorReporting('add', e, rootHref);
}
}
return {
add: error,
remove: removeError
};
};