Clone this repo:
  1. 318b9fe build: Updating mediawiki/mediawiki-codesniffer to 51.0.0 by libraryupgrader · 10 weeks ago master
  2. e8ed5d2 build: Updating mediawiki/mediawiki-phan-config to 0.20.0 by libraryupgrader · 4 months ago
  3. d5dff33 build: Updating mediawiki/mediawiki-codesniffer to 50.0.0 by libraryupgrader · 5 months ago
  4. fa53ddf build: Updating composer dependencies by libraryupgrader · 5 months ago
  5. 9ba721d build: Upgrade mediawiki-phan-config for PHP 8.5 support by James D. Forrester · 5 months ago

HtmlFormatter is a library spun off MediaWiki that allows you to load HTML into DomDocument, perform manipulations on it, and then return a HTML string.

Usage

use HtmlFormatter\HtmlFormatter;
// Load HTML that already has doctype and stuff
$formatter = new HtmlFormatter( $html );

// ...or one that doesn't have it
$formatter = new HtmlFormatter( HtmlFormatter::wrapHTML( $html ) );

// Add rules to remove some stuff
$formatter->remove( 'img' );
$formatter->remove( [ '.some_css_class', '#some_id', 'div.some_other_class' ] );
// Only the above syntax is supported, not full CSS/jQuery selectors

// These tags get replaced with their inner HTML,
// e.g. <tag>foo</tag> --> foo
// Only tag names are supported here
$formatter->flatten( 'span' );
$formatter->flatten( [ 'code', 'pre' ] );

// Actually perform the removals
$formatter->filterContent();

// Direct DomDocument manipulations are possible
$formatter->getDoc()->createElement( 'p', 'Appended paragraph' );

// Get resulting HTML
$processedHtml = $formatter->getText();

License

Copyright 2011-2024 MediaWiki contributors

Released under the GNU General Public License version 2, see COPYING.