Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#921@trivial: Continues on implementation.
- Loading branch information
1 parent
42a8ce1
commit 1871f3f
Showing
13 changed files
with
250 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
75 changes: 75 additions & 0 deletions
75
packages/happy-dom/src/css/declaration/measurement-converter/CSSMeasurementConverter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import IWindow from '../../../window/IWindow'; | ||
|
||
/** | ||
* CSS Measurement Converter. | ||
*/ | ||
export default class CSSMeasurementConverter { | ||
/** | ||
* Returns measurement in pixels. | ||
* | ||
* @param options Options. | ||
* @param options.ownerWindow Owner window. | ||
* @param options.value Measurement (e.g. "10px", "10rem" or "10em"). | ||
* @param options.rootFontSize Root font size in pixels. | ||
* @param options.parentFontSize Parent font size in pixels. | ||
* @returns Measurement in pixels. | ||
*/ | ||
public static toPixels(options: { | ||
ownerWindow: IWindow; | ||
value: string; | ||
rootFontSize: string | number; | ||
parentFontSize: string | number; | ||
}): number | null { | ||
const value = parseFloat(options.value); | ||
const unit = options.value.replace(value.toString(), ''); | ||
|
||
if (isNaN(value)) { | ||
return null; | ||
} | ||
|
||
switch (unit) { | ||
case 'px': | ||
return value; | ||
case 'rem': | ||
return this.round(value * parseFloat(<string>options.rootFontSize)); | ||
case 'em': | ||
return this.round(value * parseFloat(<string>options.parentFontSize)); | ||
case 'vw': | ||
return this.round((value * options.ownerWindow.innerWidth) / 100); | ||
case 'vh': | ||
return this.round((value * options.ownerWindow.innerHeight) / 100); | ||
case 'vmin': | ||
return this.round( | ||
(value * Math.min(options.ownerWindow.innerWidth, options.ownerWindow.innerHeight)) / 100 | ||
); | ||
case 'vmax': | ||
return ( | ||
(value * Math.max(options.ownerWindow.innerWidth, options.ownerWindow.innerHeight)) / 100 | ||
); | ||
case 'cm': | ||
return this.round(value * 37.7812); | ||
case 'mm': | ||
return this.round(value * 3.7781); | ||
case 'in': | ||
return this.round(value * 96); | ||
case 'pt': | ||
return this.round(value * 1.3281); | ||
case 'pc': | ||
return this.round(value * 16); | ||
case 'Q': | ||
return this.round(value * 0.945); | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Rounds the number with 4 decimals. | ||
* | ||
* @param value Value. | ||
* @returns Rounded value. | ||
*/ | ||
public static round(value: number): number { | ||
return Math.round(value * 10000) / 10000; | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
...ilities/CSSStyleDeclarationValueParser.ts → ...manager/CSSStyleDeclarationValueParser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.