Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 5 weeks ago

#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)

#1 @dmsnell
3 years ago

  • Milestone Awaiting Review → 6.6

I'm not sure why the PR isn't auto-linking: ​https://github.com/WordPress/wordpress-develop/pull/6387

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

#5 @dmsnell
2 years ago

  • Owner set to dmsnell
  • Resolution → fixed
  • Status new → closed

In 58281:

HTML API: Add custom text decoder.

Provides 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.
  • Use of html_entity_decode() requires manually passing non-default paramter values to ensure it decodes properly.

This decoder also provides 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.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/6387
Discussed in https://core.trac.wordpress.org/ticket/61072

Props dmsnell, gziolo, jonsurrell, jorbin, westonruter, zieladam.
Fixes #61072.

#6 @dmsnell
2 years ago

  • Keywords needs-dev-note added

#7 @dmsnell
2 years ago

Dev note incorporated into ​Updates to the HTML API

#8 @westonruter
12 months ago

In 61283:

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.

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 trunk to eliminate stale merge commits.
  • Refactored entity decoding assertions to use targeted @dataProvider datasets (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}" in data_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:

  1. Remove four rows that repeat existing coverage:
    • NULL byte repeats Raw NULL byte passes through in data_null_code_points().
    • Unknown named character reference repeats the &bogus; rows in data_edge_cases().
    • Ambiguous with equals (attr) and Ambiguous with alphanumeric (attr) repeat data_semicolonless_attribute_character_reference_no_decode_followers().
  1. 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' ).
  1. Remove test_decodes_invalid_numeric_references() and its provider. No digits and No digits (hex) repeat Unmatched numeric references in data_edge_cases(). The two "Too many digits" rows are already above U+10FFFF, so they don't test the digit limit, and the generator's Out of range cases cover the result.
  1. 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.

#14 @lancewillett
5 weeks ago

In 64286:

Tests: Expand HTML character reference decoding coverage.

Add named and numeric character reference cases for WP_HTML_Decoder, including semicolon handling, case sensitivity, Unicode boundaries, and Windows-1252 remapping. Generate numeric syntax variants with independent, literal expected strings.

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

Props huzaifaalmesbah, dmsnell.
See #61072.

​@dmsnell commented on ​PR #10810:


5 weeks ago
#16

Thanks @lancewillett — was still planning on returning to this, but I am slow.

Note: See TracTickets for help on using tickets.