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

CSS mirroring support PostCSS, postcss-pxtorem #2016

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@
"type": "boolean",
"default": false,
"description": "Turn off notification for webhint installation failures."
},
"vscode-edge-devtools.postCSSRootValue": {
"type": "number",
"default": 0,
"description": "Specifies the root value used by PostCSS."
}
}
},
Expand Down Expand Up @@ -716,4 +721,4 @@
"webpack": "5.88.2",
"webpack-cli": "5.1.4"
}
}
}
7 changes: 5 additions & 2 deletions src/devtoolsPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
CDN_FALLBACK_REVISION,
getCSSMirrorContentEnabled,
setCSSMirrorContentEnabled,
getPostCSSRootValue,
toOriginalPx,
} from './utils';
import { ErrorReporter } from './errorReporter';
import { ErrorCodes } from './common/errorCodes';
Expand Down Expand Up @@ -382,9 +384,10 @@ export class DevToolsPanel {
}
if (canMirror)
{
this.mirroredCSS.set(url, newContent);
const postCSSRootValue = getPostCSSRootValue();
this.mirroredCSS.set(url, toOriginalPx(newContent, postCSSRootValue));
void textEditor.edit(editBuilder => {
editBuilder.replace(fullRange, newContent);
editBuilder.replace(fullRange, toOriginalPx(newContent, postCSSRootValue));
});
}
else
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,22 @@ export function reportChangedExtensionSetting(event: vscode.ConfigurationChangeE
}
}

export function getPostCSSRootValue(): number | undefined {
const settings = vscode.workspace.getConfiguration(SETTINGS_STORE_NAME);
const rootValue: number | undefined = settings.get('postCSSRootValue');
return rootValue;
}

export function toOriginalPx(text:string, rootSize:number|undefined):string {
if (rootSize) {
return text.replace(/(\d*\.?\d+)rem/g, (match, remValue:string) => {
const pxValue = Math.round(parseFloat(remValue) * rootSize);
return `${pxValue}px`;
});
}
return text;
}

export function reportUrlType(url: string, telemetryReporter: Readonly<TelemetryReporter>): void {
const localhostPattern = /^https?:\/\/localhost:/;
const ipPattern = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/;
Expand Down
36 changes: 35 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import * as path from "path";

import { createFakeExtensionContext, createFakeGet, createFakeTelemetryReporter, createFakeVSCode, Mocked } from "./helpers/helpers";
import { BrowserFlavor, IRemoteTargetJson, IUserConfig } from "../src/utils";
import { BrowserFlavor, IRemoteTargetJson, IUserConfig, toOriginalPx } from "../src/utils";
import { ConfigurationChangeEvent } from "vscode";

jest.mock("vscode", () => null, { virtual: true });
Expand Down Expand Up @@ -1062,4 +1062,38 @@ describe("utils", () => {
expect(reporter.sendTelemetryEvent).toBeCalledWith('user/settingsChanged', { isHeadless: 'false' });
});
});

describe('PostCSS Utilities Tests', () => {
it("returns the stored settings", async () => {
const expected = {
postCSSRootValue: 3.75,
};

// Override the configuration mock to return our custom test values
const configMock = {
get: (name: string) => (expected as any)[name],
};
const vscodeMock = await jest.requireMock("vscode");
vscodeMock.workspace.getConfiguration.mockImplementationOnce(() => configMock);

// Ensure the new values are returned
const postCSSRootValue = utils.getPostCSSRootValue();
expect(postCSSRootValue).toBe(expected.postCSSRootValue);
});

it('converts rem to original px correctly with rootSize 3.75', async () => {
const text = `.example { margin: 2rem; }`;
const convertedText = toOriginalPx(text, 3.75);
const expectedPx = Math.round(2 * 3.75);
expect(convertedText).toBe(`.example { margin: ${expectedPx}px; }`);
});

it('returns original text if rootSize is undefined', async () => {
const text = `.example { margin: 2rem; }`;
const convertedText = toOriginalPx(text, undefined);
expect(convertedText).toBe(text);
});
});
});