Skip to content

Commit bad1c63

Browse files
committed
HTML API: Add get_full_comment_text() method.
Previously, there were a few cases where the modifiable text read from an HTML comment differs slightly from the parsed value of its inner text in a browser. This is due to the specific way that invalid HTML syntax tokens become "bogus comments." This patch introduces a new method to the Tag Processor to allow differentiating these specific cases, such as when copying or serializing HTML from one source to another. Similar code has already been in use in the html5lib tests, and this patch simplifies the test runner, evidencing the fact that this method was already needed. Developed in WordPress/wordpress-develop#7342 Discussed in https://core.trac.wordpress.org/ticket/62036 Props dmsnell, jonsurrell. See #62036. Built from https://develop.svn.wordpress.org/trunk@59075 git-svn-id: https://core.svn.wordpress.org/trunk@58471 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent e6eb293 commit bad1c63

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,6 +3385,58 @@ public function get_comment_type(): ?string {
33853385
return $this->comment_type;
33863386
}
33873387

3388+
/**
3389+
* Returns the text of a matched comment or null if not on a comment type node.
3390+
*
3391+
* This method returns the entire text content of a comment node as it
3392+
* would appear in the browser.
3393+
*
3394+
* This differs from {@see ::get_modifiable_text()} in that certain comment
3395+
* types in the HTML API cannot allow their entire comment text content to
3396+
* be modified. Namely, "bogus comments" of the form `<?not allowed in html>`
3397+
* will create a comment whose text content starts with `?`. Note that if
3398+
* that character were modified, it would be possible to change the node
3399+
* type.
3400+
*
3401+
* @since 6.7.0
3402+
*
3403+
* @return string|null The comment text as it would appear in the browser or null
3404+
* if not on a comment type node.
3405+
*/
3406+
public function get_full_comment_text(): ?string {
3407+
if ( self::STATE_FUNKY_COMMENT === $this->parser_state ) {
3408+
return $this->get_modifiable_text();
3409+
}
3410+
3411+
if ( self::STATE_COMMENT !== $this->parser_state ) {
3412+
return null;
3413+
}
3414+
3415+
switch ( $this->get_comment_type() ) {
3416+
case self::COMMENT_AS_HTML_COMMENT:
3417+
case self::COMMENT_AS_ABRUPTLY_CLOSED_COMMENT:
3418+
return $this->get_modifiable_text();
3419+
3420+
case self::COMMENT_AS_CDATA_LOOKALIKE:
3421+
return "[CDATA[{$this->get_modifiable_text()}]]";
3422+
3423+
case self::COMMENT_AS_PI_NODE_LOOKALIKE:
3424+
return "?{$this->get_tag()}{$this->get_modifiable_text()}?";
3425+
3426+
/*
3427+
* This represents "bogus comments state" from HTML tokenization.
3428+
* This can be entered by `<?` or `<!`, where `?` is included in
3429+
* the comment text but `!` is not.
3430+
*/
3431+
case self::COMMENT_AS_INVALID_HTML:
3432+
$preceding_character = $this->html[ $this->text_starts_at - 1 ];
3433+
$comment_start = '?' === $preceding_character ? '?' : '';
3434+
return "{$comment_start}{$this->get_modifiable_text()}";
3435+
}
3436+
3437+
return null;
3438+
}
3439+
33883440
/**
33893441
* Subdivides a matched text node, splitting NULL byte sequences and decoded whitespace as
33903442
* distinct nodes prefixes.

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.7-alpha-59074';
19+
$wp_version = '6.7-alpha-59075';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)