Skip to content
Snippets Groups Projects
Commit 2dc23982 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch '444-overlay-download' into 'devel_12.0.x'

Resolve "Overlay download (v12)"

See merge request !360
parents d5be0663 0b7921ac
No related branches found
No related tags found
1 merge request!360Resolve "Overlay download (v12)"
Pipeline #
......@@ -116,6 +116,10 @@ AddOverlayDialog.prototype.processFile = function (file) {
if (overlay.getDescription() !== undefined) {
descriptionInput.value = overlay.getDescription();
}
if (overlayParser.containsMixedNewLineCharacters(evt.target.result)) {
GuiConnector.warn("Selected file contains new line characters from different operating systems " +
"(MAC/Windows/Linux). This might cause confusion when reading the file in the editor later on.")
}
resolve(self.getFileContent());
} catch (error) {
reject(error);
......
......@@ -7,10 +7,21 @@ var TextDecoder = require('text-encoding').TextDecoder;
function OverlayParser() {
}
OverlayParser.prototype.parse = function (content) {
/**
*
* @param {string| Uint8Array | ArrayBuffer} content
* @returns {string}
* @private
*/
OverlayParser.prototype._extractContent = function (content) {
if (content instanceof Uint8Array || content instanceof ArrayBuffer) {
content = new TextDecoder("UTF8").decode(content);
}
return content;
};
OverlayParser.prototype.parse = function (content) {
content = this._extractContent(content);
var data = {content: content};
var lines = content.split("\n");
for (var i = 0; i < lines.length; i++) {
......@@ -37,5 +48,36 @@ OverlayParser.prototype.parse = function (content) {
return new DataOverlay(data);
};
/**
*
* @param {string| Uint8Array | ArrayBuffer} content
* @returns {boolean}
*/
OverlayParser.prototype.containsMixedNewLineCharacters = function (content) {
content = this._extractContent(content);
var newLineRegEx = /[\r\n]+/g;
var match = newLineRegEx.exec(content);
var newLineFormats = {};
var counter = 0;
while (match !== null && match !== undefined) {
var foundMultiplication = false;
var key = '';
//this is just a heuristic - let's assume there are at most 10 empty lines in a file
for (var i = 0; i < 10; i++) {
key += match[0];
if (newLineFormats[key]) {
foundMultiplication = true;
} else {
newLineFormats[key] = true;
}
}
if (!foundMultiplication) {
counter++;
}
match = newLineRegEx.exec(content);
}
return counter > 1;
};
module.exports = OverlayParser;
......@@ -42,5 +42,50 @@ describe('OverlayParser', function () {
});
});
});
describe('containsMixedNewLineCharacters', function () {
it('normal file', function () {
return ServerConnector.sendGetRequest("testFiles/overlay/good.txt").then(function (fileContent) {
var parser = new OverlayParser();
assert.notOk(parser.containsMixedNewLineCharacters(fileContent));
});
});
it('windows/linux mix', function () {
var content = "line1\n\rline2\n";
var parser = new OverlayParser();
assert.ok(parser.containsMixedNewLineCharacters(content));
});
it('windows empty lines', function () {
var content = "line1\n\rline2\n\r\n\rline4";
var parser = new OverlayParser();
assert.notOk(parser.containsMixedNewLineCharacters(content));
});
it('windows/mac mix', function () {
var content = "line1\n\rline2\r";
var parser = new OverlayParser();
assert.ok(parser.containsMixedNewLineCharacters(content));
});
it('mac empty lines', function () {
var content = "line1\rline2\r\rline4";
var parser = new OverlayParser();
assert.notOk(parser.containsMixedNewLineCharacters(content));
});
it('windows/mac mix 2', function () {
var content = "line1\n\r\rline2\n\rline3";
var parser = new OverlayParser();
assert.ok(parser.containsMixedNewLineCharacters(content));
});
it('linux empty lines', function () {
var content = "line1\nline2\n\nline4";
var parser = new OverlayParser();
assert.notOk(parser.containsMixedNewLineCharacters(content));
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment