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 and rewrite markup anchor processing #29931

Merged
merged 5 commits into from Mar 20, 2024

Conversation

lunny
Copy link
Member

@lunny lunny commented Mar 20, 2024

Fix #29877

@lunny lunny added type/bug backport/v1.21 This PR should be backported to Gitea 1.21 labels Mar 20, 2024
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 20, 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 20, 2024
@wxiaoguang
Copy link
Contributor

Why sometimes encode but sometimes not?

image

@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 20, 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 20, 2024
@lunny
Copy link
Member Author

lunny commented Mar 20, 2024

Why sometimes encode but sometimes not?

image

014c3a2

@silverwind
Copy link
Member

silverwind commented Mar 20, 2024

Not sure this is right. It seems odd that we decode something that was previously encoded. I think removing all the decodes may just be enough too.

I'd certainly want to add some unit tests in vitest for sure.

@wxiaoguang
Copy link
Contributor

I don't know whether it is right either .... does the author or approvers have more information to write into comment?

Copy link
Member

@silverwind silverwind left a comment

Choose a reason for hiding this comment

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

needs deeper investigation, i will do later.

@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels Mar 20, 2024
@silverwind
Copy link
Member

silverwind commented Mar 20, 2024

As per https://stackoverflow.com/a/50141030/808699 and https://jsfiddle.net/silverwind/pe0twrf8/, it seems unnecssary to encode when setting a href attribute.

Not sure about the decode yet, but my intuition tells me it's unnecessary too.

@silverwind
Copy link
Member

silverwind commented Mar 20, 2024

Fiddle updated: https://jsfiddle.net/silverwind/pe0twrf8/7/

My conclusion is

  • never encode or decode when working with getAttribute and setAttribute, just use UTF-8.
  • working with a.href would require at least decoding when reading but it should be forbidden in our code.
  • use CSS.escape in CSS selectors

@silverwind
Copy link
Member

silverwind commented Mar 20, 2024

Apparently it does make a difference whether a link is generated or pre-existing in HTML:

https://jsfiddle.net/silverwind/2wy39d7e/

So backend-generated links will appear escaped and need a decodeURIComponent when reading them from DOM. JS-generated links can insert as unicode into DOM, but I guess for consistency we should also encode them when writing them to DOM.

I'll work out something on this branch.

@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 20, 2024
@silverwind
Copy link
Member

I rewrote this file quite a bit now, the code is now easier to read and there is no more complex querySelector and only a single click listener that does it all.

Everything is working and I specifically tested with chinese links as well. @lunny can you try you test case on wiki as well?

@pull-request-size pull-request-size bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Mar 20, 2024
@github-actions github-actions bot added modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files labels Mar 20, 2024
@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Mar 20, 2024
@github-actions github-actions bot removed modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files labels Mar 20, 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 20, 2024
@lafriks lafriks merged commit 76ec541 into go-gitea:main Mar 20, 2024
26 checks passed
@GiteaBot GiteaBot added this to the 1.23.0 milestone Mar 20, 2024
GiteaBot pushed a commit to GiteaBot/gitea that referenced this pull request Mar 20, 2024
Fix go-gitea#29877

---------

Co-authored-by: silverwind <me@silverwind.io>
@GiteaBot GiteaBot added the backport/done All backports for this PR have been created label Mar 20, 2024
lafriks pushed a commit that referenced this pull request Mar 20, 2024
Backport #29931 by @lunny

Fix #29877

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
@lunny lunny deleted the lunny/fix_wiki_link branch March 21, 2024 02:09
@lunny
Copy link
Member Author

lunny commented Mar 21, 2024

It works but the links are still encoded which is different from Github's implementations. For modern web browsers, we don't need to encode them.

image

@silverwind
Copy link
Member

silverwind commented Mar 21, 2024

It works but the links are still encoded which is different from Github's implementations. For modern web browsers, we don't need to encode them.

Yes, but it comes out encoded from the backend, I assume from the markup renderer. So it would need to be fixed there first. If you can, raise a PR and then I will adapt the JS in the same PR to support this.

zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 21, 2024
* giteaofficial/main: (34 commits)
  Refactor URL detection (go-gitea#29960)
  Solving the issue of UI disruption when the review is deleted without refreshing (go-gitea#29951)
  Fix JS error and improve error message styles (go-gitea#29963)
  Fix the bug that user may logout if he switch pages too fast (go-gitea#29962)
  Cancel previous runs of the same PR automatically (go-gitea#29961)
  Exclude `routers/private/tests` from air (go-gitea#29949)
  Remove codecov badge (go-gitea#29950)
  Misc color tweaks (go-gitea#29943)
  Fix and rewrite markup anchor processing (go-gitea#29931)
  Remove fomantic grid module (go-gitea#29894)
  Add background to dashboard navbar, fix missing padding (go-gitea#29940)
  Prevent layout shift in `<overflow-menu>` items (go-gitea#29831)
  Fix loadOneBranch panic (go-gitea#29938)
  Fix comment review avatar alignment (go-gitea#29935)
  Remove the negative margin from `.page-content` (go-gitea#29922)
  Move notifications to a standalone file (go-gitea#29930)
  Remove unnecessary ".Link" usages (go-gitea#29929)
  Remove unnecessary ".Link" usages (go-gitea#29909)
  Show Actions post step when it's running (go-gitea#29926)
  Fix the wrong default value of ENABLE_OPENID_SIGNIN on docs (go-gitea#29925)
  ...

# Conflicts:
#	templates/user/dashboard/issues.tmpl
silverwind added a commit to silverwind/gitea that referenced this pull request Mar 21, 2024
* origin/main: (332 commits)
  Refactor external URL detection (go-gitea#29973)
  Refactor repo header/list (go-gitea#29969)
  Fix various loading states, remove `.loading` class (go-gitea#29920)
  Update register application URL for GitLab (go-gitea#29959)
  Refactor StringsToInt64s (go-gitea#29967)
  Switch to happy-dom for testing (go-gitea#29948)
  Performance improvements for pull request list page (go-gitea#29900)
  Refactor URL detection (go-gitea#29960)
  Solving the issue of UI disruption when the review is deleted without refreshing (go-gitea#29951)
  Fix JS error and improve error message styles (go-gitea#29963)
  Fix the bug that user may logout if he switch pages too fast (go-gitea#29962)
  Cancel previous runs of the same PR automatically (go-gitea#29961)
  Exclude `routers/private/tests` from air (go-gitea#29949)
  Remove codecov badge (go-gitea#29950)
  Misc color tweaks (go-gitea#29943)
  Fix and rewrite markup anchor processing (go-gitea#29931)
  Remove fomantic grid module (go-gitea#29894)
  Add background to dashboard navbar, fix missing padding (go-gitea#29940)
  Prevent layout shift in `<overflow-menu>` items (go-gitea#29831)
  Fix loadOneBranch panic (go-gitea#29938)
  ...
@lunny lunny modified the milestones: 1.23.0, 1.22.0 Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/done All backports for this PR have been created backport/v1.21 This PR should be backported to Gitea 1.21 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/js size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken links to anchors for non-ASCII symbols on wiki TOC
6 participants