Commit af9f606d authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Fix annotation error due to URI encoding

parent ae9894dd
...@@ -47,16 +47,23 @@ export class HTMLMetadata { ...@@ -47,16 +47,23 @@ export class HTMLMetadata {
* Returns the primary URI for the document being annotated * Returns the primary URI for the document being annotated
*/ */
uri(): string { uri(): string {
let uri = decodeURIComponent(this._getDocumentHref()); let uri = this._getDocumentHref(); // Get the URI without decoding it first
// Use the `link[rel=canonical]` element's href as the URL if present. // Attempt to decode the URI, handle exceptions if the URI is malformed
try {
uri = decodeURIComponent(uri);
} catch (error) {
// Log error for debugging. After this point we fall back to the original URI
console.error('Error decoding URI:', error);
}
// Use the `link[rel=canonical]` element's href as the URI if present.
const links = this._getLinks(); const links = this._getLinks();
for (const link of links) { for (const link of links) {
if (link.rel === 'canonical') { if (link.rel === 'canonical') {
uri = link.href; uri = link.href; // Assuming canonical hrefs are correctly encoded
} }
} }
return uri; return uri;
} }
......
...@@ -405,5 +405,24 @@ describe('HTMLMetadata', () => { ...@@ -405,5 +405,24 @@ describe('HTMLMetadata', () => {
assert.equal(doc.uri(), canonicalLink.href); assert.equal(doc.uri(), canonicalLink.href);
}); });
it('should log an error if URI is not decodable', () => {
// '%%CUST_ID%%' is an invalid escape sequence, so it will throw a URIError when decoded
const badURI = 'https://example.com/?foo=%%CUST_ID%%';
const doc = createDoc(badURI);
const consoleErrorSpy = sinon.stub(console, 'error');
try {
assert.equal(badURI, doc.uri());
assert.calledOnce(consoleErrorSpy);
assert.calledWith(
consoleErrorSpy,
'Error decoding URI:',
sinon.match.instanceOf(URIError),
);
} finally {
console.error.restore();
}
});
}); });
}); });
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