wp_kses_hair()
wp_kses_hair( string $attr, string[] $allowed_protocols…
wp_kses_hair( string $attr, string[] $allowed_protocols )
从包含属性的字符串生成属性列表。
Builds an attribute list from string containing attributes.
目录锚点:#说明#参数#源码
说明(Description)
这个函数做了很多工作。它将一个属性列表解析成一个包含属性数据的数组,并尝试做正确的事情,即使它得到奇怪的输入。它将在属性值周围添加引号,这些属性值周围没有任何引号或撇号,以便更容易地生成符合W3C的HTML规范的HTML代码。它还将从属性值中删除错误的URL协议。它还通过使用首先定义的属性(foo=’bar’foo=’baz’将导致foo=’bar’),减少重复属性。
参数(Parameters)
参数 | 类型 | 说明 |
---|---|---|
$attr | (string) | 从HTML元素到结束HTML元素标记的属性列表。 |
$allowed_protocols | (string[]) | 允许的URL协议数组。 |
源码(Source)
/** * Builds an attribute list from string containing attributes. * * This function does a lot of work. It parses an attribute list into an array * with attribute data, and tries to do the right thing even if it gets weird * input. It will add quotes around attribute values that don't have any quotes * or apostrophes around them, to make it easier to produce HTML code that will * conform to W3C's HTML specification. It will also remove bad URL protocols * from attribute values. It also reduces duplicate attributes by using the * attribute defined first (foo='bar' foo='baz' will result in foo='bar'). * * @since 1.0.0 * * @param string $attr Attribute list from HTML element to closing HTML element tag * @param array $allowed_protocols Allowed protocols to keep * @return array List of attributes after parsing */ function wp_kses_hair($attr, $allowed_protocols) { $attrarr = array(); $mode = 0; $attrname = ''; $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action'); // Loop through the whole attribute list while (strlen($attr) != 0) { $working = 0; // Was the last operation successful? switch ($mode) { case 0 : // attribute name, href for instance if ( preg_match('/^([-a-zA-Z:]+)/', $attr, $match ) ) { $attrname = $match[1]; $working = $mode = 1; $attr = preg_replace( '/^[-a-zA-Z:]+/', '', $attr ); } break; case 1 : // equals sign or valueless ("selected") 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 : // attribute value, a URL after href= for instance if (preg_match('%^"([^"]*)"(s+|/?$)%', $attr, $match)) // "value" { $thisval = $match[1]; if ( in_array(strtolower($attrname), $uris) ) $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) ) $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) ) $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; } // switch if ($working == 0) // not well formed, remove and try again { $attr = wp_kses_html_error($attr); $mode = 0; } } // while if ($mode == 1 && 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; }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
1.0.0 | wp-includes/kses.php | 18 | 12 |
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!