Show a cable's name and a repair button from a save as text
The city panel put the name of your own subsea cable and an incident's repair label into its HTML as they came from the save. Both are escaped now, and sanitize() keeps a cable's name to a string of 60 characters.
This commit is contained in:
parent
7f4fdf6043
commit
aef744af50
4 changed files with 35 additions and 3 deletions
|
|
@ -22,6 +22,7 @@ All notable changes to this project are documented here. The project follows
|
|||
- Cities, labels, building cones, ships, offer bubbles and money pop-ups are painted once and then reused, so the map also draws far fewer shapes each frame. (#281)
|
||||
- In Firefox on a computer, the 2D map is up to three times as fast when zoomed in: its land picture now stays on the graphics card instead of being sent to it again on every frame. Firefox on Android keeps the old way, which avoids its drawing glitches. (#281)
|
||||
- Settings › Viewport info is now Diagnostic info: besides your screen's size, it shows how the map is drawn (by the graphics card or in 2D, and why) and how fast, ready to copy into a bug report. (#281)
|
||||
- A save file can no longer put formatting or links into the game: the name of your own subsea cable and the repair button on a POP that is down always show as plain text, and a cable name from a save is cut to 60 characters.
|
||||
|
||||
## [2.9.1] — 2026-11-04
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ function sanitize(s){
|
|||
if(!isCity(s.home))s.home=Object.keys(s.pops)[0]||'fra';
|
||||
opsSanitize(s);
|
||||
const tier=v=>int(v,0,PORT.length-1,0),lag=v=>int(v,1,LAG_MAX,1),sid=v=>String(v==null?uid():v).slice(0,40);
|
||||
s.links=s.links.filter(l=>l&&isCity(l.a)&&isCity(l.b)&&l.a!==l.b).map(l=>{l.id=sid(l.id);if(l.list!==undefined){const L=l.list;if(l.kind==='df'&&L&&typeof L==='object'&&!Array.isArray(L)){const w=int(L.w,0,256,0),p=int(L.p,0,2,0);if(w||p)l.list={w,p};else delete l.list;}else delete l.list;}l.km=num(l.km,kmBetween(l.a,l.b));l.mo=num(l.mo,0);l.cut=num(l.cut,0);if(l.pairs!==undefined)l.pairs=Math.min(l.sub?SUB_PAIRS_MAX:s.lvl>=PAIRS_LV?PAIRS_BIG:PAIRS_MAX,Math.max(1,Math.floor(+l.pairs)||1));if(l.kind!=='df'&&l.kind!=='vxc')l.kind='wave';if(l.kind==='wave')l.tier=int(l.tier,0,LT.length-1,0);if(l.cable&&(!own(CAB,l.cable)||!cablePair(l.cable,l.a,l.b)))l.cable=null;if(l.kind==='vxc'&&!own(VXC,l.vt))l.vt=0;if(l.kind==='df'){if(!own(DF_SYS,l.sys))l.sys='lite';l.ch=Array.isArray(l.ch)?l.ch.filter(o=>own(OPT,o)):[];l.ready=num(l.ready,0);if(l.day!==undefined)l.day=Math.min(s.day,Math.max(0,num(l.day,s.day)));for(const k of Object.keys(SUB_UPGRADES)){const f='sub'+k;if(l[f]!==undefined){if(l.own&&l.sub&&Number.isFinite(l[f])&&l[f]>=0)l[f]=Math.round(l[f]);else delete l[f];}}if(l.seaPath!==undefined){if(l.own&&l.sub&&Array.isArray(l.seaPath)&&l.seaPath.length>=2&&l.seaPath.length<=2048&&l.seaPath.every(p=>Array.isArray(p)&&p.length===2&&p.every(Number.isFinite)&&Math.abs(p[0])<=180&&Math.abs(p[1])<=90))l.seaPath=l.seaPath.map(p=>p.slice());else delete l.seaPath;}if(l.paid!==undefined){if(l.own&&l.sub&&Number.isFinite(l.paid)&&l.paid>=0)l.paid=Math.round(l.paid);else delete l.paid;}if(l.own&&l.sub&&!own(SEA_DESIGNS,l.seaDesign))l.seaDesign='standard';l.regen=int(l.regen,0,REGEN_MAX,0);}const deg=v=>v&&typeof v==='object'?{until:num(v.until,0),f:Math.min(1,Math.max(0,num(v.f,1)))}:null;if(l.loss)l.loss=deg(l.loss);return l;});
|
||||
s.links=s.links.filter(l=>l&&isCity(l.a)&&isCity(l.b)&&l.a!==l.b).map(l=>{l.id=sid(l.id);if(l.list!==undefined){const L=l.list;if(l.kind==='df'&&L&&typeof L==='object'&&!Array.isArray(L)){const w=int(L.w,0,256,0),p=int(L.p,0,2,0);if(w||p)l.list={w,p};else delete l.list;}else delete l.list;}l.km=num(l.km,kmBetween(l.a,l.b));l.mo=num(l.mo,0);l.cut=num(l.cut,0);if(l.cname!=null)l.cname=String(l.cname).slice(0,60);if(l.pairs!==undefined)l.pairs=Math.min(l.sub?SUB_PAIRS_MAX:s.lvl>=PAIRS_LV?PAIRS_BIG:PAIRS_MAX,Math.max(1,Math.floor(+l.pairs)||1));if(l.kind!=='df'&&l.kind!=='vxc')l.kind='wave';if(l.kind==='wave')l.tier=int(l.tier,0,LT.length-1,0);if(l.cable&&(!own(CAB,l.cable)||!cablePair(l.cable,l.a,l.b)))l.cable=null;if(l.kind==='vxc'&&!own(VXC,l.vt))l.vt=0;if(l.kind==='df'){if(!own(DF_SYS,l.sys))l.sys='lite';l.ch=Array.isArray(l.ch)?l.ch.filter(o=>own(OPT,o)):[];l.ready=num(l.ready,0);if(l.day!==undefined)l.day=Math.min(s.day,Math.max(0,num(l.day,s.day)));for(const k of Object.keys(SUB_UPGRADES)){const f='sub'+k;if(l[f]!==undefined){if(l.own&&l.sub&&Number.isFinite(l[f])&&l[f]>=0)l[f]=Math.round(l[f]);else delete l[f];}}if(l.seaPath!==undefined){if(l.own&&l.sub&&Array.isArray(l.seaPath)&&l.seaPath.length>=2&&l.seaPath.length<=2048&&l.seaPath.every(p=>Array.isArray(p)&&p.length===2&&p.every(Number.isFinite)&&Math.abs(p[0])<=180&&Math.abs(p[1])<=90))l.seaPath=l.seaPath.map(p=>p.slice());else delete l.seaPath;}if(l.paid!==undefined){if(l.own&&l.sub&&Number.isFinite(l.paid)&&l.paid>=0)l.paid=Math.round(l.paid);else delete l.paid;}if(l.own&&l.sub&&!own(SEA_DESIGNS,l.seaDesign))l.seaDesign='standard';l.regen=int(l.regen,0,REGEN_MAX,0);}const deg=v=>v&&typeof v==='object'?{until:num(v.until,0),f:Math.min(1,Math.max(0,num(v.f,1)))}:null;if(l.loss)l.loss=deg(l.loss);return l;});
|
||||
for(const l of s.links)repairSubUpgradeDates(l,s.day);
|
||||
/* Metrics moved from each link to its city pair (#122). Older saves routed a pair at the lowest metric of its
|
||||
links, with all of them carrying traffic, so the pair keeps that metric and nothing goes on standby. */
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ function cityPanel(c){
|
|||
h+=`<nav class="tabs">${tabs.map(([k,l])=>`<button class="${UI.tab===k?'on':''}" data-act="tab" data-args='{"t":"${k}"}'>${l}</button>`).join('')}</nav><div class="scroll">`;
|
||||
if(C.cn&&UI.tab==='cust')h+=chinaSection(c);
|
||||
const sf=sfOf(R,c),dem=R.cityDem[c]||0;
|
||||
if(down){const i=S.inc.filter(x=>x.ref===c&&POPDOWN.includes(x.type)).sort((a,b)=>b.until-a.until)[0];h+=`<section><h3 style="color:var(--bad)">${tr('city_panel.this_pop_is_down')}</h3><p class="lead">${i?esc(i.msg):''} ${tr('city_panel.customers_here_get_nothing_until_it')}</p>${i&&i.cost?btn(`${i.fix} ${fmt$(i.cost)}`,'fixInc',{id:i.id},'red'):''}</section>`;}
|
||||
if(down){const i=S.inc.filter(x=>x.ref===c&&POPDOWN.includes(x.type)).sort((a,b)=>b.until-a.until)[0];h+=`<section><h3 style="color:var(--bad)">${tr('city_panel.this_pop_is_down')}</h3><p class="lead">${i?esc(i.msg):''} ${tr('city_panel.customers_here_get_nothing_until_it')}</p>${i&&i.cost?btn(`${esc(i.fix||tr('common.rush_repair'))} ${fmt$(i.cost)}`,'fixInc',{id:i.id},'red'):''}</section>`;}
|
||||
if(UI.tab==='cust'){
|
||||
{const nl=R.nodeLoad[c]||0,nc=R.nodeCap[c]||1;if(nl>=nc*0.95)h+=`<section><h3 style="color:var(--bad)">${tr('city_panel.router_maxed_out')}</h3><p class="lead">${tr('city_panel.your_routers_here_are_forwarding',{load:fmtG(nl),cap:fmtG(nc)})}</p>${btn(tr('common.open_infra'),'tab',{t:'links'},'pink sm')}</section>`;}
|
||||
h+=`<section><h3>${tr('common.home_broadband')}</h3>`;
|
||||
|
|
@ -104,7 +104,7 @@ function cityPanel(c){
|
|||
h+=`<section><h3>${tr('city_panel.fiber_from_city',{city:C.name})}</h3><p class="lead">${tr('city_panel.drag_from_this_router_to_any')}</p>`;
|
||||
if(!ls.length)h+=`<p class="empty">${tr('common.no_links_yet')}</p>`;
|
||||
for(const l of ls){const o=l.a===c?l.b:l.a,u=capOf(l)?(R.load[l.id]||0)/capOf(l):0,cut=linkDown(l);
|
||||
h+=`<div class="row"><div class="grow"><div class="nm">→ ${CITY[o].name}${l.mult>1?' · '+tr('common.diverse'):''} ${cut?'<span class="tag restrictive">'+tr('common.cut')+'</span>':''}</div><div class="meta">${capName(l)} · ${l.km.toLocaleString()} km ${l.kind==='vxc'?tr('city_panel.virtual_circuit_via_fab',{fab:esc(FAB[l.prov].name)}):l.kind==='df'?(l.own?(l.sub&&l.cname?tr('city_panel.your_own_subsea_cable_name',{name:l.cname}):tr('common.own_dark_fiber')):tr('city_panel.dark_fiber_iru'))+(l.ready>S.day?` · ${tr('city_panel.ready_in_n_d',{n:l.ready-S.day})}`:''):l.sub?tr('common.subsea'):tr('common.terrestrial')} · ${tr('common.price_mo',{price:fmt$(l.mo)})}</div><div class="bar ${barCls(u)}"><i style="width:${u*100}%"></i></div></div>${btn(tr('common.open'),'selLink',{id:l.id},'ghost sm')}</div>`;}
|
||||
h+=`<div class="row"><div class="grow"><div class="nm">→ ${CITY[o].name}${l.mult>1?' · '+tr('common.diverse'):''} ${cut?'<span class="tag restrictive">'+tr('common.cut')+'</span>':''}</div><div class="meta">${capName(l)} · ${l.km.toLocaleString()} km ${l.kind==='vxc'?tr('city_panel.virtual_circuit_via_fab',{fab:esc(FAB[l.prov].name)}):l.kind==='df'?(l.own?(l.sub&&l.cname?tr('city_panel.your_own_subsea_cable_name',{name:esc(l.cname)}):tr('common.own_dark_fiber')):tr('city_panel.dark_fiber_iru'))+(l.ready>S.day?` · ${tr('city_panel.ready_in_n_d',{n:l.ready-S.day})}`:''):l.sub?tr('common.subsea'):tr('common.terrestrial')} · ${tr('common.price_mo',{price:fmt$(l.mo)})}</div><div class="bar ${barCls(u)}"><i style="width:${u*100}%"></i></div></div>${btn(tr('common.open'),'selLink',{id:l.id},'ghost sm')}</div>`;}
|
||||
h+=`</section>`;
|
||||
if(Object.keys(S.pops).length>1)h+=`<section><h3>${tr('common.decommission')}</h3><p class="lead">${tr('city_panel.shut_this_pop_down_its_links')}</p><div class="btns">${btn(tr('city_panel.decommission_city',{city:C.name}),'decomOpen',{city:c},'ghost sm')}${btn(tr('city_panel.decommission_several_pops'),'decomOpen',{},'ghost sm')}</div></section>`;
|
||||
}
|
||||
|
|
|
|||
31
tests/save-text.test.js
Normal file
31
tests/save-text.test.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Text that comes from a save (an imported file, or this device's storage) is shown as text, never as HTML: the
|
||||
// name of your own subsea cable, the repair button of a POP that is down. sanitize() also keeps a cable's name short.
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { load } = require('./harness');
|
||||
|
||||
const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
|
||||
|
||||
function open() {
|
||||
const h = load({ seed: 777 });
|
||||
h.ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();S.nextFail=S.day+999;S.nextEvent=S.day+999;compute();`);
|
||||
return h;
|
||||
}
|
||||
|
||||
test('text from a save is shown as text: a cable’s name and a repair button', () => {
|
||||
const { ev, errors } = open();
|
||||
const r = JSON.parse(ev(`(()=>{const s=JSON.parse(JSON.stringify(S)),b0=s.links[0],own=(id,cname)=>({...b0,id,kind:'df',own:true,sub:true,ch:[],sys:'roadm',seaDesign:'standard',cable:null,trench:'own:'+id,ready:0,mo:100,cname});
|
||||
const l=own('zzl1','<b id="xss1">Cable</b>'+'x'.repeat(200)),t=own('zzl2',12345);s.links.push(l,t);
|
||||
const c=Object.keys(s.pops)[0];s.pops[c].down=s.day+5;s.inc.push({id:'zz',type:'crash',ref:c,until:s.day+5,cost:1000,msg:'boom',fix:'<i id="xss2">fix</i>'});
|
||||
S=migrate(s);compute();UI.tab='links';const L=S.links.find(x=>x.id===l.id),T=t&&S.links.find(x=>x.id===t.id);
|
||||
const h1=cityPanel(L.a)+cityPanel(L.b),h2=cityPanel(c);
|
||||
return JSON.stringify({len:L.cname.length,num:T.cname,raw1:/<b id="xss1">/.test(h1),esc1:/<b id=/.test(h1),raw2:/<i id="xss2">/.test(h2),esc2:/<i id=/.test(h2)});})()`));
|
||||
assert.equal(r.len, 60, 'a cable name from a save is cut to 60 characters');
|
||||
assert.equal(r.num, '12345', 'and is always text');
|
||||
assert.equal(r.raw1, false); assert.equal(r.raw2, false);
|
||||
assert.equal(r.esc1, true, 'the cable\u2019s name shows, escaped');
|
||||
assert.equal(r.esc2, true, 'the repair button shows its text escaped');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue