Skip to content
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

Improve XML handling #3

Merged
merged 2 commits into from
May 30, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ os:
- osx
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
- '4'
8 changes: 8 additions & 0 deletions fixture/youtube-with-complex-query.webloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
<string>https://www.youtube.com/watch?v=XXXXXXXXXXX&amp;complex=true</string>
</dict>
</plist>
8 changes: 8 additions & 0 deletions fixture/youtube-with-query.webloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
<string>https://www.youtube.com/watch?v=XXXXXXXXXXX</string>
</dict>
</plist>
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,39 @@ const getExt = () => {
}
};

const xmlTagRegexp = /<.*?>/g;
const xmlEscapeRegexp = /&(#\d+|lt|gt|quot|apos|amp);/g;
const xmlUnescape = str => str.replace(xmlEscapeRegexp, (_, escape) => {
switch (escape) {
case 'lt': return '<';
case 'gt': return '>';
case 'quot': return '"';
case 'apos': return '\'';
case 'amp': return '&';
default: return String.fromCharCode(parseInt(escape.substr(1), 10));
}
});

module.exports = filepath => {
filepath += path.extname(filepath) ? '' : getExt();

return pify(fs.readFile)(filepath, 'utf8')
.then(data => getUrls(data.replace(/<!doctype.*/i, ''))[0].trim())
.catch(err => {
if (err.code === 'ENOENT') {
err.message = `Couldn't find a web shortcut with the name \`${path.basename(`${filepath}\``)}`;
.then(text => {
let data = text.trim();

const isXml = data[0] === '<';
stroncium marked this conversation as resolved.
Show resolved Hide resolved
if (isXml) {
data = data.replace(xmlTagRegexp, ' ');
data = xmlUnescape(data);
}

return getUrls(data)[0].trim();
})
.catch(error => {
if (error.code === 'ENOENT') {
error.message = `Couldn't find a web shortcut with the name \`${path.basename(`${filepath}\``)}`;
}

throw err;
throw error;
});
};
14 changes: 11 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import m from './';
import m from '.';

test('main', async t => {
t.is(await m('fixture/google'), 'https://google.com');
Expand All @@ -9,6 +9,14 @@ test('with extension', async t => {
t.is(await m('fixture/google.webloc'), 'https://google.com');
});

test('unknown shortcut', t => {
t.throws(m('fixture/unknown.webloc'), 'Couldn\'t find a web shortcut with the name `unknown.webloc`');
test('webloc with query, #2', async t => {
t.is(await m('fixture/youtube-with-query.webloc'), 'https://youtube.com/watch?v=XXXXXXXXXXX');
});

test('webloc complex query, #2', async t => {
t.is(await m('fixture/youtube-with-complex-query.webloc'), 'https://youtube.com/watch?complex=true&v=XXXXXXXXXXX');
});

test('unknown shortcut', async t => {
await t.throwsAsync(() => m('fixture/unknown.webloc'), 'Couldn\'t find a web shortcut with the name `unknown.webloc`');
});