Make WordPress Core

Ticket #12480: 12480.2.diff

File 12480.2.diff, 5.4 KB (added by stevegrunwell, 8 years ago)

Refresh for WordPress 4.4 with addition of unit tests + better WordPress coding standards support

  • tests/phpunit/tests/formatting/FillEmptyLinks.php

     
     1<?php
     2/**
     3 * @group formatting
     4 */
     5class Tests_Formatting_FillEmptyLinks extends WP_UnitTestCase {
     6
     7        /**
     8         * @ticket 12480
     9         */
     10        public function test_content_with_link() {
     11                $content = 'This <a href="http://example.com"></a>is a link';
     12
     13                $this->assertEquals(
     14                        'This <a href="http://example.com">http://example.com</a>is a link',
     15                        fill_empty_links( $content )
     16                );
     17        }
     18
     19        /**
     20         * @ticket 12480
     21         */
     22        public function test_content_with_multiple_links() {
     23                $content = 'This <a href="http://example.com"></a>is a link <a href="http://example.com/test"></a>';
     24
     25                $this->assertEquals(
     26                        'This <a href="http://example.com">http://example.com</a>is a link <a href="http://example.com/test">http://example.com/test</a>',
     27                        fill_empty_links( $content )
     28                );
     29        }
     30
     31        /**
     32         * Sanity check: verify we're not changing things when links aren't present.
     33         *
     34         * @ticket 12480
     35         */
     36        public function test_content_without_links() {
     37                $content = 'Lorem ipsum sit dolor';
     38
     39                $this->assertEquals( $content, fill_empty_links( $content ) );
     40        }
     41
     42        /**
     43         * @ticket 12480
     44         */
     45        public function test_content_with_whitespace() {
     46                $this->assertEquals(
     47                        'This <a href="http://example.com">http://example.com</a>is a link',
     48                        fill_empty_links( 'This <a href="http://example.com"> </a>is a link' ),
     49                        'fill_empty_links() fails to match regular whitespace'
     50                );
     51
     52                $this->assertEquals(
     53                        'This <a href="http://example.com">http://example.com</a>is a link',
     54                        fill_empty_links( 'This <a href="http://example.com">     </a>is a link' ),
     55                        'fill_empty_links() fails to match regular whitespaces'
     56                );
     57
     58                $this->assertEquals(
     59                        'This <a href="http://example.com">http://example.com</a>is a link',
     60                        fill_empty_links( 'This <a href="http://example.com">&nbsp;</a>is a link' ),
     61                        'fill_empty_links() fails to match non-breaking whitespace'
     62                );
     63
     64                $this->assertEquals(
     65                        'This <a href="http://example.com">http://example.com</a>is a link',
     66                        fill_empty_links( 'This <a href="http://example.com">&zwj;</a>is a link' ),
     67                        'fill_empty_links() fails to match zero-width joiner'
     68                );
     69
     70                $this->assertEquals(
     71                        'This <a href="http://example.com">http://example.com</a>is a link',
     72                        fill_empty_links( 'This <a href="http://example.com">&zwnj;</a>is a link' ),
     73                        'fill_empty_links() fails to match zero-width non-joiner'
     74                );
     75        }
     76
     77        /**
     78         * @ticket 12480
     79         */
     80        public function test__fill_empty_links() {
     81                $matches = array(
     82                        '0' => 'This <a href="http://example.com"></a>is a link',
     83                        '1' => 'http://example.com',
     84                );
     85
     86                $this->assertEquals(
     87                        '<a href="http://example.com">http://example.com</a>',
     88                        _fill_empty_links( $matches )
     89                );
     90        }
     91
     92        /**
     93         * @ticket 12480
     94         */
     95        public function test__fill_empty_links_returns_false_for_invalid_data() {
     96                $matches = array(
     97                        '0' => 'This <a href="http://example.com"></a>is a link',
     98                );
     99
     100                $this->assertFalse(     _fill_empty_links( $matches ) );
     101                $this->assertFalse( _fill_empty_links( 'just a string' ) );
     102        }
     103}
  • src/wp-includes/default-filters.php

    Property changes on: tests/phpunit/tests/formatting/FillEmptyLinks.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    147147add_filter( 'comment_text', 'convert_chars'          );
    148148add_filter( 'comment_text', 'make_clickable',      9 );
    149149add_filter( 'comment_text', 'force_balance_tags', 25 );
     150add_filter( 'comment_text', 'fill_empty_links',   24 );
    150151add_filter( 'comment_text', 'convert_smilies',    20 );
    151152add_filter( 'comment_text', 'wpautop',            30 );
    152153
  • src/wp-includes/formatting.php

     
    18981898}
    18991899
    19001900/**
     1901 * Adds blank string to all HTML <a> empty elements in content.
     1902 *
     1903 * @since 4.5.0
     1904 *
     1905 * @param string $text Content that may contain HTML <a> elements.
     1906 * @return string Converted content.
     1907 */
     1908function fill_empty_links( $text ) {
     1909
     1910        // Matches any <a> elements with an 'href' attribute but no anchor text.
     1911        $regex = '#<a[^>]href="?([^"]*)"[^>]*>[\s|&nbsp;|&zwn?j;]*</a>#i';
     1912
     1913        return preg_replace_callback( $regex, '_fill_empty_links', $text );
     1914}
     1915
     1916/**
     1917 * Callback for preg_replace_callback() within fill_empty_links() to add a visual placeholder in
     1918 * comments with empty anchor text.
     1919 *
     1920 * @since 4.5.0
     1921 *
     1922 * @param array $matches An array of matches found by preg_replace_callback().
     1923 * @return string|bool A formatted anchor tag on success or a boolean FALSE if anything goes wrong.
     1924 */
     1925function _fill_empty_links( $matches ) {
     1926        if ( ! is_array( $matches ) || ! isset( $matches['1'] ) ) {
     1927                return false;
     1928        }
     1929
     1930        return sprintf( '<a href="%1$s">%1$s</a>', esc_url( $matches['1'] ) );
     1931}
     1932
     1933/**
    19011934 * Acts on text which is about to be edited.
    19021935 *
    19031936 * The $content is run through esc_textarea(), which uses htmlspecialchars()