mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: escape character references in autolink destinations (#4053)
This commit is contained in:
+10
-6
@@ -157,18 +157,22 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
|
||||
return `<del>${this.parser.parseInline(tokens)}</del>` as RendererOutput;
|
||||
}
|
||||
|
||||
link({ href, title, tokens }: Tokens.Link): RendererOutput {
|
||||
const text = this.parser.parseInline(tokens) as string;
|
||||
link({ href, title, text, tokens, autolink }: Tokens.Link): RendererOutput {
|
||||
// References are not resolved inside an autolink, so every `&` there is
|
||||
// literal. Elsewhere only an `&` that cannot start one needs escaping.
|
||||
const parsedText = autolink
|
||||
? escapeHtmlEntities(text, true)
|
||||
: this.parser.parseInline(tokens) as string;
|
||||
const cleanHref = cleanUrl(href);
|
||||
if (cleanHref === null) {
|
||||
return text as RendererOutput;
|
||||
return parsedText as RendererOutput;
|
||||
}
|
||||
href = cleanHref;
|
||||
href = escapeHtmlEntities(cleanHref, autolink);
|
||||
let out = '<a href="' + href + '"';
|
||||
if (title) {
|
||||
out += ' title="' + (escapeHtmlEntities(title)) + '"';
|
||||
}
|
||||
out += '>' + text + '</a>';
|
||||
out += '>' + parsedText + '</a>';
|
||||
return out as RendererOutput;
|
||||
}
|
||||
|
||||
@@ -182,7 +186,7 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
|
||||
}
|
||||
href = cleanHref;
|
||||
|
||||
let out = `<img src="${href}" alt="${escapeHtmlEntities(text)}"`;
|
||||
let out = `<img src="${escapeHtmlEntities(href)}" alt="${escapeHtmlEntities(text)}"`;
|
||||
if (title) {
|
||||
out += ` title="${escapeHtmlEntities(title)}"`;
|
||||
}
|
||||
|
||||
@@ -937,6 +937,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
raw: cap[0],
|
||||
text,
|
||||
href,
|
||||
autolink: true,
|
||||
tokens: [
|
||||
{
|
||||
type: 'text',
|
||||
@@ -974,6 +975,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
raw: cap[0],
|
||||
text,
|
||||
href,
|
||||
autolink: true,
|
||||
tokens: [
|
||||
{
|
||||
type: 'text',
|
||||
|
||||
@@ -136,6 +136,11 @@ export namespace Tokens {
|
||||
title?: string | null;
|
||||
text: string;
|
||||
tokens: Token[];
|
||||
/**
|
||||
* Set for autolinks and extended (GFM) urls, where character references are
|
||||
* not resolved, so the destination and text are literal.
|
||||
*/
|
||||
autolink?: boolean;
|
||||
}
|
||||
|
||||
export interface List {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<p><a href="https://example.com/?x=1&lt;2">https://example.com/?x=1&lt;2</a></p>
|
||||
<p><a href="https://example.com/?y=1&amp;2">https://example.com/?y=1&amp;2</a></p>
|
||||
<p><a href="https://example.com/?a=1&b=2">https://example.com/?a=1&b=2</a></p>
|
||||
<p><a href="https://example.com/?x=1&lt;2">https://example.com/?x=1&lt;2</a></p>
|
||||
<p><a href="https://example.com/?y=1&amp;2">https://example.com/?y=1&amp;2</a></p>
|
||||
<p><a href="https://example.com/?a=1&b=2">https://example.com/?a=1&b=2</a></p>
|
||||
<p><a href="https://example.com/?x=1<2">https://example.com/?x=1<2</a></p>
|
||||
<p><a href="https://example.com/?y=1&2">https://example.com/?y=1&2</a></p>
|
||||
<p><a href="https://example.com/?a=1&b=2">https://example.com/?a=1&b=2</a></p>
|
||||
<p><a href="https://example.com/?x=1<2">https://example.com/?x=1<2</a></p>
|
||||
<p><a href="https://example.com/?y=1&2">https://example.com/?y=1&2</a></p>
|
||||
<p><a href="https://example.com/?a=1&b=2">https://example.com/?a=1&b=2</a></p>
|
||||
<p><img src="https://example.com/?x=1<2" alt="https://example.com/?x=1<2"></p>
|
||||
<p><img src="https://example.com/?y=1&2" alt="https://example.com/?y=1&2"></p>
|
||||
<p><img src="https://example.com/?a=1&b=2" alt="https://example.com/?a=1&b=2"></p>
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
# Character references resolve in a link destination but not in an autolink, so
|
||||
# the same query string has to be escaped differently depending on where it is
|
||||
# written.
|
||||
#
|
||||
# The `<` inline links, reference links and images below do not match
|
||||
# CommonMark. It resolves the reference and percent-encodes the result, giving
|
||||
# `?x=1%3C2`, where marked keeps `?x=1<2`. That difference comes from URL
|
||||
# encoding rather than from this escaping, and it is the same on `master`.
|
||||
# Everything else here matches CommonMark.
|
||||
renderExact: true
|
||||
---
|
||||
https://example.com/?x=1<2
|
||||
|
||||
https://example.com/?y=1&2
|
||||
|
||||
https://example.com/?a=1&b=2
|
||||
|
||||
<https://example.com/?x=1<2>
|
||||
|
||||
<https://example.com/?y=1&2>
|
||||
|
||||
<https://example.com/?a=1&b=2>
|
||||
|
||||
[https://example.com/?x=1<2](https://example.com/?x=1<2)
|
||||
|
||||
[https://example.com/?y=1&2](https://example.com/?y=1&2)
|
||||
|
||||
[https://example.com/?a=1&b=2](https://example.com/?a=1&b=2)
|
||||
|
||||
[https://example.com/?x=1<2][link1]
|
||||
|
||||
[https://example.com/?y=1&2][link2]
|
||||
|
||||
[https://example.com/?a=1&b=2][link3]
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
[link1]: https://example.com/?x=1<2
|
||||
[link2]: https://example.com/?y=1&2
|
||||
[link3]: https://example.com/?a=1&b=2
|
||||
@@ -2042,6 +2042,7 @@ paragraph
|
||||
raw: '<https://example.com>',
|
||||
text: 'https://example.com',
|
||||
href: 'https://example.com',
|
||||
autolink: true,
|
||||
tokens: [
|
||||
{
|
||||
type: 'text',
|
||||
@@ -2064,6 +2065,7 @@ paragraph
|
||||
raw: '<test@example.com>',
|
||||
text: 'test@example.com',
|
||||
href: 'mailto:test@example.com',
|
||||
autolink: true,
|
||||
tokens: [
|
||||
{
|
||||
type: 'text',
|
||||
@@ -2085,6 +2087,7 @@ paragraph
|
||||
raw: 'https://example.com',
|
||||
text: 'https://example.com',
|
||||
href: 'https://example.com',
|
||||
autolink: true,
|
||||
tokens: [
|
||||
{
|
||||
type: 'text',
|
||||
@@ -2107,6 +2110,7 @@ paragraph
|
||||
raw: 'test@example.com',
|
||||
text: 'test@example.com',
|
||||
href: 'mailto:test@example.com',
|
||||
autolink: true,
|
||||
tokens: [
|
||||
{
|
||||
type: 'text',
|
||||
|
||||
Reference in New Issue
Block a user