Make WordPress Core


Ignore:
Timestamp:
12/12/2018 02:38:14 AM (6 years ago)
Author:
jeremyfelt
Message:

KSES: Allow HTML data-* attributes.

Add global support for HTML attributes prefixed data- for authors and contributors, as required by the new editor.

Merges [43727] to trunk.

Props azaozz, peterwilsoncc.
Fixes #33121.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tests/phpunit/tests/kses.php

    r42880 r43981  
    826826        );
    827827    }
     828
     829    /**
     830     * Data attributes are globally accepted.
     831     *
     832     * @ticket 33121
     833     */
     834    function test_wp_kses_attr_data_attribute_is_allowed() {
     835        $test     = '<div data-foo="foo" data-bar="bar" datainvalid="gone" data--invaild="gone"  data-also-invaild-="gone" data-two-hyphens="remains">Pens and pencils</div>';
     836        $expected = '<div data-foo="foo" data-bar="bar" data-two-hyphens="remains">Pens and pencils</div>';
     837
     838        $this->assertEquals( $expected, wp_kses_post( $test ) );
     839    }
     840
     841    /**
     842     * Ensure wildcard attributes block unprefixed wildcard uses.
     843     *
     844     * @ticket 33121
     845     */
     846    function test_wildcard_requires_hyphen_after_prefix() {
     847        $allowed_html = array(
     848            'div' => array(
     849                'data-*' => true,
     850                'on-*'   => true,
     851            ),
     852        );
     853
     854        $string   = '<div datamelformed-prefix="gone" data="gone" data-="gone" onclick="alert(1)">Malformed attributes</div>';
     855        $expected = '<div>Malformed attributes</div>';
     856
     857        $actual = wp_kses( $string, $allowed_html );
     858
     859        $this->assertSame( $expected, $actual );
     860    }
     861
     862    /**
     863     * Ensure wildcard allows two hyphen.
     864     *
     865     * @ticket 33121
     866     */
     867    function test_wildcard_allows_two_hyphens() {
     868        $allowed_html = array(
     869            'div' => array(
     870                'data-*' => true,
     871            ),
     872        );
     873
     874        $string   = '<div data-wp-id="pens-and-pencils">Well formed attribute</div>';
     875        $expected = '<div data-wp-id="pens-and-pencils">Well formed attribute</div>';
     876
     877        $actual = wp_kses( $string, $allowed_html );
     878
     879        $this->assertSame( $expected, $actual );
     880    }
     881
     882    /**
     883     * Ensure wildcard attributes only support valid prefixes.
     884     *
     885     * @dataProvider data_wildcard_attribute_prefixes
     886     *
     887     * @ticket 33121
     888     */
     889    function test_wildcard_attribute_prefixes( $wildcard_attribute, $expected ) {
     890        $allowed_html = array(
     891            'div' => array(
     892                $wildcard_attribute => true,
     893            ),
     894        );
     895
     896        $name  = str_replace( '*', strtolower( __FUNCTION__ ), $wildcard_attribute );
     897        $value = __FUNCTION__;
     898        $whole = "{$name}=\"{$value}\"";
     899
     900        $actual = wp_kses_attr_check( $name, $value, $whole, 'n', 'div', $allowed_html );
     901
     902        $this->assertSame( $expected, $actual );
     903    }
     904
     905    /**
     906     * @return array Array of arguments for wildcard testing
     907     *               [0] The prefix being tested.
     908     *               [1] The outcome of `wp_kses_attr_check` for the prefix.
     909     */
     910    function data_wildcard_attribute_prefixes() {
     911        return array(
     912            // Ends correctly
     913            array( 'data-*', true ),
     914
     915            // Does not end with trialing `-`.
     916            array( 'data*', false ),
     917
     918            // Multiple wildcards.
     919            array( 'd*ta-*', false ),
     920            array( 'data**', false ),
     921        );
     922    }
    828923}
Note: See TracChangeset for help on using the changeset viewer.