Skip to content

Commit

Permalink
Merge pull request #7 from Livefyre/LF_19535
Browse files Browse the repository at this point in the history
LF-19535: Adding better body validation
  • Loading branch information
markdoten committed Mar 16, 2016
2 parents 720c32f + 46074b1 commit cc066e1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 17 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: clean dist run test

build:
npm install

clean:
rm -rf lib node_modules

dist:
./node_modules/requirejs/bin/r.js -o ./tools/build.conf.js

run:
npm start

test:
npm test
11 changes: 6 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "streamhub-editor",
"version": "1.3.1",
"version": "1.3.2",
"main": [
"./src/editor.js"
],
"dependencies": {
"cajon": "git://github.com/requirejs/cajon.git#0.1.11",
"inherits": "git://github.com/Livefyre/inherits.git",
"streamhub-sdk": "git://github.com/Livefyre/streamhub-sdk.git#2.17.0",
"streamhub-sdk": "git://github.com/Livefyre/streamhub-sdk.git#2.21.3",
"streamhub-ui": "git://github.com/Livefyre/streamhub-ui.git#0.1.3",
"auth": "git://github.com/Livefyre/auth.git#0.4.0",
"livefyre-bootstrap": "git://github.com/Livefyre/livefyre-bootstrap.git#1.2.3",
"livefyre-bootstrap": "git://github.com/Livefyre/livefyre-bootstrap.git#1.3.4",
"jquery": "1.10.2",
"mustache": "2.2.0",
"observer": "git://github.com/Livefyre/Observer.git#0.1.0",
Expand All @@ -27,9 +27,10 @@
},
"resolutions": {
"event-emitter": "0.1",
"livefyre-bootstrap": "v1.3.4",
"livefyre-bootstrap": "1.3.4",
"view": "1.4",
"auth": "0.4.0",
"base64": "~0.4.0"
"base64": "~0.4.0",
"streamhub-ui": "0.2.0"
}
}
19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "Mark Doten",
"email": "mark@livefyre.com"
},
"version": "1.3.1",
"version": "1.3.2",
"dependencies": {
"bower": "1.3.8"
},
Expand All @@ -15,19 +15,16 @@
"http-server": "*",
"less": "1.7",
"less-middleware": "0.1.15",
"mocha-phantomjs": "3.1.6",
"mocha": "*",
"phantomjs": "1.9.2-2",
"karma-script-launcher": "0.1.0",
"karma-chrome-launcher": "0.1.0",
"karma-firefox-launcher": "0.1.0",
"karma-html2js-preprocessor": "0.1.0",
"karma-cajon": "*",
"karma-phantomjs-launcher": "0.1.0",
"karma": "0.10.5",
"karma-mocha": "0.1.0",
"karma-safari-launcher": "0.1.1",
"karma-cajon": "0.0.1",
"karma-coffee-preprocessor": "0.1.0",
"karma-chai": "0.0.2",
"karma-mocha": "0.1.0",
"karma-mocha-reporter": "0.3.0",
"karma-phantomjs-launcher": "0.1.0",
"mocha": "1.18.2",
"mocha-phantomjs": "3.1.6",
"requirejs": "2.1.9"
},
"scripts": {
Expand Down
15 changes: 14 additions & 1 deletion src/javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,24 @@ Editor.prototype.showError = function (msg) {
* @return {boolean} Whether the post data is valid or not.
*/
Editor.prototype.validate = function (data) {
if (!data.body) {
if (!this.validateBody(data.body)) {
this.showError(this._i18n.ERRORS.BODY);
return false;
}
return true;
};

/**
* Validate the body of a post.
* @param {string} body
* @return {boolean}
*/
Editor.prototype.validateBody = function (body) {
if (!body) {
return false;
}
// Ensure that the user hasn't just entered spaces and newlines.
return !/^(<p>\s*<\/p>)*$/.test(body);
};

module.exports = Editor;
1 change: 1 addition & 0 deletions src/styles/editor.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/styles/editor.less
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,9 @@ input.lf-editor-title,
width: 36px;
height: 36px;
}

input.lf-editor-title::-ms-clear {
display: none;
height: 0;
width: 0;
}
20 changes: 20 additions & 0 deletions test/spec/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ describe('streamhub-editor', function() {
expect(view.validate({body: 'test'})).to.be.true;
});

it('should validate the normalized html text from the textarea', function() {
view.showError = function(){};
view.$textareaEl.val('');
var obj = view.buildPostEventObj();
expect(obj.body).to.equal('<p></p>');
expect(view.validate(obj)).to.be.false;
view.$textareaEl.val('\n\n');
obj = view.buildPostEventObj();
expect(obj.body).to.equal('<p></p>');
expect(view.validate(obj)).to.be.false;
view.$textareaEl.val('something');
obj = view.buildPostEventObj();
expect(obj.body).to.equal('<p>something</p>');
expect(view.validate(obj)).to.be.true;
view.$textareaEl.val('\nsomething');
obj = view.buildPostEventObj();
expect(obj.body).to.equal('<p></p><p>something</p>');
expect(view.validate(obj)).to.be.true;
});

it('should reset blank field back to placeholder on blur in normal mode', function() {
view.$textareaEl.val('');
view.$textareaEl.blur();
Expand Down

0 comments on commit cc066e1

Please sign in to comment.