mirror of
https://github.com/zotero/zotero.git
synced 2026-10-03 02:21:49 +00:00
Add tests for renderItemTitle()
This commit is contained in:
parent
8082516bf6
commit
679de040ec
1 changed files with 128 additions and 0 deletions
|
|
@ -776,4 +776,132 @@ describe("Zotero.Utilities.Internal", function () {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#renderItemTitle()", function () {
|
||||
function renderToElement(title) {
|
||||
let elem = new DOMParser().parseFromString('<div></div>', 'text/html')
|
||||
.querySelector('div');
|
||||
Zotero.Utilities.Internal.renderItemTitle(title, elem);
|
||||
return elem;
|
||||
}
|
||||
|
||||
function renderToHTML(title) {
|
||||
return renderToElement(title).innerHTML;
|
||||
}
|
||||
|
||||
function renderToTextViaHTML(title) {
|
||||
return renderToElement(title).textContent;
|
||||
}
|
||||
|
||||
function renderToText(title) {
|
||||
return Zotero.Utilities.Internal.renderItemTitle(title);
|
||||
}
|
||||
|
||||
it("should render a title without tags unchanged", function () {
|
||||
assert.equal(renderToHTML('My Title'), 'My Title');
|
||||
assert.equal(renderToHTML('1 < 2'), '1 < 2');
|
||||
assert.equal(renderToHTML('<<ATTENTION>> READ THIS'), '<<ATTENTION>> READ THIS');
|
||||
});
|
||||
|
||||
it("should render single-level tags", function () {
|
||||
let examples = [
|
||||
'My <i>Title</i> XYZ',
|
||||
'My <b>Title</b> XYZ',
|
||||
'My <sub>Title</sub> XYZ',
|
||||
'My <sup>Title</sup> XYZ',
|
||||
];
|
||||
for (let example of examples) {
|
||||
assert.equal(renderToHTML(example), example);
|
||||
}
|
||||
|
||||
assert.equal(renderToHTML('My <span class="nocase">Title</span> XYZ'), 'My <span>Title</span> XYZ');
|
||||
// Citeproc wants no space, DOM output includes space. Confusing!
|
||||
assert.equal(
|
||||
renderToHTML('My <span style="font-variant:small-caps;">Title</span> XYZ'),
|
||||
'My <span style="font-variant: small-caps;">Title</span> XYZ'
|
||||
);
|
||||
});
|
||||
|
||||
it("should render a tag nested inside a different tag", function () {
|
||||
let examples = [
|
||||
'My <i><b>Title</b></i> XYZ',
|
||||
'My <b><i>Title</i></b> XYZ',
|
||||
'My <sub><i>Title</i></sub> XYZ',
|
||||
];
|
||||
for (let example of examples) {
|
||||
assert.equal(renderToHTML(example), example);
|
||||
}
|
||||
});
|
||||
|
||||
it("should preserve mismatched opening and closing tags", function () {
|
||||
assert.equal(
|
||||
renderToHTML('My <i><b>Title</i> XYZ'),
|
||||
'My <i><b>Title</i> XYZ'
|
||||
);
|
||||
assert.equal(
|
||||
renderToHTML('My <b>Title</i></b> XYZ'),
|
||||
'My <b>Title</i></b> XYZ'
|
||||
);
|
||||
assert.equal(
|
||||
renderToHTML('My <sub><span class="font-variant:small-caps;">Title</sub> XYZ'),
|
||||
'My <sub><span class="font-variant:small-caps;">Title</sub> XYZ'
|
||||
);
|
||||
assert.equal(
|
||||
renderToHTML('My <sub><span class="font-variant:small-caps;">Title</sub></span> XYZ'),
|
||||
'My <sub><span class="font-variant:small-caps;">Title</sub></span> XYZ'
|
||||
);
|
||||
});
|
||||
|
||||
it("should preserve matched but unsupported tags", function () {
|
||||
assert.equal(
|
||||
renderToHTML('My <em>Title</em> XYZ'),
|
||||
'My <em>Title</em> XYZ'
|
||||
);
|
||||
assert.equal(
|
||||
renderToHTML('My <strong>Title</strong> XYZ'),
|
||||
'My <strong>Title</strong> XYZ'
|
||||
);
|
||||
assert.equal(
|
||||
renderToHTML('My <span class="font-variant: small-caps;">Title</span> XYZ'), // Extra space
|
||||
'My <span class="font-variant: small-caps;">Title</span> XYZ'
|
||||
);
|
||||
assert.equal(
|
||||
renderToHTML('My <span>Title</span> XYZ'), // Extra space
|
||||
'My <span>Title</span> XYZ'
|
||||
);
|
||||
});
|
||||
|
||||
it("should invert a tag nested inside itself", function () {
|
||||
assert.equal(
|
||||
renderToHTML('<i>A <i>Title</i> in Italics</i>'),
|
||||
'<i>A <span style="font-style: normal;">Title</span> in Italics</i>'
|
||||
);
|
||||
assert.equal(
|
||||
renderToHTML('<b>A <b>Title</b> in Bold</b>'),
|
||||
'<b>A <span style="font-weight: normal;">Title</span> in Bold</b>'
|
||||
);
|
||||
});
|
||||
|
||||
it("should invert a tag nested inside itself, with a different tag in between", function () {
|
||||
assert.equal(
|
||||
renderToHTML('<i>A <span class="nocase"><i>Title</i></span> in Italics</i>'),
|
||||
'<i>A <span><span style="font-style: normal;">Title</span></span> in Italics</i>'
|
||||
);
|
||||
});
|
||||
|
||||
it("should render the same text directly and via DOM", function () {
|
||||
let examples = [
|
||||
'Basic Title',
|
||||
'Complicated <i>Title</i>',
|
||||
'Complicated <i>Title</i> With More Text',
|
||||
'Nested <i>T<b>i</b>tle</i> With More Text',
|
||||
'Self-Nested <i>T<i>i</i>tle</i> With More Text',
|
||||
'Fe<sub>1−<i>x</i></sub>O',
|
||||
'Title with <marquee>INVALID TAGS</marquee> <strong>like this</strong>',
|
||||
];
|
||||
for (let example of examples) {
|
||||
assert.equal(renderToTextViaHTML(example), renderToText(example));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue