Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tabs not being selected with fragments that contain unescaped html special chars #214

Merged

Conversation

nobbyfix
Copy link
Contributor

@nobbyfix nobbyfix commented Jan 27, 2025

#209 fix once again and should really be the last one.

Fragments containing unescaped special chars like ' instead of ' are not selected. I just added a retry when selecting the tab element with the escaped version if the first try failed. Checking whether its already escaped would be much more complex so this should be good enough.

I've also moved the fragment transformation and element selection into a util function and added a check to make sure the selected element is generated by tabber (the previous code, at least in the hashchange event part, would trigger for any hashchange and could trigger the click event on any element).

Summary by CodeRabbit

  • New Features

    • Added a new utility method to select tab elements based on URL hash.
  • Refactor

    • Simplified tab selection logic in the TabberBuilder.
    • Consolidated URL hash processing into a more streamlined utility function.
    • Improved code maintainability by reducing redundant hash processing steps.

Copy link

coderabbitai bot commented Jan 27, 2025

Walkthrough

The pull request introduces a new static method selectElementFromUrlHash in the Util class within the ext.tabberNeue module. This method is designed to extract a tab header element from a URL hash by decoding, escaping, and constructing an appropriate HTML element ID. The changes refactor the existing code in getActiveTab and the load function to use this new utility method, simplifying the logic for selecting tabs based on URL fragments.

Changes

File Change Summary
modules/ext.tabberNeue/Util.js Added new static method selectElementFromUrlHash(urlHash) to extract tab header element from URL hash.
modules/ext.tabberNeue/ext.tabberNeue.js Refactored getActiveTab method and hash change event handling to use Util.selectElementFromUrlHash().

Sequence Diagram

sequenceDiagram
    participant URL as URL Hash
    participant Util as Util.selectElementFromUrlHash()
    participant Tab as Tab Header Element
    
    URL->>Util: Provide URL hash
    Util-->>Util: Decode and escape hash
    Util-->>Util: Construct tab header ID
    Util->>Tab: Find corresponding tab element
    Util-->>URL: Return tab header or undefined
Loading

Possibly related PRs

Poem

🐰 A rabbit's tale of tabs so neat,
With hashes decoded, a coding feat!
URL fragments now dance with grace,
Through selectElementFromUrlHash's embrace
Simplicity wins, complexity retreats! 🔍


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c99901b and 3b016b3.

📒 Files selected for processing (1)
  • modules/ext.tabberNeue/Util.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • modules/ext.tabberNeue/Util.js

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
modules/ext.tabberNeue/Util.js (2)

81-83: Add input type validation.

The input validation could be more robust by checking the type of urlHash.

-	if( !urlHash ) {
+	if( !urlHash || typeof urlHash !== 'string' ) {
 		return;
 	}
🧰 Tools
🪛 eslint

[error] 81-81: Expected space(s) after "if".

(keyword-spacing)


89-94: Consider adding error logging for debugging.

When the tab element is not found on the first attempt, it would be helpful to log this for debugging purposes.

 		if ( !activeTabFromUrlHash ) {
+			mw.log.debug( '[TabberNeue] Tab not found with ID:', idFromUrlHash );
 			// Retry getting the tab after escaping html special chars to correctly select
 			// the tab for cases where the fragment does not use the escaped version
 			const specialCharEscapedHash = mw.html.escape( idFromUrlHash );
 			activeTabFromUrlHash = document.getElementById( specialCharEscapedHash );
modules/ext.tabberNeue/ext.tabberNeue.js (1)

Line range hint 485-490: Consider adding error logging for debugging.

When a tab is not found for a hash change, it would be helpful to log this for debugging purposes.

 			const tab = Util.selectElementFromUrlHash( hash );
 			if ( tab ) {
 				event.preventDefault();
 				tab.click();
+			} else {
+				mw.log.debug( '[TabberNeue] No matching tab found for hash:', hash );
 			}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f1ecc1a and c99901b.

📒 Files selected for processing (2)
  • modules/ext.tabberNeue/Util.js (1 hunks)
  • modules/ext.tabberNeue/ext.tabberNeue.js (2 hunks)
🧰 Additional context used
📓 Learnings (1)
modules/ext.tabberNeue/ext.tabberNeue.js (1)
Learnt from: alistair3149
PR: StarCitizenTools/mediawiki-extensions-TabberNeue#189
File: modules/ext.tabberNeue/ext.tabberNeue.js:427-430
Timestamp: 2024-11-16T08:18:43.156Z
Learning: In `modules/ext.tabberNeue/ext.tabberNeue.js`, within the `getActiveTab` method of the `TabberBuilder` class, the check `if (idFromUrlHash === escapedHash)` ensures that if replacing the `'tabber-tabpanel-'` prefix does not modify the `escapedHash`, then the hash does not represent a valid tabpanel ID, and the current check is sufficient.
🪛 eslint
modules/ext.tabberNeue/Util.js

[error] 81-81: Expected space(s) after "if".

(keyword-spacing)


[error] 95-95: Expected space(s) after "if".

(keyword-spacing)


[error] 101-101: Expected space(s) after "if".

(keyword-spacing)

🔇 Additional comments (1)
modules/ext.tabberNeue/ext.tabberNeue.js (1)

Line range hint 429-436: LGTM! Clean refactor of the tab selection logic.

The code has been simplified by moving the URL hash handling to the utility function while maintaining the necessary validation checks. The check for closest('.tabber__tabs') ensures that the selected tab belongs to the current tabber instance.

* @return {Element} The element of the matching tab header.
*/
static selectElementFromUrlHash( urlHash ) {
if( !urlHash ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix spacing after if statements.

Add space after if keywords to comply with the coding style.

-	if( !urlHash ) {
+	if ( !urlHash ) {
-			if( !activeTabFromUrlHash ) {
+			if ( !activeTabFromUrlHash ) {
-		if( activeTabFromUrlHash.classList.contains( 'tabber__tab' ) ) {
+		if ( activeTabFromUrlHash.classList.contains( 'tabber__tab' ) ) {

Also applies to: 95-95, 101-101

🧰 Tools
🪛 eslint

[error] 81-81: Expected space(s) after "if".

(keyword-spacing)

@alistair3149 alistair3149 merged commit 3cc5948 into StarCitizenTools:main Jan 27, 2025
2 checks passed
@nobbyfix nobbyfix deleted the patch-parse-unencoded-fragments branch January 28, 2025 00:57
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.

2 participants