-
Notifications
You must be signed in to change notification settings - Fork 3
/
validation.js
264 lines (228 loc) · 9.63 KB
/
validation.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict';
var $ = require('jquery');
require('jquery-ui/ui/widgets/button');
/**
* @class Validation
* @param {Object} config
* @param {Writer} config.writer
* @param {String} config.parentId
* @param {String} config.validationUrl
*/
function Validation(config) {
var w = config.writer;
var validationUrl = config.validationUrl;
if (validationUrl === undefined) {
console.error('Validation: no validationUrl specified!');
}
var id = w.getUniqueId('validation_');
$('#'+config.parentId).append(`
<div class="moduleParent">
<div id="${id}" class="moduleContent">
<ul class="validationList"></ul>
</div>
<div id="${id}_buttons" class="moduleFooter">
<button type="button" role="validate">Validate</button>
<button type="button" role="clear">Clear</button>
</div>
</div>
`);
w.event('documentLoaded').subscribe(function() {
validation.clearResult();
});
w.event('validationRequested').subscribe(function() {
var list = $('#'+id+' > ul');
list.empty();
list.append(''+
'<li class="ui-state-default">'+
'<span class="loading"></span> Validating...'+
'</li>');
w.layoutManager.showModule('validation');
validation.validate();
});
w.event('documentValidated').subscribe(function(valid, resultDoc, docString) {
$('#'+id+'_indicator').hide();
validation.showValidationResult(resultDoc, docString);
});
/**
* @lends Validation.prototype
*/
var validation = {};
validation.validate = function() {
w.converter.getDocumentContent(false, (docText) => {
const schemaUrl = w.schemaManager.getXMLUrl();
$.ajax({
url: validationUrl,
type: 'POST',
dataType: 'xml',
data: {
sch: schemaUrl,
type: 'RNG_XML',
content: docText
},
success: function(data, status, xhr) {
var valid = $('status', data).text() == 'pass';
w.event('documentValidated').publish(valid, data, docText);
},
error: function() {
w.dialogManager.show('message', {
title: 'Error',
msg: 'An error occurred while trying to validate the document.',
type: 'error'
});
w.event('documentValidated').publish(null, '', '');
}
});
});
};
/**
* Processes a validation response from the server.
* @param resultDoc The actual response
* @param docString The doc string sent to the server for validation
*/
validation.showValidationResult = function(resultDoc, docString) {
var list = $('#'+id+' > ul');
list.empty();
docString = docString.split('\n')[1]; // remove the xml header
var status = $('status', resultDoc).text();
if (status == 'pass') {
list.append(''+
'<li class="ui-state-default">'+
'<span class="ui-icon ui-icon-check" style="float: left; margin-right: 4px;"></span>Your document is valid!'+
'</li>');
}
w.tagger.removeNoteWrappersForEntities();
$('warning', resultDoc).each(function(index, el) {
var id = null;
var type = el.nodeName;
var message = $(this).find('message').text();
var path = $(this).find('path').text();
var elementId = $(this).find('elementId').text();
var column = parseInt($(this).find('column').text());
if (elementId != '') {
id = elementId;
}
if (id == null && path != '') {
// convert xpath to jquery selector
var editorPath = '';
var tags = path.split('/');
for (var i = 0; i < tags.length; i++) {
var tag = tags[i];
var tagName = tag.match(/^\w+(?=\[)?/);
if (tagName != null) {
var index = tag.match(/\[(\d+)\]/);
if (index === null) {
index = 0;
} else {
index = parseInt(index[1]);
index--; // xpath is 1-based and "eq()" is 0-based
}
editorPath += '*[_tag="'+tagName[0]+'"]:eq('+index+') > ';
}
}
editorPath = editorPath.substr(0, editorPath.length-3);
var docEl = $(editorPath, w.editor.getBody());
id = docEl.attr('id');
}
if (id == null && !isNaN(column)) {
var docSubstring = docString.substring(0, column);
var tags = docSubstring.match(/<.*?>/g);
if (tags != null) {
var tag = tags[tags.length-1];
id = tag.match(/id="(.*?)"/i);
if (id == null) {
if (message.search('text not allowed here') != -1) {
// find the parent tag
var level = 0;
for (var i = tags.length-1; i > -1; i--) {
tag = tags[i];
if (tag.search('/') != -1) {
level++; // closing tag, add a level
} else {
level--; // opening tag, remove a level
}
if (level == -1) {
var match = tag.match(/id="(.*?)"/i);
if (match != null && match[1]) id = match[1];
break;
}
}
} else {
var tagMatch = tag.match(/<\/(.*)>/);
if (tagMatch != null) {
// it's and end tag, so find the matching start tag
var tagName = tagMatch[1];
for (var i = tags.length-1; i > -1; i--) {
tag = tags[i];
var startTagName = tag.match(/<(.*?)\s/);
if (startTagName != null && startTagName[1] == tagName) {
id = tag.match(/id="(.*?)"/i)[1];
break;
}
}
} else {
// probably entity tag
}
}
} else {
id = id[1];
}
} else {
// can't find any tags!
}
}
if (id != null) {
var messageParts;
var messageDivLoc = message.indexOf(';');
if (messageDivLoc != -1) {
messageParts = [message.slice(0, messageDivLoc+1), message.slice(messageDivLoc+2)];
} else {
messageParts = [message];
}
var messageHtml = '<li class="'+(type=='warning'?'ui-state-error':'ui-state-highlight')+'">'+
'<span class="ui-icon '+(type=='warning'?'ui-icon-alert':'ui-icon-info')+'" style="float: left; margin-right: 4px;"></span>'+
'Path: '+path+'<br/>'+
'Message: '+messageParts[0];
if (messageParts[1] !== undefined) {
messageHtml += ' <span class="message_more">more...</span><span style="display: none;">'+messageParts[1]+'</span>';
}
messageHtml += '</li>';
var item = list.append(messageHtml).find('li:last');
item.find('[class="message_more"]').click(function() {
$(this).next('span').show();
$(this).hide();
});
item.data('id', id);
} else {
console.warn("validation: couldn't find element for "+path);
}
});
w.tagger.addNoteWrappersForEntities();
list.find('li').click(function() {
list.find('li').removeClass('selected');
$(this).addClass('selected');
var id = $(this).data('id');
w.utilities.selectElementById(id);
});
w.layoutManager.showModule('validation');
};
validation.clearResult = function() {
$('#'+id+'_indicator').hide();
$('#'+id+' > ul').empty();
};
var $validateButton = $('#'+id+'_buttons button[role=validate]').button();
$validateButton.click(function() {
validation.validate();
});
var $clearButton = $('#'+id+'_buttons button[role=clear]').button();
$clearButton.click(function() {
validation.clearResult();
});
validation.destroy = function() {
$validateButton.button('destroy');
$clearButton.button('destroy');
};
// add to writer
w.validation = validation;
return validation;
};
module.exports = Validation;