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 external styles and script not hoisted #2414

Merged
merged 38 commits into from Feb 25, 2024

Conversation

yiwen101
Copy link
Contributor

@yiwen101 yiwen101 commented Feb 12, 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:

Resolve issue: #2300

Overview of changes:
Widen the scope of styles and script hoisting to include external and external manager.

Anything you'd like to highlight/discuss:

Testing instructions:
Init a markbind sample project.
In contents/topic1.md, add:

<h1>
<script>
      // JavaScript code specific to this component
      console.log("Inline script executed! 35");
</script>
<style>
      /* CSS styles specific to this component */
      h1 {
        color: red;
      }
</style>
h1 text
</h1>

In index.md, add

<panel src = "contents/topic1.md">
</panel>

You should see the following.The styles in the contents/topic1.md is applied to the content in the panel successfully.
Screenshot 2024-02-12 at 15 02 34

Proposed commit message: (wrap lines at 72 characters)

Hoist external's styles and script
Also add warning in documentation that styles in hoisted
external will also affect the rest of the page

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 Feb 12, 2024

Codecov Report

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

Project coverage is 48.92%. Comparing base (db27d8a) to head (ecfab66).

Files Patch % Lines
packages/core/src/External/External.ts 0.00% 3 Missing ⚠️
packages/core/src/External/ExternalManager.ts 0.00% 2 Missing ⚠️
packages/core/src/Page/index.ts 0.00% 1 Missing ⚠️
...ckages/core/src/html/scriptAndStyleTagProcessor.ts 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2414   +/-   ##
=======================================
  Coverage   48.92%   48.92%           
=======================================
  Files         124      124           
  Lines        5245     5245           
  Branches     1110     1110           
=======================================
  Hits         2566     2566           
+ Misses       2372     2371    -1     
- Partials      307      308    +1     

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

Copy link
Contributor

@kaixin-hc kaixin-hc left a comment

Choose a reason for hiding this comment

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

Just a preliminary look - do update this PR so that it can pass the checks!

docs/userGuide/components/presentation.md Outdated Show resolved Hide resolved
packages/core/src/External/External.ts Show resolved Hide resolved
@yiwen101 yiwen101 marked this pull request as draft February 13, 2024 09:37
@yucheng11122017
Copy link
Contributor

Hi @yiwen101 could you also add functional test cases? You can add it to test_site :)

@yiwen101
Copy link
Contributor Author

Hi @yiwen101 could you also add functional test cases? You can add it to test_site :)

Definitely. Thank you for pointing out to this.
I did not look at the test in the cli package and not aware of the exist of functional test. It looks like a wonderful way to test the code. Will work on that.

@yiwen101 yiwen101 marked this pull request as ready for review February 15, 2024 15:05
@EltonGohJH
Copy link
Contributor

@kaixin-hc @yucheng11122017 @itsyme

I think all in all looks okay to me from my testing. It seems that the style and script is hosited for Externals correctly. Feel free to chime in as I may have missed something!

  1. For the code cov check failure, we can ignore it right? (as externals is not tested in the first place)
  2. I think one point I am concerned about is that the style get hoisted so it becomes global to the page. So, lets say you make H1 red, it will become global to the page. Do you think we needed to add this the documentation? Since, I think it might be unexpected behaviour for most people. (Would there be unexpected consequence of a global hositing?)

@yiwen101
Copy link
Contributor Author

@kaixin-hc @yucheng11122017 @itsyme

I think all in all looks okay to me from my testing. It seems that the style and script is hosited for Externals correctly. Feel free to chime in as I may have missed something!

  1. For the code cov check failure, we can ignore it right? (as externals is not tested in the first place)
  2. I think one point I am concerned about is that the style get hoisted so it becomes global to the page. So, lets say you make H1 red, it will become global to the page. Do you think we needed to add this the documentation? Since, I think it might be unexpected behaviour for most people. (Would there be unexpected consequence of a global hositing?)

Thanks for the comment; yes this is a very valid concern.
Will modify the documentation to warn this unintended effect of including source that contains script and css.

How do you find the idea of adding a new attribute to allow the user to choose whether to hoist the css and script? Cause I feel a documentation warning is best when complemented with feature that providing user with more options. If you approve the idea, I will open a separate issue and work on adding the attribute in addition to documentation fix.

Meanwhile, s commented in #2300, probably worth opening a new issue on "finding a more proper fix that retains the script element placement instead of hoisting it downward." as well?

@EltonGohJH
Copy link
Contributor

@yiwen101
I think that I am not sure how useful that attribute would be. It would completely disable the style and script for that external component. What do you think?

Meanwhile, s commented in #2300, probably worth opening a new issue on "finding a more proper fix that retains the script element placement instead of hoisting it downward." as well?

For CSS wise, it is global because the styling is not unique to the element.

For script wise, I am not super sure whether it matters whether element placment matters (I do see that maybe we want the script to trigger when the element is shown but I do not think that this is trivial to implement).

@yiwen101
Copy link
Contributor Author

@yiwen101 I think that I am not sure how useful that attribute would be. It would completely disable the style and script for that external component. What do you think?

Meanwhile, s commented in #2300, probably worth opening a new issue on "finding a more proper fix that retains the script element placement instead of hoisting it downward." as well?

For CSS wise, it is global because the styling is not unique to the element.

For script wise, I am not super sure whether it matters whether element placment matters (I do see that maybe we want the script to trigger when the element is shown but I do not think that this is trivial to implement).

I see;

Regarding the usefulness of the attribute, if I am correct in my understanding, without this attribute, I will need to mention in documentation that:
1 Including a source with style will cause that to be applied globally
2 If do not want this, go to copy paste from the source rather than use "src= ...". ?

Point 2 does not look graceful. By adding a new attribute (not sure how long will take me though), we can replace point 2 with "If do not want this, add importStyle = false ... ", so it looks more elegant?

@EltonGohJH
Copy link
Contributor

@yiwen101
SFLR.

  1. I think that would be okay. You can add the documentations to Panels and Includes. I cant remember other than that where do we use this so do double check.

  2. I think you can give it a try if you really want. I feel that there is no need to go to such an extent as this is a feature that is not commonly used. It can be circumvented by copy pasting as stated by you.

@kaixin-hc
Copy link
Contributor

1 Including a source with style will cause that to be applied globally
2 If do not want this, go to copy paste from the source rather than use "src= ...". ?

@yiwen101 yep, just make this documentation change (you can also suggest that in the styles of the includes they could specifically target a class name that's not used globlly) and i think this PR is ready to go!

@yiwen101
Copy link
Contributor Author

@yiwen101 SFLR.

  1. I think that would be okay. You can add the documentations to Panels and Includes. I cant remember other than that where do we use this so do double check.
  2. I think you can give it a try if you really want. I feel that there is no need to go to such an extent as this is a feature that is not commonly used. It can be circumvented by copy pasting as stated by you.

1 Including a source with style will cause that to be applied globally
2 If do not want this, go to copy paste from the source rather than use "src= ...". ?

@yiwen101 yep, just make this documentation change (you can also suggest that in the styles of the includes they could specifically target a class name that's not used globlly) and i think this PR is ready to go!

I have updated the documentation required. Thank you for all your helps and comments throughout the process.

Copy link
Contributor

@kaixin-hc kaixin-hc left a comment

Choose a reason for hiding this comment

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

Minor nits on the documentation - but let me leave my approval here first : )

docs/userGuide/syntax/panels.md Outdated Show resolved Hide resolved
docs/userGuide/syntax/panels.md Outdated Show resolved Hide resolved
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! Thank you for all the changes.
@EltonGohJH you ok with this PR? If yes I think it can be merged!
Should it be considered a breaking change?

@EltonGohJH
Copy link
Contributor

@yucheng11122017
I agree that it can be merged.
I do not think it is a breaking change because it is more of a bug fix.
Users would expect that their external script and css to work but it does not work before this PR.
What do you think?

@yucheng11122017
Copy link
Contributor

@yucheng11122017 I agree that it can be merged. I do not think it is a breaking change because it is more of a bug fix. Users would expect that their external script and css to work but it does not work before this PR. What do you think?

OK sure sounds good! I was more concerned abt the global effect of script and css that was discussed here that made it sound like a breaking change but yeah I think bug fix sounds more appropriate.

@kaixin-hc kaixin-hc merged commit ef502db into MarkBind:master Feb 25, 2024
9 of 10 checks passed
@kaixin-hc
Copy link
Contributor

@all-contributors add @yiwen101 for code

Copy link
Contributor

@kaixin-hc

I've put up a pull request to add @yiwen101! 🎉

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.

None yet

4 participants