From 2c4ae43e688a9873e86211ea0e7aeb9ba770dd77 Mon Sep 17 00:00:00 2001 From: Minteck Date: Tue, 18 Oct 2022 08:59:09 +0200 Subject: Update --- .../examples/leave-only-opening-td.js | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js (limited to 'alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js') diff --git a/alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js b/alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js new file mode 100644 index 0000000..e8ba37f --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js @@ -0,0 +1,62 @@ +// Leave only opening `td` tags + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +const someHtml = ` + + + + + + + + +
+ cell1 + + cell2 +
+ cell3 + + cell4 +
`; + +// the first way +// ----------------------------------------------------------------------------- + +assert.equal( + stripHtml(someHtml, { + // notice there's no: onlyStripTags: ["td"] + // we operate purely via callback + cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => { + if (tag.name === "td" && !tag.slashPresent) { + rangesArr.push(proposedReturn); + } + }, + }).ranges.reduce( + (acc, [from, to]) => `${acc}${someHtml.slice(from, to).trim()}`, + "" + ), + `` +); + +// the second way: +// ----------------------------------------------------------------------------- + +let resultStr = ""; +// notice we don't even assign stripHtml() output to anything - we rely only +// on the callback, it mutates the "resultStr" in the upper scope +stripHtml(someHtml, { + // notice there's no: onlyStripTags: ["td"] + // we operate purely via callback + cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => { + if (tag.name === "td" && !tag.slashPresent) { + resultStr += someHtml.slice(deleteFrom, deleteTo).trim(); + } + }, +}); +assert.equal( + resultStr, + `` +); -- cgit