Skip to content

Commit 816790b

Browse files
authored
fix: hexadecimal code-points are not converted (#2)
1 parent 163de3f commit 816790b

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to the "vscode-url-title-resolver" extension will be documen
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7+
## [1.1.1]
8+
9+
### Changed
10+
11+
- Fixed hexadecimal code-points are not converted
12+
713
## [1.1.0]
814

915
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-url-title-resolver",
33
"displayName": "URL title resolver for Markdown",
44
"description": "Resolves selected URLs of one or more HTML pages and uses the document title(s) to format Markdown links",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"publisher": "capybara1",
77
"repository": {
88
"type": "git",

src/lib/htmlHelper.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ const htmlEntityMapping = new Map<string, string>([
240240
['diams', '♦']
241241
]);
242242

243-
const htmlEntityRegExp = new RegExp(`&(#\\d+|${Array.from(htmlEntityMapping.keys()).join('|')});`, 'gi');
243+
const htmlEntityRegExp = new RegExp(`&(#\\d+|#x[\\da-fA-F]+|${Array.from(htmlEntityMapping.keys()).join('|')});`, 'gi');
244244

245245
export function getTitle(html: string): string|null {
246246
const matches = html.match(/(?<=\<title\>).*?(?=\<\/title\>)/si);
@@ -262,12 +262,16 @@ function sanitizeTitle(title: string): string {
262262
function resolveHtmlEntities(html: string) {
263263
return html.replace(
264264
htmlEntityRegExp,
265-
(_, p1: string): string => {
265+
(match, p1: string): string => {
266266
const char = htmlEntityMapping.get(p1);
267267
if (char) {
268268
return char;
269269
}
270-
const codePoint = +p1.substr(1);
271-
return String.fromCodePoint(codePoint);
270+
const codePoint = +p1.substr(1) || parseInt(p1.substr(2), 16);
271+
const result = codePoint
272+
? String.fromCodePoint(codePoint)
273+
: match;
274+
275+
return result;
272276
});
273-
}
277+
}

src/test/suite/htmlHelper.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as assert from 'assert';
2+
3+
import { getTitle } from '../../lib/htmlHelper';
4+
5+
const plainTitle = 'test';
6+
const toHtmlContent = (title: string) => `<html><head><title>${title}</title></head><body></body></html>`;
7+
8+
suite('htmlHelper Test Suite', () => {
9+
10+
test('Given no title element, getTitle returns null', () => {
11+
const input = '';
12+
const result = getTitle(input);
13+
assert.strictEqual(result, null);
14+
});
15+
16+
test('Given a title element with plain title, getTitle returns the plain title', () => {
17+
const input = toHtmlContent(plainTitle);
18+
const result = getTitle(input);
19+
assert.strictEqual(result, plainTitle);
20+
});
21+
22+
test('Given a title element with leading whitespace, getTitle returns the plain title', () => {
23+
const input = toHtmlContent(' \n\t ' + plainTitle);
24+
const result = getTitle(input);
25+
assert.strictEqual(result, plainTitle);
26+
});
27+
28+
test('Given a title element with trailing whitespace, getTitle returns the plain title', () => {
29+
const input = toHtmlContent(plainTitle + ' \n\t ');
30+
const result = getTitle(input);
31+
assert.strictEqual(result, plainTitle);
32+
});
33+
34+
test('Given a title element with named entity, getTitle returns the title with resolved entity', () => {
35+
const input = toHtmlContent('&uuml;');
36+
const expected = 'ü';
37+
const result = getTitle(input);
38+
assert.strictEqual(result, expected);
39+
});
40+
41+
test('Given a title element with decimal code point entity, getTitle returns the title with resolved entity', () => {
42+
const input = toHtmlContent('&#252;');
43+
const expected = 'ü';
44+
const result = getTitle(input);
45+
assert.strictEqual(result, expected);
46+
});
47+
48+
test('Given a title element with hexadecimal code point entity, getTitle returns the title with resolved entity', () => {
49+
const input = toHtmlContent('&#xFC;');
50+
const expected = 'ü';
51+
const result = getTitle(input);
52+
assert.strictEqual(result, expected);
53+
});
54+
55+
test('Given a title element with unknown entity, getTitle returns the title with unresolved entity', () => {
56+
const input = toHtmlContent('&unknown;');
57+
const expected = '&unknown;';
58+
const result = getTitle(input);
59+
assert.strictEqual(result, expected);
60+
});
61+
});

0 commit comments

Comments
 (0)