Skip to content

Commit

Permalink
ENH Use masterminds/html5 for HTMLValue
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 17, 2023
1 parent 6d45425 commit d7ddb00
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 145 deletions.
4 changes: 0 additions & 4 deletions _config/html.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
Name: corehtml
---
SilverStripe\Core\Injector\Injector:
SilverStripe\View\Parsers\HTMLValue:
class: SilverStripe\View\Parsers\HTML4Value
# Shorthand
HTMLValue: '%$SilverStripe\View\Parsers\HTMLValue'
SilverStripe\Forms\HTMLEditor\HTMLEditorConfig:
class: SilverStripe\Forms\HTMLEditor\TinyMCEConfig
SilverStripe\Forms\HTMLEditor\TinyMCEScriptGenerator: '%$SilverStripe\Forms\HTMLEditor\TinyMCECombinedGenerator'
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"embed/embed": "^4.4.7",
"league/csv": "^9.8.0",
"m1/env": "^2.2.0",
"masterminds/html5": "^2.7",
"monolog/monolog": "^3.2.0",
"nikic/php-parser": "^4.15.0",
"psr/container": "^2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/View/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function createTag($tag, $attributes, $content = null)
if ($content) {
throw new InvalidArgumentException("Void element \"{$tag}\" cannot have content");
}
return "<{$tag}{$preparedAttributes} />";

This comment has been minimized.

Copy link
@hudhaifas

hudhaifas Feb 22, 2024

What is the purpose of this change?

This comment has been minimized.

Copy link
@kinglozzer
return "<{$tag}{$preparedAttributes}>";
}

// Closed tag type
Expand Down
31 changes: 0 additions & 31 deletions src/View/Parsers/HTML4Value.php

This file was deleted.

28 changes: 22 additions & 6 deletions src/View/Parsers/HTMLValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

use SilverStripe\Core\Convert;
use SilverStripe\View\ViewableData;
use Masterminds\HTML5;
use DOMNodeList;
use DOMXPath;
use DOMDocument;
use SilverStripe\View\HTML;

/**
* This class handles the converting of HTML fragments between a string and a DOMDocument based
* representation.
*
* It's designed to allow dependency injection to replace the standard HTML4 version with one that
* handles XHTML or HTML5 instead
*
* @mixin DOMDocument
*/
abstract class HTMLValue extends ViewableData
class HTMLValue extends ViewableData
{

public function __construct($fragment = null)
{
if ($fragment) {
Expand All @@ -28,7 +26,25 @@ public function __construct($fragment = null)
parent::__construct();
}

abstract public function setContent($fragment);
/**
* @param string $content
* @return bool
*/
public function setContent($content)
{
$content = preg_replace('#</?(html|head|body)[^>]*>#si', '', $content);
$html5 = new HTML5(['disable_html_ns' => true]);
$document = $html5->loadHTML(
'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' .
"<body>$content</body></html>"
);
if ($document) {
$this->setDocument($document);
return true;
}
$this->valid = false;
return false;
}

/**
* @return string
Expand Down
4 changes: 2 additions & 2 deletions src/View/Parsers/ShortcodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ public function parse($content)
// use a proper DOM
list($content, $tags) = $this->replaceElementTagsWithMarkers($content);

/** @var HTMLValue $htmlvalue */
$htmlvalue = Injector::inst()->create('HTMLValue', $content);
/** @var HTMLValue $htmlvalue */
$htmlvalue = Injector::inst()->create(HTMLValue::class, $content);

// Now parse the result into a DOM
if (!$htmlvalue->isValid()) {
Expand Down
6 changes: 3 additions & 3 deletions tests/php/View/HTMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testCreateVoidTag()
'name' => 'description',
'content' => 'test tag',
]);
$this->assertEquals('<meta name="description" content="test tag" />', $tag);
$this->assertEquals('<meta name="description" content="test tag">', $tag);
}

public function testEmptyAttributes()
Expand All @@ -27,7 +27,7 @@ public function testEmptyAttributes()
'disabled' => false,
'readonly' => true,
]);
$this->assertEquals('<meta value="0" max="3" readonly="1" />', $tag);
$this->assertEquals('<meta value="0" max="3" readonly="1">', $tag);
}

public function testNormalTag()
Expand All @@ -52,7 +52,7 @@ public function testImgTag()
'alt' => '',
]);

$this->assertEquals('<img src="example.png" alt="" />', $tag);
$this->assertEquals('<img src="example.png" alt="">', $tag);
}

public function testVoidContentError()
Expand Down
98 changes: 0 additions & 98 deletions tests/php/View/Parsers/HTML4ValueTest.php

This file was deleted.

Loading

0 comments on commit d7ddb00

Please sign in to comment.