Commit c2af7a42 authored by Roy Revelt's avatar Roy Revelt

test: split test file into separate files

parent fb847cf0
import tap from "tap";
import stripHtml from "../dist/string-strip-html.esm";
// opts.cb
// -----------------------------------------------------------------------------
tap.test("01 - opts.cb - baseline, no ranges requested", (t) => {
// baseline, notice dirty whitespace:
t.same(
stripHtml(`<div style="display: inline !important;" >abc</ div>`, {
returnRangesOnly: false,
}),
"abc",
"01"
);
t.end();
});
tap.test("02 - opts.cb - baseline, ranges requested", (t) => {
t.same(
stripHtml("<div >abc</ div>", {
returnRangesOnly: true,
}),
[
[0, 6],
[9, 16],
],
"02"
);
t.end();
});
tap.test("03 - opts.cb - replace hr with tralala", (t) => {
const cb = ({
// tag,
deleteFrom,
deleteTo,
// insert,
rangesArr,
// proposedReturn
}) => {
rangesArr.push(deleteFrom, deleteTo, "<tralala>");
};
t.same(stripHtml("abc<hr>def", { cb }), "abc<tralala>def", "03.01");
t.same(
stripHtml("abc<hr>def", { returnRangesOnly: true, cb }),
[[3, 7, "<tralala>"]],
"03.02"
);
t.end();
});
tap.test("04 - opts.cb - replace div with tralala", (t) => {
const cb = ({
tag,
deleteFrom,
deleteTo,
// insert,
rangesArr,
// proposedReturn
}) => {
rangesArr.push(
deleteFrom,
deleteTo,
`<${tag.slashPresent ? "/" : ""}tralala>`
);
};
t.same(
stripHtml("<div >abc</ div>", { cb }),
"<tralala>abc</tralala>",
"04.01"
);
t.same(
stripHtml("<div >abc</ div>", {
returnRangesOnly: true,
cb,
}),
[
[0, 6, "<tralala>"],
[9, 16, "</tralala>"],
],
"04.02"
);
t.end();
});
tap.test("05 - opts.cb - replace only hr", (t) => {
const cb = ({
tag,
deleteFrom,
deleteTo,
// insert,
rangesArr,
// proposedReturn
}) => {
if (tag.name === "hr") {
rangesArr.push(
deleteFrom,
deleteTo,
`<${tag.slashPresent ? "/" : ""}tralala>`
);
}
};
t.same(
stripHtml("abc<hr>def<span>ghi</span>jkl", { cb }),
"abc<tralala>def<span>ghi</span>jkl",
"05.01"
);
t.same(
stripHtml("abc<hr>def<span>ghi</span>jkl", { returnRangesOnly: true, cb }),
[[3, 7, "<tralala>"]],
"05.02"
);
t.end();
});
tap.test("06 - opts.cb - readme example one", (t) => {
const cb = ({
// tag,
deleteFrom,
deleteTo,
insert,
rangesArr,
// proposedReturn
}) => {
rangesArr.push(deleteFrom, deleteTo, insert);
};
t.same(stripHtml("abc<hr>def", { cb }), "abc def", "06.01");
t.same(
stripHtml("abc<hr>def", { returnRangesOnly: true, cb }),
[[3, 7, " "]],
"06.02"
);
t.end();
});
tap.test(
"07 - opts.cb - ignored tags are also being pinged, with null values",
(t) => {
const capturedTags = [];
const cb = ({
tag,
deleteFrom,
deleteTo,
insert,
rangesArr,
// proposedReturn
}) => {
rangesArr.push(deleteFrom, deleteTo, insert);
capturedTags.push(tag.name);
};
const res = stripHtml("abc<hr>def<br>ghi", { cb, ignoreTags: ["hr"] });
t.same(res, "abc<hr>def ghi", "07.01");
t.same(capturedTags, ["hr", "br"], "07.02");
t.end();
}
);
tap.test(
"08 - opts.cb - ignored tags are also being pinged, with null values",
(t) => {
const capturedTags = [];
const cb = ({
tag,
deleteFrom,
deleteTo,
insert,
rangesArr,
// proposedReturn
}) => {
rangesArr.push(deleteFrom, deleteTo, insert);
capturedTags.push(tag.name);
};
const res = stripHtml("abc<hr>def<br>ghi", {
returnRangesOnly: true,
cb,
ignoreTags: ["hr"],
});
t.same(res, [[10, 14, " "]], "08.01");
t.same(capturedTags, ["hr", "br"], "08.02");
t.end();
}
);
tap.test("09 - opts.cb - cb.tag contents are right on ignored tags", (t) => {
const capturedTags = [];
// const rangesArr = [];
const cb = ({
tag,
// deleteFrom,
// deleteTo,
// insert
// rangesArr
// proposedReturn
}) => {
capturedTags.push(tag);
};
// notice there's no assigning to a variable, we just rely on a callback:
stripHtml("a<br/>b", {
cb,
ignoreTags: ["b", "strong", "i", "em", "br", "sup"],
onlyStripTags: [],
stripTogetherWithTheirContents: ["script", "style", "xml"],
skipHtmlDecoding: true,
returnRangesOnly: true,
trimOnlySpaces: true,
dumpLinkHrefsNearby: {
enabled: false,
putOnNewLine: false,
wrapHeads: "",
wrapTails: "",
},
});
t.same(
capturedTags,
[
{
attributes: [],
lastClosingBracketAt: 5,
lastOpeningBracketAt: 1,
slashPresent: 4,
leftOuterWhitespace: 1,
onlyPlausible: false,
nameStarts: 2,
nameContainsLetters: true,
nameEnds: 4,
name: "br",
},
],
"09.01"
);
t.end();
});
tap.test(
"10 - opts.cb - cb.tag contents are right on non-ignored tags",
(t) => {
const capturedTags = [];
// const rangesArr = [];
const cb = ({
tag,
// deleteFrom,
// deleteTo,
// insert
// rangesArr
// proposedReturn
}) => {
capturedTags.push(tag);
};
// notice there's no assigning to a variable, we just rely on a callback:
stripHtml("abc<br >def<br>ghi<br/>jkl<br />mno", {
cb,
ignoreTags: ["b", "strong", "i", "em", "br", "sup"],
onlyStripTags: [],
stripTogetherWithTheirContents: ["script", "style", "xml"],
skipHtmlDecoding: true,
returnRangesOnly: true,
trimOnlySpaces: true,
dumpLinkHrefsNearby: {
enabled: false,
putOnNewLine: false,
wrapHeads: "",
wrapTails: "",
},
});
t.same(
capturedTags,
[
{
attributes: [],
lastClosingBracketAt: 7,
lastOpeningBracketAt: 3,
leftOuterWhitespace: 3,
name: "br",
nameContainsLetters: true,
nameEnds: 6,
nameStarts: 4,
onlyPlausible: false,
slashPresent: false,
},
{
attributes: [],
lastClosingBracketAt: 14,
lastOpeningBracketAt: 11,
leftOuterWhitespace: 11,
name: "br",
nameContainsLetters: true,
nameEnds: 14,
nameStarts: 12,
onlyPlausible: false,
slashPresent: false,
},
{
attributes: [],
lastClosingBracketAt: 22,
lastOpeningBracketAt: 18,
leftOuterWhitespace: 18,
name: "br",
nameContainsLetters: true,
nameEnds: 21,
nameStarts: 19,
onlyPlausible: false,
slashPresent: 21,
},
{
attributes: [],
lastClosingBracketAt: 31,
lastOpeningBracketAt: 26,
leftOuterWhitespace: 26,
name: "br",
nameContainsLetters: true,
nameEnds: 29,
nameStarts: 27,
onlyPlausible: false,
slashPresent: 30,
},
],
"10.01"
);
t.end();
}
);
import tap from "tap";
import stripHtml from "../dist/string-strip-html.esm";
// CDATA
// -----------------------------------------------------------------------------
tap.test("01 - CDATA - tight", (t) => {
// surroundings are not a linebreaks
t.same(
stripHtml(`a<![CDATA[
The <, &, ', and " can be used,
*and* %MyParamEntity; can be expanded.
]]>b`),
"a b",
"01"
);
t.end();
});
tap.test("02 - CDATA - normal", (t) => {
t.same(
stripHtml(`a <![CDATA[
The <, &, ', and " can be used,
*and* %MyParamEntity; can be expanded.
]]> b`),
"a b",
"02"
);
t.end();
});
tap.test("03 - CDATA - loose", (t) => {
t.same(
stripHtml(`a \t\t<![CDATA[
The <, &, ', and " can be used,
*and* %MyParamEntity; can be expanded.
]]> b`),
"a b",
"03"
);
t.end();
});
tap.test("04 - CDATA - single linebreaks", (t) => {
// surroundings are linebreaks
t.same(
stripHtml(`a\n<![CDATA[
The <, &, ', and " can be used,
*and* %MyParamEntity; can be expanded.
]]>\nb`),
"a\n\nb",
"04"
);
t.end();
});
tap.test("05 - CDATA - excessive linebreaks", (t) => {
t.same(
stripHtml(`a\n\n\n<![CDATA[
The <, &, ', and " can be used,
*and* %MyParamEntity; can be expanded.
]]>\n\n\nb`),
"a\n\nb",
"05"
);
t.end();
});
tap.test("06 - CDATA - mixed linebreaks", (t) => {
t.same(
stripHtml(`a\n \t\n\n<![CDATA[
The <, &, ', and " can be used,
*and* %MyParamEntity; can be expanded.
]]>\n\n\n\t b`),
"a\n\nb",
"06"
);
t.end();
});
import tap from "tap";
import stripHtml from "../dist/string-strip-html.esm";
// opts.dumpLinkHrefsNearby
// -----------------------------------------------------------------------------
tap.test("01 - opts.dumpLinkHrefsNearby - clean code, double quotes", (t) => {
t.same(
stripHtml(
'Let\'s watch <a href="https://www.rt.com/" target="_blank">RT news</a> this evening'
),
"Let's watch RT news this evening",
"01.01 - control, default behaviour"
);
t.same(
stripHtml(
'Let\'s watch <a href="https://www.rt.com/" target="_blank">RT news</a> this evening',
{ dumpLinkHrefsNearby: { enabled: false } }
),
"Let's watch RT news this evening",
"01.02 - control, hardcoded default"
);
t.same(
stripHtml(
'Let\'s watch <a href="https://www.rt.com/" target="_blank">RT news</a> this evening',
{ dumpLinkHrefsNearby: { enabled: true } }
),
"Let's watch RT news https://www.rt.com/ this evening",
"01.03 - control, default behaviour"
);
t.same(
stripHtml(
'Let\'s sell some juicy gossip to the <a href="mailto:[email protected]" target="_blank">The Sun</a> right now!',
{ dumpLinkHrefsNearby: { enabled: true } }
),
"Let's sell some juicy gossip to the The Sun mailto:[email protected] right now!",
"01.04 - mailto links without customisation"
);
t.same(
stripHtml(
'Here\'s the <a href="mailto:[email protected][email protected]&subject=look%20what%20Kate%20did%20last%20night" target="_blank">chief editor\'s</a> email.',
{ dumpLinkHrefsNearby: { enabled: true } }
),
"Here's the chief editor's mailto:[email protected][email protected]&subject=look%20what%20Kate%20did%20last%20night email.",
"01.05 - mailto links with customisation"
);
t.end();
});
tap.test("02 - opts.dumpLinkHrefsNearby - clean code, single quotes", (t) => {
t.same(
stripHtml(
"Let's watch <a href='https://www.rt.com/' target='_blank'>RT news</a> this evening"
),
"Let's watch RT news this evening",
"02.01 - control, default behaviour"
);
t.same(
stripHtml(
"Let's watch <a href='https://www.rt.com/' target='_blank'>RT news</a> this evening",
{ dumpLinkHrefsNearby: { enabled: false } }
),
"Let's watch RT news this evening",
"02.02 - control, hardcoded default"
);
t.same(
stripHtml(
"Let's watch <a href='https://www.rt.com/' target='_blank'>RT news</a> this evening",
{ dumpLinkHrefsNearby: { enabled: true } }
),
"Let's watch RT news https://www.rt.com/ this evening",
"02.03 - control, default behaviour"
);
t.same(
stripHtml(
"Let's sell some juicy gossip to the <a href='mailto:[email protected]' target='_blank'>The Sun</a> right now!",
{ dumpLinkHrefsNearby: { enabled: true } }
),
"Let's sell some juicy gossip to the The Sun mailto:[email protected] right now!",
"02.04 - mailto links without customisation"
);
t.same(
stripHtml(
"Here's the <a href='mailto:[email protected][email protected]&subject=look%20what%20Kate%20did%20last%20night' target='_blank'>chief editor's</a> email.",
{ dumpLinkHrefsNearby: { enabled: true } }
),
"Here's the chief editor's mailto:[email protected][email protected]&subject=look%20what%20Kate%20did%20last%20night email.",
"02.05 - mailto links with customisation"
);
t.end();
});
tap.test(
"03 - opts.dumpLinkHrefsNearby - dirty code, HTML is chopped but href captured",
(t) => {
t.same(
stripHtml('Let\'s watch <a href="https://www.rt.com/" targ'),
"Let's watch",
"03.01 - control, default behaviour"
);
t.same(
stripHtml('Let\'s watch <a href="https://www.rt.com/" targ', {
dumpLinkHrefsNearby: { enabled: true },
}),
"Let's watch https://www.rt.com/",
"03.02 - only href contents are left after stripping"
);
t.end();
}
);
tap.test("04 - opts.dumpLinkHrefsNearby - linked image", (t) => {
t.same(
stripHtml(
`a <a href="https://codsen.com" target="_blank"><img src="http://404.codsen.com/spacer.gif" width="111" height="222" border="0" style="display:block;" alt="linked image"/></a> b`
),
"a b",
"04.01 - control, default"
);
t.same(
stripHtml(
`a <a href="https://codsen.com" target="_blank"><img src="http://404.codsen.com/spacer.gif" width="111" height="222" border="0" style="display:block;" alt="linked image"/></a> b`,
{ dumpLinkHrefsNearby: { enabled: false } }
),
"a b",
"04.02 - control, hardcoded default"
);
t.same(
stripHtml(
`a <a href="https://codsen.com" target="_blank"><img src="http://404.codsen.com/spacer.gif" width="111" height="222" border="0" style="display:block;" alt="linked image"/></a> b`,
{ dumpLinkHrefsNearby: { enabled: true } }
),
"a https://codsen.com b",
"04.03 - dumps href of a linked image"
);
t.end();
});
tap.test("05 - opts.dumpLinkHrefsNearby - .putOnNewLine", (t) => {
// control
t.same(
stripHtml(
`a <a href="https://codsen.com" target="_blank"><img src="http://404.codsen.com/spacer.gif" width="111" height="222" border="0" style="display:block;" alt="linked image"/></a> b`
),
"a b",
"05.01 - control, default, off"
);
// control
t.same(
stripHtml(
`a <a href="https://codsen.com" target="_blank"><img src="http://404.codsen.com/spacer.gif" width="111" height="222" border="0" style="display:block;" alt="linked image"/></a> b`,
{
dumpLinkHrefsNearby: {
enabled: true,
putOnNewLine: false, // <------------- !
},
}
),
"a https://codsen.com b",
"05.02 - dumpLinkHrefsNearby = on; putOnNewLine = off"
);
// control
t.same(
stripHtml(
`a <a href="https://codsen.com" target="_blank"><img src="http://404.codsen.com/spacer.gif" width="111" height="222" border="0" style="display:block;" alt="linked image"/></a> b`,
{
dumpLinkHrefsNearby: {
enabled: true,
putOnNewLine: true, // <------------- !
},
}
),
"a\n\nhttps://codsen.com\n\nb",
"05.03 - dumpLinkHrefsNearby = on; putOnNewLine = on"
);
t.same(
stripHtml(
`a <a href="https://codsen.com" target="_blank"><img src="http://404.codsen.com/spacer.gif" width="111" height="222" border="0" style="display:block;" alt="linked image"/></a> b`,
{
dumpLinkHrefsNearby: {
enabled: true,
putOnNewLine: true,
wrapHeads: "[", // <------------ !
wrapTails: "]", // <------------- !
},
}
),
"a\n\n[https://codsen.com]\n\nb",
"05.04 - dumpLinkHrefsNearby = on; putOnNewLine = on; wrapHeads = on; wrapTails = on;"
);
t.end();
});
tap.test("06 - opts.dumpLinkHrefsNearby - wrapHeads/wrapTails", (t) => {
// control
t.same(
stripHtml(
`a<a href="https://codsen.com" target="_blank"><div>z</div></a>b`
),
"a z b",
"06.01 - control, default"
);
// default dump
t.same(
stripHtml(
`a<a href="https://codsen.com" target="_blank"><div>z</div></a>b`,
{
dumpLinkHrefsNearby: {
enabled: true,
},
}
),
"a z https://codsen.com b",
"06.02 - heads only"
);
// wrap heads only
t.same(
stripHtml(
`a<a href="https://codsen.com" target="_blank"><div>z</div></a>b`,
{
dumpLinkHrefsNearby: {
enabled: true,
wrapHeads: "[",
},
}
),
"a z [https://codsen.com b",
"06.03 - heads only"
);
// wrap heads only
t.same(
stripHtml(
`a<a href="https://codsen.com" target="_blank"><div>z</div></a>b`,
{
dumpLinkHrefsNearby: {
enabled: true,
wrapTails: "]",
},
}
),
"a z https://codsen.com] b",
"06.04 - tails only"
);
// wrap heads only
t.same(
stripHtml(
`a<a href="https://codsen.com" target="_blank"><div>z</div></a>b`,
{
dumpLinkHrefsNearby: {
enabled: true,
wrapHeads: "[",
wrapTails: "]",
},
}
),
"a z [https://codsen.com] b",
"06.05 - tails only"
);
// + ignoreTags
t.same(
stripHtml(
`a<a href="https://codsen.com" target="_blank"><div>z</div></a>b`,
{
ignoreTags: "div",
dumpLinkHrefsNearby: {
enabled: true,
wrapHeads: "[",
wrapTails: "]",
},
}
),
"a <div>z</div> [https://codsen.com] b",
"06.06 - ignore on a div only"
);
// + ignoreTags
t.same(
stripHtml(
`a<a href="https://codsen.com" target="_blank"><div>z</div></a>b`,