Text::HTML::CollapseWhitespace(3)

NAME
Text::HTML::CollapseWhitespace - remove extraneous whitespace from a fragment

SYNOPSIS
  my $tree = XML::LibXML->new->parse_html_string(
      $input,
      { recover => 2, encoding => 'UTF-8' }
  );
  $tree = collapseWhitespace($tree);

FUNCTIONS

  "collapseWhitespace (%options)"
  collapseWhitespace( element => $tree,
      isVoid  => \&_isVoid,
  )

  our @voidElements = (
    'AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT',
    'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR'
  );
  our %voidElements = map { $_ => 1, lc $_ => 1 } @voidElements;
  sub _isVoid( $element ) {
      $voidElements{ $element->nodeName }
  }

This function modifies the tree in-place and removes extraneous whitespace from the elements. The "isPre", "isVoid" and "isBlock" predicates allow you to customize what elements are recognized as pre , void or block HTML elements if needed.

  remove($note)
  my $next = remove( $node );

remove(node) removes the given node from the DOM and returns the next node in the sequence.

  _next(prev, current, isPre)
  my $next = _next( $node );

returns the next node in the sequence, given the current and previous nodes.

@param {Node} prev @param {Node} current @param {Function} isPre @return {Node}

LICENSE
The collapseWhitespace function is adapted from collapse-whitespace by Luc Thevenard.

The MIT License (MIT)

Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

REPOSITORY
The public repository of this module is <https://github.com/Corion/Text-HTML-Turndown>.

SUPPORT
The public support forum of this module is <https://perlmonks.org/>.

BUG TRACKER
Please report bugs in this module via the Github bug queue at <https://github.com/Corion/Text-HTML-Turndown/issues>

AUTHOR
Max Maischein "corion@cpan.org"

COPYRIGHT (c)
Copyright 2025- by Max Maischein "corion@cpan.org".

LICENSE
This module is released under the Artistic License 2.0 as far as permitted by the license of the original code.