/
githubmirror
/
obsidian-clipper
Обзор
Документация
Войти
/
githubmirror
/
obsidian-clipper
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/utils/content-extractor.test.ts
49 строк
2 KB
Steph Ango
Mark partial highlights that span inline elements when clipping
15 июн 2026, 23:33
15 июн 2026, 23:33
7d71241
Код
Авторство
О чём код?
// @vitest-environment jsdom import { describe, test, expect } from 'vitest'; import { processHighlights } from './content-extractor'; import { TextHighlightData } from './highlighter'; // Default settings already use highlighterEnabled + 'highlight-inline', the // behavior these tests exercise. function textHighlight(content: string): TextHighlightData { // xpath is empty so processing goes straight to the content-based path, // matching the common case where the recorded xpath (live/reader DOM) // doesn't resolve against the extracted article content. return { type: 'text', id: '1', xpath: '', content, startOffset: 0, endOffset: 0 }; } describe('processHighlights — highlight-inline', () => { const article = '<p>A seismic shift is rocking the healthcare industry. Much like Uber once did.</p>'; test('wraps a whole-paragraph highlight', () => { const result = processHighlights(article, [ textHighlight('<p>A seismic shift is rocking the healthcare industry. Much like Uber once did.</p>'), ]); expect(result).toContain('<mark>A seismic shift is rocking the healthcare industry. Much like Uber once did.</mark>'); }); // Regression for #446 / #852: a sentence highlighted within a paragraph was // dropped because it never equaled the full paragraph text. test('wraps a sentence highlighted within a paragraph', () => { const result = processHighlights(article, [ textHighlight('<p>A seismic shift is rocking the healthcare industry.</p>'), ]); expect(result).toContain('<mark>A seismic shift is rocking the healthcare industry.</mark>'); expect(result).toContain('Much like Uber once did.'); }); // A partial highlight crossing an inline element must still wrap (the range // spans nodes, so it uses extractContents rather than surroundContents). test('wraps a partial highlight that spans an inline element', () => { const withLink = '<p>A <a href="#">seismic</a> shift is rocking the healthcare industry. Much like Uber.</p>'; const result = processHighlights(withLink, [ textHighlight('<p>A seismic shift is rocking the healthcare industry.</p>'), ]); const mark = new DOMParser().parseFromString(result, 'text/html').querySelector('mark'); expect(mark).not.toBeNull(); expect(mark!.textContent?.replace(/\s+/g, ' ').trim()).toBe('A seismic shift is rocking the healthcare industry.'); // The link must survive inside the mark, proving the range crossed it. expect(mark!.querySelector('a')).not.toBeNull(); }); });