html-text.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { plainText } from '../src/html-text.js';
  4. test('inline image emoji preserve the exact text order and wrapper titles do not duplicate them', () => {
  5. 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>';
  6. assert.equal(plainText(html), '好👍呀😀');
  7. });
  8. test('emoji-only and non-Unicode emoticon alt spellings remain readable', () => {
  9. assert.equal(plainText('<p><img alt="😀"></p>'), '😀');
  10. assert.equal(plainText('<img alt="(laugh)" title="unused">'), '(laugh)');
  11. assert.equal(plainText('<img alt=" " title="挥手">'), '挥手');
  12. assert.equal(plainText('<img alt="" src="https://example.invalid/private-image-id">'), '[图片]');
  13. });
  14. test('numeric entities, named entities, variation selectors, joiners and skin tones are preserved', () => {
  15. const html = '<p>A&nbsp;B<img alt="&#x1F469;&#x1F3FD;&zwj;&#x1F4BB;">&#128077;&#127996;❤️&hearts;</p>';
  16. assert.equal(plainText(html), 'A\u00a0B👩🏽‍💻👍🏼❤️♥');
  17. });
  18. test('HTML attributes containing greater-than signs and nested markup parse without losing text', () => {
  19. assert.equal(plainText('<p>before<img alt="A &gt; B" title="ignored > text">after</p><div>next<br>line</div>'), 'beforeA > Bafter\nnext\nline');
  20. assert.equal(plainText('<p><b>Unclosed formatting<img alt="😀">'), 'Unclosed formatting😀');
  21. });
  22. test('scripts, styles, templates and comments never become body text or execute', () => {
  23. globalThis.__teamsFictionalHtmlExecution = false;
  24. try {
  25. 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">'), '😀');
  26. assert.equal(globalThis.__teamsFictionalHtmlExecution, false);
  27. } finally { delete globalThis.__teamsFictionalHtmlExecution; }
  28. });
  29. test('unknown image placeholders and bounded labels never expose image URLs or split surrogate pairs', () => {
  30. const content = plainText('<img src="https://example.invalid/private-id" itemid="private-id"><img alt="' + '😀'.repeat(250) + '">');
  31. assert.equal(content, '[图片]' + '😀'.repeat(200));
  32. assert.doesNotMatch(content, /https:|private-id|\uFFFD/);
  33. });
  34. test('plain text, literal entity text and ordinary blank input remain supported', () => {
  35. assert.equal(plainText('Plain &amp; text'), 'Plain & text');
  36. assert.equal(plainText('<p>&lt;safe&gt; &amp; friends.</p>'), '<safe> & friends.');
  37. assert.equal(plainText('<p><br></p>'), '');
  38. assert.equal(plainText(null), '');
  39. });