mirror of
https://github.com/dvmarinoff/Auuki.git
synced 2026-11-28 17:28:32 +01:00
add escapeHtml for zwo workouts
This commit is contained in:
@@ -200,6 +200,16 @@ function capitalize(str) {
|
||||
return str.trim().replace(/^\w/, (c) => c.toUpperCase());
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
const text = exists(value) ? String(value) : '';
|
||||
return text
|
||||
.replaceAll('&', '&')
|
||||
.replaceAll('<', '<')
|
||||
.replaceAll('>', '>')
|
||||
.replaceAll('"', '"')
|
||||
.replaceAll("'", ''');
|
||||
}
|
||||
|
||||
function clamp(lower, upper, value) {
|
||||
if(value >= upper) {
|
||||
return upper;
|
||||
@@ -740,6 +750,7 @@ export {
|
||||
sum,
|
||||
rand,
|
||||
capitalize,
|
||||
escapeHtml,
|
||||
clamp,
|
||||
|
||||
// functions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { exists, xf, print, } from '../functions.js';
|
||||
import { exists, equals, xf, print, } from '../functions.js';
|
||||
import { OAuthService, DialogMsg, stateParam, } from './enums.js';
|
||||
import { uuid } from '../storage/uuid.js';
|
||||
import config from './config.js';
|
||||
@@ -81,19 +81,25 @@ function Router(args = {}) {
|
||||
// switch
|
||||
if(error) {
|
||||
console.error(`:api :param :error `, error);
|
||||
clearParams();
|
||||
return true;
|
||||
}
|
||||
if(!error && (code || scope || state)) {
|
||||
const { service, id } = stateParam.decode(state);
|
||||
console.log(`:api :onQueryParams service: ${service} id: ${id}`);
|
||||
if(code || scope || state) {
|
||||
if(!stateParam.consume(state)) {
|
||||
console.error(`:api :param :state`);
|
||||
clearParams();
|
||||
return true;
|
||||
}
|
||||
const { service } = stateParam.decode(state);
|
||||
console.log(`:api :onQueryParams service: ${service}`);
|
||||
|
||||
if(service === OAuthService.strava) {
|
||||
if(equals(service, OAuthService.strava)) {
|
||||
await strava.paramsHandler({state, code, scope});
|
||||
}
|
||||
if(service === OAuthService.intervals) {
|
||||
if(equals(service, OAuthService.intervals)) {
|
||||
await intervals.paramsHandler({state, code, scope});
|
||||
}
|
||||
if(service === OAuthService.trainingPeaks) {
|
||||
if(equals(service, OAuthService.trainingPeaks)) {
|
||||
await trainingPeaks.paramsHandler({state, code, scope});
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { equals, isString } from '../functions.js';
|
||||
import { uuid } from '../storage/uuid.js';
|
||||
|
||||
const DialogMsg = {
|
||||
@@ -11,11 +12,16 @@ const OAuthService = {
|
||||
};
|
||||
|
||||
function StateParam() {
|
||||
const storageKey = 'oauth-state';
|
||||
|
||||
function encode(name) {
|
||||
const param = name+'_'+uuid();
|
||||
return param;
|
||||
}
|
||||
function decode(param) {
|
||||
if(!isString(param)) {
|
||||
return { service: '', id: '' };
|
||||
}
|
||||
const parts = param.split('_');
|
||||
|
||||
return {
|
||||
@@ -24,10 +30,25 @@ function StateParam() {
|
||||
};
|
||||
}
|
||||
|
||||
function store() {
|
||||
function store(state) {
|
||||
if(!isString(state)) {
|
||||
return;
|
||||
}
|
||||
sessionStorage.setItem(storageKey, state);
|
||||
}
|
||||
|
||||
function restore() {
|
||||
const saved = sessionStorage.getItem(storageKey);
|
||||
return isString(saved) ? saved : '';
|
||||
}
|
||||
|
||||
function consume(state) {
|
||||
const saved = restore();
|
||||
const matches = !equals(saved, '') && equals(saved, state);
|
||||
if(matches) {
|
||||
sessionStorage.removeItem(storageKey);
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
return Object.freeze({
|
||||
@@ -35,6 +56,7 @@ function StateParam() {
|
||||
decode,
|
||||
store,
|
||||
restore,
|
||||
consume,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { xf, formatDate, } from '../functions.js';
|
||||
import { xf, formatDate, escapeHtml, } from '../functions.js';
|
||||
import { models } from '../models/models.js';
|
||||
import { formatTime, } from '../utils.js';
|
||||
|
||||
@@ -58,18 +58,22 @@ class ActivityList extends HTMLElement {
|
||||
}
|
||||
template(i, data) {
|
||||
const status = this.uploadStatus(data);
|
||||
const id = escapeHtml(this.id(data));
|
||||
const name = escapeHtml(this.name(data));
|
||||
const intervalsStatus = escapeHtml(status.intervals ?? 'none');
|
||||
const stravaStatus = escapeHtml(status.strava ?? 'none');
|
||||
|
||||
return `
|
||||
<activity-item id="i${i}--activity--item" class="some" data-id="${this.id(data)}">
|
||||
<activity-item id="i${i}--activity--item" class="some" data-id="${id}">
|
||||
<div class="activity--cont list--row--outer">
|
||||
<div class="list--row--inner activity--info">
|
||||
<div class="activity--info--short">
|
||||
<view-action
|
||||
class="info"
|
||||
action=":toggleExpand"
|
||||
topic=":activity:${this.id(data)}">
|
||||
topic=":activity:${id}">
|
||||
<div id="i${i}--activity--date" class="activity--name">
|
||||
${this.name(data)}
|
||||
${name}
|
||||
</div>
|
||||
<div class="activity--date">
|
||||
${this.date(data)}
|
||||
@@ -81,7 +85,7 @@ class ActivityList extends HTMLElement {
|
||||
<view-action
|
||||
class=""
|
||||
action=":options"
|
||||
topic=":activity:${this.id(data)}">
|
||||
topic=":activity:${id}">
|
||||
<svg class="control--btn--icon" width="24" height="24">
|
||||
<use href="#icon--options" />
|
||||
</svg>
|
||||
@@ -95,26 +99,26 @@ class ActivityList extends HTMLElement {
|
||||
<view-action
|
||||
class="activity--action action--intervals"
|
||||
action=":intervals:upload"
|
||||
topic=":activity:${this.id(data)}">
|
||||
topic=":activity:${id}">
|
||||
<svg class="activity--icon intervals--icon" width="24" height="24">
|
||||
<use href="#icon--intervals" />
|
||||
</svg>
|
||||
<div class="connection-icon-switch--indicator ${status.intervals ?? 'none'} intervals"></div>
|
||||
<div class="connection-icon-switch--indicator ${intervalsStatus} intervals"></div>
|
||||
</view-action>
|
||||
|
||||
<view-action
|
||||
class="activity--action"
|
||||
action=":strava:upload"
|
||||
topic=":activity:${this.id(data)}">
|
||||
topic=":activity:${id}">
|
||||
<svg class="activity--icon strava--icon" width="24" height="24">
|
||||
<use href="#icon--strava" />
|
||||
</svg>
|
||||
<div class="connection-icon-switch--indicator ${status.strava ?? 'none'} strava"></div>
|
||||
<div class="connection-icon-switch--indicator ${stravaStatus} strava"></div>
|
||||
</view-action>
|
||||
<view-action
|
||||
class="activity--action action--download"
|
||||
action=":download"
|
||||
topic=":activity:${this.id(data)}">
|
||||
topic=":activity:${id}">
|
||||
<svg class="activity--icon">
|
||||
<use href="#icon--save-btn" />
|
||||
</svg>
|
||||
@@ -122,7 +126,7 @@ class ActivityList extends HTMLElement {
|
||||
<view-action
|
||||
class="activity--action action--image"
|
||||
action=":image"
|
||||
topic=":activity:${this.id(data)}">
|
||||
topic=":activity:${id}">
|
||||
<svg class="activity--icon">
|
||||
<use href="#icon--image" />
|
||||
</svg>
|
||||
@@ -134,7 +138,7 @@ class ActivityList extends HTMLElement {
|
||||
<view-action
|
||||
class="activity--options"
|
||||
action=":remove"
|
||||
topic=":activity:${this.id(data)}">
|
||||
topic=":activity:${id}">
|
||||
<span class="activity--remove">Delete</span>
|
||||
</view-action>
|
||||
</activity-item>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { xf, empty, } from '../functions.js';
|
||||
import { xf, empty, escapeHtml, } from '../functions.js';
|
||||
import { isoDate, isoDateToWeekDay } from '../utils.js';
|
||||
import { models } from '../models/models.js';
|
||||
import { WorkoutGraphViewModel, } from './workout-graph-svg.js';
|
||||
@@ -98,14 +98,14 @@ class PlannedList extends HTMLElement {
|
||||
toListItem(workout, ftp, size) {
|
||||
const self = this;
|
||||
|
||||
const id = workout.id;
|
||||
const id = escapeHtml(workout.id);
|
||||
const weekDay = isoDateToWeekDay(workout.meta.startDateLocal ?? isoDate());
|
||||
const weekDayTemplate = `
|
||||
<div class="weekday">${weekDay.weekDay}</div>
|
||||
<div class="date">${`${weekDay.day}`.padStart(2, "0")}</div>`;
|
||||
const name = workout.meta.name;
|
||||
const category = workout.meta.category;
|
||||
const description = workout.meta.description;
|
||||
const name = escapeHtml(workout.meta.name);
|
||||
const category = escapeHtml(workout.meta.category);
|
||||
const description = escapeHtml(workout.meta.description);
|
||||
|
||||
let duration = '';
|
||||
if(workout.meta.duration) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { xf, exists, first, last, } from '../functions.js';
|
||||
import { xf, exists, first, last, escapeHtml, } from '../functions.js';
|
||||
import { formatTime } from '../utils.js';
|
||||
import { listItemTemplate, } from './planned-list.js';
|
||||
import { models } from '../models/models.js';
|
||||
@@ -155,10 +155,10 @@ class WorkoutGraphViewModel {
|
||||
|
||||
acc += `<polygon points="${points}"
|
||||
fill="${color}"
|
||||
power="${info.power}"
|
||||
cadence="${info.cadence}"
|
||||
slope="${info.slope}"
|
||||
duration="${info.duration}" />`;
|
||||
power="${escapeHtml(info.power)}"
|
||||
cadence="${escapeHtml(info.cadence)}"
|
||||
slope="${escapeHtml(info.slope)}"
|
||||
duration="${escapeHtml(info.duration)}" />`;
|
||||
}
|
||||
|
||||
return `
|
||||
@@ -221,7 +221,7 @@ class WorkoutGraph extends HTMLElement {
|
||||
const bottom = rect.height;
|
||||
|
||||
this.$info.style.display = 'block';
|
||||
this.$info.innerHTML = `<div>${power}</div><div>${cadence}</div><div>${slope}</div><div class="graph--info--time">${duration}</div>`;
|
||||
this.$info.innerHTML = `<div>${escapeHtml(power)}</div><div>${escapeHtml(cadence)}</div><div>${escapeHtml(slope)}</div><div class="graph--info--time">${escapeHtml(duration)}</div>`;
|
||||
|
||||
const width = this.$info.getBoundingClientRect().width;
|
||||
const height = this.$info.getBoundingClientRect().height;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { xf, exists, existance, equals, clamp, debounce, toFixed } from '../functions.js';
|
||||
import { xf, exists, existance, equals, clamp, debounce, toFixed, escapeHtml } from '../functions.js';
|
||||
import { formatTime, translate } from '../utils.js';
|
||||
import { models } from '../models/models.js';
|
||||
import { g } from './graph.js';
|
||||
@@ -44,10 +44,10 @@ function Interval(acc, interval, width, ftp, powerMax, viewPort) {
|
||||
const zone = (models.ftp.powerToZone(power, ftp)).name;
|
||||
const infoTime = formatTime({value: duration, format: 'mm:ss'});
|
||||
|
||||
const powerAttr = exists(power) ? `power="${power}"` : '';
|
||||
const cadenceAttr = exists(cadence) ? `cadence="${cadence}"` : '';
|
||||
const slopeAttr = exists(slope) ? `slope="${slope}"` : '';
|
||||
const durationAttr = exists(duration) ? `duration="${infoTime}"` : '';
|
||||
const powerAttr = exists(power) ? `power="${escapeHtml(power)}"` : '';
|
||||
const cadenceAttr = exists(cadence) ? `cadence="${escapeHtml(cadence)}"` : '';
|
||||
const slopeAttr = exists(slope) ? `slope="${escapeHtml(slope)}"` : '';
|
||||
const durationAttr = exists(duration) ? `duration="${escapeHtml(infoTime)}"` : '';
|
||||
|
||||
return a +
|
||||
`<div class="graph--bar zone-${zone}" style="height: ${height}px; width: ${width}%" ${powerAttr} ${cadenceAttr} ${slopeAttr} ${durationAttr}></div>`;
|
||||
@@ -87,7 +87,7 @@ function renderInfo(args = {}) {
|
||||
const bottom = args.intervalRect.height;
|
||||
|
||||
dom.info.style.display = 'block';
|
||||
dom.info.innerHTML = `<div>${power}</div><div>${cadence}</div><div>${slope}</div><div class="graph--info--time">${duration}</div>`;
|
||||
dom.info.innerHTML = `<div>${escapeHtml(power)}</div><div>${escapeHtml(cadence)}</div><div>${escapeHtml(slope)}</div><div class="graph--info--time">${escapeHtml(duration)}</div>`;
|
||||
|
||||
const width = dom.info.getBoundingClientRect().width;
|
||||
const height = dom.info.getBoundingClientRect().height;
|
||||
@@ -346,7 +346,8 @@ function courseToGraph(course, viewPort) {
|
||||
const x4 = px2;
|
||||
const y4 = yMax;
|
||||
|
||||
return acc + `<polygon points="${x1},${y1} ${x2},${y2} ${x3},${y3} ${x4},${y4}" stroke="none" fill="${color}" class="graph--bar" index="${i}" slope="${p.slope}" />`;
|
||||
const slopeAttr = exists(p.slope) ? `slope="${escapeHtml(p.slope)}"` : '';
|
||||
return acc + `<polygon points="${x1},${y1} ${x2},${y2} ${x3},${y3} ${x4},${y4}" stroke="none" fill="${color}" class="graph--bar" index="${i}" ${slopeAttr} />`;
|
||||
|
||||
}, ``);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { xf, exists, empty, equals, debounce } from '../functions.js';
|
||||
import { xf, exists, empty, equals, debounce, escapeHtml } from '../functions.js';
|
||||
import { models } from '../models/models.js';
|
||||
import { intervalsToGraph, courseToGraph, renderInfo } from './workout-graph.js';
|
||||
|
||||
@@ -25,21 +25,25 @@ function workoutTemplate(workout) {
|
||||
if(workout.meta.distance) {
|
||||
duration = `${(workout.meta.distance / 1000).toFixed(2)} km`;
|
||||
}
|
||||
return `<workout-item class='workout cf' id="${workout.id}" metric="ftp">
|
||||
const id = escapeHtml(workout.id);
|
||||
const name = escapeHtml(workout.meta.name);
|
||||
const category = escapeHtml(workout.meta.category);
|
||||
const description = escapeHtml(workout.meta.description);
|
||||
return `<workout-item class='workout cf' id="${id}" metric="ftp">
|
||||
<div class="workout--info">
|
||||
<div class="workout--short-info">
|
||||
<div class="workout--summary">
|
||||
<div class="workout--name">${workout.meta.name}</div>
|
||||
<div class="workout--type">${workout.meta.category}</div>
|
||||
<div class="workout--name">${name}</div>
|
||||
<div class="workout--type">${category}</div>
|
||||
<div class="workout--duration">${duration}</div>
|
||||
<div class="workout--select" id="btn${workout.id}">${workout.selected ? radioOn : radioOff}
|
||||
<div class="workout--select" id="btn${id}">${workout.selected ? radioOn : radioOff}
|
||||
</div>
|
||||
<div class="workout--options">${options}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="workout--full-info">
|
||||
<div class="workout-list--graph-cont">${workout.graph}</div>
|
||||
<div class="workout--description">${workout.meta.description}</div>
|
||||
<div class="workout--description">${description}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="workout--actions">
|
||||
|
||||
@@ -4,8 +4,7 @@ import { models } from './models/models.js';
|
||||
import { ControlMode, } from './ble/enums.js';
|
||||
import { TimerStatus, EventType, } from './activity/enums.js';
|
||||
|
||||
// const timer = new Worker('./timer.js');
|
||||
const timer = new Worker(new URL('./timer.js', import.meta.url));
|
||||
const timer = new Worker(new URL('./timer.js', import.meta.url), {type: 'module'});
|
||||
|
||||
class Watch {
|
||||
constructor(args) {
|
||||
|
||||
@@ -53,6 +53,7 @@ import {
|
||||
toNumber,
|
||||
toFixed,
|
||||
toBool,
|
||||
escapeHtml,
|
||||
dataviewToArray,
|
||||
dataviewToString,
|
||||
stringToCharCodes,
|
||||
@@ -599,6 +600,30 @@ describe('xor', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('escapeHtml', () => {
|
||||
test('keeps a normal workout name', () => {
|
||||
expect(escapeHtml('Dijon')).toBe('Dijon');
|
||||
});
|
||||
|
||||
test('turns tags and quotes into text', () => {
|
||||
expect(escapeHtml(`<img src=x onerror="alert(1)">`))
|
||||
.toBe('<img src=x onerror="alert(1)">');
|
||||
});
|
||||
|
||||
test('escapes an ampersand before the other characters', () => {
|
||||
expect(escapeHtml(`Tom & Jerry`)).toBe('Tom & Jerry');
|
||||
});
|
||||
|
||||
test('turns a missing value into an empty string', () => {
|
||||
expect(escapeHtml(null)).toBe('');
|
||||
expect(escapeHtml(undefined)).toBe('');
|
||||
});
|
||||
|
||||
test('turns a number into digits', () => {
|
||||
expect(escapeHtml(90)).toBe('90');
|
||||
});
|
||||
});
|
||||
|
||||
// describe('', () => {
|
||||
// test('', () => {
|
||||
// expect().toBe();
|
||||
|
||||
Reference in New Issue
Block a user