Comparing version 2.0.0 to 2.1.0
/** | ||
* AnchorJS - v2.0.0 - 2015-10-31 | ||
* AnchorJS - v2.1.0 - 2016-01-23 | ||
* https://github.com/bryanbraun/anchorjs | ||
@@ -9,4 +9,2 @@ * Copyright (c) 2015 Bryan Braun; Licensed MIT | ||
'use strict'; | ||
var that = this; | ||
this.options = options || {}; | ||
@@ -19,13 +17,23 @@ | ||
function _applyRemainingDefaultOptions(opts) { | ||
that.options.icon = that.options.hasOwnProperty('icon') ? opts.icon : '\ue9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'. | ||
that.options.visible = that.options.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always' | ||
that.options.placement = that.options.hasOwnProperty('placement') ? opts.placement : 'right'; // Also accepts 'left' | ||
that.options.class = that.options.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name. | ||
opts.icon = opts.hasOwnProperty('icon') ? opts.icon : '\ue9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'. | ||
opts.visible = opts.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always' & 'touch' | ||
opts.placement = opts.hasOwnProperty('placement') ? opts.placement : 'right'; // Also accepts 'left' | ||
opts.class = opts.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name. | ||
// Using Math.floor here will ensure the value is Number-cast and an integer. | ||
that.options.truncate = that.options.hasOwnProperty('truncate') ? Math.floor(opts.truncate) : 64; // Accepts any value that can be typecast to a number. | ||
opts.truncate = opts.hasOwnProperty('truncate') ? Math.floor(opts.truncate) : 64; // Accepts any value that can be typecast to a number. | ||
opts.trim = opts.hasOwnProperty('trim') ? opts.trim : false; // Accepts any value evalutated for truth. | ||
} | ||
_applyRemainingDefaultOptions(options); | ||
_applyRemainingDefaultOptions(this.options); | ||
/** | ||
* Checks to see if this device supports touch. Uses criteria pulled from Modernizr: | ||
* https://github.com/Modernizr/Modernizr/blob/da22eb27631fc4957f67607fe6042e85c0a84656/feature-detects/touchevents.js#L40 | ||
* @return {Boolean} - true if the current device supports touch. | ||
*/ | ||
this.isTouchDevice = function() { | ||
return !!(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch); | ||
}; | ||
/** | ||
* Add anchor links to page elements. | ||
@@ -46,3 +54,4 @@ * @param {String} selector A CSS selector for targeting the elements you wish to add anchor links to. | ||
readableID, | ||
anchor; | ||
anchor, | ||
visibleOptionToUse; | ||
@@ -55,2 +64,7 @@ // We reapply options here because somebody may have overwritten the default options object when setting options. | ||
visibleOptionToUse = this.options.visible; | ||
if (visibleOptionToUse === 'touch') { | ||
visibleOptionToUse = this.isTouchDevice() ? 'always' : 'hover'; | ||
} | ||
// Provide a sensible default selector, if none is given. | ||
@@ -116,3 +130,3 @@ if (!selector) { | ||
if (this.options.visible === 'always') { | ||
if (visibleOptionToUse === 'always') { | ||
anchor.style.opacity = '1'; | ||
@@ -175,2 +189,3 @@ } | ||
* @param {String} text - Any text. Usually pulled from the webpage element we are linking to. | ||
* @param {?Object} options - Optional option overrides. | ||
* @return {String} - hyphen-delimited text for use in IDs and URLs. | ||
@@ -183,2 +198,4 @@ */ | ||
// The reason we include this _applyRemainingDefaultOptions is so urlify can be called independently, | ||
// even after setting options. This can be useful for tests or other applications. | ||
if (!this.options.truncate) { | ||
@@ -188,2 +205,6 @@ _applyRemainingDefaultOptions(this.options); | ||
if (this.options.trim) { | ||
text = text.trim(); | ||
} | ||
// Note: we trim hyphens after truncating because truncating can cause dangling hyphens. | ||
@@ -190,0 +211,0 @@ // Example string: // " ⚡ Don't forget: URL fragments should be i18n-friendly, hyphenated, short, and clean." |
/** | ||
* AnchorJS - v2.0.0 - 2015-10-31 | ||
* AnchorJS - v2.1.0 - 2016-01-23 | ||
* https://github.com/bryanbraun/anchorjs | ||
* Copyright (c) 2015 Bryan Braun; Licensed MIT | ||
*/ | ||
function AnchorJS(A){"use strict";function t(A){o.options.icon=o.options.hasOwnProperty("icon")?A.icon:"",o.options.visible=o.options.hasOwnProperty("visible")?A.visible:"hover",o.options.placement=o.options.hasOwnProperty("placement")?A.placement:"right",o.options.class=o.options.hasOwnProperty("class")?A.class:"",o.options.truncate=o.options.hasOwnProperty("truncate")?Math.floor(A.truncate):64}function e(){if(null===document.head.querySelector("style.anchorjs")){var A,t=document.createElement("style"),e=" .anchorjs-link { opacity: 0; text-decoration: none; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }",o=" *:hover > .anchorjs-link, .anchorjs-link:focus { opacity: 1; }",n=' @font-face { font-family: "anchorjs-icons"; font-style: normal; font-weight: normal; src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMg8SBTUAAAC8AAAAYGNtYXAWi9QdAAABHAAAAFRnYXNwAAAAEAAAAXAAAAAIZ2x5Zgq29TcAAAF4AAABNGhlYWQEZM3pAAACrAAAADZoaGVhBhUDxgAAAuQAAAAkaG10eASAADEAAAMIAAAAFGxvY2EAKACuAAADHAAAAAxtYXhwAAgAVwAAAygAAAAgbmFtZQ5yJ3cAAANIAAAB2nBvc3QAAwAAAAAFJAAAACAAAwJAAZAABQAAApkCzAAAAI8CmQLMAAAB6wAzAQkAAAAAAAAAAAAAAAAAAAABEAAAAAAAAAAAAAAAAAAAAABAAADpywPA/8AAQAPAAEAAAAABAAAAAAAAAAAAAAAgAAAAAAADAAAAAwAAABwAAQADAAAAHAADAAEAAAAcAAQAOAAAAAoACAACAAIAAQAg6cv//f//AAAAAAAg6cv//f//AAH/4xY5AAMAAQAAAAAAAAAAAAAAAQAB//8ADwABAAAAAAAAAAAAAgAANzkBAAAAAAEAAAAAAAAAAAACAAA3OQEAAAAAAQAAAAAAAAAAAAIAADc5AQAAAAACADEARAJTAsAAKwBUAAABIiYnJjQ/AT4BMzIWFxYUDwEGIicmND8BNjQnLgEjIgYPAQYUFxYUBw4BIwciJicmND8BNjIXFhQPAQYUFx4BMzI2PwE2NCcmNDc2MhcWFA8BDgEjARQGDAUtLXoWOR8fORYtLTgKGwoKCjgaG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content: attr(data-anchorjs-icon); }";t.className="anchorjs",t.appendChild(document.createTextNode("")),A=document.head.querySelector('[rel="stylesheet"], style'),void 0===A?document.head.appendChild(t):document.head.insertBefore(t,A),t.sheet.insertRule(e,t.sheet.cssRules.length),t.sheet.insertRule(o,t.sheet.cssRules.length),t.sheet.insertRule(i,t.sheet.cssRules.length),t.sheet.insertRule(n,t.sheet.cssRules.length)}}var o=this;this.options=A||{},t(A),this.add=function(A){var o,n,i,s,a,r,l,c,h,g,B,u;if(t(this.options),A){if("string"!=typeof A)throw new Error("The selector provided to AnchorJS was invalid.")}else A="h1, h2, h3, h4, h5, h6";if(o=document.querySelectorAll(A),0===o.length)return!1;for(e(),n=document.querySelectorAll("[id]"),i=[].map.call(n,function(A){return A.id}),a=0;a<o.length;a++){if(o[a].hasAttribute("id"))s=o[a].getAttribute("id");else{r=o[a].textContent,l=this.urlify(r),g=l,h=0;do void 0!==c&&(g=l+"-"+h),c=i.indexOf(g),h+=1;while(-1!==c);c=void 0,i.push(g),o[a].setAttribute("id",g),s=g}B=s.replace(/-/g," "),u=document.createElement("a"),u.className="anchorjs-link "+this.options.class,u.href="#"+s,u.setAttribute("aria-label","Anchor link for: "+B),u.setAttribute("data-anchorjs-icon",this.options.icon),"always"===this.options.visible&&(u.style.opacity="1"),""===this.options.icon&&(u.style.fontFamily="anchorjs-icons",u.style.fontStyle="normal",u.style.fontVariant="normal",u.style.fontWeight="normal",u.style.lineHeight=1,"left"===this.options.placement&&(u.style.lineHeight="inherit")),"left"===this.options.placement?(u.style.position="absolute",u.style.marginLeft="-1em",u.style.paddingRight="0.5em",o[a].insertBefore(u,o[a].firstChild)):(u.style.paddingLeft="0.375em",o[a].appendChild(u))}return this},this.remove=function(A){for(var t,e=document.querySelectorAll(A),o=0;o<e.length;o++)t=e[o].querySelector(".anchorjs-link"),t&&e[o].removeChild(t);return this},this.urlify=function(A){var e,o=/[& +$,:;=?@"#{}|^~[`%!'\]\.\/\(\)\*\\]/g;return t(this.options),e=A.replace(/\'/gi,"").replace(o,"-").replace(/-{2,}/g,"-").substring(0,this.options.truncate).replace(/^-+|-+$/gm,"").toLowerCase()}}var anchors=new AnchorJS; | ||
function AnchorJS(A){"use strict";function t(A){A.icon=A.hasOwnProperty("icon")?A.icon:"",A.visible=A.hasOwnProperty("visible")?A.visible:"hover",A.placement=A.hasOwnProperty("placement")?A.placement:"right",A.class=A.hasOwnProperty("class")?A.class:"",A.truncate=A.hasOwnProperty("truncate")?Math.floor(A.truncate):64,A.trim=A.hasOwnProperty("trim")?A.trim:!1}function e(){if(null===document.head.querySelector("style.anchorjs")){var A,t=document.createElement("style"),e=" .anchorjs-link { opacity: 0; text-decoration: none; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }",o=" *:hover > .anchorjs-link, .anchorjs-link:focus { opacity: 1; }",n=' @font-face { font-family: "anchorjs-icons"; font-style: normal; font-weight: normal; src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMg8SBTUAAAC8AAAAYGNtYXAWi9QdAAABHAAAAFRnYXNwAAAAEAAAAXAAAAAIZ2x5Zgq29TcAAAF4AAABNGhlYWQEZM3pAAACrAAAADZoaGVhBhUDxgAAAuQAAAAkaG10eASAADEAAAMIAAAAFGxvY2EAKACuAAADHAAAAAxtYXhwAAgAVwAAAygAAAAgbmFtZQ5yJ3cAAANIAAAB2nBvc3QAAwAAAAAFJAAAACAAAwJAAZAABQAAApkCzAAAAI8CmQLMAAAB6wAzAQkAAAAAAAAAAAAAAAAAAAABEAAAAAAAAAAAAAAAAAAAAABAAADpywPA/8AAQAPAAEAAAAABAAAAAAAAAAAAAAAgAAAAAAADAAAAAwAAABwAAQADAAAAHAADAAEAAAAcAAQAOAAAAAoACAACAAIAAQAg6cv//f//AAAAAAAg6cv//f//AAH/4xY5AAMAAQAAAAAAAAAAAAAAAQAB//8ADwABAAAAAAAAAAAAAgAANzkBAAAAAAEAAAAAAAAAAAACAAA3OQEAAAAAAQAAAA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m9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype"); }',i=" [data-anchorjs-icon]::after { content: attr(data-anchorjs-icon); }";t.className="anchorjs",t.appendChild(document.createTextNode("")),A=document.head.querySelector('[rel="stylesheet"], style'),void 0===A?document.head.appendChild(t):document.head.insertBefore(t,A),t.sheet.insertRule(e,t.sheet.cssRules.length),t.sheet.insertRule(o,t.sheet.cssRules.length),t.sheet.insertRule(i,t.sheet.cssRules.length),t.sheet.insertRule(n,t.sheet.cssRules.length)}}this.options=A||{},t(this.options),this.isTouchDevice=function(){return!!("ontouchstart"in window||window.DocumentTouch&&document instanceof DocumentTouch)},this.add=function(A){var o,n,i,s,a,r,c,h,l,g,u,B;if(t(this.options),B=this.options.visible,"touch"===B&&(B=this.isTouchDevice()?"always":"hover"),A){if("string"!=typeof A)throw new Error("The selector provided to AnchorJS was invalid.")}else A="h1, h2, h3, h4, h5, h6";if(o=document.querySelectorAll(A),0===o.length)return!1;for(e(),n=document.querySelectorAll("[id]"),i=[].map.call(n,function(A){return A.id}),a=0;a<o.length;a++){if(o[a].hasAttribute("id"))s=o[a].getAttribute("id");else{h=this.urlify(o[a].textContent),l=h,c=0;do void 0!==r&&(l=h+"-"+c),r=i.indexOf(l),c+=1;while(-1!==r);r=void 0,i.push(l),o[a].setAttribute("id",l),s=l}g=s.replace(/-/g," "),u=document.createElement("a"),u.className="anchorjs-link "+this.options.class,u.href="#"+s,u.setAttribute("aria-label","Anchor link for: "+g),u.setAttribute("data-anchorjs-icon",this.options.icon),"always"===B&&(u.style.opacity="1"),""===this.options.icon&&(u.style.fontFamily="anchorjs-icons",u.style.fontStyle="normal",u.style.fontVariant="normal",u.style.fontWeight="normal",u.style.lineHeight=1,"left"===this.options.placement&&(u.style.lineHeight="inherit")),"left"===this.options.placement?(u.style.position="absolute",u.style.marginLeft="-1em",u.style.paddingRight="0.5em",o[a].insertBefore(u,o[a].firstChild)):(u.style.paddingLeft="0.375em",o[a].appendChild(u))}return this},this.remove=function(A){for(var t,e=document.querySelectorAll(A),o=0;o<e.length;o++)t=e[o].querySelector(".anchorjs-link"),t&&e[o].removeChild(t);return this},this.urlify=function(A){var e,o=/[& +$,:;=?@"#{}|^~[`%!'\]\.\/\(\)\*\\]/g;return this.options.truncate||t(this.options),this.options.trim&&(A=A.trim()),e=A.replace(/\'/gi,"").replace(o,"-").replace(/-{2,}/g,"-").substring(0,this.options.truncate).replace(/^-+|-+$/gm,"").toLowerCase()}}var anchors=new AnchorJS; |
{ | ||
"name": "anchor-js", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Bryan Braun" |
{ | ||
"name": "anchor-js", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "bryanbraun (https://github.com/bryanbraun)", | ||
@@ -5,0 +5,0 @@ "description": "A Javscript utility for adding deep anchor links to online docs.", |
@@ -31,3 +31,3 @@ # AnchorJS [![Build Status](https://img.shields.io/travis/bryanbraun/anchorjs/master.svg?style=flat)](https://travis-ci.org/bryanbraun/anchorjs) | ||
You could also include it via a CDN like [CDNJS](https://cdnjs.com/libraries/anchor-js). | ||
You could also include it via a CDN like [CDNJS](https://cdnjs.com/libraries/anchor-js) or [jsDelivr](http://www.jsdelivr.com/projects/anchorjs). | ||
@@ -34,0 +34,0 @@ ##Usage |
@@ -72,7 +72,4 @@ /* eslint-env jasmine */ | ||
it('does not destroy default options when setting an incomplete options object.', function() { | ||
var anchorObj, | ||
anchorLink, | ||
hasClass, | ||
opacity, | ||
icon; | ||
var anchorObj; | ||
anchors.options = { class: 'test-class' }; | ||
@@ -251,2 +248,15 @@ anchors.add(); | ||
it('optionally trims whitespace characters when making text URL-friendly', function() { | ||
var text1Before = '\n abc\r', | ||
text2Before = 'abc ', | ||
text3Before = 'abc\n ', | ||
after = 'abc'; | ||
anchors.options.trim = true; | ||
expect(anchors.urlify(text1Before)).toEqual(after); | ||
expect(anchors.urlify(text2Before)).toEqual(after); | ||
expect(anchors.urlify(text3Before)).toEqual(after); | ||
}); | ||
it('removes apostrophes when making text URL-friendly', function() { | ||
@@ -280,2 +290,24 @@ var before = 'don\'t', | ||
}); | ||
describe('touch option', function() { | ||
beforeEach(function() { | ||
anchors.options.visible = 'touch'; | ||
}); | ||
it('invokes the `always` behavior for touch devices', function() { | ||
var opacity; | ||
spyOn(anchors, 'isTouchDevice').and.returnValue(true); | ||
anchors.add('h1'); | ||
opacity = document.querySelector('.anchorjs-link').style.opacity; | ||
expect(opacity).toEqual('1'); | ||
}); | ||
it('invokes the `hover` behavior for non-touch devices', function() { | ||
var opacity; | ||
spyOn(anchors, 'isTouchDevice').and.returnValue(false); | ||
anchors.add('h1'); | ||
opacity = document.querySelector('.anchorjs-link').style.opacity; | ||
expect(opacity).toEqual(''); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
49302
596