a-blog cms PHP API Reference

AuthoringConverter
in package

入稿用タグを含むブロックエディターの HTML(authoring HTML)を、保存用の正規形へ変換する。

CSV インポート・バックアップインポート・将来の API など、編集画面を通らずに本文を書き込む 経路はすべて Application::make('block-editor.authoring')->convert() を通す。

変換する要素は block-editor.authoring-tags(AuthoringTagRegistry)に登録されたものだけで、 それ以外の HTML(既存の機能タグ・拡張アプリの独自ブロック等)は変更しない。独自ブロックの 解釈はエディターに読み込まれた Tiptap 拡張だけが知っているため、コアは推測で変換しない。

Why not DOM でパースする: HTML ブロックの中身は編集者が書いた HTML を加工せず出力する前提であり、 libxml に通すと未閉じタグの補完や <script> 内の </div> の要素化、@click のような属性名の 脱落が起きる。<script> と同じく「開始タグから最初の同名の閉じタグまで」を文字列のまま切り出す (同名のタグは入れ子にできない)。外側のタグの中身は変換せずハンドラに委ね、入れ子を許す ブロックだけが AuthoringContext::convert() で中身を変換する。

Table of Contents

Constants

ATTRIBUTE_PATTERN  = '~([^\s"\'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s"\'=<>`]+)))?~'
ATTRIBUTES_PATTERN  = '((?:[^>"\']++|"[^"]*+"|\'[^\']*+\')*+)'
属性部分: 引用符の中の `>` を開始タグの終わりと誤認しないよう、引用された値をまとめて読む

Properties

$tags  : AuthoringTagRegistry

Methods

__construct()  : mixed
convert()  : array{html: string, warnings: list}>}
convertTags()  : string
copySegment()  : string
$html の [$from, $to) を、取り除く閉じタグを除いて返す
findOrphanClosingTag()  : array{int, int}|null
中身を持たないタグの後ろにある、対になる開始タグの無い同名の閉じタグを探す
mayContain()  : bool
登録済みの入稿用タグを含む可能性があるか(走査を省略するための事前判定)
parseAttributes()  : array<string, string>
開始タグの属性部分を連想配列にする(属性名は小文字化、値は文字参照をデコード)

Constants

ATTRIBUTE_PATTERN

private mixed ATTRIBUTE_PATTERN = '~([^\s"\'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s"\'=<>`]+)))?~'

ATTRIBUTES_PATTERN

属性部分: 引用符の中の `>` を開始タグの終わりと誤認しないよう、引用された値をまとめて読む

private mixed ATTRIBUTES_PATTERN = '((?:[^>"\']++|"[^"]*+"|\'[^\']*+\')*+)'

Why not 通常の量指定子: 1文字ずつの繰り返しは PCRE の JIT スタックを消費し、数万バイトの 属性で preg_match が失敗する。各分岐の先頭文字は重ならずバックトラックが不要なため独占的にする

Properties

Methods

convert()

public convert(string $html[, bool $allowsExternalResolution = true ]) : array{html: string, warnings: list}>}
Parameters
$html : string

authoring HTML

$allowsExternalResolution : bool = true

外部への取得(埋め込みの oEmbed 等)を許可するか。 保存しないドライランや表示時の安全策では false にする

Return values
array{html: string, warnings: list}>}

copySegment()

$html の [$from, $to) を、取り除く閉じタグを除いて返す

private copySegment(string $html, int $from, int $to, array<int, int> $orphans) : string
Parameters
$html : string
$from : int
$to : int
$orphans : array<int, int>

位置 => 長さ

Return values
string

findOrphanClosingTag()

中身を持たないタグの後ろにある、対になる開始タグの無い同名の閉じタグを探す

private findOrphanClosingTag(string $html, string $tagName, int $from, string $openTagPattern) : array{int, int}|null

次の入稿用タグ(名前を問わない)の開始位置より手前だけを探す。それより後ろの閉じタグは 次のタグのもの、または次のタグの中身の一部であり、ここで取り除くと中身を壊すため。

Parameters
$html : string
$tagName : string
$from : int
$openTagPattern : string

登録済みの入稿用タグの開始タグにマッチする正規表現

Return values
array{int, int}|null —

[位置, 長さ]

mayContain()

登録済みの入稿用タグを含む可能性があるか(走査を省略するための事前判定)

private mayContain(string $html) : bool
Parameters
$html : string
Return values
bool

parseAttributes()

開始タグの属性部分を連想配列にする(属性名は小文字化、値は文字参照をデコード)

private parseAttributes(string $source) : array<string, string>
Parameters
$source : string
Return values
array<string, string>

        
On this page

Search results