From 4ef233d993d6fd951b33dbdeca69ecb3cb3d0e36 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 20 Aug 2024 11:07:13 +0800 Subject: [PATCH] MDL-82828 core: Update Html2Text to version 4.3.2 --- lib/classes/component.php | 1 + lib/html2text/README.md | 26 ++++++++++++++++++++++++++ lib/html2text/composer.json | 23 +++++++++++++++++++++++ lib/html2text/lib.php | 1 - lib/html2text/readme_moodle.txt | 6 ++++-- lib/html2text/{ => src}/Html2Text.php | 22 ++++++++++++++++------ lib/thirdpartylibs.xml | 2 +- 7 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 lib/html2text/README.md create mode 100644 lib/html2text/composer.json rename lib/html2text/{ => src}/Html2Text.php (97%) diff --git a/lib/classes/component.php b/lib/classes/component.php index fa4e64daa77..3cc12bfa2d0 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -102,6 +102,7 @@ class component { ]; /** @var array> associative array of PRS-4 namespaces and corresponding paths. */ protected static $psr4namespaces = [ + \Html2Text::class => 'lib/html2text/src', \MaxMind::class => 'lib/maxmind/MaxMind', \GeoIp2::class => 'lib/maxmind/GeoIp2', \Sabberworm\CSS::class => 'lib/php-css-parser', diff --git a/lib/html2text/README.md b/lib/html2text/README.md new file mode 100644 index 00000000000..cb619b3fa16 --- /dev/null +++ b/lib/html2text/README.md @@ -0,0 +1,26 @@ +# Html2Text + +A PHP library for converting HTML to formatted plain text. + +[![Build status](https://github.com/mtibben/html2text/actions/workflows/ci.yml/badge.svg)](https://github.com/mtibben/html2text/actions/workflows/ci.yml) + +## Installing + +``` +composer require html2text/html2text +``` + +## Basic Usage +```php +$html = new \Html2Text\Html2Text('Hello, "world"'); + +echo $html->getText(); // Hello, "WORLD" +``` + +## History + +This library started life on the blog of Jon Abernathy http://www.chuggnutt.com/html2text + +A number of projects picked up the library and started using it - among those was RoundCube mail. They made a number of updates to it over time to suit their webmail client. + +Now it has been extracted as a standalone library. Hopefully it can be of use to others. diff --git a/lib/html2text/composer.json b/lib/html2text/composer.json new file mode 100644 index 00000000000..7cfb7fe1066 --- /dev/null +++ b/lib/html2text/composer.json @@ -0,0 +1,23 @@ +{ + "name": "html2text/html2text", + "description": "Converts HTML to formatted plain text", + "type": "library", + "license": "GPL-2.0-or-later", + "autoload": { + "psr-4": { + "Html2Text\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Html2Text\\": "test/" + } + }, + "require-dev": { + "phpunit/phpunit": "~4|^9.0" + }, + "suggest": { + "ext-mbstring": "For best performance", + "symfony/polyfill-mbstring": "If you can't install ext-mbstring" + } +} \ No newline at end of file diff --git a/lib/html2text/lib.php b/lib/html2text/lib.php index 6c4588e22a6..d99b109157d 100644 --- a/lib/html2text/lib.php +++ b/lib/html2text/lib.php @@ -26,7 +26,6 @@ defined('MOODLE_INTERNAL') || die(); -require_once($CFG->libdir . '/html2text/Html2Text.php'); require_once(__DIR__ . '/override.php'); /** diff --git a/lib/html2text/readme_moodle.txt b/lib/html2text/readme_moodle.txt index 37cab254a25..fa41d264f4a 100644 --- a/lib/html2text/readme_moodle.txt +++ b/lib/html2text/readme_moodle.txt @@ -1,4 +1,4 @@ -Description of Html2Text v4.3.1 library import into Moodle +Description of Html2Text library import into Moodle Please note that we override some mb_* functions in Html2Text's namespace at run time. Until Html2Text adds some sort of fallback for the mb_* functions @@ -9,6 +9,8 @@ Instructions ------------ 1. Download the latest release of Html2Text from https://github.com/mtibben/html2text/releases/ 2. Extract the contents of the release archive into a directory. -3. Copy src/Html2Text.php to lib/html2text/ +3. Copy src to lib/html2text/src +4. Update README +5. Update composer.json Imported from: https://github.com/mtibben/html2text/releases/ diff --git a/lib/html2text/Html2Text.php b/lib/html2text/src/Html2Text.php similarity index 97% rename from lib/html2text/Html2Text.php rename to lib/html2text/src/Html2Text.php index 9fd91235927..6e0f9e50e83 100644 --- a/lib/html2text/Html2Text.php +++ b/lib/html2text/src/Html2Text.php @@ -236,16 +236,19 @@ class Html2Text */ public function __construct($html = '', $options = array()) { + $this->htmlFuncFlags = (PHP_VERSION_ID < 50400) + ? ENT_QUOTES + : ENT_QUOTES | ENT_HTML5; + // for backwards compatibility if (!is_array($options)) { - return call_user_func_array(array($this, 'legacyConstruct'), func_get_args()); + // phpcs:ignore (PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection + call_user_func_array(array($this, 'legacyConstruct'), func_get_args()); + return; } $this->html = $html; $this->options = array_merge($this->options, $options); - $this->htmlFuncFlags = (PHP_VERSION_ID < 50400) - ? ENT_COMPAT - : ENT_COMPAT | ENT_HTML5; } /** @@ -351,7 +354,11 @@ class Html2Text { $this->linkList = array(); - $text = trim($this->html); + if ($this->html === null) { + $text = ''; + } else { + $text = trim($this->html); + } $this->converter($text); @@ -389,6 +396,9 @@ class Html2Text $text = preg_replace("/[\n]{3,}/", "\n\n", $text); // remove leading empty lines (can be produced by eg. P tag on the beginning) + if ($text === null) { + $text = ''; + } $text = ltrim($text, "\n"); if ($this->options['width'] > 0) { @@ -417,7 +427,7 @@ class Html2Text } // Ignored link types - if (preg_match('!^(javascript:|mailto:|#)!i', html_entity_decode($link))) { + if (preg_match('!^(javascript:|mailto:|#)!i', html_entity_decode($link, $this->htmlFuncFlags, self::ENCODING))) { return $display; } diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml index 62fa9f82985..f535554e2c4 100644 --- a/lib/thirdpartylibs.xml +++ b/lib/thirdpartylibs.xml @@ -235,7 +235,7 @@ html2text HTML2Text PHP script to convert HTML into an approximate text equivalent. - 4.3.1 + 4.3.2 GPL 2.0+ https://github.com/mtibben/html2text