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

Add text template #49

Conversation

kt-12
Copy link

@kt-12 kt-12 commented Mar 10, 2023

Trac ticket:


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@kt-12 kt-12 self-assigned this Mar 14, 2023
@kt-12 kt-12 added the Script Loading Strategy Issues relating to the Script Loading Strategy (WPP) label Mar 14, 2023
@10upsimon 10upsimon self-requested a review March 22, 2023 12:07
Copy link

@10upsimon 10upsimon left a comment

Choose a reason for hiding this comment

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

@kt-12 this is looking good. I am approving as I did not see the missing docblocks for test data providers as a total blocker, but if you can add them that would be great!

Copy link
Collaborator

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

I found a couple of things here that I think we need to address before merging.

$after_non_standalone_handle = $this->print_inline_script( $handle, 'after-non-standalone', false );

if ( $after_non_standalone_handle ) {
$after_handle .= sprintf( "<script%s id='%s-js-after' type='text/template'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_non_standalone_handle );
Copy link
Collaborator

Choose a reason for hiding this comment

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

This approach seems wrong to me. Shouldn't we instead change the value of $this->type_attr rather than hard coding a second type attribute onto the script? You can even see in the unit tests below that the scripts are getting two type attributes applied to them. Alternately, if we don't want to update the main type_attr property on this class, we should at least remove it when we are hard coding a text/template type.

Copy link
Author

@kt-12 kt-12 Mar 31, 2023

Choose a reason for hiding this comment

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

Thanks for the catch @joemcgill. Totally my fault here, shouldn't have slipped under my radar. I was focusing on the condition and getting text/template, I forgot to see it was injecting text/javascript too.

Fixed now.

@@ -482,7 +498,11 @@ public function print_inline_script( $handle, $position = 'after', $display = tr
$output = trim( implode( "\n", $output ), "\n" );

if ( $display ) {
printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
if ( 'after-non-standalone' === $position ) {
printf( "<script%s id='%s-js-after' type='text/template'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we're hard coding the position in the ID here, I think we also need to remove the $position variable that's being passed to the printf function. We should probably add a unit test to cover this case as well.

Suggested change
printf( "<script%s id='%s-js-after' type='text/template'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
printf( "<script%s id='%s-js-after' type='text/template'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output );

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @joemcgill. This is fixed now.

kt-12 and others added 4 commits March 31, 2023 08:42
@kt-12 kt-12 requested review from joemcgill and 10upsimon March 31, 2023 08:59
Copy link
Collaborator

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

Thanks @kt-12, this is looking better now.

Base automatically changed from enhancement/update-wp-add-inline-script-standalone to enhancement/wp-add-inline-script-standalone April 1, 2023 00:43
Copy link

@10upsimon 10upsimon left a comment

Choose a reason for hiding this comment

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

@kt-12 I did another round of review on this and it 's looking good to me. Good catch on some of those issues @joemcgill thank you!

Base automatically changed from enhancement/wp-add-inline-script-standalone to enhancement/wp-script-api-strategy-base April 3, 2023 05:43
@10upsimon 10upsimon merged commit 0007785 into enhancement/wp-script-api-strategy-base Apr 3, 2023
@10upsimon 10upsimon deleted the enhancement/add_text_template branch April 3, 2023 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Script Loading Strategy Issues relating to the Script Loading Strategy (WPP)
Projects
No open projects
3 participants