Skip to content

Commit d36fb25

Browse files
committed
WIP
1 parent 93339ff commit d36fb25

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,89 @@ public function declarative_match( $pattern_html ) {
23912391
return true;
23922392
}
23932393

2394+
/**
2395+
* @since 6.3.0
2396+
*/
2397+
public function transform( $pattern_html, $transformer_html ) {
2398+
$visit_everything = array( 'tag_closers' => 'visit', 'funky_comments' => 'visit' );
2399+
$pattern = new WP_HTML_Tag_Processor( $pattern_html );
2400+
$transform = new WP_HTML_Tag_Processor( $transformer_html );
2401+
2402+
if (
2403+
! $pattern->next_tag( $visit_everything ) ||
2404+
! $transform->next_tag( $visit_everything ) ||
2405+
! $this->declarative_match( $pattern_html )
2406+
) {
2407+
return false;
2408+
}
2409+
2410+
$this->set_bookmark( 'match_start' );
2411+
2412+
$same_thing = function ( WP_HTML_Tag_Processor $pattern, WP_HTML_Tag_Processor $test ) {
2413+
if ( $pattern->is_funky_comment() ) {
2414+
// $this->placeholders++;
2415+
// $this->set_bookmark( "__placeholder_{$this->placeholders}" );
2416+
return true;
2417+
}
2418+
2419+
if ( ! (
2420+
$pattern->get_tag() === $test->get_tag() &&
2421+
$pattern->is_tag_closer() === $test->is_tag_closer() &&
2422+
$pattern->is_funky_comment() === $test->is_funky_comment()
2423+
) ) {
2424+
return false;
2425+
}
2426+
2427+
$attribute_constraints = $pattern->get_attribute_names_with_prefix( '' );
2428+
if ( null === $attribute_constraints ) {
2429+
return true;
2430+
}
2431+
2432+
foreach ( $attribute_constraints as $name ) {
2433+
if ( $pattern->get_attribute( $name ) !== $test->get_attribute( $name ) ) {
2434+
return false;
2435+
}
2436+
}
2437+
2438+
return true;
2439+
};
2440+
2441+
$budget = 10;
2442+
while ( $budget-- ) {
2443+
if ( $same_thing( $pattern, $transform ) ) {
2444+
if ( ! $transform->next_tag( $visit_everything ) ) {
2445+
goto drop_patterns;
2446+
}
2447+
$this->next_tag( $visit_everything );
2448+
$pattern->next_tag( $visit_everything );
2449+
continue;
2450+
}
2451+
2452+
$this->set_bookmark( 'here' );
2453+
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
2454+
$this->bookmarks['here']->start,
2455+
$this->bookmarks['here']->end + 1,
2456+
''
2457+
);
2458+
var_dump( substr( $this->html, $this->bookmarks['here']->start, $this->bookmarks['here']->end - $this->bookmarks['here']->start + 1 ) );
2459+
$this->get_updated_html();
2460+
$pattern->next_tag( $visit_everything );
2461+
$this->next_tag( $visit_everything );
2462+
}
2463+
2464+
drop_patterns:
2465+
while ( $pattern->next_tag( $visit_everything ) ) {
2466+
$this->set_bookmark( 'here' );
2467+
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
2468+
$this->bookmarks['here']->start,
2469+
$this->bookmarks['here']->end + 1,
2470+
''
2471+
);
2472+
var_dump( substr( $this->html, $this->bookmarks['here']->start, $this->bookmarks['here']->end - $this->bookmarks['here']->start + 1 ) );
2473+
$this->next_tag( $visit_everything );
2474+
}
2475+
}
2476+
23942477
/**
23952478
* Checks whether a given tag and its attributes match the search criteria.
23962479
*

tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,4 +2344,64 @@ public function test_declarative_match_bookmarks_markup_wildcards_delete_me_this
23442344
$this->assertSame( 'H1', $p->get_tag() );
23452345
$this->assertTrue( $p->is_tag_closer() );
23462346
}
2347+
2348+
/**
2349+
* @dataProvider data_html_transforms
2350+
*/
2351+
public function test_transforms_html( $pattern, $transformer, $input, $output ) {
2352+
$p = new WP_HTML_Tag_Processor( $input );
2353+
$p->transform( $pattern, $transformer );
2354+
2355+
$this->assertSame( $output, $p->get_updated_html() );
2356+
}
2357+
2358+
public function data_html_transforms() {
2359+
return array(
2360+
'Unwrap image tag' => array(
2361+
'<div><img></div>',
2362+
'<html><img>',
2363+
'<div class="wp-div-image"><img src="atat.png" class="full-width"></div><br>',
2364+
'<img src="atat.png" class="full-width"><br>'
2365+
),
2366+
'Remove first image' => array(
2367+
'</-img><img>',
2368+
'<img>',
2369+
'<img first><img second>',
2370+
'<img second>'
2371+
),
2372+
'Remove second image' => array(
2373+
'<img></-img>',
2374+
'<img>',
2375+
'<img first><img second>',
2376+
'<img first>'
2377+
),
2378+
'Add image before' => array(
2379+
'<img>',
2380+
'</+img><img>',
2381+
'<img first>',
2382+
'<img><img first>'
2383+
),
2384+
'Add image after' => array(
2385+
'<img>',
2386+
'<img></+img>',
2387+
'<img first>',
2388+
'<img first><img>'
2389+
),
2390+
);
2391+
}
2392+
2393+
/**
2394+
* @dataProvider data_css_selectors_to_html
2395+
*/
2396+
public function test_generates_html_from_css_selector( $selector, $html ) {
2397+
$this->assertTrue( true );
2398+
}
2399+
2400+
public function data_css_selectors_to_html() {
2401+
return array(
2402+
'Single element' => array( 'table', 'table' ),
2403+
'Single class' => array( '.is-working', '</1 class="is-working">' ),
2404+
'Adjacent Sibling' => array( '* + img', '</1><img>' ),
2405+
);
2406+
}
23472407
}

0 commit comments

Comments
 (0)