Skip to content

Switch on the normalized displayOption value - #138

Open
Denimworld12 wants to merge 1 commit into
paypal:mainfrom
Denimworld12:fix/display-option-normalized
Open

Denimworld12 wants to merge 1 commit into
paypal:mainfrom
Denimworld12:fix/display-option-normalized

Conversation

@Denimworld12

Copy link
Copy Markdown

SkipTo.init() normalizes config.displayOption with .trim().toLowerCase() into a local, but the switch that follows tested this.config.displayOption — the original string — so the normalization never reached the comparison and values such as 'Popup', ' popup' and 'FIXED' fell through to default with no class added.

skipto/src/js/skipto.js

Lines 185 to 201 in 93599ba

var displayOption = this.config.displayOption;
if (typeof displayOption === 'string') {
displayOption = displayOption.trim().toLowerCase();
if (displayOption.length) {
switch (this.config.displayOption) {
case 'fixed':
this.domNode.classList.add('fixed');
break;
case 'onfocus': // Legacy option
case 'popup':
this.domNode.classList.add('popup');
break;
default:
break;
}
}
}

var displayOption = this.config.displayOption;
if (typeof displayOption === 'string') {
  displayOption = displayOption.trim().toLowerCase();   // normalized here
  if (displayOption.length) {                           // guard reads the normalized copy
    switch (this.config.displayOption) {                // ... but the switch reads the raw value
-          switch (this.config.displayOption) {
+          switch (displayOption) {

(Filing this as a PR rather than an issue first, since issues are turned off on this repo.)

Why it matters

The class is the whole feature. From src/css/SkipTo.css:

.skip-to.popup { position: absolute; top: -30em; left: 0 }
.skip-to.fixed { position: fixed }

With no class added the button falls back to .skip-to { position: absolute; top: 0 } and stays visible. So a site configured with displayOption: 'Popup' gets a Skip To Content button pinned to the top of every page instead of one that appears on focus — the opposite of what it asked for, with no error and nothing in the console.

SkipTo reads its configuration from window.SkipToConfig or window.Joomla.getOptions('skipto-settings', {}), so the value can arrive from a hand-written object or from a settings form; both are easy places for a capital letter or a stray space to survive.

Note that the if (displayOption.length) guard on the preceding line does read the normalized copy, so ' popup' passes that guard and then fails the switch — the two lines disagreed about which value they were judging.

Testing

Chrome 144, the example/ pages loading downloads/js/skipto.js built from this branch, plus three copies of those pages with only the configured value changed. "Before" is the committed build at 93599ba.

page displayOption before after
skipTo-onfocus.html 'popup' skip-to popup, top: -480px unchanged
copy of it 'Popup' skip-to, top: 0px skip-to popup, top: -480px
copy of it ' popup' skip-to, top: 0px skip-to popup, top: -480px
skipTo-fixed.html 'fixed' skip-to fixed, position: fixed unchanged
copy of it 'FIXED' skip-to, position: absolute skip-to fixed, position: fixed

npx grunt test passes (2 files lint free). The repository has no unit test harness, so the verification above is a browser measurement of the rendered class and computed style, not an automated test.

For what it is worth, this matches what the v5 rewrite already does: setDisplayOption() in skipto-landmarks-headings/page-script-5 normalizes into value and switches on value. The same line is still present on the dev/v5.0 branch here, at src/js/skipto.js:329, if you want it carried over.

A note on downloads/

I have not regenerated the build artifacts, because npx grunt after a fresh npm install no longer reproduces the committed bundle. grunt-contrib-uglify@4.0.1 resolves uglify-js@3.x (the uglify-js: ~2.8.0 devDependency is not the copy grunt loads), and its output differs from downloads/js/skipto.min.js in roughly a hundred places unrelated to this change — it emits (()=>{ where the committed file has !function(){"use strict";, drops the "use strict" directive, and renames locals despite mangle: false.

downloads/js/skipto.js on its own is byte-reproducible (concat only, no uglify), but shipping it without the matching minified files would leave the set inconsistent, so I left all four alone.

Happy to add whatever output you want here — say the word and I will include it.

init() normalizes config.displayOption with .trim().toLowerCase() into a
local, but the switch that follows tested this.config.displayOption -- the
original string -- so the normalization never reached the comparison.
Values such as 'Popup', ' popup' and 'FIXED' fell through to default with
no class added.

The class is the feature: .skip-to.popup sets top:-30em so the button is
hidden until focus, and .skip-to.fixed pins it to the viewport. Without it
the button falls back to .skip-to { position:absolute; top:0 } and stays
visible on every page, with no error reported.

The if (displayOption.length) guard on the preceding line already read the
normalized copy, so the two lines disagreed about which value they judged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant