Commit c2e777af authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Added support for URI canonicalization

parent a855db8ab474
......@@ -21,7 +21,8 @@
import * as application from "./application";
import * as definition from "./view-def";
import * as implementation from "./view-impl";
import * as uris from "./uris";
import * as rdfEntities from "./rdf-entities";
import * as rdfMeta from "./rdf-meta";
export { application, definition, implementation, rdfEntities, rdfMeta };
export { application, definition, implementation, uris, rdfEntities, rdfMeta };
/*******************************************************************************
* Copyright 2003-2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
* contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
*
* This file is part of CubicWeb.
*
* CubicWeb is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 2.1 of the License, or (at your option)
* any later version.
*
* CubicWeb is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/**
* Gets the equivalent uri with unicode escape sequences
* @param uri The original uri
*/
export function uriEscape(uri: string): string {
let result = "";
for (var i = 0; i != uri.length; i++) {
let cc = uri.charCodeAt(i);
if (cc >= 0x100) {
let offset = cc >= 0xd800 && cc <= 0xdbff ? 1 : 0;
let cp = uri.codePointAt(i).toString();
result += "\\u";
let leading = 4 + offset * 4 - cp.length;
while (leading > 0) {
result += "0";
leading--;
}
result += cp;
i += offset;
} else {
result += uri.charAt(i);
}
}
return result;
}
/**
* Gets the equivalent extended uri
* @param uri The original uri
*/
export function uriUnescape(uri: string): string {
let result = "";
for (var i = 0; i != uri.length; i++) {
let c = uri.charAt(i);
if (c == "\\") {
let n = uri.charAt(i + 1);
if (n == "u") {
// \ u XXXX for unicode characters in the BMP
let codepoint = Number.parseInt(uri.substring(i + 2, i + 6), 16);
result += String.fromCodePoint(codepoint);
i += 5;
} else if (n == "U") {
// \ U XXXXXXXX for unicode characters outside the BMP
let codepoint = Number.parseInt(uri.substring(i + 2, i + 10), 16);
result += String.fromCodePoint(codepoint);
i += 9;
} else {
// \C for C, where C is any character other than 0, a, t, b, r, n, f, u and U
result += n;
i++;
}
} else {
// not the start of an escape sequence, replace as is
result += c;
}
}
return result;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment