Skip to content

Commit

Permalink
[fix] Fix HTML export (#29)
Browse files Browse the repository at this point in the history
With the merge of ether/etherpad-lite#3268,
`getLineHTMLForExport` should not return anything, but instead set the
HTML content of the line on `context.lineContent`.

Fix https://trello.com/c/31tPK2O3/1680.
  • Loading branch information
lpagliari authored Feb 21, 2019
1 parent 78f6e91 commit ccc5740
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 48 deletions.
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ exports.stylesForExport = function(hook, padId, cb){
cb(style);
};

// line, apool,attribLine,text
exports.getLineHTMLForExport = function (hook, context) {
var attribLine = context.attribLine;
var apool = context.apool;
var hasSceneMark = sceneMarkUtils.findSceneMarkAttribKey(context);
if (hasSceneMark){ // if it is a scene mark it is not a script element (including a general)
return;
}
var script_element = null;
script_element = findAttrib(attribLine, apool, "script_element");

//try to find a scene line attributes. if it's found it mount the HTML with it
var dataAttributes = mountAdditionalSceneData(context);
//if it is a scene mark it is not a script element (including a general)
if (hasSceneMark) return;

//try to find a scene line attributes. if it's found it mounts the HTML with it
var script_element = findAttrib(attribLine, apool, "script_element");
var text = context.lineContent;
if (script_element) {
text = text.substring(1);
} else { // if it is not a script either a scene mark so it is a general
} else { // if it is not a script nor a scene mark, then it is a general
script_element = "general";
}

//these dataAttributes refers do scene attributes like scene-name, scene-number, ...
return "<" + script_element + ">" + dataAttributes + text + "</" + script_element + ">";
var dataAttributes = mountAdditionalSceneData(context);

//finally, mount the HTML to export
context.lineContent = `<${script_element}>${dataAttributes}${text}</${script_element}>`;
}

//attrib is the element key in the pair key-value, scene-name:'whatever', in this case scene-name
Expand Down
103 changes: 65 additions & 38 deletions static/tests/backend/specs/api/sceneTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,45 @@ var supertest = require('ep_etherpad-lite/node_modules/supertest'),
apiVersion = 1;
randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;

var errorMessage = function(expected, actual){
return "Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + actual;
}

var buildExpectedHTML = function(html){
return "<!DOCTYPE HTML><html><body>"+html+"<general></general><br></body></html>";
}

var buildHTML = function(html){
return "<html><body>"+html+"</body></html>";
}

var INVALID_SCENE_ATTRIB = "<scene-invalid class=\"1\"><empty/></scene-invalid>";
var SCENE_WORKSTATE_ON_ETHERPAD = "<scene-workstate class=\"whatever\"><empty/></scene-workstate>";
var OTHER_SCENE_WORKSTATE_ON_ETHERPAD = "<scene-workstate class=\"end\"><empty/></scene-workstate>";
var SCENE_WORKSTATE_WITH_SPECIAL_CHARS_ON_ETHERPAD = "<scene-workstate class=\"=>'arrow'<=\"><empty\></scene-workstate>";
var SCENE_NUMBER_ON_ETHERPAD = "<scene-number class=\"1\"><empty/></scene-number>";
var SCENE_NUMBER_AND_WORKSTATE_ON_HTML = "<scene scene-number=\"1\" scene-workstate=\"whatever\"></scene>";

var SCENE_ON_ETHERPAD = `<heading><scene>${SCENE_WORKSTATE_ON_ETHERPAD}</scene>Once upon a time</heading>`;
var SCENE_ON_HTML = "<heading><scene scene-workstate=\"whatever\"></scene>Once upon a time</heading>";

var OTHER_SCENE_ON_ETHERPAD = `<heading><scene>${OTHER_SCENE_WORKSTATE_ON_ETHERPAD}</scene>The End</heading>`;
var OTHER_SCENE_ON_HTML = "<heading><scene scene-workstate=\"end\"></scene>The End</heading>";

var SCENE_WITH_SPECIAL_CHARS_ON_ETHERPAD = `<heading><scene>${SCENE_WORKSTATE_WITH_SPECIAL_CHARS_ON_ETHERPAD}</scene>Once upon a time</heading>`;
var SCENE_WITH_SPECIAL_CHARS_ON_HTML = "<heading><scene scene-workstate=\"=&gt;&#x27;arrow&#x27;&lt;=\"></scene>Once upon a time</heading>";

var SCENE_WITH_INVALID_ATTRIB_ON_ETHERPAD = `<heading><scene>${SCENE_WORKSTATE_ON_ETHERPAD}${INVALID_SCENE_ATTRIB}</scene>Once upon a time</heading>`;

var SCENE_WITH_WORKSTATE_AND_SCENE_NUMBER_ON_ETHERPAD = `<heading><scene>${SCENE_WORKSTATE_ON_ETHERPAD}${SCENE_NUMBER_ON_ETHERPAD}</scene>Once upon a time</heading>`;
var SCENE_WITH_WORKSTATE_AND_SCENE_NUMBER_ON_HTML = `<heading>${SCENE_NUMBER_AND_WORKSTATE_ON_HTML}Once upon a time</heading>`;

var SIMPLE_SCENE = "<heading>The End</heading>";
var ACTION = "<action>The End</action>";

describe('read scenes data', function(){
var padID, html, expected;
expected = "<!DOCTYPE HTML><html><body> <general></general></body></html>";
expected = buildExpectedHTML("");
//create a new pad before each test run
beforeEach(function(done){
padID = randomString(5);
Expand All @@ -28,7 +64,7 @@ describe('read scenes data', function(){

it('gets html without a heading', function(done) {
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
})
Expand All @@ -38,14 +74,14 @@ describe('read scenes data', function(){

before(function() {
html = function() {
return buildHTML("<heading><scene><scene-workstate class=\"whatever\"><empty/></scene-workstate></scene>Once upon a time</heading>");
return buildHTML(SCENE_ON_ETHERPAD);
}
});

it('gets html processed when exported', function(done) {
expected = buildExpectedHTML("<heading><scene scene-workstate=\"whatever\"></scene>Once upon a time</heading>");
expected = buildExpectedHTML(`${SCENE_ON_HTML}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -54,14 +90,14 @@ describe('read scenes data', function(){

before(function() {
html = function() {
return buildHTML("<heading><scene><scene-workstate class=\"whatever\"><empty/></scene-workstate><scene-number class=\"1\"><empty/></scene-number></scene>Once upon a time</heading>");
return buildHTML(SCENE_WITH_WORKSTATE_AND_SCENE_NUMBER_ON_ETHERPAD);
}
});

it('gets html processed when exported', function(done) {
expected = buildExpectedHTML("<heading><scene scene-number=\"1\" scene-workstate=\"whatever\"></scene>Once upon a time</heading>");
expected = buildExpectedHTML(`${SCENE_WITH_WORKSTATE_AND_SCENE_NUMBER_ON_HTML}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -71,14 +107,14 @@ describe('read scenes data', function(){

before(function() {
html = function() {
return buildHTML("<heading><scene><scene-workstate class=\"whatever\"><empty/></scene-workstate><scene-invalid class=\"1\"><empty/></scene-invalid></scene>Once upon a time</heading>");
return buildHTML(SCENE_WITH_INVALID_ATTRIB_ON_ETHERPAD);
}
});

it('exports only name attribute', function(done) {
expected = buildExpectedHTML("<heading><scene scene-workstate=\"whatever\"></scene>Once upon a time</heading>");
expected = buildExpectedHTML(`${SCENE_ON_HTML}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -93,14 +129,14 @@ describe('read scenes data', function(){
"<scene-temporality class=\"PRESENT\"><empty/></scene-temporality>" +
"<scene-workstate class=\"IMMATURE\"><empty/></scene-workstate>" +
"<scene-time class=\"20\"><empty/></scene-time>" +
"Once upon a time</heading>");
"Once upon a time</heading><br>");
}
});

it('gets html processed when exported', function(done) {
expected = buildExpectedHTML("<heading><scene scene-number=\"11\" scene-duration=\"30\" scene-temporality=\"PRESENT\" scene-workstate=\"IMMATURE\" scene-time=\"20\"></scene>Once upon a time</heading>");
expected = buildExpectedHTML("<heading><scene scene-number=\"11\" scene-duration=\"30\" scene-temporality=\"PRESENT\" scene-workstate=\"IMMATURE\" scene-time=\"20\"></scene>Once upon a time</heading><br>");
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -111,14 +147,14 @@ describe('read scenes data', function(){

before(function() {
html = function() {
return buildHTML("<heading><scene><scene-workstate class=\"whatever\"><empty/></scene-workstate></scene>Once upon a time</heading><br><heading><scene><scene-workstate class=\"end\"><empty/></scene-workstate></scene>The End</heading>");
return buildHTML(`${SCENE_ON_ETHERPAD}<br>${OTHER_SCENE_ON_ETHERPAD}`);
}
});

it('gets two headings when exported', function(done) {
expected = buildExpectedHTML("<heading><scene scene-workstate=\"whatever\"></scene>Once upon a time</heading> <heading><scene scene-workstate=\"end\"></scene>The End</heading>");
expected = buildExpectedHTML(`${SCENE_ON_HTML}<br>${OTHER_SCENE_ON_HTML}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -128,14 +164,14 @@ describe('read scenes data', function(){

before(function() {
html = function() {
return buildHTML("<heading><scene><scene-workstate class=\"whatever\"><empty/></scene-workstate></scene>Once upon a time</heading><br><heading>The End</heading>");
return buildHTML(`${SCENE_ON_ETHERPAD}<br>${SIMPLE_SCENE}`);
}
});

it('gets two headings when exported', function(done) {
expected = buildExpectedHTML("<heading><scene scene-workstate=\"whatever\"></scene>Once upon a time</heading> <heading>The End</heading>");
expected = buildExpectedHTML(`${SCENE_ON_HTML}<br>${SIMPLE_SCENE}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -145,14 +181,14 @@ describe('read scenes data', function(){

before(function() {
html = function() {
return buildHTML("<heading><scene><scene-workstate class=\"whatever\"><empty/></scene-workstate></scene>Once upon a time</heading><br><action>The End</action>");
return buildHTML(`${SCENE_ON_ETHERPAD}<br>${ACTION}`);
}
});

it('gets a heading with attributes and one action', function(done) {
expected = buildExpectedHTML("<heading><scene scene-workstate=\"whatever\"></scene>Once upon a time</heading> <action>The End</action>");
expected = buildExpectedHTML(`${SCENE_ON_HTML}<br>${ACTION}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -162,14 +198,14 @@ describe('read scenes data', function(){

before(function() {
html = function() {
return buildHTML("<action>The End</action><br><heading><scene><scene-workstate class=\"whatever\"><empty/></scene-workstate></scene>Once upon a time</heading>");
return buildHTML(`${ACTION}<br>${SCENE_ON_ETHERPAD}`);
}
});

it('gets an action and a heading with attributes', function(done) {
expected = buildExpectedHTML("<action>The End</action> <heading><scene scene-workstate=\"whatever\"></scene>Once upon a time</heading>");
expected = buildExpectedHTML(`${ACTION}<br>${SCENE_ON_HTML}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
Expand All @@ -178,26 +214,17 @@ describe('read scenes data', function(){
context('when scene tag has special chars', function(){
before(function() {
html = function() {
return buildHTML("<action>The End</action><br><heading><scene><scene-workstate class=\"=>'arrow'<=\"><empty\></scene-workstate></scene>Once upon a time</heading>");
return buildHTML(`${ACTION}<br>${SCENE_WITH_SPECIAL_CHARS_ON_ETHERPAD}`);
}
});

it('gets scene-workstate escaped', function(done) {
expected = buildExpectedHTML("<action>The End</action> <heading><scene scene-workstate=\"=&gt;&#x27;arrow&#x27;&lt;=\"></scene>Once upon a time</heading>");
expected = buildExpectedHTML(`${ACTION}<br>${SCENE_WITH_SPECIAL_CHARS_ON_HTML}<br>`);
getHTML(padID, function(err, html_res){
if(expected !== html_res ) throw new Error("Exported HTML doesn't match to expected HTML - Expected " + expected + " got " + html_res);
if(expected !== html_res) throw new Error(errorMessage(expected, html_res));
done();
});
});
});

});

var buildExpectedHTML = function(html){

return "<!DOCTYPE HTML><html><body> "+html+" <general></general></body></html>";
}

var buildHTML = function(html){
return "<html><body>"+html+"</body></html>";
}

0 comments on commit ccc5740

Please sign in to comment.