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

Remove jQuery .attr from the commit graph #30006

Merged
merged 5 commits into from Mar 22, 2024

Conversation

yardenshoham
Copy link
Member

@yardenshoham yardenshoham commented Mar 22, 2024

Switched from jQuery .attr to plain javascript .getAttribute and .setAttribute

Switched from jQuery `.attr` to plain javascript `.getAttribute` and `.setAttribute`

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 22, 2024
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Mar 22, 2024
@yardenshoham
Copy link
Member Author

I didn't test this one as I couldn't find a way to render the pagination buttons because my browser always crashes on this page for big repositories

if (!href) return;
const url = new URL(href, window.location);
const params = url.searchParams;
params.set('mode', 'monochrome');
url.search = `?${params.toString()}`;
$(that).attr('href', url.href);
that.href = url.href;
Copy link
Member

Choose a reason for hiding this comment

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

Use setAttribute instead of href. There are slight differences in those two methods in how the URL encodes as I recently noticed.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see @wxiaoguang suggests the opposite.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@wxiaoguang see #29931 (comment). There I came to the conclusion it's better to use setAttribute and getAttribute when working with href.

Copy link
Member

Choose a reason for hiding this comment

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

That's the difference:

console.log(a.href);
// https://fiddle.jshell.net/_display/?editor_console=true#%E6%B0%94%E5%80%99%E5%AE%9C%E4%BA%BA

console.log(a.getAttribute("href"));
//  #气候宜人

href can not deal with unicode and resolves to absolute URL.

Copy link
Member

Choose a reason for hiding this comment

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

I do not think so. el.href is the normalized and correctly escaped full URL. It could just be used as-is.

The method to choose depends on use case whether absolute escaped or relative unicode URL is needed. We should just do what jQuery did before.

@yardenshoham could you debug it out what jQuery did please? I assume it was equivalent to getAttribute and setAttribute.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Yeah so let's change it back so it's a more accurate refactor?

Copy link
Contributor

Choose a reason for hiding this comment

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

In your example, a.setAttribute("href", "#气候宜人") you are setting a invalid URL to href. So when you read the el.href again, it gets correctly escaped.

It's not a invalid URL as URL constructor accepts it:

URL also generates the normalized and fully escaped URL: href: 'https://example.com/#%E6%B0%94%E5%80%99%E5%AE%9C%E4%BA%BA'. So I insist el.href is the right thing to be directly used.

image

Copy link
Member

Choose a reason for hiding this comment

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

Both will work I assume. That said, this code looks ripe for refactor, these two functions are almost completely the same.

This reverts commit 5d11f86.
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 22, 2024
@yardenshoham yardenshoham added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Mar 22, 2024
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 22, 2024
@silverwind silverwind added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Mar 22, 2024
@silverwind silverwind enabled auto-merge (squash) March 22, 2024 23:17
@silverwind silverwind merged commit d4ac1bd into go-gitea:main Mar 22, 2024
26 checks passed
@GiteaBot GiteaBot added this to the 1.23.0 milestone Mar 22, 2024
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Mar 22, 2024
@yardenshoham yardenshoham deleted the repo-graph-jquery-attr branch March 23, 2024 08:03
@6543 6543 modified the milestones: 1.23.0, 1.22.0 Mar 24, 2024
zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 24, 2024
* giteaofficial/main: (28 commits)
  Enable a few stylelint rules (go-gitea#30038)
  Remove remaining jQuery .css code (go-gitea#30015)
  Respect DEFAULT_ORG_MEMBER_VISIBLE setting when adding creator to org (go-gitea#30013)
  Remove jQuery `.attr` from the common global functions (go-gitea#30023)
  Migrate font-size helpers to tailwind (go-gitea#30029)
  Replace all simple inline styles with tailwind (go-gitea#30032)
  Migrate font-weight helpers to tailwind (go-gitea#30027)
  Remove jQuery from the issue "go to" button (go-gitea#30028)
  Determine fuzziness of bleve indexer by keyword length (go-gitea#29706)
  Escape paths for find file correctly (go-gitea#30026)
  Remove jQuery `.attr` from the diff page (go-gitea#30021)
  Remove jQuery `.attr` from the repository settings (go-gitea#30018)
  Remove jQuery `.attr` from the image diff again (go-gitea#30022)
  Introduce `.secondary-nav` and handle `.page-content` spacing universally (go-gitea#29982)
  Remove jQuery `.attr` from the branch/tag selector (go-gitea#30010)
  Remove jQuery `.attr` from the commit graph (go-gitea#30006)
  Remove jQuery from the citation modal (except fomantic) (go-gitea#30008)
  Remove jQuery `.attr` from the project page (go-gitea#30004)
  Fix incorrect tailwind migration (go-gitea#30007)
  Enforce trailing comma in JS on multiline (go-gitea#30002)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/js size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants