From 1a5de41a0ff2f1c5b6488ee9f7f4a6ef5a767dd3 Mon Sep 17 00:00:00 2001 From: Billy Richardson Date: Mon, 2 May 2022 08:40:13 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Remove=20use=20of=20HTML=20entit?= =?UTF-8?q?y=20degree=20symbol=20in=20temperature=20title=20attribute=20(#?= =?UTF-8?q?4490)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use unicode degree symbol in title attribute --- AUTHORS.md | 1 + src/octoprint/static/js/app/helpers.js | 18 ++- .../templates/tabs/temperature.jinja2 | 2 +- tests/static/js/test-helpers.html | 13 +++ tests/static/js/test-helpers.js | 104 ++++++++++++++++++ 5 files changed, 134 insertions(+), 4 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 487a3838e..f09ba9bb7 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -165,6 +165,7 @@ date of first contribution): * [Johan Verrept](https://github.com/JoveToo) * [Nicu Surdu](https://github.com/surdu) * [Zack Lewis](https://github.com/lima3w) + * [Billy Richardson](https://github.com/richardsondev) OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by [Daid Braam](https://github.com/daid). Parts of its communication layer and diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index fcea464ef..fd0faecba 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -652,13 +652,25 @@ function cleanTemperature(temp, offThreshold) { return temp; } -function formatTemperature(temp, showF, offThreshold) { +function formatTemperature(temp, showF, offThreshold, returnUnicode) { if (temp === undefined || !_.isNumber(temp)) return "-"; if (offThreshold !== undefined && temp < offThreshold) return gettext("off"); + + var degreeSymbol = "°"; + if (returnUnicode !== undefined && returnUnicode) { + degreeSymbol = "\u00B0"; + } + if (showF) { - return _.sprintf("%.1f°C (%.1f°F)", temp, (temp * 9) / 5 + 32); + return _.sprintf( + "%.1f%sC (%.1f%sF)", + temp, + degreeSymbol, + (temp * 9) / 5 + 32, + degreeSymbol + ); } else { - return _.sprintf("%.1f°C", temp); + return _.sprintf("%.1f%sC", temp, degreeSymbol); } } diff --git a/src/octoprint/templates/tabs/temperature.jinja2 b/src/octoprint/templates/tabs/temperature.jinja2 index 57c21fb87..438ae097a 100644 --- a/src/octoprint/templates/tabs/temperature.jinja2 +++ b/src/octoprint/templates/tabs/temperature.jinja2 @@ -49,7 +49,7 @@ + + + + diff --git a/tests/static/js/test-helpers.js b/tests/static/js/test-helpers.js index f2cdc9ac9..89b947a06 100644 --- a/tests/static/js/test-helpers.js +++ b/tests/static/js/test-helpers.js @@ -85,3 +85,107 @@ QUnit.cases( "As expected: " + String(params.expected) ); }); + +QUnit.module("formatTemperature"); +QUnit.cases( + (function () { + var cases = []; + + var params = [ + { + input: undefined, + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "-" + }, + { + input: "", + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "-" + }, + { + input: 1.0, + showF: undefined, + useUnicode: undefined, + offThreshold: 1.1, + expected: "off" + }, + { + input: 1.0, + showF: undefined, + useUnicode: undefined, + offThreshold: 1.0, + expected: "1.0°C" + }, + { + input: 1.0, + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "1.0°C" + }, + { + input: 1.0, + showF: true, + useUnicode: undefined, + offThreshold: undefined, + expected: "1.0°C (33.8°F)" + }, + { + input: 1.0, + showF: true, + useUnicode: true, + offThreshold: undefined, + expected: "1.0\u00B0C (33.8\u00B0F)" + }, + { + input: 1.0, + showF: undefined, + useUnicode: false, + offThreshold: undefined, + expected: "1.0°C" + }, + { + input: 1.0, + showF: undefined, + useUnicode: true, + offThreshold: undefined, + expected: "1.0\u00B0C" + }, + { + input: 1.1, + showF: undefined, + useUnicode: undefined, + offThreshold: undefined, + expected: "1.1°C" + } + ]; + + var param, i; + for (i = 0; i < params.length; i++) { + param = params[i]; + param["title"] = + String(param.input) + + String(param.showF) + + String(param.offThreshold) + + String(param.useUnicode); + cases.push(param); + } + + return cases; + })() +).test("formatTemperature", function (params, assert) { + assert.equal( + params.expected, + formatTemperature( + params.input, + params.showF, + params.offThreshold, + params.useUnicode + ), + "As expected: " + String(params.expected) + ); +});