| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import test from 'node:test';
- import assert from 'node:assert/strict';
- import { plainText } from '../src/html-text.js';
- test('inline image emoji preserve the exact text order and wrapper titles do not duplicate them', () => {
- const html = '<p>好<span class="emoticon" title="thumbs up"><img itemtype="http://schema.skype.com/Emoji" alt="👍" title="thumbs up" src="https://example.invalid/ignored"></span>呀<img alt="😀"></p>';
- assert.equal(plainText(html), '好👍呀😀');
- });
- test('emoji-only and non-Unicode emoticon alt spellings remain readable', () => {
- assert.equal(plainText('<p><img alt="😀"></p>'), '😀');
- assert.equal(plainText('<img alt="(laugh)" title="unused">'), '(laugh)');
- assert.equal(plainText('<img alt=" " title="挥手">'), '挥手');
- assert.equal(plainText('<img alt="" src="https://example.invalid/private-image-id">'), '[图片]');
- });
- test('numeric entities, named entities, variation selectors, joiners and skin tones are preserved', () => {
- const html = '<p>A B<img alt="👩🏽‍💻">👍🏼❤️♥</p>';
- assert.equal(plainText(html), 'A\u00a0B👩🏽💻👍🏼❤️♥');
- });
- test('HTML attributes containing greater-than signs and nested markup parse without losing text', () => {
- assert.equal(plainText('<p>before<img alt="A > B" title="ignored > text">after</p><div>next<br>line</div>'), 'beforeA > Bafter\nnext\nline');
- assert.equal(plainText('<p><b>Unclosed formatting<img alt="😀">'), 'Unclosed formatting😀');
- });
- test('scripts, styles, templates and comments never become body text or execute', () => {
- globalThis.__teamsFictionalHtmlExecution = false;
- try {
- assert.equal(plainText('<script>globalThis.__teamsFictionalHtmlExecution=true</script><style>private-style</style><template>private-template</template><!-- private-comment --><img alt="😀" onerror="globalThis.__teamsFictionalHtmlExecution=true" src="https://example.invalid/ignored">'), '😀');
- assert.equal(globalThis.__teamsFictionalHtmlExecution, false);
- } finally { delete globalThis.__teamsFictionalHtmlExecution; }
- });
- test('unknown image placeholders and bounded labels never expose image URLs or split surrogate pairs', () => {
- const content = plainText('<img src="https://example.invalid/private-id" itemid="private-id"><img alt="' + '😀'.repeat(250) + '">');
- assert.equal(content, '[图片]' + '😀'.repeat(200));
- assert.doesNotMatch(content, /https:|private-id|\uFFFD/);
- });
- test('plain text, literal entity text and ordinary blank input remain supported', () => {
- assert.equal(plainText('Plain & text'), 'Plain & text');
- assert.equal(plainText('<p><safe> & friends.</p>'), '<safe> & friends.');
- assert.equal(plainText('<p><br></p>'), '');
- assert.equal(plainText(null), '');
- });
|