Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/distill.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ export function distill(html, url = '', { view = null, type = '' } = {}) {
// space that was never in the page.
if (text) {
blocks.push({ type: 'text', text, host: node.parentNode, pre: /^\s/.test(raw), post: /\s$/.test(raw) });
} else if (raw && blocks.at(-1)?.type === 'text') {
// A node that is only a space still separates the words either side
// of it. Wikipedia writes every   as a span holding nothing
// else, and the fragments around it share a parent, so without this
// an infobox read "Firstappeared" and "20February 1991".
blocks.at(-1).post = true;
}
return;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/distill.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,20 @@ test('an entity does not put spaces inside a word', () => {
assert.ok(text.includes('interface & its restraint'), 'a real space was swallowed');
});

test('a space in an element of its own still separates two words', () => {
// Wikipedia's HTML writes every &nbsp; as <span typeof="mw:Entity">&nbsp;</span>.
// That span has no text once whitespace is collapsed, and the words either
// side of it were glued: "Firstappeared", "20February 1991".
const p = distill(`<html><body><table>
<tr><th>First<span typeof="mw:Entity">&nbsp;</span>appeared</th>
<td>20<span typeof="mw:Entity">&nbsp;</span>February 1991</td></tr>
</table><p>ten<b> </b>items</p></body></html>`, 'https://example.test/');
const text = p.blocks.map((b) => b.text).join('\n');
assert.ok(text.includes('First appeared'), `words glued:\n${text}`);
assert.ok(text.includes('20 February 1991'), `words glued:\n${text}`);
assert.ok(text.includes('ten items'), `words glued:\n${text}`);
});

test('a one-character value is kept, a lone bullet or separator is not', () => {
// A Wikipedia infobox puts each value in a cell of its own, so a single
// digit is a whole block: "Children" was printed with no number after it.
Expand Down
Loading