Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Monster
Manage
Activity
Members
Plan
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Javascript
Monster
Commits
a610a872
Verified
Commit
a610a872
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: invalid input throws TypeError
parent
8c6c26a8
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
application/source/dom/dimension.mjs
+9
-1
9 additions, 1 deletion
application/source/dom/dimension.mjs
development/test/cases/dom/dimension.mjs
+10
-0
10 additions, 0 deletions
development/test/cases/dom/dimension.mjs
with
19 additions
and
1 deletion
application/source/dom/dimension.mjs
+
9
−
1
View file @
a610a872
...
@@ -70,11 +70,19 @@ function getDeviceDPI() {
...
@@ -70,11 +70,19 @@ function getDeviceDPI() {
* @copyright schukai GmbH
* @copyright schukai GmbH
* @throws {Error} Unsupported unit
* @throws {Error} Unsupported unit
* @memberOf Monster.DOM
* @memberOf Monster.DOM
* @throws {Error} Invalid value format
*/
*/
function
convertToPixels
(
value
,
parentElement
=
document
.
documentElement
,
fontSizeElement
=
document
.
documentElement
)
{
function
convertToPixels
(
value
,
parentElement
=
document
.
documentElement
,
fontSizeElement
=
document
.
documentElement
)
{
const
regex
=
/^
([\d
.
]
+
)(
.*
)
$/
;
const
regex
=
/^
([\d
.
]
+
)(
.*
)
$/
;
const
[,
num
,
unit
]
=
value
.
match
(
regex
);
const
matchResult
=
value
.
match
(
regex
);
if
(
!
matchResult
)
{
throw
new
Error
(
`Invalid value format:
${
value
}
`
);
}
const
[,
num
,
unit
]
=
matchResult
;
const
number
=
parseFloat
(
num
);
const
number
=
parseFloat
(
num
);
const
dpi
=
getDeviceDPI
();
const
dpi
=
getDeviceDPI
();
...
...
This diff is collapsed.
Click to expand it.
development/test/cases/dom/dimension.mjs
+
10
−
0
View file @
a610a872
...
@@ -61,6 +61,16 @@ describe('dimension', () => {
...
@@ -61,6 +61,16 @@ describe('dimension', () => {
expect
(
result
).
to
.
equal
(
100
);
expect
(
result
).
to
.
equal
(
100
);
});
});
it
(
"
should throw an error when the input value has an invalid format
"
,
()
=>
{
const
invalidValue
=
"
invalid_value
"
;
const
errorFn
=
()
=>
{
convertToPixels
(
invalidValue
);
};
expect
(
errorFn
).
to
.
throw
(
Error
,
`Invalid value format:
${
invalidValue
}
`
);
});
it
(
'
should correctly convert em values
'
,
()
=>
{
it
(
'
should correctly convert em values
'
,
()
=>
{
const
testElement
=
document
.
createElement
(
'
div
'
);
const
testElement
=
document
.
createElement
(
'
div
'
);
testElement
.
style
.
fontSize
=
'
16px
'
;
testElement
.
style
.
fontSize
=
'
16px
'
;
...
...
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