#61072 closed enhancement (fixed)
HTML API: Add custom text decoder
| Reported by: | dmsnell | Owned by: | dmsnell |
|---|---|---|---|
| Priority: | normal | Milestone: | 6.6 |
| Component: | HTML API | Version: | 6.6 |
| Severity: | normal | Keywords: | has-patch needs-dev-note has-unit-tests |
| Cc: | Focuses: |
Description
Provide a custom decoder for strings coming from HTML attributes and
markup. This custom decoder is necessary because of deficiencies in
PHP's html_entity_decode() function:
- It isn't aware of 720 of the possible named character references in HTML, leaving many out that should be translated.
- It isn't aware of the ambiguous ampersand rule, which allows
conversion of character references in certain contexts when they
are missing their closing
;.
- It doesn't draw a distinction for the ambiguous ampersand rule when decoding attribute values instead of markup values.
This decoder will also provide some conveniences, such as making a
single-pass and interruptable decode operation possible. This will
provide a number of opportunities to optimize detection and decoding
of things like value prefixes, and whether a value contains a given
substring.
Change History (16)
This ticket was mentioned in Slack in #core-performance by dmsnell. View the logs.
2 years ago
This ticket was mentioned in Slack in #core by dmsnell. View the logs.
2 years ago
This ticket was mentioned in Slack in #core by oglekler. View the logs.
2 years ago
#7
@
2 years ago
Dev note incorporated into Updates to the HTML API
This ticket was mentioned in PR #10810 on WordPress/wordpress-develop by @huzaifaalmesbah.
5 weeks ago
#9
- Keywords has-unit-tests added
Description
Expands and modernizes unit test coverage for WP_HTML_Decoder originally introduced in #61072 / r58197.
Key improvements added in this PR:
- Rebased cleanly against
trunkto eliminate stale merge commits. - Refactored entity decoding assertions to use targeted
@dataProviderdatasets (named, decimal numeric, and hex numeric entities). - Modernized UTF-8 replacement character representations using PHP 7.0+ `
@lancewillett commented on PR #10810:
5 weeks ago
#10
Thanks for all your review notes, @dmsnell
@huzaifaalmesbah commented on PR #10810:
5 weeks ago
#11
Thank you @dmsnell and @lancewillett for the thorough review and notes!
I've updated the PR to address all points:
- Aligned terminology to "named character references" across methods and docblocks.
- Consolidated numeric tests into a single generator covering decimal/hex, case variants, zero-padding, and semicolon terminators.
- Added test cases for the 5 unmapped C1 controls.
- Inlined
"\u{FFFD}"indata_invalid_numeric_references(). - Reordered ambiguous ampersand tests before
test_ascii_alphanumeric_attribute_follower_is_ambiguous().
@lancewillett commented on PR #10810:
5 weeks ago
#12
Ran this locally: 431 tests and 838 assertions pass, and the expected values all match the decoder. Four changes before commit:
- Remove four rows that repeat existing coverage:
NULL byterepeatsRaw NULL byte passes throughindata_null_code_points().Unknown named character referencerepeats the&bogus;rows indata_edge_cases().Ambiguous with equals (attr)andAmbiguous with alphanumeric (attr)repeatdata_semicolonless_attribute_character_reference_no_decode_followers().
- Hard-code the expected strings in the numeric generator. It builds them with
mb_chr(), the same call the decoder uses, so the five valid code points compare the function with itself. For example:'ASCII' => array( 0x41, 'A' ).
- Remove
test_decodes_invalid_numeric_references()and its provider.No digitsandNo digits (hex)repeatUnmatched numeric referencesindata_edge_cases(). The two "Too many digits" rows are already above U+10FFFF, so they don't test the digit limit, and the generator'sOut of rangecases cover the result.
- Rename the data-context rows in
data_ambiguous_ampersands(). An ampersand is only ambiguous in an attribute. For example: "Semicolonless followed by equals (data)".
@huzaifaalmesbah commented on PR #10810:
5 weeks ago
#13
Thanks @lancewillett! All four done in ea0dca0478: removed the four duplicate rows, hard-coded the expected strings in the numeric generator, removed test_decodes_invalid_numeric_references() and its provider, and renamed the data_ambiguous_ampersands() rows so only attribute cases read as ambiguous. 423 tests pass.
@lancewillett commented on PR #10810:
5 weeks ago
#15
PR #10810 landed in https://core.trac.wordpress.org/changeset/64286
@dmsnell commented on PR #10810:
5 weeks ago
#16
Thanks @lancewillett — was still planning on returning to this, but I am slow.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
I'm not sure why the PR isn't auto-linking: https://github.com/WordPress/wordpress-develop/pull/6387