Skip to content

Commit

Permalink
[5.x] Fix inline Bard with leading line break (#11038)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksleight authored Nov 1, 2024
1 parent 3bd3171 commit f74cd05
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Fieldtypes/Bard.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,16 @@ public function preProcess($value)
$value = $this->convertLegacyTiptap($value);

if ($this->config('inline')) {
// Root should be text, if it's not this must be a block field converted
// Root should be text or br, if it's not this must be a block field converted
// to inline. In that instance unwrap the content of the first node.
if ($value[0]['type'] !== 'text') {
if (! in_array($value[0]['type'], ['text', 'hardBreak'])) {
$value = $this->unwrapInlineValue($value);
}
$value = $this->wrapInlineValue($value);
} else {
// Root should not be text, if it is this must be an inline field converted
// Root should not be text or br, if it is this must be an inline field converted
// to block. In that instance wrap the content in a paragraph node.
if ($value[0]['type'] === 'text') {
if (in_array($value[0]['type'], ['text', 'hardBreak'])) {
$value = $this->wrapInlineValue($value);
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Fieldtypes/BardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,25 @@ public function it_preprocesses_inline_value()
$this->assertEquals($expected, $this->bard(['inline' => true, 'sets' => null])->preProcess($data));
}

#[Test]
public function it_preprocesses_inline_value_with_break()
{
$data = [
['type' => 'hardBreak'],
['type' => 'text', 'text' => 'This is inline text.'],
];

$expected = [[
'type' => 'paragraph',
'content' => [
['type' => 'hardBreak'],
['type' => 'text', 'text' => 'This is inline text.'],
],
]];

$this->assertEquals($expected, $this->bard(['inline' => 'break', 'sets' => null])->preProcess($data));
}

#[Test]
public function it_preprocesses_inline_value_to_block_value()
{
Expand All @@ -955,6 +974,25 @@ public function it_preprocesses_inline_value_to_block_value()
$this->assertEquals($expected, $this->bard(['input_mode' => 'block', 'sets' => null])->preProcess($data));
}

#[Test]
public function it_preprocesses_inline_value_with_break_to_block_value()
{
$data = [
['type' => 'hardBreak'],
['type' => 'text', 'text' => 'This is inline text.'],
];

$expected = [[
'type' => 'paragraph',
'content' => [
['type' => 'hardBreak'],
['type' => 'text', 'text' => 'This is inline text.'],
],
]];

$this->assertEquals($expected, $this->bard(['input_mode' => 'block', 'sets' => null])->preProcess($data));
}

#[Test]
public function it_preprocesses_block_value_to_inline_value()
{
Expand Down

0 comments on commit f74cd05

Please sign in to comment.