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

Rule based html validation against hydration #2493

Merged
merged 17 commits into from Apr 18, 2024

Conversation

yiwen101
Copy link
Contributor

@yiwen101 yiwen101 commented Apr 3, 2024

What is the purpose of this pull request?

  • Documentation update
  • Bug fix
  • Feature addition or enhancement
  • Code maintenance
  • DevOps
  • Improve developer experience
  • Others, please explain:

Work on #2025

Overview of changes:
Add extensible rule based validation check against hydration

Anything you'd like to highlight/discuss:
As commented in #2025, it turns out that modern browsers are quite robust and can tolerate most of the common html violations.
But still ideal to add check for violations that are fatal, and make checks for other violations optional.

I made the changes with extensibility in mind, as it is very likely that we will add further rules in the future.
Have not added test/documentation as not sure whether current implementation is desirable. Review required.

Testing instructions:
Add following in index.md

<table class="table">
   <tr>
     <th>Task ID</th>
     <th>Task</th>
     <th>Estimated Effort</th>
     <th>Prerequisite Task</th>
   </tr>
   <tr>
     <td>E</td>
     <td>Planning for next version</td>
     <td>1 man day</td>
     <td>D</td>
   </tr>
 </table>

Proposed commit message: (wrap lines at 72 characters)
Rule based html validation against hydration


Checklist: ☑️

  • Updated the documentation for feature additions and enhancements
  • Added tests for bug fixes or features
  • Linked all related issues
  • No unrelated changes

Reviewer checklist:

Indicate the SEMVER impact of the PR:

  • Major (when you make incompatible API changes)
  • Minor (when you add functionality in a backward compatible manner)
  • Patch (when you make backward compatible bug fixes)

At the end of the review, please label the PR with the appropriate label: r.Major, r.Minor, r.Patch.

Breaking change release note preparation (if applicable):

  • To be included in the release note for any feature that is made obsolete/breaking

Give a brief explanation note about:

  • what was the old feature that was made obsolete
  • any replacement feature (if any), and
  • how the author should modify his website to migrate from the old feature to the replacement feature (if possible).

Copy link

codecov bot commented Apr 3, 2024

Codecov Report

Attention: Patch coverage is 56.81818% with 19 lines in your changes are missing coverage. Please review.

Project coverage is 51.15%. Comparing base (104b46c) to head (addfded).

❗ Current head addfded differs from pull request most recent head ff5634c. Consider uploading reports for the commit ff5634c to get more accurate results

Files Patch % Lines
packages/core/src/utils/htmlValidationUtil.ts 57.14% 16 Missing and 2 partials ⚠️
packages/core/src/Page/index.ts 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2493      +/-   ##
==========================================
+ Coverage   51.01%   51.15%   +0.14%     
==========================================
  Files         124      125       +1     
  Lines        5389     5399      +10     
  Branches     1162     1157       -5     
==========================================
+ Hits         2749     2762      +13     
+ Misses       2350     2345       -5     
- Partials      290      292       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

Hi @yiwen101 thanks for this PR! It seems very interesting and will definitely be useful for our users.

One thing I wanted to ask is if the RuleBuilder is necessary? Could you initialise the Rule directly using a constructor?

Could you also add some test cases to test htmlValidationUtil.ts functionality? Like show that there is a warning/error when a table is missing a body and so on.

Another thing with regards to code quality is maybe we can abstract out the actual rules (like table needed a tbody) to a seperate file from htmlValidationUtil.ts. Then htmlValidationUtil.ts will load in these rules. So htmlValidationUtil.ts only handles creating rules while this other file will handle the actual rule content.

@kaixin-hc
Copy link
Contributor

Thanks for the extensibility considerations! I can definitely see it the code and I agree with the overall approach.

I second @/yucheng11122017 's comments though on whether we can use a constructor. I also don't like the idea of the pushRuleAndReset, because I feel like it opens the possibility where someone forgets to push a rule and maybe one of the other setter functions and then you don't get a well formed rule. If the user is expected to call all of those functions, it makes sense to me that they are all called at once.

Copy link
Contributor

@jingting1412 jingting1412 left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Just some nits on improving logger messages and property names. It would be good to add some tests and documentation for this as well.

Similar to other reviewers, I also think that encapsulating the existing rules to another class for example would be good to better manage rule registration and execution.

packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
@yiwen101 yiwen101 marked this pull request as draft April 12, 2024 11:34
@yiwen101
Copy link
Contributor Author

yiwen101 commented Apr 17, 2024

@kaixin-hc @tlylt @yucheng11122017 @jingting1412

Thank you for all the comments and sorry about the later reply.

On second thought, I feel that although this is a quite simple, trivial and straightforward addition, the current code still complicating it unnecessarily. I wish to remove all the over-engineered parts, and reduce this PR to a simple validation of table tag has table body (the only violation that for sure leads to hydration as of now). What is your guys idea on this?

Let me explain where the current design comes from, and why it sucks.

Initially I want to add an extensive rule based checkers. However as commented here, it turns out that modern browsers are quite robust and can tolerate most of the common html violations (eg: nesting block level in inline level, missing closing tag, missing quotation for href...) . So causeHydration tag is designed to differentiate harmless violations and violations that leads to hydration.

However, since harmless violations are so pervasive (eg: even in 404.md), the false positive rate is unacceptable. Logging check results for them may bury and hide the actual, valuable logs. I see no point wasting computation power and brain cell to fill the mental bandwidth of log reader with results that they do not need to know and do not need to fix.

With "table body missing" being the only known violations that will for sure leads to hydration, I want to remove all over-engineered parts and make it the simplest, most straightforward form. What are you thoughts on this?

@kaixin-hc
Copy link
Contributor

@yiwen101 Thanks for sharing your reasoning! Yes, it was a good surprise to me as well how robust current browsers are.

As shared in earlier comments I have some issues with the code in this "over-engineered" form. I support a PR specifically targeting "table body missing" especially if it's a small change; we can always change the code that checks this if we discover that there are more cases we must check.

@yucheng11122017
Copy link
Contributor

@kaixin-hc @tlylt @yucheng11122017 @jingting1412

Thank you for all the comments and sorry about the later reply.

On second thought, I feel that although this is a quite simple, trivial and straightforward addition, the current code still complicating it unnecessarily. I wish to remove all the over-engineered parts, and reduce this PR to a simple validation of table tag has table body (the only violation that for sure leads to hydration as of now). What is your guys idea on this?

Let me explain where the current design comes from, and why it sucks.

Initially I want to add an extensive rule based checkers. However as commented here, it turns out that modern browsers are quite robust and can tolerate most of the common html violations (eg: nesting block level in inline level, missing closing tag, missing quotation for href...) . So causeHydration tag is designed to differentiate harmless violations and violations that leads to hydration.

However, since harmless violations are so pervasive (eg: even in 404.md), the false positive rate is unacceptable. Logging check results for them may bury and hide the actual, valuable logs. I see no point wasting computation power and brain cell to fill the mental bandwidth of log reader with results that they do not need to know and do not need to fix.

With "table body missing" being the only known violations that will for sure leads to hydration, I want to remove all over-engineered parts and make it the simplest, most straightforward form. What are you thoughts on this?

This is great reasoning and very well thought through and researched! I am ok to keep it simple :)

@yiwen101 yiwen101 marked this pull request as ready for review April 17, 2024 17:09
@yiwen101
Copy link
Contributor Author

@kaixin-hc @yucheng11122017
Thank you for your response and input. I have rewritten the code and added test and docs.

Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

Some comments! Thanks for the work

docs/devGuide/design/serverSideRendering.md Outdated Show resolved Hide resolved
packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
packages/core/test/unit/utils/HtmlValidationUtil.test.ts Outdated Show resolved Hide resolved
packages/core/test/unit/utils/HtmlValidationUtil.test.ts Outdated Show resolved Hide resolved
packages/core/test/unit/utils/html_str/correct_html.txt Outdated Show resolved Hide resolved
yiwen101 and others added 5 commits April 18, 2024 11:57
Co-authored-by: Chan Yu Cheng <77204346+yucheng11122017@users.noreply.github.com>
Co-authored-by: Chan Yu Cheng <77204346+yucheng11122017@users.noreply.github.com>
Co-authored-by: Chan Yu Cheng <77204346+yucheng11122017@users.noreply.github.com>
Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

Some nits

packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
packages/core/src/utils/htmlValidationUtil.ts Outdated Show resolved Hide resolved
packages/core/test/unit/utils/HtmlValidationUtil.test.ts Outdated Show resolved Hide resolved
packages/core/test/unit/utils/HtmlValidationUtil.test.ts Outdated Show resolved Hide resolved
yiwen101 and others added 4 commits April 18, 2024 15:11
Co-authored-by: Chan Yu Cheng <77204346+yucheng11122017@users.noreply.github.com>
Co-authored-by: Chan Yu Cheng <77204346+yucheng11122017@users.noreply.github.com>
Copy link
Contributor

@EltonGohJH EltonGohJH left a comment

Choose a reason for hiding this comment

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

IMO, good to go.
After resolving all comments.

packages/core/src/utils/htmlValidationUtil.ts Show resolved Hide resolved
Copy link
Contributor

@EltonGohJH EltonGohJH left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

LGTM!
@yiwen101 you can trying merging this PR yourself :)

@yiwen101
Copy link
Contributor Author

LGTM!
@yiwen101 you can trying merging this PR yourself :)

Noted, thanks.

@yiwen101 yiwen101 merged commit f616fd2 into MarkBind:master Apr 18, 2024
7 checks passed
Copy link

@yiwen101 Each PR must have a SEMVER impact label, please remember to label the PR properly.

@yiwen101 yiwen101 added r.Patch Version resolver: increment by 0.0.1 r.Minor Version resolver: increment by 0.1.0 and removed r.Patch Version resolver: increment by 0.0.1 r.Minor Version resolver: increment by 0.1.0 labels Apr 18, 2024
itsyme pushed a commit that referenced this pull request Apr 19, 2024
* Add rule based html validation against Vue hydration issue

* Improve docs

* Update test

* Add rule to check against table tag without tboby

* Update docs/devGuide/design/serverSideRendering.md

* Update packages/core/test/unit/utils/HtmlValidationUtil.test.ts

* Update packages/core/src/utils/htmlValidationUtil.ts

* Update packages/core/test/unit/utils/HtmlValidationUtil.test.ts
---------

Co-authored-by: Chan Yu Cheng <77204346+yucheng11122017@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
r.Patch Version resolver: increment by 0.0.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants