Commit Graph

189 Commits

Author SHA1 Message Date
Weston Ruter
4a0e4153eb Media: Add optimization support for IMG tags with fetchpriority=low or fetchpriority=auto.
This updates `wp_get_loading_optimization_attributes()` and `wp_maybe_add_fetchpriority_high_attr()` to account for cases where an `IMG` has `fetchpriority=low` or `fetchpriority=auto`:

* `IMG` tags with `fetchpriority=low` are not lazy-loaded since they may be in a Navigation overlay, Details block, or Accordion Item block and need to be loaded the instant the user toggles the block.  
* `IMG` tags with `fetchpriority=auto` do not increase the media count since they may be hidden in a viewport by block visibility settings.
* Blocks with conditional visibility (such as hidden on mobile or desktop) now automatically add `fetchpriority="auto"` to their contained `IMG` tags to prevent them from erroneously receiving `fetchpriority=high` or affecting the lazy-loading of subsequent images. 
* An `IMG` with `fetchpriority=auto` which also surpasses the `wp_min_priority_img_pixels` threshold will prevent a subsequent image from getting `fetchpriority=high`. 

Developed in https://github.com/WordPress/wordpress-develop/pull/11196
Includes backport of [https://github.com/WordPress/gutenberg/pull/76302 Gutenberg#76302]. 

See related Gutenberg issues:

- [https://github.com/WordPress/gutenberg/issues/76181 76181]: Image in navigation overlay can get `fetchpriority=high` and degrade LCP metric for page.
- [https://github.com/WordPress/gutenberg/issues/76268 76268]: Image in collapsed Details block may erroneously get `fetchpriority=high` even though hidden.
- [https://github.com/WordPress/gutenberg/issues/76301 76301]: Block Visibility: `IMG` in viewport-conditional block may get `fetchpriority=high` even when not displayed.
- [https://github.com/WordPress/gutenberg/issues/76335 76335]: Image in collapsed Accordion block may erroneously get `fetchpriority=high` even though hidden.

Follow-up to r56347, r56037.

Props westonruter, mukesh27, ramonopoly, wildworks.
See #58235.
Fixes #64823.

Built from https://develop.svn.wordpress.org/trunk@61934


git-svn-id: http://core.svn.wordpress.org/trunk@61216 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-03-11 06:20:49 +00:00
John Blackbourn
f6a2aea6c4 HTML API: Prevent WP_HTML_Tag_Processor instances being unserialized and add some extra logic for validating pattern and template file paths.
Props dmsnell, xknown, jonsurrell, peterwilsoncc, johnbillion.

Built from https://develop.svn.wordpress.org/trunk@61880


git-svn-id: http://core.svn.wordpress.org/trunk@61162 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-03-10 12:20:43 +00:00
jonsurrell
bd6e249954 HTML API: Check tag namespace in ::set_modifiable_text().
The method should only apply to special "atomic" HTML tags like `SCRIPT` or `TEXTAREA`. `::set_modifiable_text()` should not apply to tags with the same name in other namespaces.

Developed in https://github.com/WordPress/wordpress-develop/pull/11083.

Props jonsurrell, dmsnell, westonruter.
Fixes #64751.

Built from https://develop.svn.wordpress.org/trunk@61796


git-svn-id: http://core.svn.wordpress.org/trunk@61102 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-03-03 14:59:46 +00:00
jonsurrell
7ae743a4d9 HTML API: Exclude internal classes and methods from documentation.
Add `@ignore` annotations to internal HTML API methods and classes to exclude them from generated documentation. The HTML API relies heavily on internal methods and classes and they create a lot of noise in documentation.

Developed in https://github.com/WordPress/wordpress-develop/pull/11078.

Props jonsurrell, westonruter.
See #64224.

Built from https://develop.svn.wordpress.org/trunk@61793


git-svn-id: http://core.svn.wordpress.org/trunk@61099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-03-03 13:11:46 +00:00
jonsurrell
28b9161f17 HTML API: Remove incorrect "stub implementation" since annotations.
Developed in https://github.com/WordPress/wordpress-develop/pull/11076.

Props jonsurrell, westonruter.
See #64224.

Built from https://develop.svn.wordpress.org/trunk@61792


git-svn-id: http://core.svn.wordpress.org/trunk@61098 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-03-03 12:37:47 +00:00
jonsurrell
8cd3628f37 HTML API: Increase HTML Processor bookmark limit to 10,000.
The limit was overly conservative and bookmark exhaustion may occur on realistic documents. The increased limit allows documents with much greater depth to be safely processed.

The limit is increased from 100 to 10,000.

Developed in https://github.com/WordPress/wordpress-develop/pull/10820.

Follow-up to [61755].

Props jonsurrell, dmsnell, westonruter.
See #64394.

Built from https://develop.svn.wordpress.org/trunk@61756


git-svn-id: http://core.svn.wordpress.org/trunk@61062 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-02-27 15:18:46 +00:00
jonsurrell
72d3fb0034 HTML API: Prevent bookmark exhaustion from throwing.
When bookmark exhaustion occurs during processing, return `false` instead of throwing an `Exception`.

Developed in https://github.com/WordPress/wordpress-develop/pull/10616.

Props jonsurrell, westonruter, dmsnell.
Fixes #64394.

Built from https://develop.svn.wordpress.org/trunk@61755


git-svn-id: http://core.svn.wordpress.org/trunk@61061 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-02-27 13:06:44 +00:00
jonsurrell
9e1e6914e7 HTML API: Preserve ::set_modifiable_text() TEXTAREA leading newlines.
HTML specifies that a single newline is ignored at the start of a `TEXTAREA`. If `::set_modifiable_text()` is called with a leading newline, ensure it is preserved in the resulting HTML.

Developed in https://github.com/WordPress/wordpress-develop/pull/11062.

Props jonsurrell, dmsnell.
See #64609.

Built from https://develop.svn.wordpress.org/trunk@61754


git-svn-id: http://core.svn.wordpress.org/trunk@61060 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-02-27 12:56:45 +00:00
jonsurrell
7e9ef6641a HTML API: Preserve newlines when normalizing special elements.
Ensures normalization preserves content in `PRE`, `LISTING`, and `TEXTAREA` elements. These elements ignore a single leading newline during parsing. Normalization now injects a newline after the tag opener to trigger this behavior, preventing significant newlines from being incorrectly stripped.

Developed in https://github.com/WordPress/wordpress-develop/pull/10871.

Props jonsurrell, dmsnell, mukesh27.
Fixes #64607.

Built from https://develop.svn.wordpress.org/trunk@61747


git-svn-id: http://core.svn.wordpress.org/trunk@61053 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-02-26 10:37:39 +00:00
Weston Ruter
bd3f20e8c7 Build/Test Tools: Integrate PHPStan into the core development workflow.
This change introduces PHPStan static analysis configured at [https://phpstan.org/user-guide/rule-levels rule level 0], which includes: "basic checks, unknown classes, unknown functions, unknown methods called on `$this`, wrong number of arguments passed to those methods and functions, always undefined variables". Contributors may elect for a higher PHPStan rule level by creating a `phpstan.neon` which overrides `phpstan.neon.dist`.

* Fix various PHPStan level 0 errors by adding `@phpstan-ignore` comments, updating PHPDoc types, and adding missing return values.
* Remove existing `@phpstan-ignore` comments that are now obsolete or inapplicable for level 0.
* Add a new GitHub Actions workflow for PHPStan Static Analysis. Reports are currently provided as warnings with inline annotations in pull requests and do not fail the build.
* Add a `phpstan` Grunt task and include it in the `precommit:php` task to run before `phpunit`.
* Introduce a `typecheck:php` npm script and a `composer phpstan` script to run analysis in local development environments.
* Add documentation for PHPStan usage in `tests/phpstan/README.md`.

Developed in https://github.com/WordPress/wordpress-develop/pull/10419

Props justlevine, westonruter, johnbillion, desrosj, SirLouen, dmsnell, oglekler, joehoyle, jorbin.
See #64238, #63268, #52217, #51423.
Fixes #61175.

Built from https://develop.svn.wordpress.org/trunk@61699


git-svn-id: http://core.svn.wordpress.org/trunk@61007 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-02-20 02:25:46 +00:00
jonsurrell
6109c149d5 HTML API: Use more universal syntax to call Closure variable.
Replace `( $fn )( ...$args )` syntax with equivalent `call_user_func( $fn, ...$args )` to prevent an issue with the WordPress documentation parser. The parser would fail when encountering this syntax, preventing documentation generation.

Developed in https://github.com/WordPress/wordpress-develop/pull/10907.

Follow-up to [58304].

Props dmsnell, jonsurrell.
See #64224, #61348.

Built from https://develop.svn.wordpress.org/trunk@61624


git-svn-id: http://core.svn.wordpress.org/trunk@60935 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-02-12 15:48:42 +00:00
jonsurrell
ed9ef8618b HTML API: Escape script tag contents automatically.
When setting JavaScript or JSON script tag content, automatically escape sequences like `<script>` and `</script>`. This renders the content safe for HTML. The semantics of any JSON and virtually any JavaScript are preserved.

Script type detection follows the HTML standard for identifying JavaScript and JSON script tags. Other script types continue to reject potentially dangerous content.

Developed in https://github.com/WordPress/wordpress-develop/pull/10635.

Props jonsurrell, dmsnell, westonruter.
Fixes #64419. See #63851, #51159.


Built from https://develop.svn.wordpress.org/trunk@61477


git-svn-id: http://core.svn.wordpress.org/trunk@60789 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-01-13 13:13:48 +00:00
jonsurrell
9d0d7c1b18 HTML API: Ensure correct encoding of modified class names.
Some class names with HTML character references could be mishandled, for example:
- Failure to remove an existing class like `&` with `::remove_class( '&' )`
- Double-encoding of an existing class like `&` after a modification, becoming `&amp;`

The second case manifested after double-encoding prevention was removed from `::set_attribute()` in [60919].

Developed in https://github.com/WordPress/wordpress-develop/pull/10591.

Props jonsurrell, dmsnell.
Fixes #64340.

Built from https://develop.svn.wordpress.org/trunk@61346


git-svn-id: http://core.svn.wordpress.org/trunk@60658 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-12-03 17:27:31 +00:00
Weston Ruter
78bb7f180e Docs: Fix return type for WP_HTML_Decoder::read_character_reference().
Developed in https://github.com/WordPress/wordpress-develop/pull/8945

Follow-up to [58281].

Props justlevine.
See #64238, #61072.

Built from https://develop.svn.wordpress.org/trunk@61283


git-svn-id: http://core.svn.wordpress.org/trunk@60595 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-11-23 06:00:26 +00:00
dmsnell
65e16daded HTML API: Replace PCRE in set_attribute() with new UTF-8 utility.
The HTML API has relied upon a single PCRE to determine whether to allow setting certain attribute names. This was because those names aren’t allowed to contain Unicode noncharacters, but detecting noncharacters without a UTF-8 parser is nontrivial.

In this change the direct PCRE has been replaced with a number of `strcpn()` calls and a call to the newer `wp_has_noncharacters()` function. Under the hood, this function will still defer to a PCRE if Unicode support is available, but otherwise will fall back to the UTF-8 pipeline in Core.

This change removes the platform variability, making the HTML API more reliable when Unicode support for PCRE is lacking.

Developed in https://github.com/WordPress/wordpress-develop/pull/9798
Discussed in https://core.trac.wordpress.org/ticket/63863

See #63863.

Built from https://develop.svn.wordpress.org/trunk@61003


git-svn-id: http://core.svn.wordpress.org/trunk@60339 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-10-21 03:50:28 +00:00
dmsnell
3d64361ffa HTML API: Escape all submitted HTML character references.
The HTML API has relied on `esc_attr()` and `esc_html()` when setting string attribute values or the contents of modifiable text. This leads to unexpected behavior when those functions attempt to prevent double-escaping of existing character references, and it can make certain contents impossible to represent.

After this change, the HTML API will reliably escape all submitted plaintext such that it appears in the browser the way it was submitted to the HTML API, with all character references escaped. This does not change the behavior of how URL attributes are escaped.

Developed in https://github.com/WordPress/wordpress-develop/pull/10143
Discussed in https://core.trac.wordpress.org/ticket/64054

Props dmsnell, jonsurrell, westonruter.
Fixes #64054.

Built from https://develop.svn.wordpress.org/trunk@60919


git-svn-id: http://core.svn.wordpress.org/trunk@60255 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-10-09 23:38:32 +00:00
jonsurrell
b271950c27 HTML API: Ensure non-string HTML input is safely handled.
Prevents an issue where passing `null` to HTML API constructors could result in runtime errors.

Developed in https://github.com/WordPress/wordpress-develop/pull/9545.

Props kraftbj, jonsurrell, westonruter.
Fixes #63854.

Built from https://develop.svn.wordpress.org/trunk@60887


git-svn-id: http://core.svn.wordpress.org/trunk@60223 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-10-01 12:59:32 +00:00
John Blackbourn
dc0b4ceef5 Code Modernization: Revert [60796]. This change needs some more work before it's fully ready.
See #63962

Built from https://develop.svn.wordpress.org/trunk@60804


git-svn-id: http://core.svn.wordpress.org/trunk@60140 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-09-28 21:56:27 +00:00
Sergey Biryukov
1780c6ec3d Code Modernization: Address __sleep() and __wakeup() deprecations in PHP 8.5.
PHP 8.5 deprecates the `__sleep()` and `__wakeup()` magic methods in favor of `__serialize()` and `__unserialize()`:
> `Deprecated:  The __wakeup() serialization magic method has been deprecated. Implement __unserialize() instead (or in addition, if support for old PHP versions is necessary)`

For PHP < 7.4 compatibility, `__sleep()` and `__wakeup()` need to be kept for the time being.

This commit moves the logic of `__wakeup()` methods in core to `__unserialize()`, and turns the former into wrappers. WordPress core does not use `__sleep()` methods, so these are the only changes required.

Reference: [https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_sleep_and_wakeup_magic_methods PHP RFC: Deprecations for PHP 8.5: Deprecate the __sleep() and __wakeup() magic methods].

Follow-up to [56835], [60787], [60795].

Props TobiasBg, tusharbharti, swissspidy, dmsnell, SergeyBiryukov.
Fixes #63962. See #63061.
Built from https://develop.svn.wordpress.org/trunk@60796


git-svn-id: http://core.svn.wordpress.org/trunk@60132 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-09-23 19:28:28 +00:00
jonsurrell
d4a1644618 HTML API: Prevent adding dangerous double-escape SCRIPT contents.
Prevent WP_Tag_Processor::set_modifiable_text() from allowing SCRIPT contents with "<script" like it does with "</script". Either of these sequences may affect the script element's close.

Developed in https://github.com/WordPress/wordpress-develop/pull/9560.

Props jonsurrell, westonruter, dmsnell.
See #63738.

Built from https://develop.svn.wordpress.org/trunk@60706


git-svn-id: http://core.svn.wordpress.org/trunk@60042 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-09-04 14:40:27 +00:00
jonsurrell
0ac15a8b8c HTML API: Improve script tag escape state processing.
Addresses some edge cases parsing of script tag contents:

- "<!-->" remains in the unescaped state and does not enter the escaped state.
- Contents in the escaped state that end with "<script" do not enter double-escaped state.
- "\f" (Form Feed) was missing as a tag name terminating character.

Developed in https://github.com/WordPress/wordpress-develop/pull/9397 and https://github.com/WordPress/wordpress-develop/pull/9402.

Props jonsurrell, dmsnell.
See #63738.

Built from https://develop.svn.wordpress.org/trunk@60649


git-svn-id: http://core.svn.wordpress.org/trunk@59985 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-08-19 19:09:31 +00:00
jonsurrell
4f8fc714a4 HTML API: Fix typo in indicated_compatibility_mode.
Developed in https://github.com/WordPress/wordpress-develop/pull/9401.

Follow-up to [60540].

Props jonsurrell, dmsnell.
Fixes #63391.

Built from https://develop.svn.wordpress.org/trunk@60647


git-svn-id: http://core.svn.wordpress.org/trunk@59983 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-08-19 15:08:31 +00:00
dmsnell
2191d0443c HTML API: Make WP_HTML_Processor::serialize_token() public.
The serialize_token() method was added in WordPress 6.7.0 as a protected member on the WP_HTML_Processor class. It wasn’t clear at the time of merging if it would be necessary to expose it as a public method. However, since that time a number of experiments have led to the conclusion that it would be very valuable to do so.

This patch opens up the method for invocation from the outside, trivializing the generation of normative HTML subspans from a parent document.

Developed in https://github.com/WordPress/wordpress-develop/pull/9456
Discussed in https://core.trac.wordpress.org/ticket/63823

Follow-up to [59076].

Props dmsnell, jonsurrell.
Fixes #38044.

Built from https://develop.svn.wordpress.org/trunk@60633


git-svn-id: http://core.svn.wordpress.org/trunk@59969 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-08-13 17:58:28 +00:00
jonsurrell
f2b80c32ee HTML API: Reduce length checks in skip_script_data.
Apply an optimization to remove several repeated string length checks in `WP_HTML_Tag_Processor::skip_script_data()`.

Developed in https://github.com/WordPress/wordpress-develop/pull/9230.

Props jonsurrell, dmsnell.
See #63738.

Built from https://develop.svn.wordpress.org/trunk@60617


git-svn-id: http://core.svn.wordpress.org/trunk@59953 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-08-07 08:33:26 +00:00
jonsurrell
2625a6ca44 Docs: Fix typos in HTML API documentation.
Corrects misspelling "compatability" to "compatibility."

Developed in https://github.com/WordPress/wordpress-develop/pull/8769.

Props dhruvang21, jonsurrell, abcd95, getsyash, truptikanzariya, nareshbheda, sergeybiryukov, dmsnell, sabernhardt.
See #63391.

Built from https://develop.svn.wordpress.org/trunk@60540


git-svn-id: http://core.svn.wordpress.org/trunk@59876 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-08-05 14:02:27 +00:00
jonsurrell
cf7330c222 HTML API: Simplify META tag processing.
META tag processing can be simplified in most cases. Add an early check and return to avoid additional processing.

Developed in https://github.com/WordPress/wordpress-develop/pull/9231.

Props jonsurrell, dmsnell.
See #63738.

Built from https://develop.svn.wordpress.org/trunk@60502


git-svn-id: http://core.svn.wordpress.org/trunk@59838 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-07-24 06:49:29 +00:00
jonsurrell
7cabf198f5 HTML API: Mark doctype info class as private.
Developed in https://github.com/WordPress/wordpress-develop/pull/9301.

Props jonsurrell, dmsnell.
See #63738.

Built from https://develop.svn.wordpress.org/trunk@60501


git-svn-id: http://core.svn.wordpress.org/trunk@59837 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-07-24 06:13:36 +00:00
Weston Ruter
08c4498647 HTML API: Stop counting no-op seek operations against the max seek count.
This allows `seek()` to be freely called when the current cursor at the provided bookmark.

Props dmsnell, jonsurrell, westonruter.
Fixes #62085.

Built from https://develop.svn.wordpress.org/trunk@59812


git-svn-id: http://core.svn.wordpress.org/trunk@59154 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-02-11 20:04:23 +00:00
Aaron Jorbin
4be161e473 Documentation: Update @since to reflect version this might ship in.
When originally committed, this code was targeting 6.7.1. However, it was not backported and included in 6.7.1. Will this be followed up by another version change? You'll need to stay tuned to next week's episode of "As the WordPress Turns" to find out!

Follow-up to [59285] and [59364].

See #62270.

Built from https://develop.svn.wordpress.org/trunk@59747


git-svn-id: http://core.svn.wordpress.org/trunk@59089 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2025-01-31 02:22:23 +00:00
gziolo
04a6ab1c8d HTML API: Remove nullable from get_breadcrumbs return type
Follow-up [58713]
Props jonsurrell, westonruter, gziolo.
Fixes #62674.


Built from https://develop.svn.wordpress.org/trunk@59503


git-svn-id: http://core.svn.wordpress.org/trunk@58889 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-12-10 11:41:16 +00:00
gziolo
70af002cdb HTML API: Prevent bookmarks from being set on virtual tokens
Fixes the issue when an HTML_Processor bookmark was set at a virtual token (a node in the resulting document that does not correspond to an HTML token present in the input string), seek behavior was unreliable.

Props jonsurrell, gziolo.
Fixes #62521.



Built from https://develop.svn.wordpress.org/trunk@59502


git-svn-id: http://core.svn.wordpress.org/trunk@58888 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-12-10 09:13:20 +00:00
gziolo
e8b5d1a702 HTML API: Step past closing HTML, BODY tags
The HTML specification does not close HTML or BODY tags (pop them off the stack of open elements) when their tag closers are encountered. The HTML processor correctly handled this behavior, however it incorrectly "paused" by returning true from the step functions. 

Props jonsurrell, dmsnell, gziolo.
Fixes #62583.


Built from https://develop.svn.wordpress.org/trunk@59500


git-svn-id: http://core.svn.wordpress.org/trunk@58886 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-12-09 12:44:17 +00:00
Bernhard Reiter
fd9519e7b2 HTML API: Make non-body fragment creation methods private.
The current implementation of `create_fragment` (and the underlying `create_fragment_at_current_node`) allows passing in a context that might result in a tree that cannot be represented by HTML. For example, a user might use `<p>` as context, and attempt to create a fragment that also consists of a paragraph element, `<p>like this`. This would result in a paragraph node nested inside another -- something that can never result from parsing HTML.

To prevent this, this changeset makes `create_fragment_at_current_node` private and limits `create_fragment` to only `<body>` as context, while a comprehensive solution to allow other contexts is being worked on.

Follow-up to [59444], [59467].
Props jonsurrell, dmsnell, bernhard-reiter.
Fixes #62584.
Built from https://develop.svn.wordpress.org/trunk@59469


git-svn-id: http://core.svn.wordpress.org/trunk@58855 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-28 14:27:18 +00:00
Bernhard Reiter
e174cbef2e HTML API: Allow more contexts in create_fragment.
This changeset modifies `WP_HTML_Processor::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.

Props jonsurrell, dmsnell, bernhard-reiter.
Fixes #62584.
Built from https://develop.svn.wordpress.org/trunk@59467


git-svn-id: http://core.svn.wordpress.org/trunk@58853 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-27 14:35:16 +00:00
cbravobernal
ee60151913 HTML API: Recognize all uppercase tag names in tag processor.
Fixes a missing "D" in the character list used by strspn to find tag openers, causing tags starting with D to be skipped by the tag processor in some circumstances.

Follow-up to [58613].

Props jonsurrell, santosguillamot, wongjn, cbravobernal.
Fixes #62522.

Built from https://develop.svn.wordpress.org/trunk@59464


git-svn-id: http://core.svn.wordpress.org/trunk@58850 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-27 09:57:15 +00:00
gziolo
c9d9ddb4d8 HTML API: Remove unused processor state context_node property
The HTML Processor State `context_node` is redundant and can be deprecated. The property has been superseded by `WP_HTML_Processor->context_node` since [58304].

Props jonsurrell, gziolo.
Fixes #62518.



Built from https://develop.svn.wordpress.org/trunk@59463


git-svn-id: http://core.svn.wordpress.org/trunk@58849 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-27 09:54:17 +00:00
cbravobernal
261373653c HTML API: Prevent fragment creation on close tag.
Prevent fragments from being created at tag closers.

Follow-up to [59444].

Props jonsurrell, bernhard-reiter.
Fixes #62357.

Built from https://develop.svn.wordpress.org/trunk@59450


git-svn-id: http://core.svn.wordpress.org/trunk@58836 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-22 12:52:19 +00:00
Bernhard Reiter
ff3fde39ee HTML API: Add method to create fragment at node.
HTML Fragment parsing always happens with a context node, which may impact how a fragment of HTML is parsed. HTML Fragment Processors can be instantiated with a `BODY` context node via `WP_HTML_Processor::create_fragment( $html )`.

This changeset adds a static method called `create_fragment_at_current_node( string $html_fragment )`. It can only be called when the processor is paused at a `#tag`, with some additional constraints:

- The opening and closing tags must appear in the HTML input (no virtual tokens).
- No "self-contained" elements are allowed ( `IFRAME`, `SCRIPT`, `TITLE`, etc.).

If successful, the method will return a `WP_HTML_Processor` instance whose context is inherited from the node that the method was called from.

Props jonsurrell, bernhard-reiter, gziolo.
Fixes #62357.
Built from https://develop.svn.wordpress.org/trunk@59444


git-svn-id: http://core.svn.wordpress.org/trunk@58830 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-21 13:29:18 +00:00
czapla
d54b7924ef HTML API: Use case insensitive tag_name comparison in ::next_tag.
The HTML API `::next_tag` method now performs case-insensitive matching when searching for tags by name. For example, searching for 'DIV' will match both '<div>' and '<DIV>' tags.

Props jonsurrell, dmsnell.
Fixes #62427.

Built from https://develop.svn.wordpress.org/trunk@59422


git-svn-id: http://core.svn.wordpress.org/trunk@58808 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-19 16:05:17 +00:00
cbravobernal
72a29cef78 HTML API: Fix normalized doctype pub/sys identifier quotes.
Changeset [59399] fixed missing DOCTYPEs in normalized HTML output. It missed an edge case where public and system identifiers may contain double quotes, in which case they must be quoted with single quotes.

This commit addresses that issue and adds tests.

Follow-up to [59399].

Props jonsurrell, luisherranz, apermo.
Fixes #62396.

Built from https://develop.svn.wordpress.org/trunk@59410


git-svn-id: http://core.svn.wordpress.org/trunk@58796 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-18 13:07:17 +00:00
Bernhard Reiter
8f5eb7a835 HTML API: Include doctype in full parser serialize.
Output DOCTYPE when calling `WP_HTML_Processor::serialize` on a full document that includes a DOCTYPE.

The DOCTYPE should be included in the serialized/normalized HTML output as it has an impact in how the document is handled, in particular whether the document should be handled in quirks or no-quirks mode.

This only affects the serialization of full parsers at this time because DOCTYPE tokens are currently ignored in all possible fragments. The omission of the DOCTYPE is subtle but can change the serialized document's quirks/no-quirks mode.

Props jonsurrell.
Fixes #62396.
Built from https://develop.svn.wordpress.org/trunk@59399


git-svn-id: http://core.svn.wordpress.org/trunk@58785 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-13 12:20:15 +00:00
Bernhard Reiter
0c6d68448b HTML API: Expect closer on foreign content void lookalike elements.
Ensure that `expects_closer` returns `false` on tags that look like void HTML tags, but are actually ''not'' void tags in foreign content.

Props jonsurrell, bernhard-reiter.
Fixes #62363.
Built from https://develop.svn.wordpress.org/trunk@59392


git-svn-id: http://core.svn.wordpress.org/trunk@58778 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-12 12:58:16 +00:00
Bernhard Reiter
0be8a89a8f HTML API: Ensure that full processor can seek to earlier bookmarks.
When the HTML Processor seeks to an earlier place, it returns the the beginning of the document and proceeds forward until it reaches the appropriate location. This requires resetting internal state so that the processor can correctly proceed from the beginning of the document.

The seeking reset logic was not adapted to account for the full processor (i.e. when created via `WP_HTML_Processor::create_full_parser()`). This change updates the seek logic to account for the full and fragment parsers as well as other state that has been introduced in the interim and should be reset.

Props jonsurrell, dmsnell, westonruter, mi5t4n.
Fixes #62290.
Built from https://develop.svn.wordpress.org/trunk@59391


git-svn-id: http://core.svn.wordpress.org/trunk@58777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-12 10:32:17 +00:00
Weston Ruter
ba9fe05e26 HTML API: Improve private method name used by WP_HTML_Processor::next_token().
This renames the private `_next_token` method to `next_visitable_token`. It also removes irrelevant assertions from the unit test.

Follow-up to [59285].

Props dmsnell, jonsurrell, westonruter.
See #62269.

Built from https://develop.svn.wordpress.org/trunk@59364


git-svn-id: http://core.svn.wordpress.org/trunk@58750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-06 19:24:20 +00:00
Weston Ruter
204d60314b HTML API: Fix extensibility of WP_HTML_Processor::next_token().
Break out logic from the `next_token()` method into a private method which may call itself recursively. This allows for subclasses to override the `next_token()` method and be assured that each call to `next_token()` corresponds with the consumption of one single token. This also parallels how `WP_HTML_Tag_Processor::next_token()` wraps a private `base_class_next_token()` method.

Props westonruter, jonsurrell.
Fixes #62269.

Built from https://develop.svn.wordpress.org/trunk@59285


git-svn-id: http://core.svn.wordpress.org/trunk@58677 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-23 22:05:17 +00:00
gziolo
a923a561ca HTML API: Fix the position update after changing the modifiable text when length differs
There was an edge case detected for updating the text placed without the wrapping HTML tag with `set_modifiable_text`.

Props gziolo, jonsurrell.
Fixes #62241.


Built from https://develop.svn.wordpress.org/trunk@59250


git-svn-id: http://core.svn.wordpress.org/trunk@58642 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-18 13:31:17 +00:00
Bernhard Reiter
1dbf1ddc5a HTML API: Ensure form closer tokens are reachable.
Form tag closers have complicated conditions. There was a bug where the HTML Processor would not stop correctly on a FORM tag closer token.
This changeset ensures that that token is reachable.

Developed in https://github.com/WordPress/wordpress-develop/pull/7582
Discussed in https://core.trac.wordpress.org/ticket/61576

Follow-up to [58779].

Props jonsurrell.
Fixes #61576.
Built from https://develop.svn.wordpress.org/trunk@59248


git-svn-id: http://core.svn.wordpress.org/trunk@58640 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-17 17:48:19 +00:00
cbravobernal
cc5e9ffb43 HTML API: Fix typo in error message in html processor.
Possesive "it's" should be "its."

Follow-up to [59076].

Props jonsurrell.
Fixes #62036.

Built from https://develop.svn.wordpress.org/trunk@59245


git-svn-id: http://core.svn.wordpress.org/trunk@58637 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-17 11:03:17 +00:00
dmsnell
ed654177cd HTML API: Switch to HTML namespace when entering Integration Points.
When encountering inline SVG and MathML content in an HTML document, there are certain "integration points" which transition back into the HTML parsing ruleset. Previously, the HTML API was incorrectly switching into the namespace of the element transitioning into that ruleset.

In this patch, the correct transition is made, where all integration points refer to HTML rules, while non-integration points refer to the rules of the namespace corresponding to the token itself.

Developed in https://github.com/wordpress/wordpress-develop/pull/7425
Discussed in https://core.trac.wordpress.org/ticket/61576

Props dmsnell, jonsurrell.
See #61576.

Built from https://develop.svn.wordpress.org/trunk@59099


git-svn-id: http://core.svn.wordpress.org/trunk@58495 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-27 00:44:19 +00:00
dmsnell
5b3b3f7df2 HTML API: Add normalize() to give us the HTML we always wanted.
HTML often appears in ways that are unexpected. It may be missing implicit tags, may have unquoted, single-quoted, or double-quoted attributes, may contain duplicate attributes, may contain unescaped text content, or any number of other possible invalid constructions. The HTML API understands all fo these inputs, but downline parsers may not, and HTML snippets which are safe on their own may introduce problems when joined with other HTML snippets.

This patch introduces the `serialize()` method on the HTML Processor, which prints a fully-normative HTML output, eliminating invalid markup along the way. It produces a string which contains every missing tag, double-quoted attributes, and no duplicates. A `normalize()` static method on the HTML Processor provides a convenient wrapper for constructing a fragment parser and immediately serializing.

Subclasses relying on the `serialize_token()` method may perform structural HTML modifications with as much security as the upcoming `\Dom\HTMLDocument()` parser will, though these are not
able to provide the full safety that will eventually appear with `set_inner_html()`.

Further work may explore serializing to XML (which involves a number of other important transformations) and adding constraints to serialization (such as only allowing inline/flow/formatting elements and text).

Developed in https://github.com/wordpress/wordpress-develop/pull/7331
Discussed in https://core.trac.wordpress.org/ticket/62036

Props dmsnell, jonsurrell, westonruter.
Fixes #62036.

Built from https://develop.svn.wordpress.org/trunk@59076


git-svn-id: http://core.svn.wordpress.org/trunk@58472 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-20 22:32:17 +00:00