Comparing version 0.3.0 to 0.3.1
/*! | ||
* AnchorJS - v0.3.0 - 2015-02-10 | ||
* AnchorJS - v0.3.1 - 2015-03-06 | ||
* https://github.com/bryanbraun/anchorjs | ||
@@ -19,3 +19,13 @@ * Copyright (c) 2015 Bryan Braun; Licensed MIT | ||
var elements = document.querySelectorAll(selector); | ||
if (elements.length === 0) { | ||
// Selector was valid but no elements were found. | ||
return false; | ||
} | ||
// Produce a list of existing IDs so we don't generate a duplicate. | ||
var elsWithIds = document.querySelectorAll('[id]'); | ||
var idList = [].map.call(elsWithIds, function assign(el) { | ||
return el.id; | ||
}); | ||
// Loop through the selected elements. | ||
@@ -28,3 +38,4 @@ for (var i = 0; i < elements.length; i++) { | ||
} else { | ||
// We need to create an ID on our element. First, we find which text selection method is available to the browser. | ||
// We need to create an ID on our element. First, we find which text | ||
// selection method is available to the browser. | ||
var textMethod = document.body.textContent ? 'textContent' : 'innerText'; | ||
@@ -43,9 +54,26 @@ | ||
// Compare our generated ID to existing IDs (and increment it if needed) | ||
// before we add it to the page. | ||
var index, | ||
count = 0, | ||
newTidyText = tidyText; | ||
do { | ||
if (index !== undefined) { | ||
newTidyText = tidyText + '-' + count; | ||
} | ||
// .indexOf is supported in IE9+. | ||
index = idList.indexOf(newTidyText); | ||
count += 1; | ||
} while (index !== -1); | ||
index = undefined; | ||
idList.push(newTidyText); | ||
// Assign it to our element. | ||
// Currently the setAttribute element is only supported in IE9 and above. | ||
elements[i].setAttribute('id', tidyText); | ||
elements[i].setAttribute('id', newTidyText); | ||
// Grab it for use in our anchor. | ||
elementID = tidyText; | ||
elementID = newTidyText; | ||
} | ||
var readableID = elementID.replace(/-/g, ' '); | ||
@@ -52,0 +80,0 @@ var anchor = '<a class="anchorjs-link" href="#' + elementID + '">' + |
/*! | ||
* AnchorJS - v0.3.0 - 2015-02-10 | ||
* AnchorJS - v0.3.1 - 2015-03-06 | ||
* https://github.com/bryanbraun/anchorjs | ||
* Copyright (c) 2015 Bryan Braun; Licensed MIT | ||
*/ | ||
function addAnchors(e){"use strict";if(e){if("string"!=typeof e)throw new Error("AnchorJS accepts only strings; you used a "+typeof e)}else e="h1, h2, h3, h4, h5, h6";for(var t=document.querySelectorAll(e),r=0;r<t.length;r++){var n;if(t[r].hasAttribute("id"))n=t[r].getAttribute("id");else{var s=document.body.textContent?"textContent":"innerText",a=t[r][s],o=a.replace(/[^\w\s-]/gi,"").replace(/\s+/g,"-").toLowerCase().substring(0,32);t[r].setAttribute("id",o),n=o}var i=n.replace(/-/g," "),c='<a class="anchorjs-link" href="#'+n+'"><span class="anchorjs-description">Anchor link for: '+i+'</span><span class="anchorjs-icon" aria-hidden="true"></span></a>';t[r].innerHTML+=c}} | ||
function addAnchors(e){"use strict";if(e){if("string"!=typeof e)throw new Error("AnchorJS accepts only strings; you used a "+typeof e)}else e="h1, h2, h3, h4, h5, h6";var t=document.querySelectorAll(e);if(0===t.length)return!1;for(var r=document.querySelectorAll("[id]"),n=[].map.call(r,function(e){return e.id}),i=0;i<t.length;i++){var s;if(t[i].hasAttribute("id"))s=t[i].getAttribute("id");else{var o,a=document.body.textContent?"textContent":"innerText",c=t[i][a],l=c.replace(/[^\w\s-]/gi,"").replace(/\s+/g,"-").toLowerCase().substring(0,32),h=0,d=l;do void 0!==o&&(d=l+"-"+h),o=n.indexOf(d),h+=1;while(-1!==o);o=void 0,n.push(d),t[i].setAttribute("id",d),s=d}var u=s.replace(/-/g," "),p='<a class="anchorjs-link" href="#'+s+'"><span class="anchorjs-description">Anchor link for: '+u+'</span><span class="anchorjs-icon" aria-hidden="true"></span></a>';t[i].innerHTML+=p}} |
{ | ||
"name": "anchor-js", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Bryan Braun" |
{ | ||
"name": "anchor-js", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"author": "bryanbraun (https://github.com/bryanbraun)", | ||
@@ -21,4 +21,4 @@ "description": "A tiny javscript utility for adding anchor links to existing page content.", | ||
"devDependencies": { | ||
"eslint": "~0.14.1", | ||
"jshint": "~2.6.0", | ||
"eslint": "~0.15.1", | ||
"jshint": "~2.6.3", | ||
"uglify-js": "~2.4.16" | ||
@@ -25,0 +25,0 @@ }, |
Sorry, the diff of this file is not supported yet
25778
179