Skip to content

Commit

Permalink
Handle   (non-breaking space) in headings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Wu authored and bryanbraun committed Oct 3, 2020
1 parent 13e098f commit 83ce396
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
7 changes: 6 additions & 1 deletion anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,13 @@
* @return {String} - hyphen-delimited text for use in IDs and URLs.
*/
this.urlify = function(text) {
// Decode HTML characters such as ' ' first.
var textareaElement = document.createElement('textarea');
textareaElement.innerHTML = text;
text = textareaElement.value;

// Regex for finding the non-safe URL characters (many need escaping): & +$,:;=?@"#{}|^~[`%!'<>]./()*\ (newlines, tabs, backspace, & vertical tabs)
var nonsafeChars = /[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v]/g,
var nonsafeChars = /[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v\u00A0]/g,
urlText;

// The reason we include this _applyRemainingDefaultOptions is so urlify can be called independently,
Expand Down
4 changes: 2 additions & 2 deletions anchor.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions docs/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@
if (this.options.placement === 'left') {
anchor.style.position = 'absolute';
anchor.style.marginLeft = '-1em';
anchor.style.paddingRight = '0.5em';
anchor.style.paddingRight = '.5em';
elements[i].insertBefore(anchor, elements[i].firstChild);
} else { // if the option provided is `right` (or anything else).
anchor.style.paddingLeft = '0.375em';
anchor.style.paddingLeft = '.375em';
elements[i].appendChild(anchor);
}
}
Expand Down Expand Up @@ -232,8 +232,13 @@
* @return {String} - hyphen-delimited text for use in IDs and URLs.
*/
this.urlify = function(text) {
// Decode HTML characters such as '&nbsp;' first.
var textareaElement = document.createElement('textarea');
textareaElement.innerHTML = text;
text = textareaElement.value;

// Regex for finding the non-safe URL characters (many need escaping): & +$,:;=?@"#{}|^~[`%!'<>]./()*\ (newlines, tabs, backspace, & vertical tabs)
var nonsafeChars = /[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v]/g,
var nonsafeChars = /[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v\u00A0]/g,
urlText;

// The reason we include this _applyRemainingDefaultOptions is so urlify can be called independently,
Expand Down Expand Up @@ -262,8 +267,8 @@
* @return {Boolean} true/false
*/
this.hasAnchorJSLink = function(el) {
var hasLeftAnchor = el.firstChild && ((' ' + el.firstChild.className + ' ').indexOf(' anchorjs-link ') > -1),
hasRightAnchor = el.lastChild && ((' ' + el.lastChild.className + ' ').indexOf(' anchorjs-link ') > -1);
var hasLeftAnchor = el.firstChild && (' ' + el.firstChild.className + ' ').indexOf(' anchorjs-link ') > -1,
hasRightAnchor = el.lastChild && (' ' + el.lastChild.className + ' ').indexOf(' anchorjs-link ') > -1;

return hasLeftAnchor || hasRightAnchor || false;
};
Expand Down
2 changes: 1 addition & 1 deletion test/spec/AnchorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('AnchorJS', function() {
var el1;

beforeEach(function() {
var titleText = ' ⚡⚡ Don\'t forget: URL fragments should be i18n-friendly, hyphenated, short, and clean.';
var titleText = ' ⚡⚡ Don\'t forget: URL fragments&nbsp;should be i18n-friendly, hyphenated, short, and clean.';
el1 = appendElementToBody('h1', titleText);
});

Expand Down

0 comments on commit 83ce396

Please sign in to comment.