Skip to content
5 changes: 2 additions & 3 deletions doc/api/http_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/node/db/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -371,22 +373,39 @@ 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);
});
}
});
}

/**
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);
Expand Down
15 changes: 9 additions & 6 deletions src/node/utils/ImportHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '><'));
Expand All @@ -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..
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just returning here will result in a timeout on the other end, i think. You could add callback(new customError("Malformed html","apierror"));

}

var result = cc.finish();

apiLogger.debug('Lines:');
var i;
for (i = 0; i < result.lines.length; i += 1)
Expand Down Expand Up @@ -90,6 +92,7 @@ function setPadHTML(pad, html, callback)
// the changeset is ready!
var theChangeset = builder.toString();
apiLogger.debug('The changeset: ' + theChangeset);
pad.setText("");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You set it to nothing before writing to it.

pad.appendRevision(theChangeset);
}

Expand Down