diff --git a/src/core/platform.ts b/src/core/platform.ts index 7df2364cb..43059a2d2 100644 --- a/src/core/platform.ts +++ b/src/core/platform.ts @@ -75,8 +75,8 @@ export const platformApi: Platform = { text = text || ''; font = font || DEFAULT_FONT; // Use font size if there is no other method can be used. - const res = /^([0-9]*?)px$/.exec(font); - const fontSize = +(res && res[1]) || DEFAULT_FONT_SIZE; + const res = /(\d+)px/.exec(font); + const fontSize = res && +res[1] || DEFAULT_FONT_SIZE; let width = 0; if (font.indexOf('mono') >= 0) { // is monospace width = fontSize * text.length; @@ -103,7 +103,7 @@ export const platformApi: Platform = { export function setPlatformAPI(newPlatformApis: Partial) { for (let key in platformApi) { - // Don't assign unkown methods. + // Don't assign unknown methods. if ((newPlatformApis as any)[key]) { (platformApi as any)[key] = (newPlatformApis as any)[key]; } diff --git a/test/ssr-measureText.html b/test/ssr-measureText.html new file mode 100644 index 000000000..e9a7a2830 --- /dev/null +++ b/test/ssr-measureText.html @@ -0,0 +1,98 @@ + + + + + Image + + + + + + + + + + + \ No newline at end of file diff --git a/test/ut/spec/core/platform.test.ts b/test/ut/spec/core/platform.test.ts index 5f307fe88..f96ca5b44 100644 --- a/test/ut/spec/core/platform.test.ts +++ b/test/ut/spec/core/platform.test.ts @@ -32,5 +32,9 @@ describe('platform', function() { createCanvas: oldCreateCanvas, measureText: oldMeasureText }); - }) + }); + + it('measureText should return correct width', function () { + expect(platform.platformApi.measureText('A', 'normal normal 18px sans-serif').width).toBe(12.06); + }); }); \ No newline at end of file