Skip to content

Commit

Permalink
Remove empty self closing tags by default
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Sep 30, 2024
1 parent 6438a0a commit 696bcd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,25 @@ protected function processListType(string $data, $list_type, $replacement_tag)
return $data;
}

public function replaceTags(string $data, $tag, $replacement_tag) : string
public function replaceTags(string $data, $tag, $replacement_tag, $keep_self_closing_tags = false) : string
{
$data = str_replace('<' . $tag . '>', '<' . $replacement_tag . '>', $data);
$data = str_replace('</' . $tag . '>', '</' . $replacement_tag . '>', $data);
$data = str_replace(
['<' . $tag . '>', '</' . $tag . '>'],
['<' . $replacement_tag . '>', '</' . $replacement_tag . '>'],
$data
);
// Also self closing tags in a couple of variations.
$self_closing_replacement = '<' . $replacement_tag . ' />';
$data = str_replace('<' . $tag . ' />', $self_closing_replacement, $data);
$data = str_replace('<' . $tag . '/>', $self_closing_replacement, $data);
$self_closing_replacement = '';
if ($keep_self_closing_tags) {
$self_closing_replacement = '<' . $replacement_tag . '>';
}
$data = str_replace(
[
'<' . $tag . ' />',
'<' . $tag . '/>'],
[$self_closing_replacement, $self_closing_replacement],
$data
);
return $data;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/assets/cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,17 @@
{
"input": "<p><italic>The course is designed for those who are at or want to reach a high level in cross-country skiing and biathlon as an athlete or coach. This is a flexible course in sports where the teaching is digital and assembly-based.</italic></p><p><italic>The course gives you theoretical and practical competence to develop yourself to a high international level and, simultaneously, an education that qualifies you to work as a coach at club, regional and national team levels. The study combines theory and practice, which gives a comprehensive understanding of conditions that affect participation, development and learning at all levels, from children's to elite sports. Emphasis is placed on the development of practical pedagogical competence and critical thinking around today's best practice, theory, and the role of trainer.</italic></p><p>The unique thing about the course is that the academics are closely linked to your development as an athlete and are tailored so that you can combine top sports and studies.</p><p> </p>",
"expected": "<p><em>The course is designed for those who are at or want to reach a high level in cross-country skiing and biathlon as an athlete or coach. This is a flexible course in sports where the teaching is digital and assembly-based.</em></p><p><em>The course gives you theoretical and practical competence to develop yourself to a high international level and, simultaneously, an education that qualifies you to work as a coach at club, regional and national team levels. The study combines theory and practice, which gives a comprehensive understanding of conditions that affect participation, development and learning at all levels, from children's to elite sports. Emphasis is placed on the development of practical pedagogical competence and critical thinking around today's best practice, theory, and the role of trainer.</em></p><p>The unique thing about the course is that the academics are closely linked to your development as an athlete and are tailored so that you can combine top sports and studies.</p>"
},
{
"input": "<p>You know, they say when you talk to God it's prayer, but when God talks to you, it's schizophrenia.<bold/></p>",
"expected": "<p>You know, they say when you talk to God it's prayer, but when God talks to you, it's schizophrenia.</p>"
},
{
"input": "<p>You know, they say when you talk to God it's prayer, but when God talks to you, it's schizophrenia.<bold /></p>",
"expected": "<p>You know, they say when you talk to God it's prayer, but when God talks to you, it's schizophrenia.</p>"
},
{
"input": "<p>You know, they say when you talk to God it's prayer, but when God talks to you, it's schizophrenia.<bold/></p>",
"expected": "<p>You know, they say when you talk to God it's prayer, but when God talks to you, it's schizophrenia.</p>"
}
]

0 comments on commit 696bcd7

Please sign in to comment.