wp_kses_hair

函式
wp_kses_hair ( $attr, $allowed_protocols )
引數
  • (string) $attr Attribute list from HTML element to closing HTML element tag.
    Required:
  • (string[]) $allowed_protocols Array of allowed URL protocols.
    Required:
返回值
  • (array[]) Array of attribute information after parsing.
定義位置
相關方法
wp_kses_attrwp_kses_hair_parsewp_kses_datawp_kses_hookwp_kses
引入
1.0.0
棄用
-

wp_kses_hair: 這個函式用來對一串HTML內容進行淨化,同時允許某些安全的HTML標籤和屬性。

從包含屬性的字串建立一個屬性列表。

這個函式做了大量的工作。它將一個屬性列表解析成一個包含屬性資料的陣列,並嘗試做正確的事情,即使它得到奇怪的輸入。它將在沒有任何引號或撇號的屬性值周圍新增引號,以使生成的HTML程式碼更容易符合W3C的HTML規範。它還會從屬性值中刪除不良的URL協議
屬性值中的不良URL協議。它還通過使用首先定義的屬性來減少重複的屬性(”foo=’bar’ foo=’baz'”將導致”foo=’bar'”)。

function wp_kses_hair( $attr, $allowed_protocols ) {
	$attrarr  = array();
	$mode     = 0;
	$attrname = '';
	$uris     = wp_kses_uri_attributes();

	// Loop through the whole attribute list.

	while ( strlen( $attr ) != 0 ) {
		$working = 0; // Was the last operation successful?

		switch ( $mode ) {
			case 0:
				if ( preg_match( '/^([_a-zA-Z][-_a-zA-Z0-9:.]*)/', $attr, $match ) ) {
					$attrname = $match[1];
					$working  = 1;
					$mode     = 1;
					$attr     = preg_replace( '/^[_a-zA-Z][-_a-zA-Z0-9:.]*/', '', $attr );
				}

				break;

			case 1:
				if ( preg_match( '/^s*=s*/', $attr ) ) { // Equals sign.
					$working = 1;
					$mode    = 2;
					$attr    = preg_replace( '/^s*=s*/', '', $attr );
					break;
				}

				if ( preg_match( '/^s+/', $attr ) ) { // Valueless.
					$working = 1;
					$mode    = 0;
					if ( false === array_key_exists( $attrname, $attrarr ) ) {
						$attrarr[ $attrname ] = array(
							'name'  => $attrname,
							'value' => '',
							'whole' => $attrname,
							'vless' => 'y',
						);
					}
					$attr = preg_replace( '/^s+/', '', $attr );
				}

				break;

			case 2:
				if ( preg_match( '%^"([^"]*)"(s+|/?$)%', $attr, $match ) ) {
					// "value"
					$thisval = $match[1];
					if ( in_array( strtolower( $attrname ), $uris, true ) ) {
						$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
					}

					if ( false === array_key_exists( $attrname, $attrarr ) ) {
						$attrarr[ $attrname ] = array(
							'name'  => $attrname,
							'value' => $thisval,
							'whole' => "$attrname="$thisval"",
							'vless' => 'n',
						);
					}
					$working = 1;
					$mode    = 0;
					$attr    = preg_replace( '/^"[^"]*"(s+|$)/', '', $attr );
					break;
				}

				if ( preg_match( "%^'([^']*)'(s+|/?$)%", $attr, $match ) ) {
					// 'value'
					$thisval = $match[1];
					if ( in_array( strtolower( $attrname ), $uris, true ) ) {
						$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
					}

					if ( false === array_key_exists( $attrname, $attrarr ) ) {
						$attrarr[ $attrname ] = array(
							'name'  => $attrname,
							'value' => $thisval,
							'whole' => "$attrname='$thisval'",
							'vless' => 'n',
						);
					}
					$working = 1;
					$mode    = 0;
					$attr    = preg_replace( "/^'[^']*'(s+|$)/", '', $attr );
					break;
				}

				if ( preg_match( "%^([^s"']+)(s+|/?$)%", $attr, $match ) ) {
					// value
					$thisval = $match[1];
					if ( in_array( strtolower( $attrname ), $uris, true ) ) {
						$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );
					}

					if ( false === array_key_exists( $attrname, $attrarr ) ) {
						$attrarr[ $attrname ] = array(
							'name'  => $attrname,
							'value' => $thisval,
							'whole' => "$attrname="$thisval"",
							'vless' => 'n',
						);
					}
					// We add quotes to conform to W3C's HTML spec.
					$working = 1;
					$mode    = 0;
					$attr    = preg_replace( "%^[^s"']+(s+|$)%", '', $attr );
				}

				break;
		} // End switch.

		if ( 0 == $working ) { // Not well-formed, remove and try again.
			$attr = wp_kses_html_error( $attr );
			$mode = 0;
		}
	} // End while.

	if ( 1 == $mode && false === array_key_exists( $attrname, $attrarr ) ) {
		// Special case, for when the attribute list ends with a valueless
		// attribute like "selected".
		$attrarr[ $attrname ] = array(
			'name'  => $attrname,
			'value' => '',
			'whole' => $attrname,
			'vless' => 'y',
		);
	}

	return $attrarr;
}

常見問題

FAQs
檢視更多 >