From 7d5f3537df0b002779a8e16e66b70a486a4db6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Thu, 17 Jan 2019 15:55:41 +0100 Subject: [PATCH] Merge pull request #1414 from sharelatex/as-fix-pdf-link-referrer Fix links in PDF to have noreferrer GitOrigin-RevId: fa14e552b0d4947781c88ce22fc70696b04cf8a4 --- .../ide/pdfng/directives/pdfAnnotations.js | 138 ++++++++---------- 1 file changed, 57 insertions(+), 81 deletions(-) diff --git a/services/web/public/src/ide/pdfng/directives/pdfAnnotations.js b/services/web/public/src/ide/pdfng/directives/pdfAnnotations.js index 0b545f3de7..2bd4afe4a3 100644 --- a/services/web/public/src/ide/pdfng/directives/pdfAnnotations.js +++ b/services/web/public/src/ide/pdfng/directives/pdfAnnotations.js @@ -1,87 +1,63 @@ -/* eslint-disable - max-len, - no-return-assign, - no-undef, - no-unreachable, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -/* - * decaffeinate suggestions: - * DS101: Remove unnecessary use of Array.from - * DS102: Remove unnecessary code created because of implicit returns - * DS205: Consider reworking code to avoid use of IIFEs - * DS206: Consider reworking classes to avoid initClass - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md - */ -define(['base'], App => - // App = angular.module 'pdfAnnotations', [] - App.factory('pdfAnnotations', [ - function() { - let pdfAnnotations - return (pdfAnnotations = (function() { - pdfAnnotations = class pdfAnnotations { - static initClass() { - this.EXTERNAL_LINK_TARGET = '_blank' - } +/* global PDFJS */ - constructor(options) { - this.annotationsLayerDiv = options.annotations - this.viewport = options.viewport - this.navigateFn = options.navigateFn - } +define(['base'], App => { + const EXTERNAL_LINK_TARGET = '_blank' + const REL_NOOPENER = 'noreferrer noopener' - setAnnotations(annotations) { - return (() => { - const result = [] - for (let annotation of Array.from(annotations)) { - switch (annotation.subtype) { - case 'Link': - result.push(this.addLink(annotation)) - break - case 'Text': - continue - break - default: - result.push(undefined) - } - } - return result - })() - } + App.factory('pdfAnnotations', function() { + class pdfAnnotations { + constructor(options) { + this.annotationsLayerDiv = options.annotations + this.viewport = options.viewport + this.navigateFn = options.navigateFn + } - addLink(link) { - const element = this.buildLinkElementFromRect(link.rect) - this.setLinkTarget(element, link) - return this.annotationsLayerDiv.appendChild(element) - } - - buildLinkElementFromRect(rect) { - rect = this.viewport.convertToViewportRectangle(rect) - rect = PDFJS.Util.normalizeRect(rect) - const element = document.createElement('a') - element.style.left = Math.floor(rect[0]) + 'px' - element.style.top = Math.floor(rect[1]) + 'px' - element.style.width = Math.ceil(rect[2] - rect[0]) + 'px' - element.style.height = Math.ceil(rect[3] - rect[1]) + 'px' - return element - } - - setLinkTarget(element, link) { - if (link.url) { - element.href = link.url - return (element.target = this.EXTERNAL_LINK_TARGET) - } else if (link.dest) { - element.href = `#${link.dest}` - return (element.onclick = e => { - this.navigateFn(link) - return false - }) - } + setAnnotations(annotations) { + const result = [] + for (let annotation of annotations) { + switch (annotation.subtype) { + case 'Link': + result.push(this.addLink(annotation)) + break + case 'Text': + continue + default: + result.push(undefined) } } - pdfAnnotations.initClass() - return pdfAnnotations - })()) + } + + addLink(link) { + const element = this.buildLinkElementFromRect(link.rect) + this.setLinkTarget(element, link) + this.annotationsLayerDiv.appendChild(element) + } + + buildLinkElementFromRect(rect) { + rect = this.viewport.convertToViewportRectangle(rect) + rect = PDFJS.Util.normalizeRect(rect) + const element = document.createElement('a') + element.style.left = Math.floor(rect[0]) + 'px' + element.style.top = Math.floor(rect[1]) + 'px' + element.style.width = Math.ceil(rect[2] - rect[0]) + 'px' + element.style.height = Math.ceil(rect[3] - rect[1]) + 'px' + return element + } + + setLinkTarget(element, link) { + if (link.url) { + element.href = link.url + element.target = EXTERNAL_LINK_TARGET + element.rel = REL_NOOPENER + } else if (link.dest) { + element.href = `#${link.dest}` + element.onclick = () => { + this.navigateFn(link) + return false + } + } + } } - ])) + return pdfAnnotations + }) +})