correct case where %20 decoded as space in auto link

correct case where onebox is not applied when url is decoded
This commit is contained in:
Sam 2017-07-03 16:32:53 -04:00
parent 340a3ee5cb
commit d2ba543ed3
3 changed files with 20 additions and 6 deletions

View File

@ -89,6 +89,12 @@ function renderHoisted(tokens, idx, options) {
} }
} }
function setupUrlDecoding(md) {
// this fixed a subtle issue where %20 is decoded as space in
// automatic urls
md.utils.lib.mdurl.decode.defaultChars = ';/?:@&=+$,# ';
}
function setupHoister(md) { function setupHoister(md) {
md.renderer.rules.html_raw = renderHoisted; md.renderer.rules.html_raw = renderHoisted;
} }
@ -152,6 +158,7 @@ export function setup(opts, siteSettings, state) {
typographer: siteSettings.enable_markdown_typographer typographer: siteSettings.enable_markdown_typographer
}); });
setupUrlDecoding(opts.engine);
setupHoister(opts.engine); setupHoister(opts.engine);
setupBlockBBCode(opts.engine); setupBlockBBCode(opts.engine);
setupInlineBBCode(opts.engine); setupInlineBBCode(opts.engine);

View File

@ -45,10 +45,7 @@ function applyOnebox(state, silent) {
continue; continue;
} }
// check text matches href // we already know text matches cause it is an auto link
if (text.type !== "text" || attrs[0][1] !== text.content) {
continue;
}
if (!close || close.type !== "link_close") { if (!close || close.type !== "link_close") {
continue; continue;

View File

@ -656,7 +656,6 @@ HTML
end end
it 'handles onebox correctly' do it 'handles onebox correctly' do
# we expect 2 oneboxes
expect(PrettyText.cook("http://a.com\nhttp://b.com").split("onebox").length).to eq(3) expect(PrettyText.cook("http://a.com\nhttp://b.com").split("onebox").length).to eq(3)
expect(PrettyText.cook("http://a.com\n\nhttp://b.com").split("onebox").length).to eq(3) expect(PrettyText.cook("http://a.com\n\nhttp://b.com").split("onebox").length).to eq(3)
expect(PrettyText.cook("a\nhttp://a.com")).to include('onebox') expect(PrettyText.cook("a\nhttp://a.com")).to include('onebox')
@ -664,7 +663,6 @@ HTML
expect(PrettyText.cook("a\nhttp://a.com a")).not_to include('onebox') expect(PrettyText.cook("a\nhttp://a.com a")).not_to include('onebox')
expect(PrettyText.cook("a\nhttp://a.com\na")).to include('onebox') expect(PrettyText.cook("a\nhttp://a.com\na")).to include('onebox')
expect(PrettyText.cook("http://a.com")).to include('onebox') expect(PrettyText.cook("http://a.com")).to include('onebox')
expect(PrettyText.cook("a.com")).not_to include('onebox')
expect(PrettyText.cook("http://a.com ")).to include('onebox') expect(PrettyText.cook("http://a.com ")).to include('onebox')
expect(PrettyText.cook("http://a.com a")).not_to include('onebox') expect(PrettyText.cook("http://a.com a")).not_to include('onebox')
expect(PrettyText.cook("- http://a.com")).not_to include('onebox') expect(PrettyText.cook("- http://a.com")).not_to include('onebox')
@ -796,6 +794,18 @@ HTML
expect(cooked).to eq(html) expect(cooked).to eq(html)
end end
it "support special handling for space in urls" do
cooked = PrettyText.cook "http://testing.com?a%20b"
html = '<p><a href="http://testing.com?a%20b" class="onebox" rel="nofollow noopener">http://testing.com?a%20b</a></p>'
expect(cooked).to eq(html)
end
it "supports onebox for decoded urls" do
cooked = PrettyText.cook "http://testing.com?a%50b"
html = '<p><a href="http://testing.com?a%50b" class="onebox" rel="nofollow noopener">http://testing.com?aPb</a></p>'
expect(cooked).to eq(html)
end
end end
end end