fix relative URLs bugs

This commit is contained in:
alexadam 2016-09-07 13:50:37 +03:00
parent e7aab8cd00
commit c752f7a3b6
2 changed files with 32 additions and 30 deletions

View file

@ -163,14 +163,14 @@ function sanitize(rawContentString) {
tattrs = attrs.filter(function(attr) { tattrs = attrs.filter(function(attr) {
return attr.name === 'src'; return attr.name === 'src';
}).map(function(attr) { }).map(function(attr) {
return getImageSrc(decodeHtmlEntity(attr.value)); return getImageSrc(attr.value);
}); });
lastFragment = tattrs.length === 0 ? '<img></img>' : '<img src="' + tattrs[0] + '" alt=""></img>'; lastFragment = tattrs.length === 0 ? '<img></img>' : '<img src="' + tattrs[0] + '" alt=""></img>';
} else if (tag === 'a') { } else if (tag === 'a') {
tattrs = attrs.filter(function(attr) { tattrs = attrs.filter(function(attr) {
return attr.name === 'href'; return attr.name === 'href';
}).map(function(attr) { }).map(function(attr) {
return getHref(decodeHtmlEntity(attr.value)); return getHref(attr.value);
}); });
lastFragment = tattrs.length === 0 ? '<a>' : '<a href="' + tattrs[0] + '">'; lastFragment = tattrs.length === 0 ? '<a>' : '<a href="' + tattrs[0] + '">';
} else { } else {

View file

@ -54,7 +54,7 @@ function getFileExtension(fileName) {
if (tmpFileName === 'jpg') { if (tmpFileName === 'jpg') {
tmpFileName = 'jpeg'; tmpFileName = 'jpeg';
} else if (tmpFileName.trim() === '') { } else if (tmpFileName.trim() === '') {
return ''; tmpFileName = '';
} }
return tmpFileName; return tmpFileName;
} catch (e) { } catch (e) {
@ -72,39 +72,41 @@ function getImageType(fileName) {
} }
function getHref(hrefTxt) { function getHref(hrefTxt) {
if (!hrefTxt) { return getAbsoluteUrl(hrefTxt);
return '';
}
hrefTxt = hrefTxt.trim();
if (hrefTxt === '') {
return '';
}
if (hrefTxt.indexOf('#') === 0) {
hrefTxt = window.location.href + hrefTxt;
}
if (hrefTxt.indexOf('http') !== 0) {
var separator = '/';
if (hrefTxt.indexOf('/') === 0) {
separator = '';
}
hrefTxt = window.location.protocol + '//' + window.location.hostname + separator + hrefTxt;
}
// hrefTxt = escape(hrefTxt);
return hrefTxt;
} }
function getImgDownloadUrl(imgSrc) { function getImgDownloadUrl(imgSrc) {
var baseUrl = getOriginUrl(); return getAbsoluteUrl(imgSrc);
if (imgSrc.indexOf('//') === 0) {
return baseUrl.split('//')[0] + imgSrc;
} }
if (imgSrc.indexOf('http') !== 0) {
if (imgSrc.indexOf('/') === 0) { function getAbsoluteUrl(urlStr) {
return baseUrl + imgSrc; if (!urlStr) {
return '';
} }
return baseUrl + '/' + imgSrc; try {
urlStr = urlStr.trim().toLowerCase();
if (urlStr.length === 0) {
return '';
}
urlStr = decodeHtmlEntity(urlStr);
var currentUrl = getCurrentUrl();
var originUrl = getOriginUrl();
var absoluteUrl = urlStr;
if (urlStr.indexOf('//') === 0) {
absoluteUrl = window.location.protocol + urlStr;
} else if (urlStr.indexOf('/') === 0) {
absoluteUrl = originUrl + urlStr;
} else if (urlStr.indexOf('#') === 0) {
absoluteUrl = currentUrl + urlStr;
} else if (urlStr.indexOf('http') !== 0) {
absoluteUrl = currentUrl + '/' + urlStr;
}
return absoluteUrl;
} catch (e) {
console.log('Error:', e);
return urlStr;
} }
return imgSrc;
} }
// https://gist.github.com/jonleighton/958841 // https://gist.github.com/jonleighton/958841