Skip to content

Commit

Permalink
Ticket OscarGodson#204 - applying recommended fixes
Browse files Browse the repository at this point in the history
getFiles includes content by default again
getFiles has a flag to exclude content now
exportFiles has a 'raw' option
exportFiles plaintext escape is now abstracted to a private function
updated docs and tests to reflect this
  • Loading branch information
Alexis Beingessner committed Jun 6, 2013
1 parent c4d80ec commit 5f45428
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 35 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,15 @@ importFileBtn.onclick = function () {

### exportFile([_filename_],[_type_])

Returns the raw content of the file by default (warning: this may be browser-specific), or if given a `type` will return the content converted into that type. If you leave both parameters `null` it will return the current document's raw content.
Returns the plain text of the file by default, or if given a type will return the content in the specified type. If you leave both parameters null it will return the current document's content in plain text. The supported export file types are:

* text (default)
* html
* raw (warning: this is browser specific!)

```javascript
syncWithServerBtn.onclick = function () {
var theContent = editor.exportFile(null, 'text');
var theContent = editor.exportFile();
saveToServerAjaxCall('/save', {data:theContent}, function () {
console.log('Data was saved to the database.');
});
Expand Down Expand Up @@ -329,9 +333,9 @@ removeFileBtn.onclick = function () {
}
```

### getFiles([_name_])
### getFiles([_name_], [_excludeContent_])

If no `name` is given it returns an object containing the names and metadata of all file objects. If a `name` is specified it will return just the metadata of that single file object.
If no `name` is given it returns an object containing the names and metadata of all file objects. If a `name` is specified it will return just the metadata of that single file object. If `excludeContent` is not true, it also includes the contents of the file, in the default format of `exportFile`.

```javascript
var files = editor.getFiles();
Expand Down
46 changes: 34 additions & 12 deletions epiceditor/js/epiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,19 @@
}
}

/**
* Converts the 'raw' format of a file's contents into plaintext
* @param {string} content Contents of the file
* @returns {string} the sanitized content
*/
function sanitizeRawContent(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 @@ -1527,39 +1540,48 @@

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 = sanitizeRawContent(content);
return self.settings.parser(content);
case 'text':
content = content.replace(/\u00a0/g, ' ').replace(/ /g, ' ');
return sanitizeRawContent(content);
case 'raw':
return content;
default:
console.warn('Invalid type given; assuming "raw"');
return content;
}
}

/**
* Gets the metadata for files
* @param {string} name Name of the file whose metadata you want (case sensitive)
* @returns {object} An object with the names and metadata of every file, or just the metadata of one file if a name was given
* Gets the contents and metadata for files
* @param {string} name Name of the file whose data you want (case sensitive)
* @param {boolean} excludeContent whether the contents of files should be excluded
* @returns {object} An object with the names and data of every file, or just the data of one file if a name was given
*/
EpicEditor.prototype.getFiles = function (name) {
EpicEditor.prototype.getFiles = function (name, excludeContent) {
var file
, data = this._getFileStore(name);

if (name) {
if (data !== undefined) {
delete data.content;
if (excludeContent) {
delete data.content;
}
else {
data.content = sanitizeRawContent(data.content);
}
}
return data;
}
else {
for (file in data) {
if (data.hasOwnProperty(file)) {
delete data[file].content;
if (excludeContent) {
delete data[file].content;
}
else {
data[file].content = sanitizeRawContent(data[file].content);
}
}
}
return data;
Expand Down
2 changes: 1 addition & 1 deletion epiceditor/js/epiceditor.min.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,16 @@ <h3>importFile([<em>filename</em>],[<em>content</em>])</h3>
editor.importFile(&#39;some-file&#39;,&quot;#Imported markdown\nFancy, huh?&quot;); //Imports a file when the user clicks this button
}</code></pre>
<h3>exportFile([<em>filename</em>],[<em>type</em>])</h3>
<p>Returns the raw content of the file by default (warning: this may be browser-specific), or if given a <code>type</code> will return the content converted into that type. If you leave both parameters <code>null</code> it will return the current document&#39;s raw content.
<p>Returns the plain text of the file by default, or if given a type will return the content in the specified type. If you leave both parameters null it will return the current document&#39;s content in plain text. The supported export file types are:

</p>
<ul>
<li>text (default)</li>
<li>html</li>
<li>raw (warning: this is browser specific!)</li>
</ul>
<pre><code class="lang-javascript">syncWithServerBtn.onclick = function () {
var theContent = editor.exportFile(null, &#39;text&#39;);
var theContent = editor.exportFile();
saveToServerAjaxCall(&#39;/save&#39;, {data:theContent}, function () {
console.log(&#39;Data was saved to the database.&#39;);
});
Expand All @@ -304,8 +309,8 @@ <h3>remove(<em>name</em>)</h3>
<pre><code class="lang-javascript">removeFileBtn.onclick = function () {
editor.remove(&#39;example.md&#39;);
}</code></pre>
<h3>getFiles([<em>name</em>])</h3>
<p>If no <code>name</code> is given it returns an object containing the names and metadata of all file objects. If a <code>name</code> is specified it will return just the metadata of that single file object.
<h3>getFiles([<em>name</em>], [<em>excludeContent</em>])</h3>
<p>If no <code>name</code> is given it returns an object containing the names and metadata of all file objects. If a <code>name</code> is specified it will return just the metadata of that single file object. If <code>excludeContent</code> is not true, it also includes the contents of the file, in the default format of <code>exportFile</code>.

</p>
<pre><code class="lang-javascript">var files = editor.getFiles();
Expand Down
46 changes: 34 additions & 12 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,19 @@
}
}

/**
* Converts the 'raw' format of a file's contents into plaintext
* @param {string} content Contents of the file
* @returns {string} the sanitized content
*/
function sanitizeRawContent(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(/&nbsp;/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 @@ -1527,39 +1540,48 @@

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(/&nbsp;/g, ' ');
content = sanitizeRawContent(content);
return self.settings.parser(content);
case 'text':
content = content.replace(/\u00a0/g, ' ').replace(/&nbsp;/g, ' ');
return sanitizeRawContent(content);
case 'raw':
return content;
default:
console.warn('Invalid type given; assuming "raw"');
return content;
}
}

/**
* Gets the metadata for files
* @param {string} name Name of the file whose metadata you want (case sensitive)
* @returns {object} An object with the names and metadata of every file, or just the metadata of one file if a name was given
* Gets the contents and metadata for files
* @param {string} name Name of the file whose data you want (case sensitive)
* @param {boolean} excludeContent whether the contents of files should be excluded
* @returns {object} An object with the names and data of every file, or just the data of one file if a name was given
*/
EpicEditor.prototype.getFiles = function (name) {
EpicEditor.prototype.getFiles = function (name, excludeContent) {
var file
, data = this._getFileStore(name);

if (name) {
if (data !== undefined) {
delete data.content;
if (excludeContent) {
delete data.content;
}
else {
data.content = sanitizeRawContent(data.content);
}
}
return data;
}
else {
for (file in data) {
if (data.hasOwnProperty(file)) {
delete data[file].content;
if (excludeContent) {
delete data[file].content;
}
else {
data[file].content = sanitizeRawContent(data[file].content);
}
}
}
return data;
Expand Down
11 changes: 9 additions & 2 deletions test/test.getFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ describe('.getFiles([name])', function () {
expect(fileCount).to.be(3);
});

it('should return just one file when the name is specified', function () {
it('should return the right file when the name is specified', function () {
var file = editor.getFiles(fooFile);
expect(file).not.to.be(undefined);
expect(file).to.have.property("modified");
expect(file).to.have.property('modified');
expect(file).to.have.property('created');
expect(file.content).to.be('foo');
});

it('should exclude content when excludeContent is set', function () {
var file = editor.getFiles(fooFile, true);
expect(file).not.to.have.property('content');
});
});

1 comment on commit 5f45428

@OscarGodson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems pretty good. I'm testing it now and will make a few changes, but I think I'll be able to get this merged in tonight.

Please sign in to comment.