Comparing version 1.3.0 to 2.0.0
/** | ||
* AnchorJS - v1.3.0 - 2015-09-22 | ||
* AnchorJS - v2.0.0 - 2015-10-31 | ||
* https://github.com/bryanbraun/anchorjs | ||
@@ -9,14 +9,26 @@ * Copyright (c) 2015 Bryan Braun; Licensed MIT | ||
'use strict'; | ||
var that = this; | ||
this.options = options || {}; | ||
this._applyRemainingDefaultOptions = function(opts) { | ||
this.options.icon = this.options.hasOwnProperty('icon') ? opts.icon : '\ue9cb'; // 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. | ||
}; | ||
/** | ||
* Assigns options to the internal options object, and provides defaults. | ||
* @param {Object} opts - Options object | ||
*/ | ||
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. | ||
// 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. | ||
} | ||
this._applyRemainingDefaultOptions(options); | ||
_applyRemainingDefaultOptions(options); | ||
/** | ||
* Add anchor links to page elements. | ||
* @param {String} selector A CSS selector for targeting the elements you wish to add anchor links to. | ||
* @return {this} The AnchorJS object | ||
*/ | ||
this.add = function(selector) { | ||
@@ -28,6 +40,5 @@ var elements, | ||
i, | ||
roughText, | ||
tidyText, | ||
index, | ||
count, | ||
tidyText, | ||
newTidyText, | ||
@@ -37,3 +48,7 @@ readableID, | ||
this._applyRemainingDefaultOptions(this.options); | ||
// We reapply options here because somebody may have overwritten the default options object when setting options. | ||
// For example, this overwrites all options but visible: | ||
// | ||
// anchors.options = { visible: 'always'; } | ||
_applyRemainingDefaultOptions(this.options); | ||
@@ -52,3 +67,3 @@ // Provide a sensible default selector, if none is given. | ||
this._addBaselineStyles(); | ||
_addBaselineStyles(); | ||
@@ -66,15 +81,4 @@ // We produce a list of existing IDs so we don't generate a duplicate. | ||
} else { | ||
roughText = elements[i].textContent; | ||
tidyText = this.urlify(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 they definitely don't belong in a URL fragment.' | ||
tidyText = roughText.replace(/[^\w\s-]/gi, '') // ' Unicode icons are cool--but they definitely dont belong in a URL fragment' | ||
.replace(/\s+/g, '-') // '-Unicode-icons-are-cool--but-they-definitely-dont-belong-in-a-URL-fragment' | ||
.replace(/-{2,}/g, '-') // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL-fragment' | ||
.substring(0, 64) // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL' | ||
.replace(/^-+|-+$/gm, '') // 'Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL' | ||
.toLowerCase(); // 'unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-url' | ||
// Compare our generated ID to existing IDs (and increment it if needed) | ||
@@ -88,3 +92,4 @@ // before we add it to the page. | ||
} | ||
// .indexOf is supported in IE9+. | ||
// .indexOf is only supported in IE9+. | ||
index = idList.indexOf(newTidyText); | ||
@@ -123,2 +128,3 @@ count += 1; | ||
anchor.style.lineHeight = 1; | ||
// We set lineHeight = 1 here because the `anchorjs-icons` font family could otherwise affect the | ||
@@ -147,2 +153,7 @@ // height of the heading. This isn't the case for icons with `placement: left`, so we restore | ||
/** | ||
* Removes all anchorjs-links from elements targed by the selector. | ||
* @param {String} selector A CSS selector used to target elements containing anchor links. | ||
* @return {this} The AnchorJS object | ||
*/ | ||
this.remove = function(selector) { | ||
@@ -160,3 +171,37 @@ var domAnchor, | ||
this._addBaselineStyles = function() { | ||
/** | ||
* Urlify - Refine text so it makes a good ID. | ||
* | ||
* To do this, we remove apostrophes, replace nonsafe characters with hyphens, | ||
* remove extra hyphens, truncate, trim hyphens, and make lowercase. | ||
* | ||
* @param {String} text - Any text. Usually pulled from the webpage element we are linking to. | ||
* @return {String} - hyphen-delimited text for use in IDs and URLs. | ||
*/ | ||
this.urlify = function(text) { | ||
// Regex for finding the nonsafe URL characters (many need escaping): & +$,:;=?@"#{}|^~[`%!']./()*\ | ||
var nonsafeChars = /[& +$,:;=?@"#{}|^~[`%!'\]\.\/\(\)\*\\]/g, | ||
urlText; | ||
if (!this.options.truncate) { | ||
_applyRemainingDefaultOptions(this.options); | ||
} | ||
// Note: we trim hyphens after truncating because truncating can cause dangling hyphens. | ||
// Example string: // " ⚡ Don't forget: URL fragments should be i18n-friendly, hyphenated, short, and clean." | ||
urlText = text.replace(/\'/gi, '') // " ⚡ Dont forget: URL fragments should be i18n-friendly, hyphenated, short, and clean." | ||
.replace(nonsafeChars, '-') // "-⚡-Dont-forget--URL-fragments-should-be-i18n-friendly--hyphenated--short--and-clean-" | ||
.replace(/-{2,}/g, '-') // "-⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated-short-and-clean-" | ||
.substring(0, this.options.truncate) // "-⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated-" | ||
.replace(/^-+|-+$/gm, '') // "⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated" | ||
.toLowerCase(); // "⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated" | ||
return urlText; | ||
}; | ||
/** | ||
* _addBaselineStyles | ||
* Adds baseline styles to the page, used by all AnchorJS links irregardless of configuration. | ||
*/ | ||
function _addBaselineStyles() { | ||
// We don't want to add global baseline styles if they've been added before. | ||
@@ -210,5 +255,5 @@ if (document.head.querySelector('style.anchorjs') !== null) { | ||
style.sheet.insertRule(anchorjsLinkFontFace, style.sheet.cssRules.length); | ||
}; | ||
} | ||
} | ||
var anchors = new AnchorJS(); |
/** | ||
* AnchorJS - v1.3.0 - 2015-09-22 | ||
* AnchorJS - v2.0.0 - 2015-10-31 | ||
* https://github.com/bryanbraun/anchorjs | ||
* Copyright (c) 2015 Bryan Braun; Licensed MIT | ||
*/ | ||
function AnchorJS(A){"use strict";this.options=A||{},this._applyRemainingDefaultOptions=function(A){this.options.icon=this.options.hasOwnProperty("icon")?A.icon:"",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,h,r,c,g;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,64).replace(/^-+|-+$/gm,"").toLowerCase(),r=a,h=0;do void 0!==l&&(r=a+"-"+h),l=n.indexOf(r),h+=1;while(-1!==l);l=void 0,n.push(r),e[i].setAttribute("id",r),o=r}c=o.replace(/-/g," "),g=document.createElement("a"),g.className="anchorjs-link "+this.options.class,g.href="#"+o,g.setAttribute("aria-label","Anchor link for: "+c),g.setAttribute("data-anchorjs-icon",this.options.icon),"always"===this.options.visible&&(g.style.opacity="1"),""===this.options.icon&&(g.style.fontFamily="anchorjs-icons",g.style.fontStyle="normal",g.style.fontVariant="normal",g.style.fontWeight="normal",g.style.lineHeight=1,"left"===this.options.placement&&(g.style.lineHeight="inherit")),"left"===this.options.placement?(g.style.position="absolute",g.style.marginLeft="-1em",g.style.paddingRight="0.5em",e[i].insertBefore(g,e[i].firstChild)):(g.style.paddingLeft="0.375em",e[i].appendChild(g))}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//8ADwABAAAAAAAAAAAAAgAANzkBAAAAAAEAAAAAAAAAAAACAAA3O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aQBjAG8AbgBzRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype"); }',i=" [data-anchorjs-icon]::after { content: attr(data-anchorjs-icon); }";e.className="anchorjs",e.appendChild(document.createTextNode("")),A=document.head.querySelector('[rel="stylesheet"], style'),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; | ||
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,AAEAAAALAIAAAwAwT1MvMg8SBTUAAAC8AAAAYGNtYXAWi9QdAAABHAAAAFRnYXNwAAAAEAAAAXAAAAAIZ2x5Zgq29TcAAAF4AAABNGhlYWQEZM3pAAACrAAAADZoaGVhBhUDxgAAAuQAAAAkaG10eASAADEAAAMIAAAAFGxvY2EAKACuAAADHAAAAAxtYXhwAAgAVwAAAygAAAAgbmFtZQ5yJ3cAAANIAAA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A1AAMAAQQJAAYAHACDAAMAAQQJAAoANAD4YW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzVmVyc2lvbiAxLjAAVgBlAHIAcwBpAG8AbgAgADEALgAwYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzUmVndWxhcgBSAGUAZwB1AGwAYQByYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) 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)}}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; |
{ | ||
"name": "anchor-js", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Bryan Braun" |
{ | ||
"name": "anchor-js", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"author": "bryanbraun (https://github.com/bryanbraun)", | ||
@@ -16,3 +16,3 @@ "description": "A Javscript utility for adding deep anchor links to online docs.", | ||
"scripts": { | ||
"lint": "eslint anchor.js", | ||
"lint": "eslint anchor.js test/spec/AnchorSpec.js", | ||
"jasmine": "karma start test/config.js --single-run", | ||
@@ -23,10 +23,11 @@ "release": "uglifyjs anchor.js --compress --mangle --screw-ie8 --comments \"/Copyright/\" -o anchor.min.js", | ||
"devDependencies": { | ||
"eslint": "~1.5.1", | ||
"eslint": "~1.9.0", | ||
"jasmine-core": "~2.3.4", | ||
"karma": "~0.13.10", | ||
"karma-cli": "~0.1.0", | ||
"karma": "~0.13.15", | ||
"karma-cli": "~0.1.1", | ||
"karma-jasmine": "~0.3.6", | ||
"karma-phantomjs-launcher": "~0.2.1", | ||
"uglify-js": "~2.4.24" | ||
"phantomjs": "~1.9.18", | ||
"uglify-js": "~2.5.0" | ||
} | ||
} |
@@ -6,7 +6,5 @@ // Karma configuration | ||
config.set({ | ||
// base path that will be used to resolve all patterns (eg. files, exclude) | ||
basePath: '', | ||
// frameworks to use | ||
@@ -16,3 +14,2 @@ // available frameworks: https://npmjs.org/browse/keyword/karma-adapter | ||
// list of files / patterns to load in the browser | ||
@@ -24,3 +21,2 @@ files: [ | ||
// list of files to exclude | ||
@@ -30,3 +26,2 @@ exclude: [ | ||
// preprocess matching files before serving them to the browser | ||
@@ -37,3 +32,2 @@ // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | ||
// test results reporter to use | ||
@@ -44,11 +38,8 @@ // possible values: 'dots', 'progress' | ||
// web server port | ||
port: 9876, | ||
// enable / disable colors in the output (reporters and logs) | ||
colors: true, | ||
// level of logging | ||
@@ -58,7 +49,5 @@ // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | ||
// enable / disable watching file and executing tests whenever any file changes | ||
autoWatch: false, | ||
// start these browsers | ||
@@ -68,3 +57,2 @@ // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | ||
// Continuous Integration mode | ||
@@ -71,0 +59,0 @@ // if true, Karma captures browsers, runs the tests and exits |
@@ -1,2 +0,6 @@ | ||
describe("AnchorJS", function() { | ||
/* eslint-env jasmine */ | ||
/* global anchors, AnchorJS */ | ||
describe('AnchorJS', function() { | ||
'use strict'; | ||
var h, | ||
@@ -6,4 +10,4 @@ t; | ||
beforeEach(function() { | ||
h = document.createElement("h1"); | ||
t = document.createTextNode("⚡⚡⚡ Unicode icons are cool--but they definitely don't belong in a URL fragment."); | ||
h = document.createElement('h1'); | ||
t = document.createTextNode(' ⚡ Don\'t forget: URL fragments should be i18n-friendly, hyphenated, short, and clean.'); | ||
h.appendChild(t); | ||
@@ -17,4 +21,3 @@ document.body.appendChild(h); | ||
it("should add an anchor link to an h1 by default", function() { | ||
it('should add an anchor link to an h1 by default', function() { | ||
var anchorLink; | ||
@@ -26,3 +29,3 @@ anchors.add(); | ||
it("should have default options (right placement, hover visiblity, & link icon)", function() { | ||
it('should have default options (right placement, hover visiblity, & link icon)', function() { | ||
var anchorLink, | ||
@@ -43,3 +46,3 @@ hasClass, | ||
it("should set baseline styles in the document head", function() { | ||
it('should set baseline styles in the document head', function() { | ||
var hasClass; | ||
@@ -53,3 +56,3 @@ anchors.add(); | ||
it("allows you to instantiate a new AnchorJS object that behaves like the default one.", function() { | ||
it('allows you to instantiate a new AnchorJS object that behaves like the default one.', function() { | ||
var anchorObj, | ||
@@ -63,5 +66,5 @@ anchorLink; | ||
it("ensures new AnchorJS instances don't add multiple baseline style tags.", function() { | ||
it('ensures new AnchorJS instances do not add multiple baseline style tags.', function() { | ||
var anchorObj, | ||
anchorLink; | ||
styleNodes; | ||
anchors.add(); | ||
@@ -74,3 +77,3 @@ anchorObj = new AnchorJS(); | ||
it("doesn't destroy default options when setting an incomplete options object.", function() { | ||
it('does not destroy default options when setting an incomplete options object.', function() { | ||
var anchorObj, | ||
@@ -93,3 +96,3 @@ anchorLink, | ||
it("can remove anchors, using the .remove() method.", function() { | ||
it('can remove anchors, using the .remove() method.', function() { | ||
var anchorLinkBefore, | ||
@@ -106,3 +109,3 @@ anchorLinkAfter; | ||
it("can chain methods.", function() { | ||
it('can chain methods.', function() { | ||
var anchorLinkBefore, | ||
@@ -119,3 +122,3 @@ anchorLinkAfter; | ||
it("should create a URL-appropriate ID (and href) for targeted elements without IDs.", function() { | ||
it('should create a URL-appropriate ID (and href) for targeted elements without IDs.', function() { | ||
var href, | ||
@@ -126,10 +129,10 @@ id; | ||
id = document.getElementsByTagName('h1')[0].getAttribute('id'); | ||
expect(href).toEqual('#unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-url'); | ||
expect(id).toEqual('unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-url'); | ||
expect(href).toEqual('#⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated'); | ||
expect(id).toEqual('⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated'); | ||
}); | ||
it("should leave existing IDs in place, and use them as the href for anchors.", function() { | ||
it('should leave existing IDs in place, and use them as the href for anchors.', function() { | ||
var href, | ||
id; | ||
document.getElementsByTagName('h1')[0].setAttribute('id', 'test-id') | ||
document.getElementsByTagName('h1')[0].setAttribute('id', 'test-id'); | ||
anchors.add('h1'); | ||
@@ -142,3 +145,3 @@ href = document.querySelector('.anchorjs-link').getAttribute('href'); | ||
it("should increment new IDs if multiple IDs are found on a page.", function() { | ||
it('should increment new IDs if multiple IDs are found on a page.', function() { | ||
var h1, | ||
@@ -159,10 +162,10 @@ t1, | ||
i; | ||
h1 = document.createElement("h2"); | ||
t1 = document.createTextNode("Example Title"); | ||
h1 = document.createElement('h2'); | ||
t1 = document.createTextNode('Example Title'); | ||
h1.appendChild(t1); | ||
h2 = document.createElement("h2"); | ||
t2 = document.createTextNode("Example Title"); | ||
h2 = document.createElement('h2'); | ||
t2 = document.createTextNode('Example Title'); | ||
h2.appendChild(t2); | ||
h3 = document.createElement("h2"); | ||
t3 = document.createTextNode("Example Title"); | ||
h3 = document.createElement('h2'); | ||
t3 = document.createTextNode('Example Title'); | ||
h3.appendChild(t3); | ||
@@ -194,9 +197,9 @@ document.body.appendChild(h1); | ||
it("should throw an error if an inappropriate selector is provided.", function() { | ||
it('should throw an error if an inappropriate selector is provided.', function() { | ||
expect(function() { | ||
anchors.add(25); | ||
}).toThrowError("The selector provided to AnchorJS was invalid."); | ||
}).toThrowError('The selector provided to AnchorJS was invalid.'); | ||
}); | ||
it("should place the ¶ icon, when that option is set.", function() { | ||
it('should place the ¶ icon, when that option is set.', function() { | ||
var icon; | ||
@@ -209,3 +212,3 @@ anchors.options.icon = '¶'; | ||
it("should place anchors to the left, when that option is set.", function() { | ||
it('should place anchors to the left, when that option is set.', function() { | ||
var anchorNode; | ||
@@ -219,3 +222,3 @@ anchors.options.placement = 'left'; | ||
it("should make anchors always visible, when that option is set.", function() { | ||
it('should make anchors always visible, when that option is set.', function() { | ||
var opacity; | ||
@@ -228,3 +231,3 @@ anchors.options.visible = 'always'; | ||
it("should apply a provided class, when that option is set.", function() { | ||
it('should apply a provided class, when that option is set.', function() { | ||
var anchorLink; | ||
@@ -236,2 +239,58 @@ anchors.options.class = 'test-class'; | ||
}); | ||
it('preserves unicode characters when making text URL-friendly ', function() { | ||
var text1Before = 'Заголовок, содержащий 29 не-ASCII символов', | ||
text1After = 'заголовок-содержащий-29-не-ascii-символов', | ||
text2Before = '船や航海を連想させるものとして、シンボル的に用いられることも多い。', | ||
text2After = '船や航海を連想させるものとして、シンボル的に用いられることも多い。', | ||
text3Before = 'അടിത്തട്ടിലെ മണ്ണിൽ ആഴ്ന്നിറങ്ങുക, ഭാരത്താൽ താഴ്ന്നു കിടക്കുക, കപ്പലിന്റെ ഗുരുത്വകേന്ദ്രവും', | ||
text3After = 'അടിത്തട്ടിലെ-മണ്ണിൽ-ആഴ്ന്നിറങ്ങുക-ഭാരത്താൽ-താഴ്ന്നു-കിടക്കുക-കപ്', | ||
text4Before = 'Use ⚓ and 👪 all over the 🌐 can 🔗 inside your webpages.', | ||
text4After = 'use-⚓-and-👪-all-over-the-🌐-can-🔗-inside-your-webpages'; | ||
expect(anchors.urlify(text1Before)).toEqual(text1After); | ||
expect(anchors.urlify(text2Before)).toEqual(text2After); | ||
expect(anchors.urlify(text3Before)).toEqual(text3After); | ||
expect(anchors.urlify(text4Before)).toEqual(text4After); | ||
}); | ||
it('removes non-url-safe characters when making text URL-friendly', function() { | ||
var text1Before = 'one&two three+four$five,six:seven;eight=nine?ten', | ||
text2Before = 'one@two"three#four{five}six|seven^eight~nine[ten', | ||
text3Before = 'one`two%three!four]five.six/seven(eight)nine*ten\\', | ||
after = 'one-two-three-four-five-six-seven-eight-nine-ten'; | ||
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() { | ||
var before = 'don\'t', | ||
after = 'dont'; | ||
expect(anchors.urlify(before)).toEqual(after); | ||
}); | ||
it('truncates IDs via truncate option when making text URL-friendly', function() { | ||
var before = 'Today you are you! That is truer than true! There is no one alive who is you-er than you!', | ||
trunc1 = 'today-you-are-you-that-is-truer-than-true-there-is-no-one-alive', | ||
trunc2 = 'today-you-are-you-that-is-truer-than-true', | ||
trunc3 = 'today-you-are-you', | ||
trunc4 = 'today-you-are-you-that-is-truer-than-true-there-is-no-one-alive-who-is-you-er-than-you'; | ||
expect(anchors.urlify(before)).toEqual(trunc1); | ||
anchors.options.truncate = 41; | ||
expect(anchors.urlify(before)).toEqual(trunc2); | ||
anchors.options.truncate = 17; | ||
expect(anchors.urlify(before)).toEqual(trunc3); | ||
anchors.options.truncate = 87; | ||
expect(anchors.urlify(before)).toEqual(trunc4); | ||
// Verify that the final answer remains the same for non-integer numbers or strings. | ||
anchors.options.truncate = 87.9999999999; | ||
expect(anchors.urlify(before)).toEqual(trunc4); | ||
anchors.options.truncate = '87'; | ||
expect(anchors.urlify(before)).toEqual(trunc4); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
46961
532
8