From 860758253a176c431653e90a5a9b5ff33e3deacd Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Fri, 7 Nov 2014 13:00:30 +0100 Subject: [PATCH] v1.0.6 - Show anchor at left of text if possible. --- extension/manifest.json | 2 +- extension/toggle-anchors.js | 2 +- test/clipped-container.html | 28 +++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/extension/manifest.json b/extension/manifest.json index 0361e58..9aef390 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -1,7 +1,7 @@ { "name": "Display #Anchors", "description": "Displays anchors for all content in the current web page without breaking the layout.", - "version": "1.0.5", + "version": "1.0.6", "manifest_version": 2, "background": { "scripts": ["background.js"], diff --git a/extension/toggle-anchors.js b/extension/toggle-anchors.js index 114fcb6..e6c344d 100644 --- a/extension/toggle-anchors.js +++ b/extension/toggle-anchors.js @@ -83,7 +83,7 @@ function getAnchor(anchorValue, elem) { } else { var paddingLeft = parseFloat(currentStyle.getPropertyValue('padding-left')) || 0; var borderLeft = parseFloat(currentStyle.getPropertyValue('border-left-width')) || 0; - var visibleHorizontalSpace = elem.offsetWidth - paddingLeft - borderLeft; + var visibleHorizontalSpace = elem.offsetLeft + elem.offsetWidth - paddingLeft - borderLeft; if (visibleHorizontalSpace < MINIMUM_REQ_WIDTH_PX) { anchor.style.left = '0'; anchor.style.right = 'auto'; diff --git a/test/clipped-container.html b/test/clipped-container.html index 4388727..244fb8f 100644 --- a/test/clipped-container.html +++ b/test/clipped-container.html @@ -27,6 +27,14 @@

C. Header with ancho

Static header

+ + +
+
    +
  • List item
  • +
+
+
@@ -48,19 +56,37 @@

Static header

var elemAtPos = document.elementFromPoint(Math.max(0, rect.left), Math.max(0, rect.top + 2)); assertEqual(elemAtPos, ahrefElement); } +/** + * @param {Element} elem - Parent node of <:a.href:> element + */ +function assertAnchorIsAtLeft(elem) { + var elemLeftOffset = elem.getBoundingClientRect().left; + var ahrefElement = elem.querySelector('\\:a\\.href\\:'); + var anchorNodeShadow = ahrefElement.shadowRoot || ahrefElement.webkitShadowRoot; + var shadowLeftOffset = anchorNodeShadow.querySelector('a').getBoundingClientRect().left; + + console.assert(shadowLeftOffset < elemLeftOffset, + '<:a.href:> must be at the left of the target element, ' + + shadowLeftOffset + ' < ' + elemLeftOffset); +} var anchors = document.querySelectorAll('\\:a\\.href\\:'); // Sanity check -assertEqual(anchors.length, 3); +assertEqual(anchors.length, 4); assertEqual(anchors[0].parentNode, document.getElementById('some-anchor')); assertEqual(anchors[1].parentNode, document.getElementById('visible-anchor')); assertEqual(anchors[2].parentNode, document.getElementById('static-header')); +assertEqual(anchors[3].parentNode, document.getElementById('before-li')); // Assert visibility of element. If any of the following test fail, then the :a.href: > a element is not visible. assertAnchorVisible(anchors[0]); assertAnchorVisible(anchors[1]); assertAnchorVisible(anchors[2]); +assertAnchorVisible(anchors[3]); + +// Anchor must be displayed at the left of the list item. +assertAnchorIsAtLeft(document.getElementById('before-li')); if (!location.hash) document.body.scrollTop = 0;