Skip to content

Commit

Permalink
Ticket OscarGodson#204 - escaping content in firefox getFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Beingessner committed Jun 5, 2013
1 parent 2b05b96 commit fc271ff
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
37 changes: 29 additions & 8 deletions epiceditor/js/epiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,14 @@
return this;
};

function sanitizeContent(content) {
// Get this, 2 spaces in a content editable actually converts to:
// 0020 00a0, meaning, "space no-break space". So, manually convert
// no-break spaces to spaces again before handing to marked.
// Also, WebKit converts no-break to unicode equivalent and FF HTML.
return content.replace(/\u00a0/g, ' ').replace(/ /g, ' ');
}

/**
* Exports a file as a string in a supported format
* @param {string} name Name of the file you want to export (case sensitive)
Expand All @@ -1504,30 +1512,43 @@

switch (kind) {
case 'html':
// Get this, 2 spaces in a content editable actually converts to:
// 0020 00a0, meaning, "space no-break space". So, manually convert
// no-break spaces to spaces again before handing to marked.
// Also, WebKit converts no-break to unicode equivalent and FF HTML.
content = content.replace(/\u00a0/g, ' ').replace(/ /g, ' ');
content = sanitizeContent(content);
return self.settings.parser(content);
case 'text':
content = content.replace(/\u00a0/g, ' ').replace(/ /g, ' ');
return content;
return sanitizeContent(content);
default:
return content;
}
}

function sanitizeFile(file) {
if (file === undefined) {
return file;
}
var content = file.content;
Object.defineProperty(file, "content", {
get: function () {
return sanitizeContent(content);
}
});
return file;
}

EpicEditor.prototype.getFiles = function (name, _isPreviewDraft) {
var previewDraftName = '';
if (_isPreviewDraft) {
previewDraftName = this._previewDraftLocation;
}
var files = JSON.parse(this._storage[previewDraftName + this.settings.localStorageName]);
if (name) {
return files[name];
return sanitizeFile(files[name]);
}
else {
for (var file in files) {
if (files.hasOwnProperty(file)) {
sanitizeFile(files[file]);
}
}
return files;
}
}
Expand Down
2 changes: 1 addition & 1 deletion epiceditor/js/epiceditor.min.js

Large diffs are not rendered by default.

37 changes: 29 additions & 8 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,14 @@
return this;
};

function sanitizeContent(content) {
// Get this, 2 spaces in a content editable actually converts to:
// 0020 00a0, meaning, "space no-break space". So, manually convert
// no-break spaces to spaces again before handing to marked.
// Also, WebKit converts no-break to unicode equivalent and FF HTML.
return content.replace(/\u00a0/g, ' ').replace(/ /g, ' ');
}

/**
* Exports a file as a string in a supported format
* @param {string} name Name of the file you want to export (case sensitive)
Expand All @@ -1504,30 +1512,43 @@

switch (kind) {
case 'html':
// Get this, 2 spaces in a content editable actually converts to:
// 0020 00a0, meaning, "space no-break space". So, manually convert
// no-break spaces to spaces again before handing to marked.
// Also, WebKit converts no-break to unicode equivalent and FF HTML.
content = content.replace(/\u00a0/g, ' ').replace(/ /g, ' ');
content = sanitizeContent(content);
return self.settings.parser(content);
case 'text':
content = content.replace(/\u00a0/g, ' ').replace(/ /g, ' ');
return content;
return sanitizeContent(content);
default:
return content;
}
}

function sanitizeFile(file) {
if (file === undefined) {
return file;
}
var content = file.content;
Object.defineProperty(file, "content", {
get: function () {
return sanitizeContent(content);
}
});
return file;
}

EpicEditor.prototype.getFiles = function (name, _isPreviewDraft) {
var previewDraftName = '';
if (_isPreviewDraft) {
previewDraftName = this._previewDraftLocation;
}
var files = JSON.parse(this._storage[previewDraftName + this.settings.localStorageName]);
if (name) {
return files[name];
return sanitizeFile(files[name]);
}
else {
for (var file in files) {
if (files.hasOwnProperty(file)) {
sanitizeFile(files[file]);
}
}
return files;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/test.getFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ describe('.getFiles([name])', function () {
it('should return the correct file when the name is specified', function () {
expect(editor.getFiles(fooFile).content).to.be('foo');
});

it('should not warp white space', function () {
var spaceString = "foo\n foo\n foo";
editor.importFile(fooFile, spaceString);
editor.save();
expect(editor.getFiles(fooFile).content).to.be(spaceString);
});
});

0 comments on commit fc271ff

Please sign in to comment.