Skip to content

Commit

Permalink
fix: decode urlencoded spaces in asset path before opening (#2279)
Browse files Browse the repository at this point in the history
* fix: decode urlencoded spaces in asset path before opening

* chore: add test for urlencoded file path and skip faulty test

* chore: rename test file
  • Loading branch information
hikchoi committed Jan 29, 2022
1 parent 5f5e3d9 commit c60743d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class PreviewLinkHandler implements IPreviewLinkHandler {
}
}

private vaultlessAssetPath({
public vaultlessAssetPath({
data,
wsRoot,
}: {
Expand Down Expand Up @@ -303,7 +303,7 @@ export class PreviewLinkHandler implements IPreviewLinkHandler {

const assetPathFull = path.join(
vault2Path({ vault: note.vault, wsRoot }),
assetPathRelative
decodeURIComponent(assetPathRelative)
);

return assetPathFull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestNoteFactory } from "@dendronhq/common-test-utils";
import { AssertUtils, TestNoteFactory } from "@dendronhq/common-test-utils";
import { afterEach, beforeEach, describe, it } from "mocha";
import path from "path";
import sinon from "sinon";
Expand All @@ -13,13 +13,77 @@ import { MockDendronExtension } from "../MockDendronExtension";
import { expect, runSingleVaultTest } from "../testUtilsv2";
import { setupBeforeAfter } from "../testUtilsV3";

suite("ShowPreview utility methods", () => {
suite("PreviewLinkHandler", () => {
const ctx: vscode.ExtensionContext = setupBeforeAfter(this, {
noSetTimeout: true,
beforeHook: () => {},
});

describe(`handleLink`, () => {
describe("vaultlessAssetPath", () => {
describe("GIVEN valid href", () => {
beforeEach(async () => {
const factory = TestNoteFactory.defaultUnitTestFactory();

await factory.createForFName("foo");
});

afterEach(() => {
sinon.restore();
});
it("THEN correct asset path is corectly decoded", (done) => {
runSingleVaultTest({
ctx,
onInit: async ({ wsRoot, vault }) => {
const handler = new PreviewLinkHandler(
new MockDendronExtension({
engine: ExtensionProvider.getEngine(),
wsRoot,
vaults: [vault],
})
);

// without urlencoded parts
const withoutURLEncode = handler.vaultlessAssetPath({
data: {
href: "vscode-webview://e380a62c-2dea-46a8-ae1e-a34868c9719e/assets/dummy-pdf.pdf",
id: "foo",
},
wsRoot,
});
expect(withoutURLEncode !== undefined).toBeTruthy();
expect(
await AssertUtils.assertInString({
body: withoutURLEncode as string,
match: ["assets/dummy-pdf.pdf"],
})
);

// with urlencoded parts (space as %20)
const withURLEncode = handler.vaultlessAssetPath({
data: {
href: "vscode-webview://e380a62c-2dea-46a8-ae1e-a34868c9719e/assets/file%20with%20space.pdf",
id: "foo",
},
wsRoot,
});
expect(withURLEncode !== undefined).toBeTruthy();
expect(
await AssertUtils.assertInString({
body: withURLEncode as string,
match: ["assets/file with space.pdf"],
})
);

done();
},
});
});
});
});

// this test block passes because it doesn't wait for the actual test to run.
// the actual test does not pass.
describe.skip(`handleLink`, () => {
describe(`LinkType.ASSET`, () => {
describe(`WHEN valid href`, () => {
let assetOpenerStub: any;
Expand Down
Binary file not shown.
16 changes: 14 additions & 2 deletions test-workspace/vault/dendron.ref.assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
id: Hf27I1UR3HvKyd6HRh8C0
title: Assets
desc: ''
updated: 1637726939252
updated: 1643206013490
created: 1637568178090
---

## PDF pasted with `Dendron: Paste File` command
Should open the following assets with default app when clicking the link within preview:
* [dummy-pdf.pdf](assets/dummy-pdf.pdf)
* [dummy-pdf.pdf](./assets/dummy-pdf.pdf)
* [dummy-pdf.pdf](/assets/dummy-pdf.pdf)
* [dummy-pdf.pdf](/assets/dummy-pdf.pdf)

* [with space encoded](assets/file%20with%20space.pdf)
* [with space encoded](./assets/file%20with%20space.pdf)
* [with space encoded](/assets/file%20with%20space.pdf)

* [with space wrapped](<assets/file with space.pdf>)
* [with space wrapped](<./assets/file with space.pdf>)
* [with space wrapped](</assets/file with space.pdf>)

- These shouldn't be render correctly. (thus unable to open)
* [with space raw](assets/file with space.pdf)
* [with space raw](./assets/file with space.pdf)
* [with space raw](/assets/file with space.pdf)

## Regular navigation should still work
* Quick check navigate to this: [[dendron://assets/note-in-asset-vault]]
Expand Down

0 comments on commit c60743d

Please sign in to comment.