Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for more angle units #324

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export function toPrecision (n, precision) {
}
}

const angleFactor = {
deg: 1,
grad: 0.9,
rad: 180/Math.PI,
turn: 360,
};

/**
* Parse a CSS function, regardless of its name and arguments
* @param String str String to parse
Expand All @@ -57,23 +64,30 @@ export function parseFunction (str) {

const isFunctionRegex = /^([a-z]+)\((.+?)\)$/i;
const isNumberRegex = /^-?[\d.]+$/;
const unitValueRegex = /%|deg|g?rad|turn$/;
let parts = str.match(isFunctionRegex);

if (parts) {
// It is a function, parse args
let args = [];
parts[2].replace(/\/?\s*([-\w.]+(?:%|deg)?)/g, ($0, arg) => {
if (/%$/.test(arg)) {
// Convert percentages to 0-1 numbers
arg = new Number(arg.slice(0, -1) / 100);
arg.type = "<percentage>";
}
else if (/deg$/.test(arg)) {
// Drop deg from degrees and convert to number
// TODO handle other units too
arg = new Number(+arg.slice(0, -3));
arg.type = "<angle>";
arg.unit = "deg";
parts[2].replace(/\/?\s*([-\w.]+(?:%|deg|g?rad|turn)?)/g, ($0, arg) => {
let match = arg.match(unitValueRegex);
if (match) {
let unit = match[0];
// Drop unit from value
let unitlessArg = arg.slice(0, -unit.length);

if (unit === "%") {
// Convert percentages to 0-1 numbers
arg = new Number(unitlessArg / 100);
arg.type = "<percentage>";
}
else {
// Multiply angle by appropriate factor for its unit
arg = new Number(unitlessArg * angleFactor[unit]);
arg.type = "<angle>";
arg.unit = unit;
}
}
else if (isNumberRegex.test(arg)) {
// Convert numerical args to numbers
Expand Down
16 changes: 14 additions & 2 deletions tests/parse.html
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,20 @@ <h1>hsl()</h1>
<td>{"spaceId":"hsl","coords":[900,50,50],"alpha":1}</td>
</tr>
<tr title="hsla(), degrees for hue, spaces and slash">
<td>hsl(0deg 0% 0% / .5)</td>
<td>{"spaceId":"hsl","coords":[0,0,0],"alpha":0.5}</td>
<td>hsl(90deg 0% 0% / .5)</td>
<td>{"spaceId":"hsl","coords":[90,0,0],"alpha":0.5}</td>
</tr>
<tr title="hsla(), rad for hue, spaces and slash">
<td>hsl(1.5707963267948966rad 0% 0% / .5)</td>
<td>{"spaceId":"hsl","coords":[90,0,0],"alpha":0.5}</td>
</tr>
<tr title="hsla(), grad for hue, spaces and slash">
<td>hsl(100grad 0% 0% / .5)</td>
<td>{"spaceId":"hsl","coords":[90,0,0],"alpha":0.5}</td>
</tr>
<tr title="hsla(), turns for hue, spaces and slash">
<td>hsl(0.25turn 0% 0% / .5)</td>
<td>{"spaceId":"hsl","coords":[90,0,0],"alpha":0.5}</td>
</tr>
</table>
</section>
Expand Down