Refactor LinkAnnotation
slightly to add data.url
/data.dest
at the end
This patch also makes sure that all URLs are converted to the correct encoding.
This commit is contained in:
parent
4a601ffc28
commit
b63ef7a8b6
@ -37,6 +37,7 @@ var AnnotationFlag = sharedUtil.AnnotationFlag;
|
|||||||
var AnnotationType = sharedUtil.AnnotationType;
|
var AnnotationType = sharedUtil.AnnotationType;
|
||||||
var OPS = sharedUtil.OPS;
|
var OPS = sharedUtil.OPS;
|
||||||
var Util = sharedUtil.Util;
|
var Util = sharedUtil.Util;
|
||||||
|
var isString = sharedUtil.isString;
|
||||||
var isArray = sharedUtil.isArray;
|
var isArray = sharedUtil.isArray;
|
||||||
var isInt = sharedUtil.isInt;
|
var isInt = sharedUtil.isInt;
|
||||||
var isValidUrl = sharedUtil.isValidUrl;
|
var isValidUrl = sharedUtil.isValidUrl;
|
||||||
@ -705,68 +706,78 @@ var LinkAnnotation = (function LinkAnnotationClosure() {
|
|||||||
var data = this.data;
|
var data = this.data;
|
||||||
data.annotationType = AnnotationType.LINK;
|
data.annotationType = AnnotationType.LINK;
|
||||||
|
|
||||||
var action = dict.get('A');
|
var action = dict.get('A'), url, dest;
|
||||||
if (action && isDict(action)) {
|
if (action && isDict(action)) {
|
||||||
var linkType = action.get('S').name;
|
var linkType = action.get('S').name;
|
||||||
if (linkType === 'URI') {
|
switch (linkType) {
|
||||||
var url = action.get('URI');
|
case 'URI':
|
||||||
if (isName(url)) {
|
url = action.get('URI');
|
||||||
// Some bad PDFs do not put parentheses around relative URLs.
|
if (isName(url)) {
|
||||||
url = '/' + url.name;
|
// Some bad PDFs do not put parentheses around relative URLs.
|
||||||
} else if (url) {
|
url = '/' + url.name;
|
||||||
url = addDefaultProtocolToUrl(url);
|
} else if (url) {
|
||||||
}
|
url = addDefaultProtocolToUrl(url);
|
||||||
// TODO: pdf spec mentions urls can be relative to a Base
|
}
|
||||||
// entry in the dictionary.
|
// TODO: pdf spec mentions urls can be relative to a Base
|
||||||
if (!isValidUrl(url, false)) {
|
// entry in the dictionary.
|
||||||
url = '';
|
break;
|
||||||
}
|
|
||||||
// According to ISO 32000-1:2008, section 12.6.4.7,
|
|
||||||
// URI should to be encoded in 7-bit ASCII.
|
|
||||||
// Some bad PDFs may have URIs in UTF-8 encoding, see Bugzilla 1122280.
|
|
||||||
try {
|
|
||||||
data.url = stringToUTF8String(url);
|
|
||||||
} catch (e) {
|
|
||||||
// Fall back to a simple copy.
|
|
||||||
data.url = url;
|
|
||||||
}
|
|
||||||
} else if (linkType === 'GoTo') {
|
|
||||||
data.dest = action.get('D');
|
|
||||||
} else if (linkType === 'GoToR') {
|
|
||||||
var urlDict = action.get('F');
|
|
||||||
if (isDict(urlDict)) {
|
|
||||||
// We assume that the 'url' is a Filspec dictionary
|
|
||||||
// and fetch the url without checking any further
|
|
||||||
url = urlDict.get('F') || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: pdf reference says that GoToR
|
case 'GoTo':
|
||||||
// can also have 'NewWindow' attribute
|
dest = action.get('D');
|
||||||
if (!isValidUrl(url, false)) {
|
break;
|
||||||
url = '';
|
|
||||||
}
|
case 'GoToR':
|
||||||
data.url = url;
|
var urlDict = action.get('F');
|
||||||
data.dest = action.get('D');
|
if (isDict(urlDict)) {
|
||||||
} else if (linkType === 'Named') {
|
// We assume that the 'url' is a Filspec dictionary
|
||||||
data.action = action.get('N').name;
|
// and fetch the url without checking any further
|
||||||
} else {
|
url = urlDict.get('F') || '';
|
||||||
warn('unrecognized link type: ' + linkType);
|
}
|
||||||
|
|
||||||
|
// TODO: pdf reference says that GoToR
|
||||||
|
// can also have 'NewWindow' attribute
|
||||||
|
dest = action.get('D');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'Named':
|
||||||
|
data.action = action.get('N').name;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
warn('unrecognized link type: ' + linkType);
|
||||||
}
|
}
|
||||||
} else if (dict.has('Dest')) {
|
} else if (dict.has('Dest')) { // Simple destination link.
|
||||||
// simple destination link
|
dest = dict.get('Dest');
|
||||||
var dest = dict.get('Dest');
|
}
|
||||||
|
|
||||||
|
if (url) {
|
||||||
|
if (isValidUrl(url, /* allowRelative = */ false)) {
|
||||||
|
data.url = tryConvertUrlEncoding(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dest) {
|
||||||
data.dest = isName(dest) ? dest.name : dest;
|
data.dest = isName(dest) ? dest.name : dest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lets URLs beginning with 'www.' default to using the 'http://' protocol.
|
// Lets URLs beginning with 'www.' default to using the 'http://' protocol.
|
||||||
function addDefaultProtocolToUrl(url) {
|
function addDefaultProtocolToUrl(url) {
|
||||||
if (url && url.indexOf('www.') === 0) {
|
if (isString(url) && url.indexOf('www.') === 0) {
|
||||||
return ('http://' + url);
|
return ('http://' + url);
|
||||||
}
|
}
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tryConvertUrlEncoding(url) {
|
||||||
|
// According to ISO 32000-1:2008, section 12.6.4.7, URIs should be encoded
|
||||||
|
// in 7-bit ASCII. Some bad PDFs use UTF-8 encoding, see Bugzilla 1122280.
|
||||||
|
try {
|
||||||
|
return stringToUTF8String(url);
|
||||||
|
} catch (e) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Util.inherit(LinkAnnotation, Annotation, {});
|
Util.inherit(LinkAnnotation, Annotation, {});
|
||||||
|
|
||||||
return LinkAnnotation;
|
return LinkAnnotation;
|
||||||
|
@ -284,7 +284,7 @@ var LinkAnnotationElement = (function LinkAnnotationElementClosure() {
|
|||||||
if (this.data.action) {
|
if (this.data.action) {
|
||||||
this._bindNamedAction(link, this.data.action);
|
this._bindNamedAction(link, this.data.action);
|
||||||
} else {
|
} else {
|
||||||
this._bindLink(link, ('dest' in this.data) ? this.data.dest : null);
|
this._bindLink(link, (this.data.dest || null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,7 +312,7 @@ function isSameOrigin(baseUrl, otherUrl) {
|
|||||||
|
|
||||||
// Validates if URL is safe and allowed, e.g. to avoid XSS.
|
// Validates if URL is safe and allowed, e.g. to avoid XSS.
|
||||||
function isValidUrl(url, allowRelative) {
|
function isValidUrl(url, allowRelative) {
|
||||||
if (!url) {
|
if (!url || typeof url !== 'string') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// RFC 3986 (http://tools.ietf.org/html/rfc3986#section-3.1)
|
// RFC 3986 (http://tools.ietf.org/html/rfc3986#section-3.1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user