类文档

WP_HTML_Span

💡 云策文档标注

概述

WP_HTML_Span 是 WP_HTML_Tag_Processor 内部使用的核心类,用于表示 HTML 文档中的文本跨度,以优化内存使用。

关键要点

  • WP_HTML_Span 是一个内部类,专为 WP_HTML_Tag_Processor 设计,避免使用数组带来的内存开销。
  • 类包含两个公共属性:start(字节偏移量)和 length(字节长度),用于定义文本跨度的位置和大小。
  • 构造函数接受 start 和 length 参数,初始化这些属性。

代码示例

class WP_HTML_Span {
    public $start;
    public $length;

    public function __construct( int $start, int $length ) {
        $this->start  = $start;
        $this->length = $length;
    }
}

注意事项

  • WP_HTML_Span 是内部类,开发者通常不应直接使用,而是通过 WP_HTML_Tag_Processor 操作。
  • 在版本 6.5.0 中,属性从 end 改为 length,以更贴近 substr() 的行为。

📄 原文内容

Core class used by the HTML tag processor to represent a textual span inside an HTML document.

Description

This is a two-tuple in disguise, used to avoid the memory overhead involved in using an array for the same purpose.

This class is for internal usage of the WP_HTML_Tag_Processor class.

See also

Methods

Name Description
WP_HTML_Span::__construct Constructor.

Source

class WP_HTML_Span {
	/**
	 * Byte offset into document where span begins.
	 *
	 * @since 6.2.0
	 *
	 * @var int
	 */
	public $start;

	/**
	 * Byte length of this span.
	 *
	 * @since 6.5.0
	 *
	 * @var int
	 */
	public $length;

	/**
	 * Constructor.
	 *
	 * @since 6.2.0
	 *
	 * @param int $start  Byte offset into document where replacement span begins.
	 * @param int $length Byte length of span.
	 */
	public function __construct( int $start, int $length ) {
		$this->start  = $start;
		$this->length = $length;
	}
}

Changelog

Version Description
6.5.0 Replaced end with length to more closely align with substr().
6.2.0 Introduced.