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

ENH Use masterminds/html5 for HTMLValue #10647

Merged
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
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'
maxime-rainville marked this conversation as resolved.
Show resolved Hide resolved
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} />";
return "<{$tag}{$preparedAttributes}>";
maxime-rainville marked this conversation as resolved.
Show resolved Hide resolved
}

// 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