Skip to content

Commit 9e9d874

Browse files
bjaspanBarry Jaspanpre-commit-ci-lite[bot]
authored
Fix FontMeasurementsImpl to work in a web environment (#2260)
FontMeasurementsImpl currently uses an inline script to compute font size ratios which is not compatible with a web environment due to Content Security Policy (CSP) rules. This PR switches to a separate script file and adds a CSP rule for it according to the instructions in https://code.visualstudio.com/api/extension-guides/webview#content-security-policy. To test, change the `Editor: Font Family` setting (you can just append `, x` to the end or make any other change). Success is seeing a tab flash and disappear; failure is the tab showing "Loading Cursorless..." remaining open. --------- Co-authored-by: Barry Jaspan <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent f92f889 commit 9e9d874

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const letter = document.querySelector("#letter");
2+
const container = document.querySelector("#container");
3+
const baselineHeight =
4+
letter.offsetTop +
5+
letter.offsetHeight -
6+
container.offsetHeight -
7+
container.offsetTop;
8+
const vscode = acquireVsCodeApi();
9+
vscode.postMessage({
10+
widthRatio: letter.offsetWidth / 1000,
11+
heightRatio: (letter.offsetHeight - baselineHeight) / 1000,
12+
});

packages/cursorless-vscode/src/ide/vscode/hats/FontMeasurementsImpl.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class FontMeasurementsImpl implements FontMeasurements {
3434
}>("fontRatios");
3535

3636
if (fontRatiosCache == null || fontRatiosCache.fontFamily !== fontFamily) {
37-
const fontRatios = await getFontRatios();
37+
const fontRatios = await getFontRatios(this.extensionContext);
3838
this.extensionContext.globalState.update("fontRatios", {
3939
...fontRatios,
4040
fontFamily,
@@ -57,7 +57,7 @@ export class FontMeasurementsImpl implements FontMeasurements {
5757
*
5858
* @returns The width and height ratios of the font
5959
*/
60-
function getFontRatios() {
60+
function getFontRatios(extensionContext: vscode.ExtensionContext) {
6161
const panel = vscode.window.createWebviewPanel(
6262
"cursorless.loading",
6363
"Cursorless",
@@ -70,7 +70,14 @@ function getFontRatios() {
7070
},
7171
);
7272

73-
panel.webview.html = getWebviewContent();
73+
const font_measurement_js = panel.webview.asWebviewUri(
74+
vscode.Uri.joinPath(
75+
extensionContext.extensionUri,
76+
"resources",
77+
"font_measurements.js",
78+
),
79+
);
80+
panel.webview.html = getWebviewContent(panel.webview, font_measurement_js);
7481

7582
interface FontRatios {
7683
/**
@@ -103,25 +110,23 @@ function getFontFamily() {
103110
return config.get<string>("fontFamily")!;
104111
}
105112

106-
function getWebviewContent() {
113+
function getWebviewContent(
114+
webview: vscode.Webview,
115+
font_measurement_js: vscode.Uri,
116+
) {
107117
// baseline adjustment based on https://stackoverflow.com/a/27295528
108118
return `<!DOCTYPE html>
109119
<html lang="en">
120+
<head>
121+
<meta http-equiv="Content-Security-Policy" content="script-src ${webview.cspSource};" />
122+
</head>
123+
110124
<body>
111125
<h1>Loading Cursorless...</h1>
112126
<div id="container">
113127
<span id="letter" style="line-height: 0; visibility:hidden; font-size: 1000px; font-family: var(--vscode-editor-font-family); font-weight: var(--vscode-editor-font-weight);">A</span>
114128
</div>
115-
<script>
116-
const letter = document.querySelector('#letter');
117-
const container = document.querySelector('#container');
118-
const baselineHeight = letter.offsetTop + letter.offsetHeight - container.offsetHeight - container.offsetTop;
119-
const vscode = acquireVsCodeApi();
120-
vscode.postMessage({
121-
widthRatio: letter.offsetWidth / 1000,
122-
heightRatio: (letter.offsetHeight - baselineHeight) / 1000
123-
});
124-
</script>
129+
<script src="${font_measurement_js}"></script>
125130
</body>
126131
</html>`;
127132
}

packages/cursorless-vscode/src/scripts/populateDist/assets.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export const assets: Asset[] = [
2626
{ source: "../../images/hats", destination: "images/hats" },
2727
{ source: "./images/logo.png", destination: "images/logo.png" },
2828
{ source: "../../images/logo.svg", destination: "images/logo.svg" },
29+
{
30+
source: "resources/font_measurements.js",
31+
destination: "resources/font_measurements.js",
32+
},
2933
{ source: "../../schemas", destination: "schemas" },
3034
{
3135
source: "../../third-party-licenses.csv",

0 commit comments

Comments
 (0)