Skip to content

[Bug]: json_decode coerces numeric string attributes to integers #91

Description

@mhortulanus
### PHP Version
8.4.19

### Version
2.1.0

### Bug Description

When `setContent()` receives a string that contains only digits (e.g. `"42"`), `getContentType()` misidentifies it as JSON because `json_decode("42")` is valid JSON. This causes the document to be set to an integer instead of being parsed as HTML text content.

The issue is in `getContentType()` (Editor.php:89–101): it checks if `json_decode` succeeds, but bare numbers, booleans, and `null` are all valid JSON scalars — so any numeric string passes the JSON check.

### Steps to Reproduce

```php
$editor = new \Tiptap\Editor();
$editor->setContent("42");

$doc = $editor->getDocument();
// Expected: parsed HTML document with text "42"
// Actual: int(42)

Expected Behavior

A plain numeric string like "42" should be treated as HTML content and parsed into a proper tiptap document node.

Actual Behavior

The string is identified as JSON, json_decode("42", true) returns int(42), and the document becomes an integer.

Suggested Fix

In getContentType(), after json_decode succeeds, verify the result is actually a valid tiptap document structure (an array with a type key):

$decoded = json_decode($value, true, 512, JSON_THROW_ON_ERROR);

if (is_array($decoded) && isset($decoded['type'])) {
    return 'JSON';
}

return 'HTML';

Expected Behavior (Detailed)

A plain numeric string like "42" passed to setContent() should be treated as HTML content, resulting in a valid tiptap document structure:

$editor = new \Tiptap\Editor();
$editor->setContent("42");

$doc = $editor->getDocument();

// Expected:
// [
//     "type" => "doc",
//     "content" => [
//         [
//             "type" => "paragraph",
//             "content" => [
//                 ["type" => "text", "text" => "42"]
//             ]
//         ]
//     ]
// ]

Additional Context (Optional)

Filament v4 usage.

Dependency Updates

  • Yes, I've updated all my dependencies.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions