2021-12-22 11:28:20 +00:00
|
|
|
import * as parser from 'node-html-parser';
|
|
|
|
import { parse } from './html';
|
2020-09-08 10:26:17 +00:00
|
|
|
|
2021-08-18 05:46:56 +00:00
|
|
|
describe('util/html', () => {
|
2020-09-08 10:26:17 +00:00
|
|
|
it('parses HTML', () => {
|
|
|
|
const body = parse('<div>Hello, world!</div>');
|
2021-03-01 14:21:04 +00:00
|
|
|
expect(body.childNodes).toHaveLength(1);
|
2021-12-22 11:28:20 +00:00
|
|
|
const div = body.childNodes[0] as parser.HTMLElement;
|
2020-09-08 10:26:17 +00:00
|
|
|
expect(div.tagName).toBe('DIV');
|
|
|
|
expect(div.textContent).toBe('Hello, world!');
|
2021-12-22 11:28:20 +00:00
|
|
|
expect(div instanceof parser.HTMLElement).toBeTrue();
|
2020-09-08 10:26:17 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2020-09-08 10:26:17 +00:00
|
|
|
it('returns empty', () => {
|
|
|
|
const body = parse('');
|
2021-03-01 14:21:04 +00:00
|
|
|
expect(body.childNodes).toHaveLength(0);
|
2020-09-08 10:26:17 +00:00
|
|
|
});
|
2021-10-01 08:39:29 +00:00
|
|
|
|
|
|
|
it('parses HTML: PRE block hides child nodes', () => {
|
|
|
|
const body = parse('<div>Hello, world!</div>\n<pre><a>node A</a></pre>');
|
|
|
|
const childNodesA = body.querySelectorAll('a');
|
|
|
|
expect(childNodesA).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('parses HTML: use additional options to discover child nodes on PRE blocks', () => {
|
|
|
|
const body = parse('<div>Hello, world!</div>\n<pre><a>node A</a></pre>', {
|
|
|
|
blockTextElements: {},
|
|
|
|
});
|
|
|
|
const childNodesA = body.querySelectorAll('a');
|
|
|
|
expect(childNodesA).toHaveLength(1);
|
|
|
|
const div = childNodesA[0];
|
|
|
|
expect(div.tagName).toBe('A');
|
|
|
|
expect(div.textContent).toBe('node A');
|
2021-12-22 11:28:20 +00:00
|
|
|
expect(div instanceof parser.HTMLElement).toBe(true);
|
2021-10-01 08:39:29 +00:00
|
|
|
});
|
2020-09-08 10:26:17 +00:00
|
|
|
});
|