Make WordPress Core

Opened 8 months ago

Closed 6 months ago

Last modified 6 months ago

#61044 closed enhancement (fixed)

Interactivity API: Debug - Warning about Server Directives Processing errors.

Reported by: cbravobernal's profile cbravobernal Owned by: cbravobernal's profile cbravobernal
Milestone: 6.6 Priority: normal
Severity: normal Version: 6.5
Component: Interactivity API Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description (last modified by cbravobernal)

This PR aims to improve the developer experience of the Interactivity API server directives processing.
Right now, the SSR won't work if there is any directive inside a SVG or MATHML tag. Or the HTML is unbalanced (there is some closer tag missing).

This PR adds a warning in those cases, if the developer is in debug mode.

I added also notices for cases where the directive is empty, or the namespace is an empty string, an empty object or null.

https://github.com/WordPress/wordpress-develop/assets/37012961/1eb1adec-d99f-42ed-a0d1-5a635cf4001a

https://github.com/WordPress/wordpress-develop/assets/37012961/1d8e3d17-0696-4ad6-abb5-fcad059e8562

https://github.com/WordPress/wordpress-develop/assets/37012961/c5b0f34a-47b3-40f9-8ced-68ee3702b062

Change History (16)

This ticket was mentioned in PR #6413 on WordPress/wordpress-develop by @cbravobernal.


8 months ago
#1

  • Keywords has-patch added

Trac ticket: https://core.trac.wordpress.org/ticket/61044

Draft PR. Is still work in progress.

#2 @cbravobernal
7 months ago

  • Milestone changed from Awaiting Review to 6.6
  • Type changed from defect (bug) to enhancement

#3 @jonsurrell
7 months ago

  • Component changed from General to Interactivity API

#4 @gziolo
7 months ago

  • Version set to 6.5

@cbravobernal commented on PR #6413:


7 months ago
#5

I think the messages could be a bit more brief (#6413 (review)) but this is working well generally. Thanks!

Done in https://github.com/WordPress/wordpress-develop/pull/6413/commits/4b78690b19728bab8cf9b69c6eca0178bb0c3987

@cbravobernal commented on PR #6413:


7 months ago
#6

I'm adding more checks to the SSR processing. Now it will also check that the namespace is not {}, "" "empty string", or null written as a string.
That way it won't cause weird behaviors in the runtime.

#7 @cbravobernal
7 months ago

  • Description modified (diff)
  • Owner set to cbravobernal
  • Status changed from new to assigned

@czapla commented on PR #6413:


6 months ago
#8

Overall, it looks good and is close to be ready to merge IMO. I left a comment and had the same comments as David above.

Also, it would be good to add a unit test that asserts that the warning message appears correctly in the right circumstances.

It could be something like:

/**
         * Tests that the `process_directives` issues a warning when the HTML
         * containing directives contains unbalanced tags.
         *
         * @ticket 61044
         *
         * @covers ::process_directives
         *
         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
         */
        public function test_process_directives_warns_on_unbalanced_tags() {
                $this->expectOutputString( 'Interactivity directives failed to process in "" due to a missing "P" end tag' );

                $this->interactivity->state( 'myPlugin', array( 'id' => '1231341324' ) );

                $this->interactivity->process_directives(
                        '<div>
                                <p data-wp-bind--id="myPlugin::state.id">
                                        Hello, world
                        </div>
                        </div>
                '
                );
        }

I have tried adding that test but I wasn't able to make it work for some reason:

  • The warning message seems to be hidden when using the @expectedIncorrectUsage WP_Interactivity_API::process_directives_args annotation.
  • When not using the @expectedIncorrectUsage WP_Interactivity_API::process_directives_args annotation, I used the $this->expectOutputString() or $this->expectNoticeMessage() but they don't seem to be triggered by the doing_it_wrong() call in the body of process_directives().

@cbravobernal commented on PR #6413:


6 months ago
#9

Overall, it looks good and is close to be ready to merge IMO. I left a comment and had the same comments as David above.

Also, it would be good to add a unit test that asserts that the warning message appears correctly in the right circumstances.

It could be something like:

	/**
	 * Tests that the `process_directives` issues a warning when the HTML
	 * containing directives contains unbalanced tags.
	 *
	 * @ticket 61044
	 *
	 * @covers ::process_directives
	 *
	 * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
	 */
	public function test_process_directives_warns_on_unbalanced_tags() {
		$this->expectOutputString( 'Interactivity directives failed to process in "" due to a missing "P" end tag' );

		$this->interactivity->state( 'myPlugin', array( 'id' => '1231341324' ) );

		$this->interactivity->process_directives(
			'<div>
				<p data-wp-bind--id="myPlugin::state.id">
					Hello, world
			</div>
			</div>
		'
		);
	}

I have tried adding that test but I wasn't able to make it work for some reason:

  • The warning message seems to be hidden when using the @expectedIncorrectUsage WP_Interactivity_API::process_directives_args annotation.
  • When not using the @expectedIncorrectUsage WP_Interactivity_API::process_directives_args annotation, I used the $this->expectOutputString() or $this->expectNoticeMessage() but they don't seem to be triggered by the doing_it_wrong() call in the body of process_directives().

The only case that makes me think about it would be how would work in a future a test like this with two different warnings. 🤔

@czapla commented on PR #6413:


6 months ago
#10

The only case that makes me think about it would be how would work in a future a test like this with two different warnings. 🤔

I'm sorry, I don't understand what you mean. Why would there be two different warnings?

@darerodz commented on PR #6413:


6 months ago
#11

I'm sorry, I don't understand what you mean. Why would there be two different warnings?

I think he means that the only warning message the process_directives_args method outputs is the one for unbalanced tags, and thus, testing the warning message content is not required.

Although the message content changes depending on the tag that caused the error, right? @cbravobernal

@cbravobernal commented on PR #6413:


6 months ago
#12

I think he means that the only warning message the process_directives_args method outputs is the one for unbalanced tags, and thus, testing the warning message content is not required.

I mean that. Maybe in a future, we warn for another cause (I don't know, a directive value processed in that function that requires any specific rule).
We only test that a warning is expected, not which one.

Although the message content changes depending on the tag that caused the error, right?

Yes, the message includes the tag processed when the unbalance variable sets to true.

@cbravobernal commented on PR #6413:


6 months ago
#13

Now we differentiate between SVG and MATH tags and their inner tags with slightly different messages.
All tests has been updated.
https://github.com/WordPress/wordpress-develop/pull/6413/commits/b15ad5f3cb9f6ac1f8249d50908d675cb9673204

#14 @gziolo
6 months ago

  • Keywords has-unit-tests commit added

#15 @gziolo
6 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 58321:

Interactivity API: Print debug warning when server directives processing encounters errors

Aims to improve the developer experience of the Interactivity API server directives processing.

Props cbravobernal, jonsurrell, westonruter, darerodz, czapla, gziolo.
Fixes #61044.

Note: See TracTickets for help on using tickets.