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

HTML API: Use create_fragment_at_node to allow more contexts in create_fragment #7777

Open
wants to merge 47 commits into
base: trunk
Choose a base branch
from

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Nov 12, 2024

This PR builds on (and requires) #7348.

This modifies ::create_fragment( $html, $context ) to use a full processor and create_fragment_at_node instead of the other way around. This makes more sense and makes the main factory methods more clear, where the state required for fragments is set up in create_fragment_at_node instead of in both create_fragment and create_fragment_at_current_node.

This allows for more HTML contexts to be provided to the basic create_fragment where the provided context HTML is appended to <!DOCTYPE html>, a full processor is created, the last tag opener is found, and a fragment parser is created at that node via create_fragment_at_current_node.

The HTML5lib tests are updated accordingly to use this new method to create fragments.

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

Closes #7141.


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.

null should not be returned in this case, but it is part of the signature
and should be covered here.
…i/use-create-fragment-at-node-for-main-create-fragment-method
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

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

I spent some amount of time scrutinizing this, and it seems like you have done an exceptional job here @sirreal.

At first I didn’t like accepting arbitrary HTML as the context — there’s a hidden performance surprise in there. On the other hand, I realize that to do this properly we have to build the stack of open elements, and the way that your create_fragment_at_current_node() handles that with resetting the bookmarks while leaving the stack of open elements intact seem fitting.

Do we need to prefix the DOCTYPE declaration though? What if someone wants a parser in <!DOCTYPE quirks><p><table><td>? I don’t know if that makes sense or is warranted.

@sirreal
Copy link
Member Author

sirreal commented Nov 13, 2024

there’s a hidden performance surprise in there

I thought about that. One thing we could do is optimize the most common case where the context is <body> and have some optimized setup that skips creating a full parser.

Do we need to prefix the DOCTYPE declaration though? What if someone wants a parser in <!DOCTYPE quirks><p><table><td>? I don’t know if that makes sense or is warranted.

I added the DOCTYPE because I suspect we usually want to be in no-quirks mode. If that is not added, then the <body> context that has been used until now would behave differently. The advanced option is still available to create a full parser and then create a fragment parser from that if folks really want a fragment in quirks mode.

I'd like to better document this behavior and explain the more advanced option.

@dmsnell
Copy link
Member

dmsnell commented Nov 13, 2024

I thought about that. One thing we could do is optimize the most common case where the context is and have some optimized setup that skips creating a full parser.

The opposite of that is that <body> doesn’t require that much processing in a case like this.

The advanced option is still available…I'd like to better document this behavior and explain the more advanced option.

This seems proper. That makes a safe default with an escape hatch for those who want to do something where they adopt their own risk.

…i/use-create-fragment-at-node-for-main-create-fragment-method
sirreal added a commit to sirreal/wordpress-develop that referenced this pull request Nov 20, 2024
@sirreal
Copy link
Member Author

sirreal commented Nov 20, 2024

This can pull in tests from #7141 and should also do this (mentioned in that description):

Review all documentation looking for places stating that is the only supported context.

@sirreal
Copy link
Member Author

sirreal commented Nov 21, 2024

I've pulled some valuable changes like tests and documentation from #7141 into this PR.

@sirreal sirreal marked this pull request as ready for review November 21, 2024 19:22
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, dmsnell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@sirreal
Copy link
Member Author

sirreal commented Nov 21, 2024

#7348 has landed. I've merged trunk, added some improvements like tests and _doing_it_wrong from #7141.

I'm opening this up for review now 🎉

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.

2 participants