Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
f9f34190
Commit
f9f34190
authored
8 years ago
by
Tom Robinson
Browse files
Options
Downloads
Patches
Plain Diff
Linkify URLs in table view, object detail, etc.
parent
f5ab79fa
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
frontend/src/metabase/lib/formatting.js
+27
-0
27 additions, 0 deletions
frontend/src/metabase/lib/formatting.js
frontend/test/unit/lib/formatting.spec.js
+19
-1
19 additions, 1 deletion
frontend/test/unit/lib/formatting.spec.js
with
46 additions
and
1 deletion
frontend/src/metabase/lib/formatting.js
+
27
−
0
View file @
f9f34190
...
...
@@ -5,6 +5,7 @@ import Humanize from "humanize";
import
React
from
"
react
"
;
import
{
isDate
,
isNumber
,
isCoordinate
}
from
"
metabase/lib/schema_metadata
"
;
import
{
isa
,
TYPE
}
from
"
metabase/lib/types
"
;
import
{
parseTimestamp
}
from
"
metabase/lib/time
"
;
const
PRECISION_NUMBER_FORMATTER
=
d3
.
format
(
"
.2r
"
);
...
...
@@ -98,6 +99,30 @@ function formatTimeWithUnit(value, unit, options = {}) {
}
}
// prevent `javascript:` etc URLs
const
URL_WHITELIST_REGEX
=
/^
(
https
?
|mailto
)
:/
;
export
function
formatUrl
(
value
,
{
jsx
}
=
{})
{
if
(
jsx
&&
URL_WHITELIST_REGEX
.
test
(
value
))
{
return
(
<
a
href
=
{
value
}
className
=
"
link
"
// open in a new tab
target
=
"
_blank
"
// prevent malicious pages from navigating us away
rel
=
"
noopener
"
// disables quickfilter in tables
onClickCapture
=
{(
e
)
=>
e
.
stopPropagation
()}
>
{
value
}
<
/a
>
);
}
else
{
return
value
;
}
}
export
function
formatValue
(
value
,
options
=
{})
{
let
column
=
options
.
column
;
options
=
{
...
...
@@ -107,6 +132,8 @@ export function formatValue(value, options = {}) {
};
if
(
value
==
undefined
)
{
return
null
;
}
else
if
(
column
&&
isa
(
column
.
special_type
,
TYPE
.
URL
))
{
return
formatUrl
(
value
,
options
);
}
else
if
(
column
&&
column
.
unit
!=
null
)
{
return
formatTimeWithUnit
(
value
,
column
.
unit
,
options
);
}
else
if
(
isDate
(
column
)
||
moment
.
isDate
(
value
)
||
moment
.
isMoment
(
value
)
||
moment
(
value
,
[
"
YYYY-MM-DD'T'HH:mm:ss.SSSZ
"
],
true
).
isValid
())
{
...
...
This diff is collapsed.
Click to expand it.
frontend/test/unit/lib/formatting.spec.js
+
19
−
1
View file @
f9f34190
import
{
formatNumber
,
formatValue
}
from
'
metabase/lib/formatting
'
;
import
{
isElementOfType
}
from
"
react-addons-test-utils
"
;
import
{
formatNumber
,
formatValue
,
formatUrl
}
from
'
metabase/lib/formatting
'
;
import
{
TYPE
}
from
"
metabase/lib/types
"
;
describe
(
'
formatting
'
,
()
=>
{
...
...
@@ -54,4 +57,19 @@ describe('formatting', () => {
expect
(
formatValue
(
-
122.4194
,
{
column
:
{
base_type
:
TYPE
.
Number
,
special_type
:
TYPE
.
Longitude
}})).
toEqual
(
"
-122.41940000
"
);
});
});
describe
(
"
formatUrl
"
,
()
=>
{
it
(
"
should return a string when not in jsx mode
"
,
()
=>
{
expect
(
formatUrl
(
"
http://metabase.com/
"
)).
toEqual
(
"
http://metabase.com/
"
)
});
it
(
"
should return a component for http:, https:, and mailto: links in jsx mode
"
,
()
=>
{
expect
(
isElementOfType
(
formatUrl
(
"
http://metabase.com/
"
,
{
jsx
:
true
}),
"
a
"
)).
toEqual
(
true
);
expect
(
isElementOfType
(
formatUrl
(
"
https://metabase.com/
"
,
{
jsx
:
true
}),
"
a
"
)).
toEqual
(
true
);
expect
(
isElementOfType
(
formatUrl
(
"
mailto:tom@metabase.com
"
,
{
jsx
:
true
}),
"
a
"
)).
toEqual
(
true
);
});
it
(
"
should return a string for javascript:, data:, and other links in jsx mode
"
,
()
=>
{
expect
(
formatUrl
(
"
javascript:alert('pwnd')
"
,
{
jsx
:
true
})).
toEqual
(
"
javascript:alert('pwnd')
"
);
expect
(
formatUrl
(
"
data:text/plain;charset=utf-8,hello%20world
"
,
{
jsx
:
true
})).
toEqual
(
"
data:text/plain;charset=utf-8,hello%20world
"
);
});
})
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment