Skip to content

Commit

Permalink
Escape "&" character
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesús Ángel committed Jan 24, 2019
1 parent 5c1a3ef commit d3aa56f
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions public/directives/wz-xml-file-editor/wz-xml-file-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,56 @@ app.directive('wzXmlFileEditor', function() {
targetName: '=targetName'
},
controller($scope, $document, errorHandler, groupHandler) {

/**
* Custom .replace method. Instead of using .replace which
* evaluates regular expressions.
* Alternative using split + join, same result.
*/
String.prototype.xmlReplace = function(str, newstr) {
return this.split(str).join(newstr);
};

let firstTime = true;
const parser = new DOMParser(); // eslint-disable-line

/**
* Escape "&" characters.
* @param {*} text
*/
const replaceIllegalXML = text => {
const oDOM = parser.parseFromString(text, 'text/html');
const lines = oDOM.documentElement.textContent.split('\n');

for (const line of lines) {
const sanitized = line.trim().xmlReplace('&', '&');

/**
* Do not remove this condition. We don't want to replace
* non-sanitized lines.
*/
if (!line.includes(sanitized)) {
text = text.xmlReplace(line.trim(), sanitized);
}
}
return text;
};

// Block function if there is another check in progress
let checkingXmlError = false;
const checkXmlParseError = () => {
if (checkingXmlError) return;
checkingXmlError = true;
try {
const parser = new DOMParser(); // eslint-disable-line
const xml = $scope.xmlCodeBox.getValue();
const text = $scope.xmlCodeBox.getValue();

const xml = replaceIllegalXML(text);

const xmlDoc = parser.parseFromString(
'<file>' + xml + '</file>',
'text/xml'
);

$scope.validFn({
valid:
!!xmlDoc.getElementsByTagName('parsererror').length ||
Expand All @@ -44,6 +85,7 @@ app.directive('wzXmlFileEditor', function() {
} catch (error) {
errorHandler.handle(error, 'Error validating XML');
}
checkingXmlError = false;
if (!$scope.$$phase) $scope.$digest();
return;
};
Expand All @@ -59,8 +101,9 @@ app.directive('wzXmlFileEditor', function() {

const saveFile = async params => {
try {
const content = $scope.xmlCodeBox.getValue().trim();
await groupHandler.sendConfiguration(params.group, content);
const text = $scope.xmlCodeBox.getValue();
const xml = replaceIllegalXML(text);
await groupHandler.sendConfiguration(params.group, xml);
errorHandler.info('Success. Group has been updated', '');
} catch (error) {
errorHandler.handle(error, 'Send file error');
Expand Down

0 comments on commit d3aa56f

Please sign in to comment.