Skip to content

Commit 9460289

Browse files
author
Tim Berners-Lee
committed
Fix UI crash in label() when URI had strange characters. Tweak the fallback label generation
1 parent 6144cc2 commit 9460289

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/utils.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,27 @@ UI.utils.label = function (x, initialCap) { // x is an object
491491
}
492492
var s = x.uri
493493
if (typeof s == 'undefined') return x.toString(); // can't be a symbol
494-
s = decodeURI(s)
495-
if (s.slice(-5) == '#this') s = s.slice(0, -5)
496-
else if (s.slice(-3) == '#me') s = s.slice(0, -3)
494+
// s = decodeURI(s) // This can crash is random valid @ signs are presentation
495+
// The idea was to clean up eg URIs encoded in query strings
496+
// Also encoded character in what was filenames like @ [] {}
497+
try{
498+
s = s.split('/').map(decodeURIComponent).join('/') // If it is properly encoded
499+
} catch(e){ // try individual decoding of ASCII code points
500+
for (var i =s.length - 3; i > 0; i --) {
501+
const hex = '0123456789abcefABCDEF' // The while upacks multiple layers of encoding
502+
while (s[i] === '%' && hex.indexOf(s[i+1]) >=0 && hex.indexOf(s[i+2]) >=0 ) {
503+
s = s.slice(0, i) + String.fromCharCode(parseInt(s.slice(i+1, i+3), 16)) + s.slice(i+3)
504+
}
505+
}
506+
}
507+
if (s.slice(-5) === '#this') s = s.slice(0, -5)
508+
else if (s.slice(-3) === '#me') s = s.slice(0, -3)
497509

498510
var hash = s.indexOf('#')
499511
if (hash >= 0) return cleanUp(s.slice(hash + 1))
500512

501-
if (s.slice(-9) == '/foaf.rdf') s = s.slice(0, -9)
502-
else if (s.slice(-5) == '/foaf') s = s.slice(0, -5)
513+
if (s.slice(-9) === '/foaf.rdf') s = s.slice(0, -9)
514+
else if (s.slice(-5) === '/foaf') s = s.slice(0, -5)
503515

504516
if (1) { // Eh? Why not do this? e.g. dc:title needs it only trim URIs, not rdfs:labels
505517
var slash = s.lastIndexOf('/', s.length - 2); // (len-2) excludes trailing slash

src/widgets/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ UI.widgets.field[UI.ns.ui('Choice').uri] = function (
898898
}
899899
var subForm = kb.any(form, ui('use')) // Optional
900900
var possible = []
901+
var possibleProperties
901902
var opts = { 'multiple': multiple, 'nullLabel': np, 'disambiguate': false }
902903
possible = kb.each(undefined, ns.rdf('type'), from)
903904
for (var x in kb.findMembersNT(from)) {
@@ -1887,7 +1888,7 @@ UI.widgets.index.twoLine['http://www.w3.org/ns/pim/trip#Trip'] = function (dom,
18871888
// Stick a stylesheet link the document if not already there
18881889
UI.widgets.addStyleSheet = function(dom, href) {
18891890
var links = dom.querySelectorAll('link');
1890-
for (i=0; i<links.length; i++){
1891+
for (var i=0; i<links.length; i++){
18911892
if ((links[i].getAttribute('rel') ||'') === 'stylesheet'
18921893
&& (links[i].getAttribute('href') ||'') === href ) return ;
18931894
}

0 commit comments

Comments
 (0)