API / Minn\Content / Autop

Autop final class

Minn\Content\Autop · public/minn/src/Minn/Content/Autop.php · 168 lines

Classic-content paragraphing (wpautop): blank lines become paragraphs, single newlines become line breaks when asked, and block-level tags are never wrapped. Rebuilt against the reference's output on a corpus of fuzzed and real content plus targeted experiments (probe autop); every rule below is one that corpus demanded, written as the behaviour seen, in the order it applies:

- <pre> elements are cut out first and put back untouched at the end. - A pair of <br> tags is a paragraph break; block tags get blank lines around them, so they always sit outside paragraphs. - Newlines inside a tag, comment or CDATA section (an unclosed one runs to the end) are held aside, so they never split a paragraph and come back as plain newlines. While held, such a newline ends the tag as far as the block rules can tell. - Whitespace around object, param, embed, audio, video, source, track, figcaption and option tags is squeezed out when those tags are used. - Blank-line separated runs become <p> paragraphs, then paragraphs that only hold a block tag lose their <p> and </p>. - With line breaks on, remaining newlines become <br />, except inside script, style, svg and math elements, and breaks next to block tags go.

Constants and properties

privateBLOCK = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'

Tag names that open and close blocks; the cleanup rules match them as name prefixes, case-sensitively.

privateBREAK_DROPPERS = '(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)'

Tags (again by name prefix) that drop a <br /> written just before them.

privateHELD_CORE = '<>'

Core of a held newline. It reads as a tag of its own: a run of plain text stops at its "<" and a tag match ends at its ">", which is how a held newline keeps the block rules from reaching past it.

privateHELD_NEWLINE = ' <> '

A newline inside a tag, comment or CDATA section while paragraphs are made.

privateRAW_NEWLINE = ''

A newline inside script, style, svg or math while line breaks are added.

Public

staticapply(string $text, bool $lineBreaks = true): string#

The classic paragraph rules: a blank line makes a paragraph, a single newline a line break when asked.

Internals 9

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

privatestashPre(string $text): array#

Cuts each <pre> element out and leaves a stand-in that acts as an empty block.

privatespaceBlocks(string $text): string#

Turns <br> pairs into blank lines and puts blank lines before block openers and after block closers.

privateholdTagNewlines(string $text): string#

Holds newlines found inside tags, comments and CDATA sections.

privatesqueezeMedia(string $text): string#

Squeezes whitespace around embedded media tags, each family only when the text uses it.

privatesqueezeFormParts(string $text): string#

Squeezes whitespace before figcaption and option openers and after their closers, when used.

privatewrapParagraphs(string $text): string#

Wraps every blank-line separated run in <p>...</p>, trimming only its outer newlines.

privatetidyParagraphs(string $text): string#

Removes empty paragraphs and the <p> or </p> that would wrap a block tag.

privateaddLineBreaks(string $text): string#

Writes <br> as <br /> and turns each newline (and the whitespace before it) into a line break.

privatetidyBreaks(string $text): string#

Drops a <br /> right after a block tag, or right before one of the tags that swallow it.