Skip to content
Closed
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
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**Tags:** TOC, Table of Contents, Block, Accessibility, Table
**Requires at least:** 6.2
**Tested up to:** 7.0
**Stable tag:** 7.1.0
**Stable tag:** 7.2.0
**Requires PHP:** 7.3
**License:** GPLv2 or later
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -72,14 +72,8 @@ Hidden TOCs use native `<details>` and `<summary>` semantics without extra ARIA
SimpleTOC is open-source and developed on [GitHub Pages](https://github.com/mtoensing/SimpleTOC). If you find a bug or have an idea for a feature please feel free to contribute and create a pull request.

## Changelog ##
### 7.1.0 ###
* Fixed: Avoid DOMDocument HTML parsing for highlighted heading markup by using the WordPress HTML API.
* Changed: Minimum required WordPress version is now 6.2.
* Added: PHPUnit coverage for core rendering helpers and an editor e2e smoke test.

### 7.0.10 ###
* Fixed: Prevent the SimpleTOC editor preview from collapsing during WordPress autosaves.
* Changed: Automatic editor refresh now runs after manual post saves and keeps the previous TOC visible while updating.
### 7.2.0 ###
* Fixed: Prevented WordPress HTML API notices when nested block content is not a string.

## Installation ##

Expand Down
2 changes: 1 addition & 1 deletion build/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "simpletoc/toc",
"version": "7.1.0",
"version": "7.2.0",
"title": "SimpleTOC",
"category": "layout",
"icon": "<svg clip-rule='evenodd' fill-rule='evenodd' stroke-linejoin='round' stroke-miterlimit='2' viewBox='0 0 500 500' xmlns='http://www.w3.org/2000/svg'><path d='m365.983 78.248c2.72-32.415 29.921-57.908 63.029-57.908 34.91 0 63.253 28.342 63.253 63.252s-28.343 63.252-63.253 63.252v269.582c0 25.232-20.485 45.718-45.718 45.718h-313.62c-25.233 0-45.719-20.486-45.719-45.718v-292.46c0-25.233 20.486-45.718 45.719-45.718z' fill='#070D0D'/><g fill='#fff'><path d='m423.898 112.744v-24.188h-24.008v-10.108h24.008v-24.008h10.229v24.008h24.008v10.108h-24.008v24.188z'/><path d='m120.04 403.105c-16.883 0-30.511-13.719-30.511-30.714 0-16.79 13.628-30.714 30.511-30.714 16.679 0 30.511 13.924 30.511 30.714 0 16.995-13.832 30.714-30.511 30.714z'/><path d='m170.891 362.153h183.063v20.476h-183.063z'/><path d='m120.04 300.725c-16.883 0-30.511-13.719-30.511-30.714 0-16.79 13.628-30.714 30.511-30.714 16.679 0 30.511 13.924 30.511 30.714 0 16.995-13.832 30.714-30.511 30.714z'/><path d='m170.891 259.773h183.063v20.476h-183.063z'/><path d='m120.04 198.345c-16.883 0-30.511-13.719-30.511-30.714 0-16.791 13.628-30.714 30.511-30.714 16.679 0 30.511 13.923 30.511 30.714 0 16.995-13.832 30.714-30.511 30.714z'/><path d='m170.891 157.393h183.063v20.476h-183.063z'/></g></svg>",
Expand Down
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '8ec3bdb72d0c6301fa9c');
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-server-side-render'), 'version' => '1512672d04ad478e8e8e');
2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
== Changelog ==

= 7.2.0 =
* Fixed: Prevented WordPress HTML API notices when nested block content is not a string.

= 7.1.0 =
* Fixed: Avoid DOMDocument HTML parsing for highlighted heading markup by using the WordPress HTML API.
* Changed: Minimum required WordPress version is now 6.2.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simpletoc",
"version": "7.1.0",
"version": "7.2.0",
"author": "Marc Tönsing",
"description": "SEO-friendly Table of Contents Gutenberg block. No JavaScript or CSS by default.",
"main": "build/index.js",
Expand Down
54 changes: 40 additions & 14 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: SimpleTOC - Table of Contents Block
* Plugin URI: https://marc.tv/simpletoc-wordpress-inhaltsverzeichnis-plugin-gutenberg/
* Description: SEO-friendly Table of Contents Gutenberg block. No JavaScript or CSS by default.
* Version: 7.1.0
* Version: 7.2.0
* Requires at least: 6.2
* Requires PHP: 7.3
* Author: Marc Tönsing
Expand All @@ -21,7 +21,7 @@
require_once __DIR__ . '/simpletoc-class-headline-ids.php';

const DEFAULT_BOX_COLOR = '#ebebeb';
const SIMPLETOC_VERSION = '7.1.0';
const SIMPLETOC_VERSION = '7.2.0';

/**
* Prevents direct execution of the plugin file.
Expand Down Expand Up @@ -525,6 +525,23 @@ function simpletoc_load_html_tag_processor() {
return class_exists( '\WP_HTML_Tag_Processor' );
}

/**
* Creates an HTML Tag Processor for a valid HTML fragment.
*
* Parsed block content can contain non-string placeholders for nested blocks.
* The WordPress HTML API accepts strings only.
*
* @param mixed $html The HTML fragment to inspect.
* @return \WP_HTML_Tag_Processor|null The processor, or null when unavailable or invalid.
*/
function simpletoc_get_html_tag_processor( $html ) {
if ( ! is_string( $html ) || ! simpletoc_load_html_tag_processor() ) {
return null;
}

return new \WP_HTML_Tag_Processor( $html );
}

/**
* Returns true when the provided HTML contains a heading tag.
*
Expand All @@ -542,8 +559,9 @@ function simpletoc_is_heading_html( $html ) {
* @return int|false The heading depth, or false when no heading was found.
*/
function simpletoc_get_heading_depth( $html ) {
if ( simpletoc_load_html_tag_processor() ) {
$processor = new \WP_HTML_Tag_Processor( $html );
$processor = simpletoc_get_html_tag_processor( $html );

if ( $processor ) {

while ( $processor->next_tag() ) {
$tag_name = $processor->get_tag();
Expand All @@ -565,8 +583,9 @@ function simpletoc_get_heading_depth( $html ) {
* @return string The updated HTML.
*/
function simpletoc_add_page_number_to_headline( $html, $page_number ) {
if ( simpletoc_load_html_tag_processor() ) {
$processor = new \WP_HTML_Tag_Processor( $html );
$processor = simpletoc_get_html_tag_processor( $html );

if ( $processor ) {

while ( $processor->next_tag() ) {
if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
Expand All @@ -589,8 +608,9 @@ function simpletoc_add_page_number_to_headline( $html, $page_number ) {
* @return bool True when the class exists.
*/
function simpletoc_heading_has_class( $html, $class_name ) {
if ( simpletoc_load_html_tag_processor() ) {
$processor = new \WP_HTML_Tag_Processor( $html );
$processor = simpletoc_get_html_tag_processor( $html );

if ( $processor ) {

while ( $processor->next_tag() ) {
if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
Expand Down Expand Up @@ -619,6 +639,9 @@ function simpletoc_heading_has_class( $html, $class_name ) {
* @return string The modified HTML content with ID attributes added to the Heading tags
*/
function add_anchor_attribute( $html, $headline_class_instance = null, $block = array() ) {
if ( ! is_string( $html ) ) {
return $html;
}

// remove non-breaking space entites from input HTML.
$html_wo_nbs = str_replace( '&nbsp;', ' ', $html );
Expand All @@ -628,8 +651,9 @@ function add_anchor_attribute( $html, $headline_class_instance = null, $block =
return $html;
}

if ( simpletoc_load_html_tag_processor() ) {
$processor = new \WP_HTML_Tag_Processor( $html_wo_nbs );
$processor = simpletoc_get_html_tag_processor( $html_wo_nbs );

if ( $processor ) {

while ( $processor->next_tag() ) {
if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
Expand Down Expand Up @@ -982,8 +1006,9 @@ function add_accordion_end( $html, $attributes ) {
* @return string|false Returns the extracted ID value, or false if no ID value is found.
*/
function extract_id( $headline ) {
if ( simpletoc_load_html_tag_processor() ) {
$processor = new \WP_HTML_Tag_Processor( $headline );
$processor = simpletoc_get_html_tag_processor( $headline );

if ( $processor ) {

while ( $processor->next_tag() ) {
if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
Expand All @@ -1008,8 +1033,9 @@ function extract_id( $headline ) {
* @return string The page number (in the format "X/") if it exists and is greater than 1, or an empty string otherwise.
*/
function get_page_number_from_headline( $headline ) {
if ( simpletoc_load_html_tag_processor() ) {
$processor = new \WP_HTML_Tag_Processor( $headline );
$processor = simpletoc_get_html_tag_processor( $headline );

if ( $processor ) {

while ( $processor->next_tag() ) {
if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
Expand Down
12 changes: 3 additions & 9 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: MarcDK
Tags: TOC, Table of Contents, Block, Accessibility, Table
Requires at least: 6.2
Tested up to: 7.0
Stable tag: 7.1.0
Stable tag: 7.2.0
Requires PHP: 7.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -72,14 +72,8 @@ Hidden TOCs use native `<details>` and `<summary>` semantics without extra ARIA
SimpleTOC is open-source and developed on [GitHub Pages](https://github.com/mtoensing/SimpleTOC). If you find a bug or have an idea for a feature please feel free to contribute and create a pull request.

== Changelog ==
= 7.1.0 =
* Fixed: Avoid DOMDocument HTML parsing for highlighted heading markup by using the WordPress HTML API.
* Changed: Minimum required WordPress version is now 6.2.
* Added: PHPUnit coverage for core rendering helpers and an editor e2e smoke test.

= 7.0.10 =
* Fixed: Prevent the SimpleTOC editor preview from collapsing during WordPress autosaves.
* Changed: Automatic editor refresh now runs after manual post saves and keeps the previous TOC visible while updating.
= 7.2.0 =
* Fixed: Prevented WordPress HTML API notices when nested block content is not a string.

== Installation ==

Expand Down
2 changes: 1 addition & 1 deletion src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "simpletoc/toc",
"version": "7.1.0",
"version": "7.2.0",
"title": "SimpleTOC",
"category": "layout",
"icon": "<svg clip-rule='evenodd' fill-rule='evenodd' stroke-linejoin='round' stroke-miterlimit='2' viewBox='0 0 500 500' xmlns='http://www.w3.org/2000/svg'><path d='m365.983 78.248c2.72-32.415 29.921-57.908 63.029-57.908 34.91 0 63.253 28.342 63.253 63.252s-28.343 63.252-63.253 63.252v269.582c0 25.232-20.485 45.718-45.718 45.718h-313.62c-25.233 0-45.719-20.486-45.719-45.718v-292.46c0-25.233 20.486-45.718 45.719-45.718z' fill='#070D0D'/><g fill='#fff'><path d='m423.898 112.744v-24.188h-24.008v-10.108h24.008v-24.008h10.229v24.008h24.008v10.108h-24.008v24.188z'/><path d='m120.04 403.105c-16.883 0-30.511-13.719-30.511-30.714 0-16.79 13.628-30.714 30.511-30.714 16.679 0 30.511 13.924 30.511 30.714 0 16.995-13.832 30.714-30.511 30.714z'/><path d='m170.891 362.153h183.063v20.476h-183.063z'/><path d='m120.04 300.725c-16.883 0-30.511-13.719-30.511-30.714 0-16.79 13.628-30.714 30.511-30.714 16.679 0 30.511 13.924 30.511 30.714 0 16.995-13.832 30.714-30.511 30.714z'/><path d='m170.891 259.773h183.063v20.476h-183.063z'/><path d='m120.04 198.345c-16.883 0-30.511-13.719-30.511-30.714 0-16.791 13.628-30.714 30.511-30.714 16.679 0 30.511 13.923 30.511 30.714 0 16.995-13.832 30.714-30.511 30.714z'/><path d='m170.891 157.393h183.063v20.476h-183.063z'/></g></svg>",
Expand Down
11 changes: 11 additions & 0 deletions tests/test-simpletoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ public function test_get_heading_depth_reads_heading_tag_with_html_tag_processor
$this->assertSame( 4, $result );
}

/**
* @covers \MToensing\SimpleTOC\simpletoc_get_html_tag_processor
* @covers \MToensing\SimpleTOC\simpletoc_get_heading_depth
*/
public function test_html_tag_processor_ignores_non_string_block_content() {
$this->assertNull( MToensing\SimpleTOC\simpletoc_get_html_tag_processor( null ) );
$this->assertFalse( MToensing\SimpleTOC\simpletoc_get_heading_depth( null ) );
$this->assertSame( '', MToensing\SimpleTOC\get_page_number_from_headline( null ) );
$this->assertFalse( MToensing\SimpleTOC\extract_id( null ) );
}

/**
* @covers \MToensing\SimpleTOC\simpletoc_add_page_number_to_headline
*/
Expand Down