Reduce complexity in _filter_url()

Created on 6 December 2022, over 1 year ago
Updated 30 March 2023, about 1 year ago

Problem/Motivation

While reviewing πŸ› preg_split in _filter_url breaks for long html tags Fixed , @larowlan and I noticed some ways we could simplify _filter_url(). We agreed that those changes are out of scope for that issue, so we plan to make the changes here.

Proposed resolution

The outer loop starts with escaping HTML comments and ends with restoring them:

  foreach ($tasks as $task => $pattern) {
    // HTML comments need to be handled separately, as they may contain HTML
    // markup, especially a '>'. Therefore, remove all comment contents and add
    // them back later.
    _filter_url_escape_comments('', TRUE);
    $text = $text ? preg_replace_callback('`<!--(.*?)-->`s', '_filter_url_escape_comments', $text) : $text;

// ...

    // Revert to the original comment contents
    _filter_url_escape_comments('', FALSE);
    $text = $text ? preg_replace_callback('`<!--(.*?)-->`', '_filter_url_escape_comments', $text) : $text;
  }

None of the callbacks in the body of the loop introduce new HTML comments, so we can move the calls to _filter_url_escape_comments() out of the loop.

Once we do that, we can replace the change from πŸ› preg_split in _filter_url breaks for long html tags Fixed with an exit-early strategy (if (...) { continue; }).

Next, the inner loop has the structure

    for ($i = 0; $i < count($chunks); $i++) {
      if ($chunk_type == 'text') {
//...
      }
      else {
        if ($open_tag == '') {
// ...
        }
        else {
// ...
      }
    }

That can be simplified: make it three if blocks, with a continue statement at the end of the first two.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/A

πŸ“Œ Task
Status

Active

Version

10.1 ✨

Component
FilterΒ  β†’

Last updated about 10 hours ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States benjifisher Boston area

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024