` markup would
* corrupt the document:
*
* - Inside an HTML tag's attribute list (``)
* - Inside an attribute value (``)
* - Inside a JSON blob (Gutenberg block markup)
* - Inside an existing `...` (would create nested anchors)
*
* The guard scans the content and answers each of these questions for a
* given offset range. Implementation is regex-based and intentionally
* defensive: if a check is uncertain, it returns *unsafe* rather than
* risk corrupting content.
*
* Each check method takes the same parameter shape so callers can wrap
* them in a single combined check via {@see is_safe()}.
*
* @since 1.0.0
*/
final class HtmlSafetyGuard {
/**
* Returns whether the offset range is safe for link insertion.
*
* Convenience method that runs every individual check and returns
* `false` on the first failure.
*
* @since 1.0.0
*
* @param string $content Full content the offsets refer to.
* @param int $position_start Byte offset (UTF-8) where the candidate text starts.
* @param int $position_end Byte offset where it ends (exclusive).
*
* @return bool True when insertion at this offset would not corrupt the document.
*/
public function is_safe( string $content, int $position_start, int $position_end ): bool {
if ( $this->is_inside_anchor( $content, $position_start, $position_end ) ) {
return false;
}
if ( $this->is_inside_tag( $content, $position_start, $position_end ) ) {
return false;
}
if ( $this->is_inside_attribute( $content, $position_start, $position_end ) ) {
return false;
}
if ( $this->is_inside_json( $content, $position_start, $position_end ) ) {
return false;
}
if ( $this->is_inside_heading( $content, $position_start, $position_end ) ) {
return false;
}
return true;
}
/**
* Returns whether the offset range is inside an `