Skip to content

fix: hexadecimal code-points are not converted #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2021
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to the "vscode-url-title-resolver" extension will be documen

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

## [1.1.1]

### Changed

- Fixed hexadecimal code-points are not converted

## [1.1.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-url-title-resolver",
"displayName": "URL title resolver for Markdown",
"description": "Resolves selected URLs of one or more HTML pages and uses the document title(s) to format Markdown links",
"version": "1.1.0",
"version": "1.1.1",
"publisher": "capybara1",
"repository": {
"type": "git",
Expand Down
14 changes: 9 additions & 5 deletions src/lib/htmlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const htmlEntityMapping = new Map<string, string>([
['diams', '♦']
]);

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

export function getTitle(html: string): string|null {
const matches = html.match(/(?<=\<title\>).*?(?=\<\/title\>)/si);
Expand All @@ -262,12 +262,16 @@ function sanitizeTitle(title: string): string {
function resolveHtmlEntities(html: string) {
return html.replace(
htmlEntityRegExp,
(_, p1: string): string => {
(match, p1: string): string => {
const char = htmlEntityMapping.get(p1);
if (char) {
return char;
}
const codePoint = +p1.substr(1);
return String.fromCodePoint(codePoint);
const codePoint = +p1.substr(1) || parseInt(p1.substr(2), 16);
const result = codePoint
? String.fromCodePoint(codePoint)
: match;

return result;
});
}
}
61 changes: 61 additions & 0 deletions src/test/suite/htmlHelper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as assert from 'assert';

import { getTitle } from '../../lib/htmlHelper';

const plainTitle = 'test';
const toHtmlContent = (title: string) => `<html><head><title>${title}</title></head><body></body></html>`;

suite('htmlHelper Test Suite', () => {

test('Given no title element, getTitle returns null', () => {
const input = '';
const result = getTitle(input);
assert.strictEqual(result, null);
});

test('Given a title element with plain title, getTitle returns the plain title', () => {
const input = toHtmlContent(plainTitle);
const result = getTitle(input);
assert.strictEqual(result, plainTitle);
});

test('Given a title element with leading whitespace, getTitle returns the plain title', () => {
const input = toHtmlContent(' \n\t ' + plainTitle);
const result = getTitle(input);
assert.strictEqual(result, plainTitle);
});

test('Given a title element with trailing whitespace, getTitle returns the plain title', () => {
const input = toHtmlContent(plainTitle + ' \n\t ');
const result = getTitle(input);
assert.strictEqual(result, plainTitle);
});

test('Given a title element with named entity, getTitle returns the title with resolved entity', () => {
const input = toHtmlContent('&uuml;');
const expected = 'ü';
const result = getTitle(input);
assert.strictEqual(result, expected);
});

test('Given a title element with decimal code point entity, getTitle returns the title with resolved entity', () => {
const input = toHtmlContent('&#252;');
const expected = 'ü';
const result = getTitle(input);
assert.strictEqual(result, expected);
});

test('Given a title element with hexadecimal code point entity, getTitle returns the title with resolved entity', () => {
const input = toHtmlContent('&#xFC;');
const expected = 'ü';
const result = getTitle(input);
assert.strictEqual(result, expected);
});

test('Given a title element with unknown entity, getTitle returns the title with unresolved entity', () => {
const input = toHtmlContent('&unknown;');
const expected = '&unknown;';
const result = getTitle(input);
assert.strictEqual(result, expected);
});
});