Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Htmldiff Laravel Package

caxy/php-htmldiff

Compare two HTML snippets/files and generate a marked-up diff highlighting insertions, deletions, and changes. Easy Composer install, simple API (HtmlDiff->build()), configurable behavior and CSS-friendly output; includes a live demo and Symfony bundle option.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: The package excels at visual HTML diffing (e.g., comparing rendered content, CMS previews, or dynamic HTML snippets). It’s ideal for:
    • Content Management Systems (CMS): Highlighting changes in WYSIWYG editors (e.g., Craft CMS, Laravel Nova).
    • Testing Frameworks: Generating diffs for regression testing of HTML output (e.g., Laravel Dusk, Pest).
    • Collaboration Tools: Showing user-generated content changes (e.g., comments, forum posts).
    • API Documentation: Comparing API response HTML (e.g., Swagger/OpenAPI UI changes).
  • Laravel Synergy: Integrates seamlessly with Laravel’s Blade templating, queue workers (for async diff generation), and testing tools (e.g., assertStringContainsMarkup with diff visualization).
  • Alternatives: Outperforms pure string diffs (e.g., str_diff) for HTML by preserving structure (tables, lists, nested tags). More lightweight than DOM-based tools like DOMDocument for diffing.

Integration Feasibility

  • Dependencies:
    • Core: PHP 7.3+ (Laravel 8+ compatible). Uses ezyang/htmlpurifier (for sanitization) and doctrine/cache (optional).
    • Symfony Bundle: caxy/htmldiff-bundle provides Laravel-like integration (e.g., Twig extensions, service container binding).
    • No Heavy Overhead: ~1MB package size; minimal runtime impact.
  • Laravel-Specific Hooks:
    • Service Provider: Register HtmlDiff as a singleton with configurable defaults (e.g., config/htmldiff.php).
    • Facade: HtmlDiff::diff($oldHtml, $newHtml) for concise usage.
    • Queue Jobs: Wrap diff generation in ShouldQueue jobs for long-running comparisons (e.g., large HTML emails).
    • Artisan Command: php artisan htmldiff:generate to precompute diffs for static content.
  • Database Integration:
    • Store diffs in longtext columns (e.g., posts.diff_html) or use Laravel Filesystem for large diffs.
    • Cache diffs with doctrine/cache or Laravel’s cache() helper.

Technical Risk

Risk Area Mitigation Strategy
Performance Benchmark with large HTML (e.g., 10KB+). Use setCacheProvider() for repeated diffs.
HTML Sanitization Disable setPurifierEnabled(false) if input is trusted (e.g., internal CMS content).
PHP 8+ Compatibility Already supported (tested up to PHP 8.3). Pin v0.1.9 if stuck on PHP 7.3.
CSS Styling Provide default CSS via Laravel’s mix or vite (e.g., resources/css/htmldiff.css).
Edge Cases Test with:
  • Malformed HTML (use htmlspecialchars_decode pre-processing).
  • Unicode/RTL text (e.g., Arabic, CJK).
  • Self-closing tags (e.g., <img/>). | | License (GPL-2.0) | Ensure compliance if redistributing diffs (e.g., open-source projects). |

Key Questions

  1. Where will diffs be used?
    • UI (e.g., admin panel) → Optimize for readability (CSS, collapsible sections).
    • Backend (e.g., logs) → Optimize for storage (compress diffs with gzencode).
  2. What’s the scale?
    • Small: Direct HtmlDiff calls in controllers.
    • Large: Queue jobs + cache (e.g., Redis).
  3. Do you need diff metadata?
    • Extend HtmlDiff to return { changes: [], stats: { additions: 0, deletions: 0 } }.
  4. How to handle binary data?
    • Base64-encode images/PDFs before diffing (e.g., <img src="data:image/png;base64,...">).
  5. Fallback for unsupported HTML?
    • Use setUseTableDiffing(false) to treat tables as text if structure is irrelevant.

Integration Approach

Stack Fit

Laravel Component Integration Strategy
Blade Templates @diff($oldHtml, $newHtml) helper with inline CSS.
Queues HtmlDiffJob for async generation (e.g., diffing user-generated content).
Testing assertHtmlDiff($expected, $actual) custom assertion in Pest.
APIs Return diffs in responses (e.g., application/html with Content-Disposition: attachment).
Caching Cache diffs with cache()->remember() or doctrine/cache.
Storage Store diffs in:
  • Database (longtext).
  • Filesystem (storage/app/diffs/).
  • S3 (for large diffs). | | Symfony Bundle | Use caxy/htmldiff-bundle if already using Symfony components (e.g., Twig). |

Migration Path

  1. Phase 1: Proof of Concept

    • Install via Composer: composer require caxy/php-htmldiff.
    • Test basic diffs in a Tinker session:
      use Caxy\HtmlDiff\HtmlDiff;
      $diff = new HtmlDiff($oldHtml, $newHtml);
      echo $diff->build();
      
    • Validate output with the demo.
  2. Phase 2: Laravel Integration

    • Service Provider:
      // app/Providers/HtmlDiffServiceProvider.php
      public function register()
      {
          $this->app->singleton(HtmlDiff::class, function () {
              $config = new HtmlDiffConfig();
              $config->setMatchThreshold(90)->setGroupDiffs(true);
              return new HtmlDiff('', '', $config);
          });
      }
      
    • Facade:
      // app/Facades/HtmlDiff.php
      public static function diff(string $oldHtml, string $newHtml): string
      {
          return app(HtmlDiff::class)->setHtml($oldHtml, $newHtml)->build();
      }
      
    • Config:
      // config/htmldiff.php
      return [
          'match_threshold' => 85,
          'group_diffs' => true,
          'purifier_enabled' => env('APP_DEBUG') ? true : false,
      ];
      
  3. Phase 3: Scaling

    • Queue Jobs:
      // app/Jobs/GenerateHtmlDiff.php
      public function handle()
      {
          $diff = HtmlDiff::diff($this->oldHtml, $this->newHtml);
          $this->user->diff_html = $diff;
          $this->user->save();
      }
      
    • Caching:
      // Cache diffs for 1 hour
      $diff = cache()->remember("diff_{$postId}", 3600, function () use ($oldHtml, $newHtml) {
          return HtmlDiff::diff($oldHtml, $newHtml);
      });
      
  4. Phase 4: Advanced Use Cases

    • Database Triggers: Use Laravel Events (ModelObservers) to auto-generate diffs on updated.
    • Webhooks: Trigger diffs via post-update webhooks (e.g., GitHub Pages changes).
    • CLI Tool: Extend with Artisan commands for bulk diffing:
      // app/Console/Commands/DiffPosts.php
      public function handle()
      {
          $posts = Post::where('updated_at', '>', now()->subDays(7))->get();
          foreach ($posts as $post) {
              $diff = HtmlDiff::diff($post->old_content, $post->content);
              // Store or notify...
          }
      }
      

Compatibility

  • Laravel Versions: Tested with Laravel 8+ (PHP 7.3+). For Laravel 7, pin v0.1.9.
  • HTML Input:
    • Supported: Well-formed HTML (tables, lists, nested tags).
    • Workarounds:
      • Malformed HTML → Pre-process with DOMDocument::loadHTML().
      • Binary data → Base64-encode or exclude from diff.
  • CSS: Provide a default stylesheet (e.g., via Laravel Mix):
    /* resources/css/htmldiff.css */
    .htmldiff .ins { background: #ddffdd; }
    .htmldiff .del { background: #ffdddd
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui