API / Minn\Html / Edits

Edits final class

Minn\Html\Edits · public/minn/src/Minn/Html/Edits.php · 336 lines

The edits pending on the current token: attribute sets and removals, class additions and removals, and a text replacement, in call order. They answer reads before they are written, and become the source replacements the reader splices in when it moves on.

Used by 1

Constants and properties

privatearray $attributes
privatearray $classes
private?string $text

Public

staticvalidName(string $name): bool#

Whether an attribute name may be set at all, by the reference's rules.

methodsetAttribute(string $name, string|true $value): void#

Records a set; setting class outright forgets the class edits before it.

methodsetEscapedAttribute(string $name, string $escaped): void#

Records an attribute value that comes already escaped (a URL through esc_url), written as it is.

methodremoveAttribute(string $name): void#

Records a removal of an attribute the source has.

methodcancelAttribute(string $name): void#

Forgets a pending set that never reached the source: cancelling an addition is not a removal.

methodattribute(string $lower): ?array#

The pending set or removal for a lowercase name, or null when there is none. @return array{name: string, value: string|true|null}|null

@return array{name: string, value: string|true|null}|null
methodaddClass(string $class): void#

Records a class to add; a later removal of the same class wins.

methodremoveClass(string $class): void#

Records a class to remove; a later addition of the same class wins.

methodhasClassEdits(): bool#

Whether any class edit is pending.

methodhasTagEdits(): bool#

Whether any attribute or class edit is pending.

methodclassesAfter(array $base): array#

The class list after the pending edits: additions appended once, removals gone.

$baselist<string>
@return list<string>
staticclosesComment(string $text): bool#

Whether text holds a comment closer, "-->" or "--!>".

methodsetTextFor(?string $type, ?string $tagName, ?string $commentType, string $text): bool#

Records a text replacement for a token of a kind that takes one: text (escaped), an ordinary HTML comment, or a raw-text element's body (script and style bodies get their closers escaped). False when the kind takes none, or the text would end the element early.

$tagName?stringthe lowercase tag name of an opening tag, null for anything else
methodtakeText(): ?string#

The pending text replacement, taken: it is cleared on read.

methodtakeReplacements(array $attributes, int $nameEnd, ?string $baseClassValue): array#

The source replacements the attribute and class edits amount to, and forgets them: a changed attribute is rewritten in place (its duplicates removed), a removed one is cut, a new one is inserted after the tag name (several in the order of their text).

$attributeslist<array{name: string, lower: string, start: int, end: int, value: ?string}>the tag's own
@return list<array{int, int, string}> start, end, text
methodclear(): void#

Forgets everything pending.

staticsplice(string $html, array $replacements, array $bookmarks, int $tokenStart, int $tokenEnd): array#

Splices replacements into the source, ascending, and moves the bookmarks and the cursor by what the token at [$tokenStart, $tokenEnd] gained or lost.

$replacementslist<array{int, int, string}>
$bookmarksarray<string, array{0: int, 1: int}>
@return array{0: string, 1: array<string, array{0: int, 1: int}>, 2: int} the source, the bookmarks, the cursor
staticclassValue(?array $pending, array $attributes): ?string#

The class attribute's decoded value before class edits: a pending set, else the source's; null when there is none.

$pendingarray{value: string|true}|null
$attributeslist<array{lower: string, value: ?string}>
staticclassList(?array $pending, array $attributes): array#

The class names before class edits.

$pendingarray{value: string|true}|null
$attributeslist<array{lower: string, value: ?string}>
@return list<string>

Internals 3

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privaterebuiltClassValue(string $base): string#

The class attribute after edits: source order and inner whitespace kept, removed classes gone with the space before them, additions appended.

privateexistingName(array $attributes, string $lower): ?string#
$attributeslist<array{name: string, lower: string}>
privateescape(string $value): string#