Skip to content

Commit

Permalink
Fix (double) encoding on urls
Browse files Browse the repository at this point in the history
In f21c1b1, an algorithm was pulled in that *also* encodes.
We’re operating on the AST here, encoding will be handled at the end.
As it happened here, things were encoded twice.

Closes GH-67.
  • Loading branch information
wooorm committed Sep 28, 2022
1 parent c393d0a commit dc00a59
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @typedef {import('./index.js').H} H
*/

import {sanitizeUri} from 'micromark-util-sanitize-uri'
import {normalizeUri} from 'micromark-util-sanitize-uri'
import {u} from 'unist-builder'
import {all} from './traverse.js'
import {wrap} from './wrap.js'
Expand All @@ -27,7 +27,7 @@ export function footer(h) {

const content = all(h, def)
const id = String(def.identifier)
const safeId = sanitizeUri(id.toLowerCase())
const safeId = normalizeUri(id.toLowerCase())
let referenceIndex = 0
/** @type {Array<ElementContent>} */
const backReferences = []
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/footnote-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @typedef {import('../index.js').Handler} Handler
*/

import {sanitizeUri} from 'micromark-util-sanitize-uri'
import {normalizeUri} from 'micromark-util-sanitize-uri'
import {u} from 'unist-builder'

/**
Expand All @@ -12,7 +12,7 @@ import {u} from 'unist-builder'
*/
export function footnoteReference(h, node) {
const id = String(node.identifier)
const safeId = sanitizeUri(id.toLowerCase())
const safeId = normalizeUri(id.toLowerCase())
const index = h.footnoteOrder.indexOf(id)
/** @type {number} */
let counter
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/image-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @typedef {import('../index.js').Handler} Handler
*/

import {sanitizeUri} from 'micromark-util-sanitize-uri'
import {normalizeUri} from 'micromark-util-sanitize-uri'
import {revert} from '../revert.js'

/**
Expand All @@ -19,7 +19,7 @@ export function imageReference(h, node) {
}

/** @type {Properties} */
const props = {src: sanitizeUri(def.url || ''), alt: node.alt}
const props = {src: normalizeUri(def.url || ''), alt: node.alt}

if (def.title !== null && def.title !== undefined) {
props.title = def.title
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* @typedef {import('../index.js').Handler} Handler
*/

import {sanitizeUri} from 'micromark-util-sanitize-uri'
import {normalizeUri} from 'micromark-util-sanitize-uri'

/**
* @type {Handler}
* @param {Image} node
*/
export function image(h, node) {
/** @type {Properties} */
const props = {src: sanitizeUri(node.url), alt: node.alt}
const props = {src: normalizeUri(node.url), alt: node.alt}

if (node.title !== null && node.title !== undefined) {
props.title = node.title
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/link-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @typedef {import('../index.js').Handler} Handler
*/

import {sanitizeUri} from 'micromark-util-sanitize-uri'
import {normalizeUri} from 'micromark-util-sanitize-uri'
import {revert} from '../revert.js'
import {all} from '../traverse.js'

Expand All @@ -20,7 +20,7 @@ export function linkReference(h, node) {
}

/** @type {Properties} */
const props = {href: sanitizeUri(def.url || '')}
const props = {href: normalizeUri(def.url || '')}

if (def.title !== null && def.title !== undefined) {
props.title = def.title
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @typedef {import('../index.js').Handler} Handler
*/

import {sanitizeUri} from 'micromark-util-sanitize-uri'
import {normalizeUri} from 'micromark-util-sanitize-uri'
import {all} from '../traverse.js'

/**
Expand All @@ -13,7 +13,7 @@ import {all} from '../traverse.js'
*/
export function link(h, node) {
/** @type {Properties} */
const props = {href: sanitizeUri(node.url)}
const props = {href: normalizeUri(node.url)}

if (node.title !== null && node.title !== undefined) {
props.title = node.title
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@types/hast": "^2.0.0",
"@types/mdast": "^3.0.0",
"mdast-util-definitions": "^5.0.0",
"micromark-util-sanitize-uri": "^1.0.0",
"micromark-util-sanitize-uri": "^1.1.0",
"trim-lines": "^3.0.0",
"unist-builder": "^3.0.0",
"unist-util-generated": "^2.0.0",
Expand Down
10 changes: 10 additions & 0 deletions test/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,15 @@ test('Link', (t) => {
'should correctly decode/encode urls'
)

t.deepEqual(
toHast(u('link', {url: 'https://a.com/b.png#c=d&e=f'}, [u('text', 'a')])),
u(
'element',
{tagName: 'a', properties: {href: 'https://a.com/b.png#c=d&e=f'}},
[u('text', 'a')]
),
'should correctly decode/encode dangerous characters'
)

t.end()
})

0 comments on commit dc00a59

Please sign in to comment.