gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

WIP: Introduce class for sourcing block attributes from HTML

Open dmsnell opened this issue 3 years ago • 5 comments

What?

Makes it possible to source some block attributes from a chunk of HTML given the attribute definitions (as passed in registerBlock())

Why?

Who doesn't want sourced block attributes from the PHP side?

How?

Uses the WP_HTML_Tag_Processor and some basic assumptions about HTML structure to source attributes iff:

  • it can confidently parse the CSS selector in the attribute definition
  • it can construct a query to get that information out of the html

dmsnell avatar Dec 07 '22 04:12 dmsnell

👋 I'm now using get_content_inside_balanced_tags over at https://github.com/WordPress/block-hydration-experiments/pull/118#issuecomment-1351772785, and it's working great!

One thing that caught me a bit by surprise is that get_content_inside_balanced_tags moved the ... pointer thingy? past the closing tag. Could we maybe consider resetting it to where it started? I'm currently doing that manually, but it's a bit verbose -- especially given that get_content_inside_balanced_tags keeps track of the start and end internally (so it should be fairly easy to reset after getting the content).

ockham avatar Dec 14 '22 17:12 ockham

Given that WP_HTML_Processor's get_content_inside_balanced_tags is pretty handy in its own right, whereas the CSS selector logic might need a bit more work -- would it make sense to file a separate PR just for WP_HTML_Processor (and its tests)?

I thought that this might also unblock work on a set_content_inside_balanced_tags method, which we'll need for the Block Interactivity SSR stuff (see https://github.com/WordPress/block-hydration-experiments/pull/118) 😊

(I can file that PR if you're okay with this plan @dmsnell @adamziel)

ockham avatar Dec 14 '22 17:12 ockham

(I can file that PR if you're okay with this plan @dmsnell @adamziel)

@ockham consider everything in this PR tentative at best and more-than-likely to change. I don't even know if we'll end up with `get_content_inside_balanced_tags() or have something else instead.

Feel free to write your own PRs and explore different approaches and share here. This PR is more like my own notes as I work through the problem and try to understand where things belong and what the different parts of the system are.

dmsnell avatar Dec 14 '22 19:12 dmsnell

Flaky tests detected in 6103139e2119d6995ee26bce5f4ad2047707b6ac. Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3858581592 📝 Reported issues:

  • #39805 in specs/editor/various/block-deletion.test.js

github-actions[bot] avatar Jan 06 '23 21:01 github-actions[bot]

All the test cases that I thought up are passing as of 79c24bd, but if you pressed me to explain how or why I wouldn't be able to say 🙃

Current thought is to continue exploring cases and see how this now interacts with other tag processor operations and modifications, see how deeply we can nest. See about how this does when given example Core block output.

Please for all who review this consider it a draft that's likely to be unlike a final version. Would like to convert the iteration into recursion and remove the goto statements, would like to extract the WP_HTML_Processor() outside the select() function and allow for passing it around through recursion but also for prepping it on an input document.

dmsnell avatar Jan 09 '23 21:01 dmsnell