函数文档

_wptexturize_pushpop_element()

💡 云策文档标注

概述

_wptexturize_pushpop_element() 是一个 WordPress 内部函数,用于在文本处理中管理禁用元素的标签栈。它检查标签是否为禁用元素,并在标签打开时推入栈、关闭时弹出,以控制文本化(texturize)的启用状态。

关键要点

  • 函数用于搜索禁用元素标签,基于标签的开闭状态操作栈。
  • 假设输入文本的首字符为标签开头、末字符为标签结尾,第二个字符可选 '/' 表示关闭标签。
  • 参数包括 $text(要检查的标签文本)、$stack(引用传递的开放标签元素数组)和 $disabled_elements(要匹配的禁用标签名数组)。
  • 通过解析标签名,如果标签在禁用列表中,则在打开时推入栈、关闭时弹出栈,以禁用文本化处理。
  • 函数在 WordPress 2.9.0 版本引入,主要用于 wptexturize() 函数中。

代码示例

function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) {
    // Is it an opening tag or closing tag?
    if ( isset( $text[1] ) && '/' !== $text[1] ) {
        $opening_tag = true;
        $name_offset = 1;
    } elseif ( 0 === count( $stack ) ) {
        // Stack is empty. Just stop.
        return;
    } else {
        $opening_tag = false;
        $name_offset = 2;
    }

    // Parse out the tag name.
    $space = strpos( $text, ' ' );
    if ( false === $space ) {
        $space = -1;
    } else {
        $space -= $name_offset;
    }
    $tag = substr( $text, $name_offset, $space );

    // Handle disabled tags.
    if ( in_array( $tag, $disabled_elements, true ) ) {
        if ( $opening_tag ) {
            array_push( $stack, $tag );
        } elseif ( end( $stack ) === $tag ) {
            array_pop( $stack );
        }
    }
}

注意事项

  • 标签名中不允许包含空格,需确保 $disabled_elements 数组中的标签名格式正确。
  • 函数假设输入文本格式正确,如标签开头和结尾字符匹配,否则可能导致未定义行为。
  • 栈操作基于标签名匹配,如果嵌套无效,仍会处理直到找到匹配的关闭标签。

📄 原文内容

Searches for disabled element tags. Pushes element to stack on tag open and pops on tag close.

Description

Assumes first char of $text is tag opening and last char is tag closing.
Assumes second char of $text is optionally / to indicate closing as in </html>.

Parameters

$textstringrequired
Text to check. Must be a tag like <html> or [shortcode].
$stackstring[]required
Array of open tag elements.
$disabled_elementsstring[]required
Array of tag names to match against. Spaces are not allowed in tag names.

Source

function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) {
	// Is it an opening tag or closing tag?
	if ( isset( $text[1] ) && '/' !== $text[1] ) {
		$opening_tag = true;
		$name_offset = 1;
	} elseif ( 0 === count( $stack ) ) {
		// Stack is empty. Just stop.
		return;
	} else {
		$opening_tag = false;
		$name_offset = 2;
	}

	// Parse out the tag name.
	$space = strpos( $text, ' ' );
	if ( false === $space ) {
		$space = -1;
	} else {
		$space -= $name_offset;
	}
	$tag = substr( $text, $name_offset, $space );

	// Handle disabled tags.
	if ( in_array( $tag, $disabled_elements, true ) ) {
		if ( $opening_tag ) {
			/*
			 * This disables texturize until we find a closing tag of our type
			 * (e.g. <pre>) even if there was invalid nesting before that.
			 *
			 * Example: in the case <pre>sadsadasd</code>"baba"</pre>
			 *          "baba" won't be texturized.
			 */

			array_push( $stack, $tag );
		} elseif ( end( $stack ) === $tag ) {
			array_pop( $stack );
		}
	}
}

Changelog

Version Description
2.9.0 Introduced.