Skip to content

Commit

Permalink
Experiment: Run the Tag Processor on every tag on output.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Nov 12, 2023
1 parent 0079130 commit ae3a8fc
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/_index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,93 @@
*/
define( 'WP_USE_THEMES', true );

ob_start( 'dms_everything' );

function dms_parse_url( $url ) {
$fragment_at = strpos( $url, '#' );
$fragment = false !== $fragment_at ? substr( $url, $fragment_at + 1 ) : '';
$url_without_fragment = false !== $fragment_at ? substr( $url, 0, $fragment_at ) : $url;
$query_at = strpos( $url_without_fragment, '?' );

$query = false !== $query_at ? substr( $url_without_fragment, $query_at + 1 ) : '';
$pairs = explode( '&', $query );
$args = [];

Check failure on line 26 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Short array syntax is not allowed
foreach ( $pairs as $pair ) {
if ( '' === $pair ) {
continue;
}
list( $key, $value ) = explode( '=', $pair );
$args[ $key ] = urldecode( $value );
}

$url_without_query = false !== $query_at ? substr( $url_without_fragment, 0, $query_at ) : $url_without_fragment;
$is_relative = '/' === $url_without_query[0];
$has_schema = preg_match( '~^([a-z]+)://~i', $url_without_query, $schema_match );
$schema = $has_schema ? $schema_match[1] : '';
$domain_path = $has_schema ? substr( $url_without_query, strlen( $schema_match[0] ) ) : $url_without_query;

if ( ! $is_relative ) {
$first_slash = strpos( $domain_path, '/' );
$domain = false !== $first_slash ? substr( $domain_path, 0, $first_slash ) : '';
$path = false !== $first_slash ? substr( $domain_path, $first_slash ) : $domain_path;
} else {
$domain = '';
$path = $domain_path;
}

return [

Check failure on line 50 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Short array syntax is not allowed
'schema' => $schema,
'domain' => $domain,
'path' => $path,
'query' => count( $args ) > 0 ? $args : null,
'fragment' => $fragment,
];
}

function dms_everything( $html ) {
$processor = new WP_HTML_Tag_Processor( $html );
$tag_count = 0;
$seen_ids = [];

Check failure on line 62 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Short array syntax is not allowed

$errors = [];

Check failure on line 64 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Short array syntax is not allowed

while ( $processor->next_tag() ) {
$tag_count++;
$tag_name = $processor->get_tag();

$id = $processor->get_attribute( 'id' );
if ( is_string( $id ) ) {
if ( isset( $seen_ids[ $id ] ) ) {
$errors[] = "Repeated id = <{$tag_name} id=\"{$id}\">; seen first in {$seen_ids[$id]} tag.";
} else {
$seen_ids[ $id ] = $tag_name;
}
}

$src = $processor->get_attribute( 'src' );
if ( is_string( $src ) ) {
$url = dms_parse_url( $src );
$errors[] = "Found URL in <{$tag_name} src=\"{$src}\">.";
$errors[] = print_r( $url, true );
$errors[] = "\n";
}

$href = $processor->get_attribute( 'href' );
if ( is_string( $href ) ) {
$url = dms_parse_url( $href );
$errors[] = "Found URL in <{$tag_name} href=\"{$href}\">.";
$errors[] = print_r( $url, true );
$errors[] = "\n";
}
}

return $html;
// return count( $errors ) > 0

Check failure on line 97 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 1 tabs, found 0

Check failure on line 97 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Spaces must be used for mid-line alignment; tabs are not allowed
// ? '<plaintext>' . "Found {$tag_count} tags.\n\n" . implode( "\n", array_map( function( $e ) { return print_r( $e, true ); }, $errors ) )

Check failure on line 98 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 1 tabs, found 0

Check failure on line 98 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Spaces must be used for mid-line alignment; tabs are not allowed
// : $html;

Check failure on line 99 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 1 tabs, found 0

Check failure on line 99 in src/_index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Spaces must be used for mid-line alignment; tabs are not allowed
}

/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php';

ob_end_flush();

0 comments on commit ae3a8fc

Please sign in to comment.