New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

anchor-js

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

anchor-js - npm Package Compare versions

Comparing version 0.4.0 to 1.0.0

test/config.js

243

anchor.js
/*!
* AnchorJS - v0.4.0 - 2015-04-20
* AnchorJS - v1.0.0 - 2015-05-14
* https://github.com/bryanbraun/anchorjs

@@ -7,82 +7,191 @@ * Copyright (c) 2015 Bryan Braun; Licensed MIT

function addAnchors(selector) {
function AnchorJS(options) {
'use strict';
// Sensible default selector, if none is provided.
if (!selector) {
selector = 'h1, h2, h3, h4, h5, h6';
} else if (typeof selector !== 'string') {
throw new Error('AnchorJS accepts only strings; you used a ' + typeof selector);
}
this.options = options || {};
// Select any elements that match the provided selector.
var elements = document.querySelectorAll(selector);
if (elements.length === 0) {
// Selector was valid but no elements were found.
return false;
}
this._applyRemainingDefaultOptions = function(opts) {
this.options.icon = this.options.hasOwnProperty('icon') ? opts.icon : '&#xe9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
this.options.visible = this.options.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always'
this.options.placement = this.options.hasOwnProperty('placement') ? opts.placement : 'right'; // Also accepts 'left'
this.options.class = this.options.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name.
};
// 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;
});
this._applyRemainingDefaultOptions(options);
// Loop through the selected elements.
for (var i = 0; i < elements.length; i++) {
var elementID;
this.add = function(selector) {
var elements,
elsWithIds,
idList,
elementID,
i,
roughText,
tidyText,
index,
count,
newTidyText,
readableID,
anchor,
div,
anchorNodes;
if (elements[i].hasAttribute('id')) {
elementID = elements[i].getAttribute('id');
} else {
// 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';
this._applyRemainingDefaultOptions(this.options);
// Get the text inside our element
var roughText = elements[i][textMethod];
// Provide a sensible default selector, if none is given.
if (!selector) {
selector = 'h1, h2, h3, h4, h5, h6';
} else if (typeof selector !== 'string') {
throw new Error('The selector provided to AnchorJS was invalid.');
}
// Refine it so it makes a good ID. Strip out non-safe characters, replace
// spaces with hyphens, truncate to 32 characters, and make toLowerCase.
//
// Example string: // "⚡⚡⚡ Unicode icons are cool--but don't belong in a URL."
var tidyText = roughText.replace(/[^\w\s-]/gi, '') // " Unicode icons are cool--but dont belong in a URL"
.replace(/\s+/g, '-') // "-Unicode-icons-are-cool--but-dont-belong-in-a-URL"
.replace(/-{2,}/g, '-') // "-Unicode-icons-are-cool-but-dont-belong-in-a-URL"
.substring(0, 32) // "-Unicode-icons-are-cool-but-dont"
.replace(/^-+|-+$/gm, '') // "Unicode-icons-are-cool-but-dont"
.toLowerCase(); // "unicode-icons-are-cool-but-dont"
elements = document.querySelectorAll(selector);
if (elements.length === 0) {
return false;
}
// 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);
this._addBaselineStyles();
// Assign it to our element.
// Currently the setAttribute element is only supported in IE9 and above.
elements[i].setAttribute('id', newTidyText);
// We produce a list of existing IDs so we don't generate a duplicate.
elsWithIds = document.querySelectorAll('[id]');
idList = [].map.call(elsWithIds, function assign(el) {
return el.id;
});
// Grab it for use in our anchor.
elementID = newTidyText;
for (i = 0; i < elements.length; i++) {
if (elements[i].hasAttribute('id')) {
elementID = elements[i].getAttribute('id');
} else {
roughText = elements[i].textContent;
// Refine it so it makes a good ID. Strip out non-safe characters, replace
// spaces with hyphens, truncate to 32 characters, and make toLowerCase.
//
// Example string: // '⚡⚡⚡ Unicode icons are cool--but don't belong in a URL.'
tidyText = roughText.replace(/[^\w\s-]/gi, '') // ' Unicode icons are cool--but dont belong in a URL'
.replace(/\s+/g, '-') // '-Unicode-icons-are-cool--but-dont-belong-in-a-URL'
.replace(/-{2,}/g, '-') // '-Unicode-icons-are-cool-but-dont-belong-in-a-URL'
.substring(0, 32) // '-Unicode-icons-are-cool-but-dont'
.replace(/^-+|-+$/gm, '') // 'Unicode-icons-are-cool-but-dont'
.toLowerCase(); // 'unicode-icons-are-cool-but-dont'
// Compare our generated ID to existing IDs (and increment it if needed)
// before we add it to the page.
newTidyText = tidyText;
count = 0;
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', newTidyText);
elementID = newTidyText;
}
readableID = elementID.replace(/-/g, ' ');
anchor = '<a class="anchorjs-link ' + this.options.class + '" href="#' + elementID + '" aria-label="Anchor link for: ' + readableID + '" data-anchorjs-icon="' + this.options.icon + '"></a>';
div = document.createElement('div');
div.innerHTML = anchor;
anchorNodes = div.childNodes;
if (this.options.visible === 'always') {
anchorNodes[0].style.opacity = '1';
}
if (this.options.icon === '&#xe9cb') {
anchorNodes[0].style.fontFamily = 'anchorjs-icons';
anchorNodes[0].style.fontStyle = 'normal';
anchorNodes[0].style.fontVariant = 'normal';
anchorNodes[0].style.fontWeight = 'normal';
}
if (this.options.placement === 'left') {
anchorNodes[0].style.position = 'absolute';
anchorNodes[0].style.marginLeft = '-1em';
anchorNodes[0].style.paddingRight = '0.5em';
elements[i].insertBefore(anchorNodes[0], elements[i].firstChild);
} else { // if the option provided is `right` (or anything else).
anchorNodes[0].style.paddingLeft = '0.375em';
elements[i].appendChild(anchorNodes[0]);
}
}
var readableID = elementID.replace(/-/g, ' ');
var anchor = '<a class="anchorjs-link" href="#' + elementID + '">' +
'<span class="anchorjs-description">Anchor link for: ' + readableID + '</span>' +
'<span class="anchorjs-icon" aria-hidden="true"></span>' +
'</a>';
return this;
};
elements[i].innerHTML += anchor;
}
this.remove = function(selector) {
var domAnchor,
elements = document.querySelectorAll(selector);
for (var i = 0; i < elements.length; i++) {
domAnchor = elements[i].querySelector('.anchorjs-link');
if (domAnchor) {
elements[i].removeChild(domAnchor);
}
}
return this;
};
this._addBaselineStyles = function() {
// We don't want to add global baseline styles if they've been added before.
if (document.head.querySelector('style.anchorjs') !== null) {
return;
}
var style = document.createElement('style'),
linkRule =
' .anchorjs-link {' +
' opacity: 0;' +
' text-decoration: none;' +
' -webkit-font-smoothing: antialiased;' +
' -moz-osx-font-smoothing: grayscale;' +
' }',
hoverRule =
' *:hover > .anchorjs-link,' +
' .anchorjs-link:focus {' +
' opacity: 1;' +
' }',
anchorjsLinkFontFace =
' @font-face {' +
' font-family: "anchorjs-icons";' +
' font-style: normal;' +
' font-weight: normal;' + // Icon from icomoon; 10px wide & 10px tall; 2 empty below & 4 above
' src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMg8SBTUAAAC8AAAAYGNtYXAWi9QdAAABHAAAAFRnYXNwAAAAEAAAAXAAAAAIZ2x5Zgq29TcAAAF4AAABNGhlYWQEZM3pAAACrAAAADZoaGVhBhUDxgAAAuQAAAAkaG10eASAADEAAAMIAAAAFGxvY2EAKACuAAADHAAAAAxtYXhwAAgAVwAAAygAAAAgbmFtZQ5yJ3cAAANIAAAB2nBvc3QAAwAAAAAFJAAAACAAAwJAAZAABQAAApkCzAAAAI8CmQLMAAAB6wAzAQkAAAAAAAAAAAAAAAAAAAABEAAAAAAAAAAAAAAAAAAAAABAAADpywPA/8AAQAPAAEAAAAABAAAAAAAAAAAAAAAgAAAAAAADAAAAAwAAABwAAQADAAAAHAADAAEAAAAcAAQAOAAAAAoACAACAAIAAQAg6cv//f//AAAAAAAg6cv//f//AAH/4xY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G8AbgBzUmVndWxhcgBSAGUAZwB1AGwAYQByYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype");' +
' }',
pseudoElContent =
' [data-anchorjs-icon]::after {' +
' content: attr(data-anchorjs-icon);' +
' }',
firstStyleEl;
style.className = 'anchorjs';
style.appendChild(document.createTextNode('')); // Necessary for Webkit.
// We place it in the head with the other style tags, if possible, so as to
// not look out of place. We insert before the others so these styles can be
// overridden if necessary.
firstStyleEl = document.head.getElementsByTagName('style')[0];
if (firstStyleEl === undefined) {
document.head.appendChild(style);
} else {
document.head.insertBefore(style, firstStyleEl);
}
style.sheet.insertRule(linkRule, style.sheet.cssRules.length);
style.sheet.insertRule(hoverRule, style.sheet.cssRules.length);
style.sheet.insertRule(pseudoElContent, style.sheet.cssRules.length);
style.sheet.insertRule(anchorjsLinkFontFace, style.sheet.cssRules.length);
};
}
var anchors = new AnchorJS();
/*!
* AnchorJS - v0.4.0 - 2015-04-20
* AnchorJS - v1.0.0 - 2015-05-14
* 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";var r=document.querySelectorAll(e);if(0===r.length)return!1;for(var t=document.querySelectorAll("[id]"),n=[].map.call(t,function(e){return e.id}),i=0;i<r.length;i++){var s;if(r[i].hasAttribute("id"))s=r[i].getAttribute("id");else{var a,o=document.body.textContent?"textContent":"innerText",c=r[i][o],l=c.replace(/[^\w\s-]/gi,"").replace(/\s+/g,"-").replace(/-{2,}/g,"-").substring(0,32).replace(/^-+|-+$/gm,"").toLowerCase(),h=0,d=l;do void 0!==a&&(d=l+"-"+h),a=n.indexOf(d),h+=1;while(-1!==a);a=void 0,n.push(d),r[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>';r[i].innerHTML+=p}}
function AnchorJS(A){"use strict";this.options=A||{},this._applyRemainingDefaultOptions=function(A){this.options.icon=this.options.hasOwnProperty("icon")?A.icon:"&#xe9cb",this.options.visible=this.options.hasOwnProperty("visible")?A.visible:"hover",this.options.placement=this.options.hasOwnProperty("placement")?A.placement:"right",this.options.class=this.options.hasOwnProperty("class")?A.class:""},this._applyRemainingDefaultOptions(A),this.add=function(A){var e,t,n,o,i,s,a,l,c,h,r,g,B,Q;if(this._applyRemainingDefaultOptions(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(e=document.querySelectorAll(A),0===e.length)return!1;for(this._addBaselineStyles(),t=document.querySelectorAll("[id]"),n=[].map.call(t,function(A){return A.id}),i=0;i<e.length;i++){if(e[i].hasAttribute("id"))o=e[i].getAttribute("id");else{s=e[i].textContent,a=s.replace(/[^\w\s-]/gi,"").replace(/\s+/g,"-").replace(/-{2,}/g,"-").substring(0,32).replace(/^-+|-+$/gm,"").toLowerCase(),h=a,c=0;do void 0!==l&&(h=a+"-"+c),l=n.indexOf(h),c+=1;while(-1!==l);l=void 0,n.push(h),e[i].setAttribute("id",h),o=h}r=o.replace(/-/g," "),g='<a class="anchorjs-link '+this.options.class+'" href="#'+o+'" aria-label="Anchor link for: '+r+'" data-anchorjs-icon="'+this.options.icon+'"></a>',B=document.createElement("div"),B.innerHTML=g,Q=B.childNodes,"always"===this.options.visible&&(Q[0].style.opacity="1"),"&#xe9cb"===this.options.icon&&(Q[0].style.fontFamily="anchorjs-icons",Q[0].style.fontStyle="normal",Q[0].style.fontVariant="normal",Q[0].style.fontWeight="normal"),"left"===this.options.placement?(Q[0].style.position="absolute",Q[0].style.marginLeft="-1em",Q[0].style.paddingRight="0.5em",e[i].insertBefore(Q[0],e[i].firstChild)):(Q[0].style.paddingLeft="0.375em",e[i].appendChild(Q[0]))}return this},this.remove=function(A){for(var e,t=document.querySelectorAll(A),n=0;n<t.length;n++)e=t[n].querySelector(".anchorjs-link"),e&&t[n].removeChild(e);return this},this._addBaselineStyles=function(){if(null===document.head.querySelector("style.anchorjs")){var A,e=document.createElement("style"),t=" .anchorjs-link { opacity: 0; text-decoration: none; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }",n=" *:hover > .anchorjs-link, .anchorjs-link:focus { opacity: 1; }",o=' @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//8ADwABAAAAAAAAAAAAAgAANzkBAAAAAAEAAAAAAAAAAAACAAA3OQEAAAAAAQAAAAAAAAAAAAIAADc5AQAAAAACADEARAJTAsAAKwBUAAABIi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AG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype"); }',i=" [data-anchorjs-icon]::after { content: attr(data-anchorjs-icon); }";e.className="anchorjs",e.appendChild(document.createTextNode("")),A=document.head.getElementsByTagName("style")[0],void 0===A?document.head.appendChild(e):document.head.insertBefore(e,A),e.sheet.insertRule(t,e.sheet.cssRules.length),e.sheet.insertRule(n,e.sheet.cssRules.length),e.sheet.insertRule(i,e.sheet.cssRules.length),e.sheet.insertRule(o,e.sheet.cssRules.length)}}}var anchors=new AnchorJS;
{
"name": "anchor-js",
"version": "0.4.0",
"version": "1.0.0",
"authors": [
"Bryan Braun"
],
"description": "A JavaScript utility for adding deep anchor links to page content.",
"description": "A JavaScript utility for adding deep anchor links to online docs and web content.",
"main": "anchor.js",

@@ -9,0 +9,0 @@ "license": "MIT",

{
"name": "anchor-js",
"version": "0.4.0",
"version": "1.0.0",
"author": "bryanbraun (https://github.com/bryanbraun)",
"description": "A tiny javscript utility for adding anchor links to existing page content.",
"description": "A Javscript utility for adding deep anchor links to online docs and web content.",
"homepage": "https://github.com/bryanbraun/anchorjs",

@@ -16,10 +16,15 @@ "repository": {

"scripts": {
"lint": "jshint anchor.js && eslint anchor.js",
"lint": "eslint anchor.js",
"jasmine": "karma start test/config.js --single-run",
"release": "uglifyjs anchor.js --compress --mangle --screw-ie8 --comments \"/Copyright/\" -o anchor.min.js",
"test": "npm run lint"
"test": "npm run lint && npm run jasmine"
},
"devDependencies": {
"eslint": "~0.19.0",
"jshint": "~2.7.0",
"uglify-js": "~2.4.16"
"eslint": "~0.21.0",
"jasmine-core": "~2.3.0",
"karma": "~0.12.31",
"karma-cli": "0.0.4",
"karma-jasmine": "~0.3.5",
"karma-phantomjs-launcher": "~0.1.4",
"uglify-js": "~2.4.21"
},

@@ -26,0 +31,0 @@ "engines": {

@@ -1,28 +0,27 @@

# AnchorJS [![Build Status](https://img.shields.io/travis/bryanbraun/anchorjs/gh-pages.svg?style=flat)](https://travis-ci.org/bryanbraun/anchorjs) [![devDependency Status](https://img.shields.io/david/dev/bryanbraun/anchorjs.svg?style=flat)](https://david-dm.org/bryanbraun/anchorjs#info=devDependencies)
# AnchorJS [![Build Status](https://img.shields.io/travis/bryanbraun/anchorjs/master.svg?style=flat)](https://travis-ci.org/bryanbraun/anchorjs)
![AnchorJS logo](http://bryanbraun.com/sites/default/files/anchorjs_logo.png)
A JavaScript utility for adding deep anchor links ([like these](http://ux.stackexchange.com/questions/36304/use-of-mouse-over-paragraph-marker-in-headlines-for-permalink)) to existing page content. AnchorJS is lightweight, accessible, and has no dependencies.
A tiny JavaScript utility for adding deep anchor links ([like these](http://ux.stackexchange.com/questions/36304/use-of-mouse-over-paragraph-marker-in-headlines-for-permalink)) to existing page content.
**[See Live Examples in the Documentation](http://bryanbraun.github.io/anchorjs#examples).**
![Anchoring links](http://bryanbraun.com/sites/default/files/anchoring-links_0.png)
By default, AnchorJS displays a link icon appended to an element which is made visible on hover.
## Installation
**[See the demo for an example](http://bryanbraun.github.io/anchorjs/).**
Download AnchorJS using npm,
Deep links are useful for linking to specific places in online books and documentation (like, for example, this README file when rendered on the Github project page--hover over headings to see what I mean).
```bash
npm install anchor-js
```
## Installation
or bower:
Download AnchorJS via npm:
```bash
bower install anchor-js --save-dev
```
npm install anchor-js
(or just [download it from github](https://github.com/bryanbraun/anchorjs/releases)).
or alternatively via bower:
Then include the anchor.js file (or anchor.min.js) in your webpage.
bower install anchor-js --save-dev
## Including AnchorJS
Include the anchor.js file (or alternatively anchor.min.js) in your webpage.
```html

@@ -32,45 +31,24 @@ <script src="anchor.js"></script>

For the default anchor link styling (demonstrated in the [demo](http://bryanbraun.github.io/anchorjs/)) you should also include anchor.css.
You could also include it via a CDN like [CDNJS](https://cdnjs.com/libraries/anchor-js).
```html
<link rel="stylesheet" href="anchor.css">
```
##Usage
See **[the Documentation](http://bryanbraun.github.io/anchorjs#basic-usage)** for detailed instructions.
Alternatively, you can provide your own styling.
## Using AnchorJS
AnchorJS provides the `addAnchors()` method for adding anchors to the page. This method accepts a selector as a parameter in the form of a string. The selector can be used to target specific elements that you want to add anchors to. Here's an example.
```js
/**
* Example 1
* Add anchors to all h1's on the page
*/
addAnchors('h1');
/**
* Example 2
* Adds anchors to elements that have been assigned the class '.anchored'
*/
addAnchors('.anchored');
/**
* Example 3
* Adds anchors to all h1, h2, & h3's inside a container with an ID of '#post'
*/
addAnchors('#post h1, #post h2, #post h3');
/**
* Example 4
* If no selector is provided, it falls back to a default selector of 'h1, h2, h3, h4, h5, h6'
* which adds anchors to all headings.
*/
addAnchors();
```
## Compatibility
Currently Supports: IE9+ and modern browsers
## Contributing [![devDependency Status](https://img.shields.io/david/dev/bryanbraun/anchorjs.svg?style=flat)](https://david-dm.org/bryanbraun/anchorjs#info=devDependencies)
To contribute:
1. Fork/Clone the repo.
2. Make your changes.
3. Write tests as needed.
4. Run tests locally to confirm everything is working:
- Install phantomjs: `brew install phantomjs`
- Install test modules: Run `npm install`
- Run all tests: `npm test`
5. Minify the code: `npm run release`
6. Submit a Pull Request.
## License
Licensed with the [MIT License](http://opensource.org/licenses/MIT).

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc