diff --git a/src/node/db/API.js b/src/node/db/API.js index a9df2a12f..bf8afacf0 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -408,12 +408,18 @@ exports.setHTML = function(padID, html, callback) getPadSafe(padID, true, function(err, pad) { if(ERR(err, callback)) return; - // add a new changeset with the new html to the pad - importHtml.setPadHTML(pad, cleanText(html), callback); - - //update the clients on the pad - padMessageHandler.updatePadClients(pad, callback); + importHtml.setPadHTML(pad, cleanText(html), function(e){ + if(e){ + callback(new customError("HTML is malformed","apierror")); + return; + }else{ + //update the clients on the pad + padMessageHandler.updatePadClients(pad, callback); + callback(); + return; + } + }); }); } diff --git a/src/node/utils/ImportHtml.js b/src/node/utils/ImportHtml.js index 59802f9bf..6bcf078d3 100644 --- a/src/node/utils/ImportHtml.js +++ b/src/node/utils/ImportHtml.js @@ -22,9 +22,8 @@ var cheerio = require("cheerio"); function setPadHTML(pad, html, callback) { var apiLogger = log4js.getLogger("ImportHtml"); - +console.warn("setting padhtml"); var $ = cheerio.load(html); - // Appends a line break, used by Etherpad to ensure a caret is available // below the last line of an import $('body').append("
"); @@ -40,7 +39,7 @@ function setPadHTML(pad, html, callback) cc.collectContent(doc); }catch(e){ apiLogger.warn("HTML was not properly formed", e); - return; // We don't process the HTML because it was bad.. + return callback(e); // We don't process the HTML because it was bad.. } var result = cc.finish(); @@ -91,6 +90,7 @@ function setPadHTML(pad, html, callback) apiLogger.debug('The changeset: ' + theChangeset); pad.setText(""); pad.appendRevision(theChangeset); + callback(); } exports.setPadHTML = setPadHTML; diff --git a/tests/backend/specs/api/pad.js b/tests/backend/specs/api/pad.js index 700c498f6..a5634d605 100644 --- a/tests/backend/specs/api/pad.js +++ b/tests/backend/specs/api/pad.js @@ -61,6 +61,9 @@ describe('Permission', function(){ -> setText(padId) -> getLastEdited(padID) -- Should be when setText was performed -> padUsers(padID) -- Should be when setText was performed + -> setHTML(padID) -- Should fail on invalid HTML + -> setHTML(padID) -- Should fail on invalid HTML + -> getHTML(padID) -- Should return HTML close to posted HTML */ describe('deletePad', function(){ @@ -265,6 +268,42 @@ describe('padUsers', function(){ }); }) +describe('setHTML', function(){ + it('Sets the HTML of a Pad', function(done) { + var html = "