tests: fix importexport tests

The testing approach was redone to fix numerous issues:
  * Even if the tests had been working, none of them would have caught
    https://github.com/ether/etherpad-lite/issues/4808 because they
    didn't exercise the client-side import logic. Now they do.
  * Follow-up logic was not in the `helper.waitFor()` callback like it
    should have been. Now the code uses `async` and `await` to ensure
    proper execution order.
  * All `$.ajax()` calls used `async: false`. Now they're properly
    asynchronous.
  * The `helper.waitFor()` condition callbacks threw instead of
    returning false.
  * The string comparisons didn't allow for different attribute
    order (e.g., `<ol start="1" class="list-number1">` vs. `<ol
    class="list-number1" start="1">`). Now `Node.isEqualNode()` is
    used to reduce fragility. (`Node.isEqualNode()` is not perfect, so
    the tests are still a bit fragile: If class names or style strings
    are in a different order then `Node.isEqualNode()` will return
    false even if the nodes are semantically equivalent.)

Co-authored-by: Richard Hansen <rhansen@rhansen.org>
This commit is contained in:
John McLear 2021-02-19 13:24:54 +00:00 committed by Richard Hansen
parent e9cb1692eb
commit 8364546e70

View file

@ -1,329 +1,604 @@
'use strict'; 'use strict';
describe('import functionality', function () { describe('importexport.js', function () {
beforeEach(function (cb) { const testCases = [
helper.newPad(cb); // creates a new pad {
this.timeout(60000); name: 'text with newlines',
}); inputText: [
'imported text\n',
function getinnertext() { 'newline',
const inner = helper.padInner$; ].join(''),
if (!inner) { wantPadLines: [
return ''; '<span class="">imported text</span>',
} '<span class="">newline</span>',
let newtext = ''; ],
inner('div').each((line, el) => { wantExportHtmlBody: [
newtext += `${el.innerHTML}\n`; 'imported text<br>',
}); 'newline<br>',
return newtext; ].join(''),
} wantExportText: [
function importrequest(data, importurl, type) { 'imported text\n',
let error; 'newline\n',
const result = $.ajax({ ].join(''),
url: importurl,
type: 'post',
processData: false,
async: false,
contentType: 'multipart/form-data; boundary=boundary',
accepts: {
text: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
}, },
data: [ {
'Content-Type: multipart/form-data; boundary=--boundary', name: 'HTML with newlines',
'', inputHtmlBody: [
'--boundary', 'htmltext<br>',
`Content-Disposition: form-data; name="file"; filename="import.${type}"`, 'newline',
'Content-Type: text/plain', ].join(''),
'', wantPadLines: [
data, '<span class="">htmltext</span>',
'', '<span class="">newline</span>',
'--boundary', '<br>',
].join('\r\n'), ],
error(res) { wantExportHtmlBody: [
error = res; 'htmltext<br>',
'newline<br>',
'<br>',
].join(''),
wantExportText: [
'htmltext\n',
'newline\n',
'\n',
].join(''),
}, },
}); {
expect(error).to.be(undefined); name: 'HTML with attributes',
return result; inputHtmlBody: [
} 'htmltext<br>',
function exportfunc(link) { '<span class="b s i u"><b><i><s><u>newline</u></s></i></b>',
const exportresults = []; ].join(''),
$.ajaxSetup({ wantPadLines: [
async: false, '<span class="">htmltext</span>',
}); '<span class="b i s u"><b><i><s><u>newline</u></s></i></b></span>',
$.get(`${link}/export/html`, (data) => { '<br>',
const start = data.indexOf('<body>'); ],
const end = data.indexOf('</body>'); wantExportHtmlBody: [
const html = data.substr(start + 6, end - start - 6); 'htmltext<br>',
exportresults.push(['html', html]); '<strong><em><s><u>newline</u></s></em></strong><br>',
}); '<br>',
$.get(`${link}/export/txt`, (data) => { ].join(''),
exportresults.push(['txt', data]); wantExportText: [
}); 'htmltext\n',
return exportresults; 'newline\n',
} '\n',
].join(''),
xit('import a pad with newlines from txt', function (done) { },
const importurl = `${helper.padChrome$.window.location.href}/import`; {
const textWithNewLines = 'imported text\nnewline'; name: 'HTML with bullets',
importrequest(textWithNewLines, importurl, 'txt'); inputHtmlBody: [
helper.waitFor(() => expect(getinnertext()) '<ul class="list-bullet1">',
.to.be('<span class="">imported text</span>\n<span class="">newline</span>\n<br>\n')); ' <li>bullet line 1</li>',
const results = exportfunc(helper.padChrome$.window.location.href); ' <li>bullet line 2',
expect(results[0][1]).to.be('imported text<br>newline<br><br>'); ' <ul class="list-bullet2">',
expect(results[1][1]).to.be('imported text\nnewline\n\n'); ' <li>bullet2 line 1</li>',
done(); ' <li>bullet2 line 2</li>',
}); ' </ul>',
xit('import a pad with newlines from html', function (done) { ' </li>',
const importurl = `${helper.padChrome$.window.location.href}/import`; '</ul>',
const htmlWithNewLines = '<html><body>htmltext<br/>newline</body></html>'; ].join(''),
importrequest(htmlWithNewLines, importurl, 'html'); wantPadLines: [
helper.waitFor(() => expect(getinnertext()) '<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>',
.to.be('<span class="">htmltext</span>\n<span class="">newline</span>\n<br>\n')); '<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>',
const results = exportfunc(helper.padChrome$.window.location.href); '<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>',
expect(results[0][1]).to.be('htmltext<br>newline<br><br>'); '<ul class="list-bullet2"><li><span class="">bullet2 line 2</span></li></ul>',
expect(results[1][1]).to.be('htmltext\nnewline\n\n'); '<br>',
done(); ],
}); wantExportHtmlBody: [
xit('import a pad with attributes from html', function (done) { '<ul class=bullet>',
const importurl = `${helper.padChrome$.window.location.href}/import`; ' <li>bullet line 1</li>',
const htmlWithNewLines = '<html><body>htmltext<br/><span class="b s i u">' + ' <li>bullet line 2',
'<b><i><s><u>newline</u></s></i></b></body></html>'; ' <ul class=bullet>',
importrequest(htmlWithNewLines, importurl, 'html'); ' <li>bullet2 line 1</li>',
helper.waitFor(() => expect(getinnertext()) ' <li>bullet2 line 2</li>',
.to.be('<span class="">htmltext</span>\n<span class="b i s u">' + ' </ul>',
'<b><i><s><u>newline</u></s></i></b></span>\n<br>\n')); ' </li>',
const results = exportfunc(helper.padChrome$.window.location.href); '</ul>',
expect(results[0][1]) '<br>',
.to.be('htmltext<br><strong><em><s><u>newline</u></s></em></strong><br><br>'); ].map((l) => l.replace(/^\s+/, '')).join(''),
expect(results[1][1]).to.be('htmltext\nnewline\n\n'); wantExportText: [
done(); '\t* bullet line 1\n',
}); '\t* bullet line 2\n',
xit('import a pad with bullets from html', function (done) { '\t\t* bullet2 line 1\n',
const importurl = `${helper.padChrome$.window.location.href}/import`; '\t\t* bullet2 line 2\n',
const htmlWithBullets = '<html><body><ul class="list-bullet1"><li>bullet line 1</li>' + '\n',
'<li>bullet line 2</li><ul class="list-bullet2"><li>bullet2 line 1</li>' + ].join(''),
'<li>bullet2 line 2</li></ul></ul></body></html>'; },
importrequest(htmlWithBullets, importurl, 'html'); {
helper.waitFor(() => expect(getinnertext()).to.be( name: 'HTML with bullets and newlines',
'<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>\n' + inputHtmlBody: [
'<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>\n' + '<ul class="list-bullet1">',
'<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>\n' + ' <li>bullet line 1</li>',
'<ul class="list-bullet2"><li><span class="">bullet2 line 2</span></li></ul>\n' + '</ul>',
'<br>\n')); '<br>',
const results = exportfunc(helper.padChrome$.window.location.href); '<ul class="list-bullet1">',
expect(results[0][1]).to.be( ' <li>bullet line 2',
'<ul class="bullet"><li>bullet line 1</li><li>bullet line 2</li>' + ' <ul class="list-bullet2">',
'<ul class="bullet"><li>bullet2 line 1</li><li>bullet2 line 2</li></ul></ul><br>'); ' <li>bullet2 line 1</li>',
expect(results[1][1]) ' </ul>',
.to.be('\t* bullet line 1\n\t* bullet line 2\n' + ' </li>',
'\t\t* bullet2 line 1\n\t\t* bullet2 line 2\n\n'); '</ul>',
done(); '<br>',
}); '<ul class="list-bullet1">',
xit('import a pad with bullets and newlines from html', function (done) { ' <li>',
const importurl = `${helper.padChrome$.window.location.href}/import`; ' <ul class="list-bullet2">',
const htmlWithBullets = '<html><body><ul class="list-bullet1"><li>bullet line 1</li>' + ' <li>bullet2 line 2</li>',
'</ul><br/><ul class="list-bullet1"><li>bullet line 2</li><ul class="list-bullet2">' + ' </ul>',
'<li>bullet2 line 1</li></ul></ul><br/><ul class="list-bullet1">' + ' </li>',
'<ul class="list-bullet2"><li>bullet2 line 2</li></ul></ul></body></html>'; '</ul>',
importrequest(htmlWithBullets, importurl, 'html'); ].join(''),
helper.waitFor(() => expect(getinnertext()).to.be( wantPadLines: [
'<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>\n' + '<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>',
'<br>\n' + '<br>',
'<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>\n' + '<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>',
'<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>\n' + '<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>',
'<br>\n' + '<br>',
'<ul class="list-bullet2"><li><span class="">bullet2 line 2</span></li></ul>\n' + '<ul class="list-bullet2"><li><span class="">bullet2 line 2</span></li></ul>',
'<br>\n')); '<br>',
const results = exportfunc(helper.padChrome$.window.location.href); ],
expect(results[0][1]).to.be( wantExportHtmlBody: [
'<ul class="bullet"><li>bullet line 1</li></ul><br><ul class="bullet">' + '<ul class=bullet>',
'<li>bullet line 2</li><ul class="bullet"><li>bullet2 line 1</li></ul>' + ' <li>bullet line 1</li>',
'</ul><br><ul><ul class="bullet"><li>bullet2 line 2</li></ul></ul><br>'); '</ul>',
expect(results[1][1]).to.be( '<br>',
'\t* bullet line 1\n\n\t* bullet line 2\n\t\t* bullet2 line 1\n\n\t\t* bullet2 line 2\n\n'); '<ul class=bullet>',
done(); ' <li>bullet line 2',
}); ' <ul class=bullet>',
xit('import a pad with bullets and newlines and attributes from html', function (done) { ' <li>bullet2 line 1</li>',
const importurl = `${helper.padChrome$.window.location.href}/import`; ' </ul>',
const htmlWithBullets = '<html><body><ul class="list-bullet1"><li>bullet line 1</li>' + ' </li>',
'</ul><br/><ul class="list-bullet1"><li>bullet line 2</li>' + '</ul>',
'<ul class="list-bullet2"><li>bullet2 line 1</li></ul></ul>' + '<br>',
'<br/><ul class="list-bullet1"><ul class="list-bullet2"><ul class="list-bullet3">' + '<ul class=bullet>',
'<ul class="list-bullet4"><li><span class="b s i u"><b><i>' + ' <li>',
'<s><u>bullet4 line 2 bisu</u></s></i></b></span></li><li>' + ' <ul class=bullet>',
'<span class="b s "><b><s>bullet4 line 2 bs</s></b></span></li>' + ' <li>bullet2 line 2</li>',
'<li><span class="u"><u>bullet4 line 2 u</u></span><span class="u i s">' + ' </ul>',
'<i><s><u>uis</u></s></i></span></li></ul></ul></ul></ul></body></html>'; ' </li>',
importrequest(htmlWithBullets, importurl, 'html'); '</ul>',
helper.waitFor(() => expect(getinnertext()).to.be( '<br>',
'<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>\n<br>\n' + ].map((l) => l.replace(/^\s+/, '')).join(''),
'<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>\n' + wantExportText: [
'<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>\n<br>\n' + '\t* bullet line 1\n',
'<ul class="list-bullet4"><li><span class="b i s u">' + '\n',
'<b><i><s><u>bullet4 line 2 bisu</u></s></i></b></span></li></ul>\n' + '\t* bullet line 2\n',
'<ul class="list-bullet4"><li><span class="b s">' + '\t\t* bullet2 line 1\n',
'<b><s>bullet4 line 2 bs</s></b></span></li></ul>\n' + '\n',
'<ul class="list-bullet4"><li><span class="u"><u>bullet4 line 2 u</u>' + '\t\t* bullet2 line 2\n',
'</span><span class="i s u"><i><s><u>uis</u></s></i></span></li></ul>\n' + '\n',
'<br>\n')); ].join(''),
const results = exportfunc(helper.padChrome$.window.location.href); },
expect(results[0][1]).to.be( {
'<ul class="bullet"><li>bullet line 1</li></ul>' + name: 'HTML with bullets, newlines, and attributes',
'<br><ul class="bullet"><li>bullet line 2</li><ul class="bullet"><li>bullet2 line 1</li>' + inputHtmlBody: [
'</ul></ul><br><ul><ul><ul><ul class="bullet"><li><strong><em><s><u>bullet4 line 2 bisu' + '<ul class="list-bullet1">',
'</u></s></em></strong></li><li><strong><s>bullet4 line 2 bs</s></strong>' + ' <li>bullet line 1</li>',
'</li><li><u>bullet4 line 2 u<em><s>uis</s></em></u></li></ul></ul></ul></ul><br>'); '</ul>',
expect(results[1][1]).to.be( '<br>',
'\t* bullet line 1\n\n\t* bullet line 2\n\t\t* bullet2 line 1\n\n\t\t\t\t* bullet4 line 2' + '<ul class="list-bullet1">',
' bisu\n\t\t\t\t* bullet4 line 2 bs\n\t\t\t\t* bullet4 line 2 uuis\n\n'); ' <li>bullet line 2',
done(); ' <ul class="list-bullet2">',
}); ' <li>bullet2 line 1</li>',
xit('import a pad with nested bullets from html', function (done) { ' </ul>',
const importurl = `${helper.padChrome$.window.location.href}/import`; ' </li>',
const htmlWithBullets = '<html><body><ul class="list-bullet1"><li>bullet line 1</li>' + '</ul>',
'</ul><ul class="list-bullet1"><li>bullet line 2</li><ul class="list-bullet2">' + '<br>',
'<li>bullet2 line 1</li></ul></ul><ul class="list-bullet1"><ul class="list-bullet2">' + '<ul class="list-bullet1">',
'<ul class="list-bullet3"><ul class="list-bullet4"><li>bullet4 line 2</li>' + ' <li>',
'<li>bullet4 line 2</li><li>bullet4 line 2</li></ul><li>bullet3 line 1</li></ul>' + ' <ul class="list-bullet2">',
'</ul><li>bullet2 line 1</li></ul></body></html>'; ' <li>',
importrequest(htmlWithBullets, importurl, 'html'); ' <ul class="list-bullet3">',
const oldtext = getinnertext(); ' <li>',
helper.waitFor(() => oldtext !== getinnertext() ' <ul class="list-bullet4">',
// return expect(getinnertext()).to.be('\ ' <li><span class="b s i u"><b><i><s><u>bullet4 line 2 bisu</u>' +
// <ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>\n\ '</s></i></b></span></li>',
// <ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>\n\ ' <li><span class="b s "><b><s>bullet4 line 2 bs</s></b></span></li>',
// <ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>\n\ ' <li><span class="u"><u>bullet4 line 2 u</u></span>' +
// <ul class="list-bullet4"><li><span class="">bullet4 line 2</span></li></ul>\n\ '<span class="u i s"><i><s><u>uis</u></s></i></span></li>',
// <ul class="list-bullet4"><li><span class="">bullet4 line 2</span></li></ul>\n\ ' </ul>',
// <ul class="list-bullet4"><li><span class="">bullet4 line 2</span></li></ul>\n\ ' </li>',
// <br>\n') ' </ul>',
); ' </li>',
' </ul>',
const results = exportfunc(helper.padChrome$.window.location.href); ' </li>',
expect(results[0][1]).to.be( '</ul>',
'<ul class="bullet"><li>bullet line 1</li><li>bullet line 2</li>' + ].join(''),
'<ul class="bullet"><li>bullet2 line 1</li><ul><ul class="bullet"><li>bullet4 line 2</li>' + wantPadLines: [
'<li>bullet4 line 2</li><li>bullet4 line 2</li></ul><li>bullet3 line 1</li></ul></ul>' + '<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>',
'<li>bullet2 line 1</li></ul><br>'); '<br>',
expect(results[1][1]).to.be( '<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>',
'\t* bullet line 1\n\t* bullet line 2\n\t\t* bullet2 line 1\n\t\t\t\t* bullet4 line 2' + '<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>',
'\n\t\t\t\t* bullet4 line 2\n\t\t\t\t* bullet4 line 2\n\t\t\t* bullet3 line 1' + '<br>',
'\n\t* bullet2 line 1\n\n'); '<ul class="list-bullet4"><li><span class="b i s u"><b><i><s><u>' +
done(); 'bullet4 line 2 bisu</u></s></i></b></span></li></ul>',
}); '<ul class="list-bullet4"><li><span class="b s"><b><s>bullet4 line 2 bs</s>' +
xit('import with 8 levels of bullets and newlines and attributes from html', function (done) { '</b></span></li></ul>',
const importurl = `${helper.padChrome$.window.location.href}/import`;
const htmlWithBullets =
'<html><body><ul class="list-bullet1"><li>bullet line 1</li>' +
'</ul><br/><ul class="list-bullet1"><li>bullet line 2</li><ul class="list-bullet2"><li>' +
'bullet2 line 1</li></ul></ul><br/><ul class="list-bullet1"><ul class="list-bullet2">' +
'<ul class="list-bullet3"><ul class="list-bullet4"><li><span class="b s i u"><b><i>' +
'<s><u>bullet4 line 2 bisu</u></s></i></b></span></li><li><span class="b s "><b><s>' +
'bullet4 line 2 bs</s></b></span></li><li><span class="u"><u>bullet4 line 2 u' +
'</u></span><span class="u i s"><i><s><u>uis</u></s></i></span></li>' +
'<ul class="list-bullet5"><ul class="list-bullet6"><ul class="list-bullet7">' +
'<ul class="list-bullet8"><li><span class="">foo</span></li><li><span class="b s">' +
'<b><s>foobar bs</b></s></span></li></ul></ul></ul></ul><ul class="list-bullet5">' +
'<li>foobar</li></ul></ul></ul></ul></body></html>';
importrequest(htmlWithBullets, importurl, 'html');
helper.waitFor(() => expect(getinnertext()).to.be(
'<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>\n<br>\n' +
'<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>\n' +
'<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>\n<br>\n' +
'<ul class="list-bullet4"><li><span class="b i s u"><b><i><s><u>bullet4 line 2 bisu</u>' +
'</s></i></b></span></li></ul>\n' +
'<ul class="list-bullet4"><li><span class="b s"><b><s>bullet4 line 2 bs</s></b>' +
'</span></li></ul>\n' +
'<ul class="list-bullet4"><li><span class="u"><u>bullet4 line 2 u</u></span>' + '<ul class="list-bullet4"><li><span class="u"><u>bullet4 line 2 u</u></span>' +
'<span class="i s u"><i><s><u>uis</u></s>' + '<span class="i s u"><i><s><u>uis</u></s></i></span></li></ul>',
'</i></span></li></ul>\n' + '<br>',
'<ul class="list-bullet8"><li><span class="">foo</span></li></ul>\n' + ],
'<ul class="list-bullet8"><li><span class="b s"><b><s>foobar bs</s></b>' + wantExportHtmlBody: [
'</span></li></ul>\n' + '<ul class=bullet><li>bullet line 1</li></ul>',
'<ul class="list-bullet5"><li><span class="">foobar</span></li></ul>\n' + '<br>',
'<br>\n')); '<ul class=bullet>',
const results = exportfunc(helper.padChrome$.window.location.href); ' <li>bullet line 2',
expect(results[0][1]).to.be( ' <ul class=bullet><li>bullet2 line 1</li></ul>',
'<ul class="bullet"><li>bullet line 1</li></ul><br><ul class="bullet">' + ' </li>',
'<li>bullet line 2</li><ul class="bullet"><li>bullet2 line 1</li></ul></ul>' + '</ul>',
'<br><ul><ul><ul><ul class="bullet"><li><strong><em><s><u>' + '<br>',
'bullet4 line 2 bisu</u></s></em></strong></li><li><strong><s>' + '<ul class=bullet>',
'bullet4 line 2 bs</s></strong></li><li><u>bullet4 line 2 u<em>' + ' <li>',
'<s>uis</s></em></u></li><ul><ul><ul><ul class="bullet"><li>foo</li>' + ' <ul class=bullet>',
'<li><strong><s>foobar bs</s></strong></li></ul></ul></ul><li>foobar</li>' + ' <li>',
'</ul></ul></ul></ul></ul><br>'); ' <ul class=bullet>',
expect(results[1][1]).to.be( ' <li>',
'\t* bullet line 1\n\n\t* bullet line 2\n\t\t* ' + ' <ul class=bullet>',
'bullet2 line 1\n\n\t\t\t\t* bullet4 line 2 bisu\n\t\t\t\t* bullet4 line 2 ' + ' <li><strong><em><s><u>bullet4 line 2 bisu</u></s></em></strong></li>',
'bs\n\t\t\t\t* bullet4 line 2 uuis\n\t\t\t\t\t\t\t\t* foo\n\t\t\t\t\t\t\t\t* ' + ' <li><strong><s>bullet4 line 2 bs</s></strong></li>',
'foobar bs\n\t\t\t\t\t* foobar\n\n'); ' <li><u>bullet4 line 2 u<em><s>uis</s></em></u></li>',
done(); ' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
'</ul>',
'<br>',
].map((l) => l.replace(/^\s+/, '')).join(''),
wantExportText: [
'\t* bullet line 1\n',
'\n',
'\t* bullet line 2\n',
'\t\t* bullet2 line 1\n',
'\n',
'\t\t\t\t* bullet4 line 2 bisu\n',
'\t\t\t\t* bullet4 line 2 bs\n',
'\t\t\t\t* bullet4 line 2 uuis\n',
'\n',
].join(''),
},
{
name: 'HTML with nested bullets',
inputHtmlBody: [
'<ul class="list-bullet1"><li>bullet line 1</li></ul>',
'<ul class="list-bullet1">',
' <li>bullet line 2',
' <ul class="list-bullet2">',
' <li>bullet2 line 1</li>',
' </ul>',
' </li>',
'</ul>',
'<ul class="list-bullet1">',
' <li>',
' <ul class="list-bullet2">',
' <li>',
' <ul class="list-bullet3">',
' <li>',
' <ul class="list-bullet4">',
' <li>bullet4 line 2</li>',
' <li>bullet4 line 2</li>',
' <li>bullet4 line 2</li>',
' </ul>',
' </li>',
' <li>bullet3 line 1</li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
'</ul>',
].join(''),
wantPadLines: [
'<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>',
'<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>',
'<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>',
'<ul class="list-bullet4"><li><span class="">bullet4 line 2</span></li></ul>',
'<ul class="list-bullet4"><li><span class="">bullet4 line 2</span></li></ul>',
'<ul class="list-bullet4"><li><span class="">bullet4 line 2</span></li></ul>',
'<ul class="list-bullet3"><li><span class="">bullet3 line 1</span></li></ul>',
'<br>',
],
wantExportHtmlBody: [
'<ul class=bullet>',
' <li>bullet line 1</li>',
' <li>bullet line 2',
' <ul class=bullet>',
' <li>bullet2 line 1',
' <ul class=bullet>',
' <li>',
' <ul class=bullet>',
' <li>bullet4 line 2</li>',
' <li>bullet4 line 2</li>',
' <li>bullet4 line 2</li>',
' </ul>',
' </li>',
' <li>bullet3 line 1</li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
'</ul>',
'<br>',
].map((l) => l.replace(/^\s+/, '')).join(''),
wantExportText: [
'\t* bullet line 1\n',
'\t* bullet line 2\n',
'\t\t* bullet2 line 1\n',
'\t\t\t\t* bullet4 line 2\n',
'\t\t\t\t* bullet4 line 2\n',
'\t\t\t\t* bullet4 line 2\n',
'\t\t\t* bullet3 line 1\n',
'\n',
].join(''),
},
{
name: 'HTML with 8 levels of bullets, newlines, and attributes',
inputHtmlBody: [
'<ul class="list-bullet1">',
' <li>bullet line 1</li>',
'</ul>',
'<br>',
'<ul class="list-bullet1">',
' <li>bullet line 2',
' <ul class="list-bullet2">',
' <li>bullet2 line 1</li>',
' </ul>',
' </li>',
'</ul>',
'<br>',
'<ul class="list-bullet1">',
' <li>',
' <ul class="list-bullet2">',
' <li>',
' <ul class="list-bullet3">',
' <li>',
' <ul class="list-bullet4">',
' <li><span class="b s i u"><b><i><s><u>bullet4 line 2 bisu' +
'</u></s></i></b></span></li>',
' <li><span class="b s "><b><s>bullet4 line 2 bs</s></b></span></li>',
' <li><span class="u"><u>bullet4 line 2 u</u></span>' +
'<span class="u i s"><i><s><u>uis</u></s></i></span></li>',
' <li>',
' <ul class="list-bullet5">',
' <li>',
' <ul class="list-bullet6">',
' <li>',
' <ul class="list-bullet7">',
' <li>',
' <ul class="list-bullet8">',
' <li><span class="">foo</span></li>',
' <li><span class="b s"><b><s>foobar bs</b></s></span></li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' <li>',
' <ul class="list-bullet5">',
' <li>foobar</li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
'</ul>',
].join(''),
wantPadLines: [
'<ul class="list-bullet1"><li><span class="">bullet line 1</span></li></ul>',
'<br>',
'<ul class="list-bullet1"><li><span class="">bullet line 2</span></li></ul>',
'<ul class="list-bullet2"><li><span class="">bullet2 line 1</span></li></ul>',
'<br>',
'<ul class="list-bullet4"><li><span class="b i s u"><b><i><s><u>' +
'bullet4 line 2 bisu</u></s></i></b></span></li></ul>',
'<ul class="list-bullet4"><li><span class="b s"><b><s>bullet4 line 2 bs</s>' +
'</b></span></li></ul>',
'<ul class="list-bullet4"><li><span class="u"><u>bullet4 line 2 u</u></span>' +
'<span class="i s u"><i><s><u>uis</u></s></i></span></li></ul>',
'<ul class="list-bullet8"><li><span class="">foo</span></li></ul>',
'<ul class="list-bullet8"><li><span class="b s"><b><s>foobar bs</s></b></span></li></ul>',
'<ul class="list-bullet5"><li><span class="">foobar</span></li></ul>',
'<br>',
],
wantExportHtmlBody: [
'<ul class=bullet>',
' <li>bullet line 1</li>',
'</ul>',
'<br>',
'<ul class=bullet>',
' <li>bullet line 2',
' <ul class=bullet>',
' <li>bullet2 line 1</li>',
' </ul>',
' </li>',
'</ul>',
'<br>',
'<ul class=bullet>',
' <li>',
' <ul class=bullet>',
' <li>',
' <ul class=bullet>',
' <li>',
' <ul class=bullet>',
' <li><strong><em><s><u>bullet4 line 2 bisu</u></s></em></strong></li>',
' <li><strong><s>bullet4 line 2 bs</s></strong></li>',
' <li><u>bullet4 line 2 u<em><s>uis</s></em></u>',
' <ul class=bullet>',
' <li>',
' <ul class=bullet>',
' <li>',
' <ul class=bullet>',
' <li>',
' <ul class=bullet>',
' <li>foo</li>',
' <li><strong><s>foobar bs</s></strong></li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' <li>foobar</li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
'</ul>',
'<br>',
].map((l) => l.replace(/^\s+/, '')).join(''),
wantExportText: [
'\t* bullet line 1\n',
'\n',
'\t* bullet line 2\n',
'\t\t* bullet2 line 1\n',
'\n',
'\t\t\t\t* bullet4 line 2 bisu\n',
'\t\t\t\t* bullet4 line 2 bs\n',
'\t\t\t\t* bullet4 line 2 uuis\n',
'\t\t\t\t\t\t\t\t* foo\n',
'\t\t\t\t\t\t\t\t* foobar bs\n',
'\t\t\t\t\t* foobar\n',
'\n',
].join(''),
},
{
name: 'HTML with ordered lists',
inputHtmlBody: [
'<ol class="list-number1" start="1"><li>number 1 line 1</li></ol>',
'<ol class="list-number1" start="2"><li>number 2 line 2</li></ol>',
].join(''),
wantPadLines: [
'<ol start="1" class="list-number1"><li><span class="">number 1 line 1</span></li></ol>',
'<ol start="2" class="list-number1"><li><span class="">number 2 line 2</span></li></ol>',
'<br>',
],
wantExportHtmlBody: [
'<ol start=1 class=number>',
' <li>number 1 line 1</li>',
' <li>number 2 line 2</li>',
'</ol>',
'<br>',
].map((l) => l.replace(/^\s+/, '')).join(''),
wantExportText: [
'\t1. number 1 line 1\n',
'\t2. number 2 line 2\n',
'\n',
].join(''),
},
];
let confirm;
before(async function () {
this.timeout(60000);
await new Promise(
(resolve, reject) => helper.newPad((err) => err != null ? reject(err) : resolve()));
confirm = helper.padChrome$.window.confirm;
helper.padChrome$.window.confirm = () => true;
// As of 2021-02-22 a mutable FileList cannot be directly created so DataTransfer is used as a
// hack to access a mutable FileList for testing the '<input type="file">' element. DataTransfer
// itself is quite new so support for it is tested here. See:
// * https://github.com/whatwg/html/issues/3269
// * https://stackoverflow.com/q/47119426
try {
const dt = new DataTransfer();
dt.items.add(new File(['testing'], 'file.txt', {type: 'text/plain'}));
// Supposedly all modern browsers support a settable HTMLInputElement.files property, but
// Firefox 52 complains.
helper.padChrome$('#importform input[type=file]')[0].files = dt.files;
} catch (err) {
return this.skip();
}
}); });
xit('import a pad with ordered lists from html', function (done) { after(async function () {
const importurl = `${helper.padChrome$.window.location.href}/import`; helper.padChrome$.window.confirm = confirm;
const htmlWithBullets = '<html><body><ol class="list-number1" start="1">' +
'<li>number 1 line 1</li></ol><ol class="list-number1" start="2">' +
'<li>number 2 line 2</li></ol></body></html>';
importrequest(htmlWithBullets, importurl, 'html');
console.error(getinnertext());
expect(getinnertext()).to.be(
'<ol class="list-number1" start="1"><li><span class="">number 1 line 1</span></li></ol>\n' +
'<ol class="list-number1" start="2"><li><span class="">number 2 line 2</span></li></ol>\n' +
'<br>\n');
const results = exportfunc(helper.padChrome$.window.location.href);
expect(results[0][1]).to.be(
'<ol class="list-number1" start="1"><li>number 1 line 1</li>' +
'</ol><ol class="list-number1" start="2"><li>number 2 line 2</li></ol>');
expect(results[1][1]).to.be('');
done();
}); });
xit('import a pad with ordered lists and newlines from html', function (done) {
const importurl = `${helper.padChrome$.window.location.href}/import`; beforeEach(async function () {
const htmlWithBullets = '<html><body><ol class="list-number1" start="1">' + const popup = helper.padChrome$('#import_export');
'<li>number 9 line 1</li></ol><br/><ol class="list-number1" start="2">' + const isVisible = () => popup.hasClass('popup-show');
'<li>number 10 line 2</li><ol class="list-number2">' + if (isVisible()) return;
'<li>number 2 times line 1</li></ol></ol><br/><ol class="list-bullet1">' + const button = helper.padChrome$('button[data-l10n-id="pad.toolbar.import_export.title"]');
'<ol class="list-number2"><li>number 2 times line 2</li></ol></ol></body></html>'; button.click();
importrequest(htmlWithBullets, importurl, 'html'); await helper.waitForPromise(isVisible);
expect(getinnertext()).to.be(
'<ol class="list-number1" start="1"><li><span class="">number 9 line 1</span></li></ol>\n' +
'<br>\n' +
'<ol class="list-number1" start="2"><li><span class="">number 10 line 2</span></li>' +
'</ol>\n' +
'<ol class="list-number2"><li><span class="">number 2 times line 1</span></li></ol>\n' +
'<br>\n' +
'<ol class="list-number2"><li><span class="">number 2 times line 2</span></li></ol>\n' +
'<br>\n');
const results = exportfunc(helper.padChrome$.window.location.href);
console.error(results);
done();
}); });
xit('import with nested ordered lists and attributes and newlines from html', function (done) {
const importurl = `${helper.padChrome$.window.location.href}/import`; const docToHtml = (() => {
const htmlWithBullets = '<html><body><ol class="list-number1" start="1"><li>' + const s = new XMLSerializer();
'<span class="b s i u"><b><i><s><u>bold strikethrough italics underline</u>' + return (doc) => s.serializeToString(doc);
'</s><i/></b></span> line <span class="b"><b>1bold</b></span></li>' + })();
'</ol><br/><span class="i"><i><ol class="list-number1" start="2">' +
'<li>number 10 line 2</li><ol class="list-number2">' + const htmlToDoc = (() => {
'<li>number 2 times line 1</li></ol></ol></i></span><br/>' + const p = new DOMParser();
'<ol class="list-bullet1"><ol class="list-number2">' + return (html) => p.parseFromString(html, 'text/html');
'<li>number 2 times line 2</li></ol></ol></body></html>'; })();
importrequest(htmlWithBullets, importurl, 'html');
expect(getinnertext()).to.be( const htmlBodyToDoc = (htmlBody) => {
'<ol class="list-number1"><li><span class="b i s u"><b><i><s><u>' + const doc = document.implementation.createHTMLDocument();
'bold strikethrough italics underline</u></s></i></b></span><span class="">' + $('body', doc).html(htmlBody);
' line </span><span class="b"><b>1bold</b></span></li></ol>\n' + return doc;
'<br>\n' + };
'<ol class="list-number1"><li><span class="i"><i>number 10 line 2</i></span></li></ol>\n' +
'<ol class="list-number2"><li><span class="i">' + for (const tc of testCases) {
'<i>number 2 times line 1</i></span></li></ol>\n' + describe(tc.name, function () {
'<br>\n' + it('import', async function () {
'<ol class="list-number2"><li><span class="">number 2 times line 2</span></li></ol>\n' + const ext = tc.inputHtmlBody ? 'html' : 'txt';
'<br>\n'); const contents = ext === 'html' ? docToHtml(htmlBodyToDoc(tc.inputHtmlBody)) : tc.inputText;
const results = exportfunc(helper.padChrome$.window.location.href); // DataTransfer is used as a hacky way to get a mutable FileList. For details, see:
console.error(results); // https://stackoverflow.com/q/47119426
done(); const dt = new DataTransfer();
dt.items.add(new File([contents], `file.${ext}`, {type: 'text/plain'}));
const form = helper.padChrome$('#importform');
form.find('input[type=file]')[0].files = dt.files;
form.find('#importsubmitinput').submit();
try {
await helper.waitForPromise(() => {
const got = helper.linesDiv();
if (got.length !== tc.wantPadLines.length) return false;
for (let i = 0; i < got.length; i++) {
const gotDiv = $('<div>').html(got[i].html());
const wantDiv = $('<div>').html(tc.wantPadLines[i]);
if (!gotDiv[0].isEqualNode(wantDiv[0])) return false;
}
return true;
});
} catch (err) {
const formatLine = (l) => ` ${JSON.stringify(l)}`;
const g = helper.linesDiv().map((div) => formatLine(div.html())).join('\n');
const w = tc.wantPadLines.map(formatLine).join('\n');
throw new Error(`Import failed. Got pad lines:\n${g}\nWant pad lines:\n${w}`);
}
});
it('export to HTML', async function () {
const link = helper.padChrome$('#exporthtmla').attr('href');
const url = new URL(link, helper.padChrome$.window.location.href).href;
const gotHtml = await $.ajax({url, dataType: 'html'});
const gotBody = $('body', htmlToDoc(gotHtml));
gotBody.html(gotBody.html().replace(/^\s+|\s+$/g, ''));
const wantBody = $('body', htmlBodyToDoc(tc.wantExportHtmlBody));
if (!gotBody[0].isEqualNode(wantBody[0])) {
throw new Error(`Got exported HTML body:\n ${JSON.stringify(gotBody.html())}\n` +
`Want HTML body:\n ${JSON.stringify(wantBody.html())}`);
}
});
it('export to text', async function () {
const link = helper.padChrome$('#exportplaina').attr('href');
const url = new URL(link, helper.padChrome$.window.location.href).href;
const got = await $.ajax({url, dataType: 'text'});
expect(got).to.be(tc.wantExportText);
}); });
}); });
}
});