diff options
-rw-r--r-- | doc/api/http_api.md | 5 | ||||
-rw-r--r-- | src/node/db/API.js | 21 | ||||
-rw-r--r-- | src/node/utils/ImportHtml.js | 15 |
3 files changed, 31 insertions, 10 deletions
diff --git a/doc/api/http_api.md b/doc/api/http_api.md index 19d2e7f4..1ae2ea1c 100644 --- a/doc/api/http_api.md +++ b/doc/api/http_api.md @@ -294,15 +294,14 @@ returns the text of a pad formatted as HTML * `{code: 0, message:"ok", data: {html:"Welcome Text<br>More Text"}}` * `{code: 1, message:"padID does not exist", data: null}` -#### setHTML(padID, text) +#### setHTML(padID, html) * API >= 1 -sets the html of a pad +sets the text of a pad based on HTML, HTML must be well formed. Malformed HTML will send a warning to the API log. *Example returns:* * `{code: 0, message:"ok", data: null}` * `{code: 1, message:"padID does not exist", data: null}` - * `{code: 1, message:"text too long", data: null}` #### getAttributePool(padID) * API >= 1.2.8 diff --git a/src/node/db/API.js b/src/node/db/API.js index 349953cb..98bc8029 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -361,6 +361,8 @@ exports.getHTML = function(padID, rev, callback) exportHtml.getPadHTML(pad, rev, function(err, html) { if(ERR(err, callback)) return; + html = "<!DOCTYPE HTML><html><body>" +html; // adds HTML head + html += "</body></html>"; data = {html: html}; callback(null, data); }); @@ -371,6 +373,8 @@ exports.getHTML = function(padID, rev, callback) exportHtml.getPadHTML(pad, undefined, function (err, html) { if(ERR(err, callback)) return; + html = "<!DOCTYPE HTML><html><body>" +html; // adds HTML head + html += "</body></html>"; data = {html: html}; callback(null, data); }); @@ -378,15 +382,30 @@ exports.getHTML = function(padID, rev, callback) }); } +/** +setHTML(padID, html) sets the text of a pad based on HTML + +Example returns: + +{code: 0, message:"ok", data: null} +{code: 1, message:"padID does not exist", data: null} +*/ exports.setHTML = function(padID, html, callback) { + //html is required + if(typeof html != "string") + { + callback(new customError("html is no string","apierror")); + return; + } + //get the pad 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)); + importHtml.setPadHTML(pad, cleanText(html), callback); //update the clients on the pad padMessageHandler.updatePadClients(pad, callback); diff --git a/src/node/utils/ImportHtml.js b/src/node/utils/ImportHtml.js index 322e567e..abba2ac1 100644 --- a/src/node/utils/ImportHtml.js +++ b/src/node/utils/ImportHtml.js @@ -25,11 +25,6 @@ function setPadHTML(pad, html, callback) { var apiLogger = log4js.getLogger("ImportHtml"); - // Clean the pad. This makes the rest of the code easier - // by several orders of magnitude. - pad.setText(""); - var padText = pad.text(); - // Parse the incoming HTML with jsdom try{ var doc = jsdom(html.replace(/>\n+</g, '><')); @@ -44,8 +39,15 @@ function setPadHTML(pad, html, callback) // Convert a dom tree into a list of lines and attribute liens // using the content collector object var cc = contentcollector.makeContentCollector(true, null, pad.pool); - cc.collectContent(doc.childNodes[0]); + try{ // we use a try here because if the HTML is bad it will blow up + cc.collectContent(doc.childNodes[0]); + }catch(e){ + apiLogger.warn("HTML was not properly formed", e); + return; // We don't process the HTML because it was bad.. + } + var result = cc.finish(); + apiLogger.debug('Lines:'); var i; for (i = 0; i < result.lines.length; i += 1) @@ -90,6 +92,7 @@ function setPadHTML(pad, html, callback) // the changeset is ready! var theChangeset = builder.toString(); apiLogger.debug('The changeset: ' + theChangeset); + pad.setText(""); pad.appendRevision(theChangeset); } |