Skip to content

Commit

Permalink
Url decode link column display values if regex is used (#8258)
Browse files Browse the repository at this point in the history
## Describe your changes

This PR decodes the extracted display values. This is needed since its
extracted from an URL, which often requires URL encoding certain chars.

This also includes a small slightly unrelated one-liner addition to
resolve this issue with the link column:
#7064 (Closes #7064)

## Testing Plan

- Added unit test

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
  • Loading branch information
LukasMasuch committed Mar 9, 2024
1 parent 86fd3d7 commit 9a121f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ describe("LinkColumn", () => {
expect(cell.displayData).toBe("roadmap")
})

it("sets displayed value as the applied regex to the href when displayText is a regex with URL encoding", () => {
const mockColumn = LinkColumn({
...MOCK_LINK_COLUMN_PROPS,
columnTypeOptions: {
display_text: "https://streamlit\\.app\\?app=(.*)",
},
})

const cell = mockColumn.getCell(
"https://streamlit.app?app=foo%20app%20%25",
true
) as UriCell

expect(cell.displayData).toBe("foo app %")
})

it("sets displayed value as the href, when displayText is a regex but there is no match", () => {
const mockColumn = LinkColumn({
...MOCK_LINK_COLUMN_PROPS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ function LinkColumn(props: BaseColumnProps): BaseColumn {
displayData: displayText,
isMissingValue: isNullOrUndefined(href),
onClickUri: a => {
window.open(href, "_blank", "noopener,noreferrer")
window.open(
href.startsWith("www.") ? `https://${href}` : href,
"_blank",
"noopener,noreferrer"
)
a.preventDefault()
},
copyData: href,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ export function getLinkDisplayValueFromRegex(
const patternMatch = href.match(displayTextRegex)
if (patternMatch && patternMatch[1] !== undefined) {
// return the first matching group
return patternMatch[1]
// Since this might be a URI encoded value, we decode it.
return decodeURI(patternMatch[1])
}

// if the regex doesn't find a match with the url, just use the url as display value
Expand Down

0 comments on commit 9a121f8

Please sign in to comment.