Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FontFamily extension with parsing and rendering support #68

Merged
merged 3 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Extensions/FontFamily.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tiptap\Extensions;

use Tiptap\Core\Extension;
use Tiptap\Utils\InlineStyle;

class FontFamily extends Extension
{
public static $name = 'fontFamily';

public function addOptions()
{
return [
'types' => ['textStyle'],
];
}

public function addGlobalAttributes()
{
return [
[
'types' => $this->options['types'],
'attributes' => [
'fontFamily' => [
'default' => null,
'parseHTML' => function ($DOMNode) {
$attribute = InlineStyle::getAttribute($DOMNode, 'font-family');

if ($attribute === null) {
return null;
}

return $attribute;
},
'renderHTML' => function ($attributes) {
$fontFamily = $attributes?->fontFamily ?? null;

if ($fontFamily === null) {
return null;
}

return ['style' => "font-family: {$fontFamily}"];
},
],
],
],
];
}
}
118 changes: 118 additions & 0 deletions tests/DOMParser/Extensions/FontFamilyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Tiptap\Tests\DOMParser\Extensions;

use Tiptap\Editor;
use Tiptap\Extensions\FontFamily;
use Tiptap\Extensions\StarterKit;
use Tiptap\Marks\TextStyle;

test('font family is parsed correctly', function () {
$html = '<p><span style="font-family: Arial;">Arial text</span></p>';

$result =
(new Editor([
'extensions' => [
new StarterKit,
new TextStyle(),
new FontFamily(),
],
]))
->setContent($html)
->getDocument();

expect($result)->toEqual([
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [
[
'type' => 'text',
'marks' => [
[
'type' => 'textStyle',
'attrs' => [
'fontFamily' => 'Arial',
],
],
],
'text' => 'Arial text',
],
],
],
],
]);
});

test('multiple font family values are parsed correctly', function () {
$html = '<p><span style="font-family: Helvetica Neue, Arial, \'Times New Roman\', &quot;Open Sans&quot;, sans-serif;">Multiple fonts</span></p>';

$result =
(new Editor([
'extensions' => [
new StarterKit,
new TextStyle(),
new FontFamily(),
],
]))
->setContent($html)
->getDocument();

expect($result)->toEqual([
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [
[
'type' => 'text',
'marks' => [
[
'type' => 'textStyle',
'attrs' => [
'fontFamily' => 'Helvetica Neue, Arial, \'Times New Roman\', "Open Sans", sans-serif',
],
],
],
'text' => 'Multiple fonts',
],
],
],
],
]);
});

test('font family extension respects the types option', function () {
$html = '<h1 style="font-family: Times New Roman;">Times New Roman heading</h1>';

$result = (new Editor([
'extensions' => [
new StarterKit(),
new FontFamily([
'types' => ['heading'],
]),
],
]))
->setContent($html)
->getDocument();

expect($result)->toEqual([
'type' => 'doc',
'content' => [
[
'type' => 'heading',
'attrs' => [
'level' => 1,
'fontFamily' => 'Times New Roman',
],
'content' => [
[
'type' => 'text',
'text' => 'Times New Roman heading',
],
],
],
],
]);
});
45 changes: 45 additions & 0 deletions tests/DOMSerializer/Extensions/FontFamilyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tiptap\Tests\DOMSerializer\Extensions;

use Tiptap\Editor;
use Tiptap\Extensions\FontFamily;
use Tiptap\Extensions\StarterKit;
use Tiptap\Marks\TextStyle;

test('font family is rendered correctly', function () {
$json = [
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [
[
'type' => 'text',
'marks' => [
[
'type' => 'textStyle',
'attrs' => [
'fontFamily' => 'Helvetica, Arial, sans-serif',
],
],
],
'text' => 'custom font text',
],
],
],
],
];

$result = (new Editor([
'extensions' => [
new StarterKit(),
new TextStyle(),
new FontFamily(),
],
]))
->setContent($json)
->getHTML();

expect($result)->toEqual('<p><span style="font-family: Helvetica, Arial, sans-serif;">custom font text</span></p>');
});