Skip to content

Commit

Permalink
fix URI local linkRefs did not URL encode in tests or code
Browse files Browse the repository at this point in the history
fix URL decode links before passing them to findFileByPath on link click
  • Loading branch information
vsch committed Dec 4, 2015
1 parent c6a4264 commit f1bbc1e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 26 deletions.
4 changes: 2 additions & 2 deletions idea-multimarkdown.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<excludeFolder url="file://$MODULE_DIR$/src/misc" />
<excludeFolder url="file://$MODULE_DIR$/src/test/resources/GitHubIssues/Issue-46" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="module-library" exported="">
<orderEntry type="jdk" jdkName="IntelliJ IDEA Community Edition IC-143.382.35 jdk 1.8.60" jdkType="IDEA JDK" />
<orderEntry type="module-library">
<library name="lib">
<CLASSES>
<root url="file://$MODULE_DIR$/lib" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import com.intellij.util.Processor
import com.intellij.util.indexing.FileBasedIndex
import com.intellij.util.indexing.FileBasedIndexImpl
import com.vladsch.idea.multimarkdown.MultiMarkdownPlugin
import org.apache.log4j.Logger
import java.util.*
import kotlin.text.RegexOption


class GitHubLinkResolver(projectResolver: LinkResolver.ProjectResolver, containingFile: FileRef, branchOrTag: String? = null) : LinkResolver(projectResolver, containingFile, branchOrTag) {
private val logger = Logger.getLogger(GitHubLinkResolver::class.java)

companion object {
@JvmStatic @JvmField val GITHUB_BLOB_NAME = "blob"
Expand Down Expand Up @@ -211,7 +213,7 @@ class GitHubLinkResolver(projectResolver: LinkResolver.ProjectResolver, containi
} else {
// URL only, we need to convert to URI file:// type
if (wantLocal(options) && wantOnlyURI(options)) return LinkRef(containingFile, "file://" + LinkRef.urlEncode(targetRef.filePath), null, targetRef as FileRef?)
// if (wantLocal(options) && wantOnlyURI(options)) return LinkRef(containingFile, "file://" + targetRef.filePath, null, targetRef as FileRef?)
// if (wantLocal(options) && wantOnlyURI(options)) return LinkRef(containingFile, "file://" + targetRef.filePath, null, targetRef as FileRef?)
}
} else {
// local, remote or URL
Expand Down Expand Up @@ -341,7 +343,6 @@ class GitHubLinkResolver(projectResolver: LinkResolver.ProjectResolver, containi
}
}

val exactFileRefMatches = HashSet<FileRef>()
val rawWikiFileRefMatches = HashSet<FileRef>()

// now we need to weed out the matches that will not work, unless this is a loose match
Expand All @@ -361,7 +362,6 @@ class GitHubLinkResolver(projectResolver: LinkResolver.ProjectResolver, containi
if (fileOrAnchorMatch != null) {
for (fileRef in matches) {
if (fileRef is FileRef && fileRef.filePath.matches(fileOrAnchorMatch)) {
exactFileRefMatches.add(fileRef)
rawWikiFileRefMatches.add(fileRef)
if (fileRef.isUnderWikiDir) fileRef.isRawFile = true
}
Expand All @@ -378,11 +378,8 @@ class GitHubLinkResolver(projectResolver: LinkResolver.ProjectResolver, containi
if (fileMatch != null) {
for (fileRef in matches) {
if (fileRef is FileRef) {
val exactMatch = fileRef.filePath.matches(fileMatch)
if (exactMatch) exactFileRefMatches.add(fileRef)

// it will be raw access if it is under the wiki directory and has a 'real' extension
if (!fileRef.isWikiPageExt || exactMatch) {
if (!fileRef.isWikiPageExt || fileRef.filePath.matches(fileMatch)) {
rawWikiFileRefMatches.add(fileRef)
if (fileRef.isUnderWikiDir) fileRef.isRawFile = true
}
Expand All @@ -395,7 +392,6 @@ class GitHubLinkResolver(projectResolver: LinkResolver.ProjectResolver, containi
// these are already set for raw if taken from the raw/ access URL and all are exact matches
for (fileRef in matches) {
if (fileRef is FileRef) {
exactFileRefMatches.add(fileRef)
rawWikiFileRefMatches.add(fileRef)
if (fileRef.isUnderWikiDir) fileRef.isRawFile = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,11 @@ object MarkdownTestData : LinkResolver.ProjectResolver {
}

fun List<PathInfo>.asFilePaths(): List<String> {
val result = this.map { it.filePath }
return result
return this.map { it.filePath }
}

fun List<String>.asURI(): List<String> {
val result = this.map { "file://" + it }
return result
return this.map { "file://" + it.replace(" ", "%20").replace("#", "%23") }
}

fun List<String>.with(list: List<String>): List<String> {
Expand Down Expand Up @@ -222,10 +220,6 @@ fun List<String>.asRemoteUriType(branchOrTag: String? = null, gitHubLink: String
return result
}

fun List<String>.asLocalURI(branchOrTag: String? = null): List<String> {
return this.map { "file://" + it }
}

val EMPTY_LIST = arrayListOf<String>()

val gitHubLinks = arrayListOf(
Expand Down Expand Up @@ -518,11 +512,9 @@ val availableLists = mapOf<List<String>, String>(
val availablePermutations = mapOf<(List<String>) -> List<String>, String>(
Pair({ it -> it.with(gitHubLinks) }, ".with(gitHubLinks)"),
Pair({ it -> it.asURI() }, ".asURI()"),
Pair({ it -> it.asLocalURI() }, ".asLocalURI()"),
Pair({ it -> it.asRemoteURI() }, ".asRemoteURI()"),
Pair({ it -> it.asRemoteImageURI() }, ".asRemoteImageURI()"),
Pair({ it -> it.asURI().with(gitHubLinks) }, ".asURI().with(gitHubLinks)"),
Pair({ it -> it.asLocalURI().with(gitHubLinks) }, ".asLocalURI().with(gitHubLinks)"),
Pair({ it -> it.asRemoteURI().with(gitHubLinks) }, ".asRemoteURI().with(gitHubLinks)"),
Pair({ it -> it.asRemoteImageURI().with(gitHubLinks) }, ".asRemoteImageURI().with(gitHubLinks)")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import kotlin.test.assertEquals
@RunWith(value = Parameterized::class)

//arrayOf<Any?>("fullPath", "linkType", "linkRef", "linkAnchor", "options", "optionsText", "multiResolve") * /
class TestGitHubVcsRoot_Basic constructor(val rowId: Int, val containingFile: String
, val linkAddress: String
, val normalizedAddress: String
class TestGitHubLinkResolver_normalizedLinkRef constructor(val rowId: Int, val containingFile: String
, val linkAddress: String
, val normalizedAddress: String
) {
val projectResolver: LinkResolver.ProjectResolver = MarkdownTestData
val containingFileRef = FileRef(containingFile)
Expand Down Expand Up @@ -64,7 +64,7 @@ class TestGitHubVcsRoot_Basic constructor(val rowId: Int, val containingFile: St
fun data(): Collection<Array<Any?>> {
val data = completionData
val amendedData = ArrayList<Array<Any?>>()
val cleanData = true
val cleanData = false

var i = 0
for (row in data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class TestLinkResolver_Basic_Readme constructor(val rowId: Int, val fullPath: St
val targetRef = resolver.resolve(linkRef, LinkResolver.ANY, fileList)
val uriRef = resolver.resolve(linkRef, LinkResolver.ONLY_URI, fileList)
val href = if (targetRef != null && targetRef is FileRef && this.uriText != null) {
"file://" + targetRef.filePath
"file://" + targetRef.filePath.replace(" ", "%20").replace("#", "%23")
} else if (targetRef is LinkRef) targetRef.filePathWithAnchor
else null
assertEqualsMessage("LinkResolver.ONLY_URI link address does not match ${resolver.getMatcher(linkRef, false).linkAllMatch}", href, (uriRef as? LinkRef)?.filePathWithAnchor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
TestLinkRef.class,
TestLinkRef_from.class,
TestLinkMatcher_MultiSub.class,
TestGitHubVcsRoot_Basic.class,
TestGitHubLinkResolver_normalizedLinkRef.class,
TestLinkResolver_Basic_wiki_Home.class,
TestLinkResolver_Basic_wiki_normal_file.class,
TestLinkResolver_Basic_Readme.class,
Expand Down

0 comments on commit f1bbc1e

Please sign in to comment.