Skip to content
Open
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
52 changes: 52 additions & 0 deletions util/block_lines.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2024 DigitalOcean

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

const blockLines = require('./block_lines');

it('extracts lines from a markdown-it block state', () => {
const src = 'one\ntwo\nthree';
const state = {
src,
bMarks: [ 0, 4, 8 ],
eMarks: [ 3, 7, 13 ],
tShift: [ 0, 0, 0 ],
};
expect(blockLines(state, 0, 3)).toEqual([ 'one', 'two', 'three' ]);
});

it('respects start and end line bounds', () => {
const src = 'alpha\nbeta\ngamma';
const state = {
src,
bMarks: [ 0, 6, 11 ],
eMarks: [ 5, 10, 16 ],
tShift: [ 0, 0, 0 ],
};
expect(blockLines(state, 1, 3)).toEqual([ 'beta', 'gamma' ]);
});

it('applies tShift when reading lines', () => {
const src = ' indented';
const state = {
src,
bMarks: [ 0 ],
eMarks: [ 10 ],
tShift: [ 2 ],
};
expect(blockLines(state, 0, 1)).toEqual([ 'indented' ]);
});
101 changes: 101 additions & 0 deletions util/dom_utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2024 DigitalOcean

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

const { Parser } = require('htmlparser2');
const { DomHandler, Element } = require('domhandler');
const { textContent, appendChild } = require('domutils');

const {
domRemoveEmpty,
domOffsetNode,
domSplit,
domCommonAncestor,
domNextSiblingsEmpty,
domPreviousSiblingsEmpty,
} = require('./dom_utils');

/**
* Parse HTML into a root element containing the document nodes.
*
* @param {string} html HTML to parse.
* @returns {import('domhandler').Element}
*/
const parseRoot = html => {
let dom;
const handler = new DomHandler((err, nodes) => {
if (err) throw err;
dom = nodes;
});
new Parser(handler).end(html);
const root = new Element('root', {});
dom.forEach(node => appendChild(root, node));
return root;
};

it('removes empty nodes recursively', () => {
const root = parseRoot('<p>keep</p><span></span>');
domRemoveEmpty(root);
expect(textContent(root)).toContain('keep');
expect(root.children.some(n => n.name === 'span')).toBe(false);
});

it('finds a common ancestor of two nodes', () => {
const root = parseRoot('<div><b>a</b><i>b</i></div>');
const div = root.children[0];
const bold = div.children[0];
const italic = div.children[1];
expect(domCommonAncestor(bold, italic)).toBe(div);
});

it('reports whether next siblings are empty', () => {
const root = parseRoot('<p>text<span></span></p>');
const p = root.children[0];
const text = p.children[0];
expect(domNextSiblingsEmpty(text)).toBe(true);
});

it('reports whether previous siblings are empty', () => {
const root = parseRoot('<p><span></span>text</p>');
const p = root.children[0];
const text = p.children[1];
expect(domPreviousSiblingsEmpty(text)).toBe(true);
});

it('resolves a text offset within a document', () => {
const root = parseRoot('<p>hello world</p>');
const p = root.children[0];
const text = p.children[0];
const result = domOffsetNode(text, 6, root);
expect(result.node).toBe(text);
expect(result.offset).toBe(6);
expect(textContent(result.node).slice(result.offset)).toBe('world');
});

it('splits a text node under a root', () => {
const root = parseRoot('<p>abcdef</p>');
const p = root.children[0];
const text = p.children[0];
const { left, right } = domSplit(p, text, 3, true);
expect(left.map(n => textContent(n)).join('')).toBe('abc');
expect(right.map(n => textContent(n)).join('')).toBe('def');
});

it('throws when offset is negative', () => {
const root = parseRoot('<p>x</p>');
expect(() => domOffsetNode(root.children[0].children[0], -1, root)).toThrow(/Negative offset/);
});
42 changes: 42 additions & 0 deletions util/find_attr.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024 DigitalOcean

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

const findAttr = require('./find_attr');

it('finds a double-quoted attribute', () => {
const tag = '<div class="foo">';
const pos = findAttr('class', tag);
expect(pos).toEqual({ start: 5, end: 16 });
expect(tag.slice(pos.start, pos.end)).toBe('class="foo"');
});

it('finds a single-quoted attribute', () => {
const tag = '<span id=\'bar\'>';
const pos = findAttr('id', tag);
expect(tag.slice(pos.start, pos.end)).toBe('id=\'bar\'');
});

it('returns null when the attribute is missing', () => {
expect(findAttr('class', '<div id="x">')).toBeNull();
});

it('ignores attribute-like text inside other attribute values', () => {
const tag = '<div title=" class=nope " class="real">';
const pos = findAttr('class', tag);
expect(tag.slice(pos.start, pos.end)).toBe('class="real"');
});
46 changes: 46 additions & 0 deletions util/find_block_embed.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2024 DigitalOcean

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

const findBlockEmbed = require('./find_block_embed');

it('finds a simple embed block closing index', () => {
const lines = [ '[details', 'summary', 'body', ']' ];
expect(findBlockEmbed(lines, 'details')).toBe(3);
});

it('returns false when there are too few lines', () => {
expect(findBlockEmbed([ '[details', ']' ], 'details')).toBe(false);
});

it('returns false when the opening type does not match', () => {
expect(findBlockEmbed([ '[other', 'x', ']' ], 'details')).toBe(false);
});

it('supports an opening line with trailing attributes', () => {
const lines = [ '[details open', 'title', 'content', ']' ];
expect(findBlockEmbed(lines, 'details')).toBe(3);
});

it('handles nested bracket pairs inside the block', () => {
const lines = [ '[details', '[nested', ']', 'done', ']' ];
expect(findBlockEmbed(lines, 'details')).toBe(4);
});

it('returns false when the closing bracket is missing', () => {
expect(findBlockEmbed([ '[details', 'a', 'b' ], 'details')).toBe(false);
});
42 changes: 42 additions & 0 deletions util/find_tag_open.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024 DigitalOcean

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

const findTagOpen = require('./find_tag_open');

it('finds a simple opening tag', () => {
const html = '<p>hello</p>';
const pos = findTagOpen('p', html);
expect(pos).toEqual({ start: 0, end: 3 });
expect(html.slice(pos.start, pos.end)).toBe('<p>');
});

it('finds a tag with attributes', () => {
const html = '<div class="x">content</div>';
const pos = findTagOpen('div', html);
expect(html.slice(pos.start, pos.end)).toBe('<div class="x">');
});

it('returns null when the tag is not present', () => {
expect(findTagOpen('span', '<div></div>')).toBeNull();
});

it('ignores greater-than signs inside attribute values', () => {
const html = '<a title="a > b">link</a>';
const pos = findTagOpen('a', html);
expect(html.slice(pos.start, pos.end)).toBe('<a title="a > b">');
});
37 changes: 37 additions & 0 deletions util/helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2024 DigitalOcean

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

const { dedupeArray, alwaysArray, regexEscape } = require('./helpers');

it('deduplicates an array while preserving order', () => {
expect(dedupeArray([ 1, 2, 2, 3, 1 ])).toEqual([ 1, 2, 3 ]);
});

it('wraps a non-array value in an array', () => {
expect(alwaysArray('x')).toEqual([ 'x' ]);
});

it('returns the same array when already an array', () => {
const arr = [ 1, 2 ];
expect(alwaysArray(arr)).toBe(arr);
});

it('escapes special RegExp characters', () => {
expect(regexEscape('a.b*c?')).toBe('a\\.b\\*c\\?');
expect(new RegExp(regexEscape('(test)')).test('(test)')).toBe(true);
});
38 changes: 38 additions & 0 deletions util/prism_keep_html.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 DigitalOcean

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

const Prism = require('../vendor/prismjs');
require('../vendor/prismjs/components/prism-clike')(Prism);
require('../vendor/prismjs/components/prism-javascript')(Prism);
const keepHtml = require('./prism_keep_html');

it('registers the KeepHTML plugin once', () => {
keepHtml(Prism);
expect(Prism.plugins.KeepHTML).toBe(true);
keepHtml(Prism);
expect(Prism.plugins.KeepHTML).toBe(true);
});

it('preserves existing HTML markup while highlighting', () => {
keepHtml(Prism);
const input = 'const <mark>x</mark> = 1;';
const output = Prism.highlight(input, Prism.languages.javascript, 'javascript');
expect(output).toContain('<mark>');
expect(output).toContain('</mark>');
expect(output).toContain('token');
});
Loading