@wordpress/autop
Advanced tools
| // packages/autop/src/index.ts | ||
| var htmlSplitRegex = (() => { | ||
| const comments = "!(?:-(?!->)[^\\-]*)*(?:-->)?"; | ||
| const cdata = "!\\[CDATA\\[[^\\]]*(?:](?!]>)[^\\]]*)*?(?:]]>)?"; | ||
| const escaped = "(?=!--|!\\[CDATA\\[)((?=!-)" + // If yes, which type? | ||
| comments + "|" + cdata + ")"; | ||
| const regex = "(<(" + // Conditional expression follows. | ||
| escaped + // Find end of escaped element. | ||
| "|[^>]*>?))"; | ||
| return new RegExp(regex); | ||
| })(); | ||
| function htmlSplit(input) { | ||
| const parts = []; | ||
| let workingInput = input; | ||
| let match; | ||
| while (match = workingInput.match(htmlSplitRegex)) { | ||
| const index = match.index; | ||
| parts.push(workingInput.slice(0, index)); | ||
| parts.push(match[0]); | ||
| workingInput = workingInput.slice(index + match[0].length); | ||
| } | ||
| if (workingInput.length) { | ||
| parts.push(workingInput); | ||
| } | ||
| return parts; | ||
| } | ||
| function replaceInHtmlTags(haystack, replacePairs) { | ||
| const textArr = htmlSplit(haystack); | ||
| let changed = false; | ||
| const needles = Object.keys(replacePairs); | ||
| for (let i = 1; i < textArr.length; i += 2) { | ||
| for (let j = 0; j < needles.length; j++) { | ||
| const needle = needles[j]; | ||
| if (-1 !== textArr[i].indexOf(needle)) { | ||
| textArr[i] = textArr[i].replace( | ||
| new RegExp(needle, "g"), | ||
| replacePairs[needle] | ||
| ); | ||
| changed = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (changed) { | ||
| haystack = textArr.join(""); | ||
| } | ||
| return haystack; | ||
| } | ||
| function autop(text, br = true) { | ||
| const preTags = []; | ||
| if (text.trim() === "") { | ||
| return ""; | ||
| } | ||
| text = text + "\n"; | ||
| if (text.indexOf("<pre") !== -1) { | ||
| const textParts = text.split("</pre>"); | ||
| const lastText = textParts.pop(); | ||
| text = ""; | ||
| for (let i = 0; i < textParts.length; i++) { | ||
| const textPart = textParts[i]; | ||
| const start = textPart.indexOf("<pre"); | ||
| if (start === -1) { | ||
| text += textPart; | ||
| continue; | ||
| } | ||
| const name = "<pre wp-pre-tag-" + i + "></pre>"; | ||
| preTags.push([name, textPart.substr(start) + "</pre>"]); | ||
| text += textPart.substr(0, start) + name; | ||
| } | ||
| text += lastText; | ||
| } | ||
| text = text.replace(/<br\s*\/?>\s*<br\s*\/?>/g, "\n\n"); | ||
| const allBlocks = "(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)"; | ||
| text = text.replace( | ||
| new RegExp("(<" + allBlocks + "[\\s/>])", "g"), | ||
| "\n\n$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</" + allBlocks + ">)", "g"), | ||
| "$1\n\n" | ||
| ); | ||
| text = text.replace(/\r\n|\r/g, "\n"); | ||
| text = replaceInHtmlTags(text, { "\n": " <!-- wpnl --> " }); | ||
| if (text.indexOf("<option") !== -1) { | ||
| text = text.replace(/\s*<option/g, "<option"); | ||
| text = text.replace(/<\/option>\s*/g, "</option>"); | ||
| } | ||
| if (text.indexOf("</object>") !== -1) { | ||
| text = text.replace(/(<object[^>]*>)\s*/g, "$1"); | ||
| text = text.replace(/\s*<\/object>/g, "</object>"); | ||
| text = text.replace(/\s*(<\/?(?:param|embed)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<source") !== -1 || text.indexOf("<track") !== -1) { | ||
| text = text.replace(/([<\[](?:audio|video)[^>\]]*[>\]])\s*/g, "$1"); | ||
| text = text.replace(/\s*([<\[]\/(?:audio|video)[>\]])/g, "$1"); | ||
| text = text.replace(/\s*(<(?:source|track)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<figcaption") !== -1) { | ||
| text = text.replace(/\s*(<figcaption[^>]*>)/, "$1"); | ||
| text = text.replace(/<\/figcaption>\s*/, "</figcaption>"); | ||
| } | ||
| text = text.replace(/\n\n+/g, "\n\n"); | ||
| const texts = text.split(/\n\s*\n/).filter(Boolean); | ||
| text = ""; | ||
| texts.forEach((textPiece) => { | ||
| text += "<p>" + textPiece.replace(/^\n*|\n*$/g, "") + "</p>\n"; | ||
| }); | ||
| text = text.replace(/<p>\s*<\/p>/g, ""); | ||
| text = text.replace( | ||
| /<p>([^<]+)<\/(div|address|form)>/g, | ||
| "<p>$1</p></$2>" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace(/<p>(<li.+?)<\/p>/g, "$1"); | ||
| text = text.replace(/<p><blockquote([^>]*)>/gi, "<blockquote$1><p>"); | ||
| text = text.replace(/<\/blockquote><\/p>/g, "</p></blockquote>"); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| if (br) { | ||
| text = text.replace( | ||
| /<(script|style).*?<\/\\1>/g, | ||
| (match) => match[0].replace(/\n/g, "<WPPreserveNewline />") | ||
| ); | ||
| text = text.replace(/<br>|<br\/>/g, "<br />"); | ||
| text = text.replace( | ||
| /(<br \/>)?\s*\n/g, | ||
| (a, b) => b ? a : "<br />\n" | ||
| ); | ||
| text = text.replace(/<WPPreserveNewline \/>/g, "\n"); | ||
| } | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*<br />", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g, | ||
| "$1" | ||
| ); | ||
| text = text.replace(/\n<\/p>$/g, "</p>"); | ||
| preTags.forEach((preTag) => { | ||
| const [name, original] = preTag; | ||
| text = text.replace(name, () => original); | ||
| }); | ||
| if (-1 !== text.indexOf("<!-- wpnl -->")) { | ||
| text = text.replace(/\s?<!-- wpnl -->\s?/g, "\n"); | ||
| } | ||
| return text; | ||
| } | ||
| function removep(html) { | ||
| const blocklist = "blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure"; | ||
| const blocklist1 = blocklist + "|div|p"; | ||
| const blocklist2 = blocklist + "|pre"; | ||
| const preserve = []; | ||
| let preserveLinebreaks = false; | ||
| let preserveBr = false; | ||
| if (!html) { | ||
| return ""; | ||
| } | ||
| if (html.indexOf("<script") !== -1 || html.indexOf("<style") !== -1) { | ||
| html = html.replace( | ||
| /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, | ||
| (match) => { | ||
| preserve.push(match); | ||
| return "<wp-preserve>"; | ||
| } | ||
| ); | ||
| } | ||
| if (html.indexOf("<pre") !== -1) { | ||
| preserveLinebreaks = true; | ||
| html = html.replace(/<pre[^>]*>[\s\S]+?<\/pre>/g, (a) => { | ||
| a = a.replace(/<br ?\/?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| a = a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| return a.replace(/\r?\n/g, "<wp-line-break>"); | ||
| }); | ||
| } | ||
| if (html.indexOf("[caption") !== -1) { | ||
| preserveBr = true; | ||
| html = html.replace(/\[caption[\s\S]+?\[\/caption\]/g, (a) => { | ||
| return a.replace(/<br([^>]*)>/g, "<wp-temp-br$1>").replace(/[\r\n\t]+/, ""); | ||
| }); | ||
| } | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist1 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist1 + ")(?: [^>]*)?)>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace(/(<p [^>]+>[\s\S]*?)<\/p>/g, "$1</p#>"); | ||
| html = html.replace(/<div( [^>]*)?>\s*<p>/gi, "<div$1>\n\n"); | ||
| html = html.replace(/\s*<p>/gi, ""); | ||
| html = html.replace(/\s*<\/p>\s*/gi, "\n\n"); | ||
| html = html.replace(/\n[\s\u00a0]+\n/g, "\n\n"); | ||
| html = html.replace(/(\s*)<br ?\/?>\s*/gi, (_, space) => { | ||
| if (space && space.indexOf("\n") !== -1) { | ||
| return "\n\n"; | ||
| } | ||
| return "\n"; | ||
| }); | ||
| html = html.replace(/\s*<div/g, "\n<div"); | ||
| html = html.replace(/<\/div>\s*/g, "</div>\n"); | ||
| html = html.replace( | ||
| /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, | ||
| "\n\n[caption$1[/caption]\n\n" | ||
| ); | ||
| html = html.replace(/caption\]\n\n+\[caption/g, "caption]\n\n[caption"); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist2 + ")(?: [^>]*)?)\\s*>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist2 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace(/<((li|dt|dd)[^>]*)>/g, " <$1>"); | ||
| if (html.indexOf("<option") !== -1) { | ||
| html = html.replace(/\s*<option/g, "\n<option"); | ||
| html = html.replace(/\s*<\/select>/g, "\n</select>"); | ||
| } | ||
| if (html.indexOf("<hr") !== -1) { | ||
| html = html.replace(/\s*<hr( [^>]*)?>\s*/g, "\n\n<hr$1>\n\n"); | ||
| } | ||
| if (html.indexOf("<object") !== -1) { | ||
| html = html.replace(/<object[\s\S]+?<\/object>/g, (a) => { | ||
| return a.replace(/[\r\n]+/g, ""); | ||
| }); | ||
| } | ||
| html = html.replace(/<\/p#>/g, "</p>\n"); | ||
| html = html.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, "\n$1"); | ||
| html = html.replace(/^\s+/, ""); | ||
| html = html.replace(/[\s\u00a0]+$/, ""); | ||
| if (preserveLinebreaks) { | ||
| html = html.replace(/<wp-line-break>/g, "\n"); | ||
| } | ||
| if (preserveBr) { | ||
| html = html.replace(/<wp-temp-br([^>]*)>/g, "<br$1>"); | ||
| } | ||
| if (preserve.length) { | ||
| html = html.replace(/<wp-preserve>/g, () => { | ||
| return preserve.shift(); | ||
| }); | ||
| } | ||
| return html; | ||
| } | ||
| export { | ||
| autop, | ||
| removep | ||
| }; | ||
| //# sourceMappingURL=index.mjs.map |
| { | ||
| "version": 3, | ||
| "sources": ["../src/index.ts"], | ||
| "sourcesContent": ["/**\n * The regular expression for an HTML element.\n */\nconst htmlSplitRegex: RegExp = ( () => {\n\tconst comments =\n\t\t'!' + // Start of comment, after the <.\n\t\t'(?:' + // Unroll the loop: Consume everything until --> is found.\n\t\t'-(?!->)' + // Dash not followed by end of comment.\n\t\t'[^\\\\-]*' + // Consume non-dashes.\n\t\t')*' + // Loop possessively.\n\t\t'(?:-->)?'; // End of comment. If not found, match all input.\n\n\tconst cdata =\n\t\t'!\\\\[CDATA\\\\[' + // Start of comment, after the <.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t'(?:' + // Unroll the loop: Consume everything until ]]> is found.\n\t\t'](?!]>)' + // One ] not followed by end of comment.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t')*?' + // Loop possessively.\n\t\t'(?:]]>)?'; // End of comment. If not found, match all input.\n\n\tconst escaped =\n\t\t'(?=' + // Is the element escaped?\n\t\t'!--' +\n\t\t'|' +\n\t\t'!\\\\[CDATA\\\\[' +\n\t\t')' +\n\t\t'((?=!-)' + // If yes, which type?\n\t\tcomments +\n\t\t'|' +\n\t\tcdata +\n\t\t')';\n\n\tconst regex =\n\t\t'(' + // Capture the entire match.\n\t\t'<' + // Find start of element.\n\t\t'(' + // Conditional expression follows.\n\t\tescaped + // Find end of escaped element.\n\t\t'|' + // ... else ...\n\t\t'[^>]*>?' + // Find end of normal element.\n\t\t')' +\n\t\t')';\n\n\treturn new RegExp( regex );\n} )();\n\n/**\n * Separate HTML elements and comments from the text.\n *\n * @param input The text which has to be formatted.\n *\n * @return The formatted text.\n */\nfunction htmlSplit( input: string ): string[] {\n\tconst parts = [];\n\tlet workingInput = input;\n\n\tlet match;\n\twhile ( ( match = workingInput.match( htmlSplitRegex ) ) ) {\n\t\t// The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.\n\t\t// If the `g` flag is omitted, `index` is included.\n\t\t// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.\n\t\t// Assert `match.index` is a number.\n\t\tconst index = match.index as number;\n\n\t\tparts.push( workingInput.slice( 0, index ) );\n\t\tparts.push( match[ 0 ] );\n\t\tworkingInput = workingInput.slice( index + match[ 0 ].length );\n\t}\n\n\tif ( workingInput.length ) {\n\t\tparts.push( workingInput );\n\t}\n\n\treturn parts;\n}\n\n/**\n * Replace characters or phrases within HTML elements only.\n *\n * @param haystack The text which has to be formatted.\n * @param replacePairs In the form {from: 'to', \u2026}.\n *\n * @return The formatted text.\n */\nfunction replaceInHtmlTags(\n\thaystack: string,\n\treplacePairs: Record< string, string >\n): string {\n\t// Find all elements.\n\tconst textArr = htmlSplit( haystack );\n\tlet changed = false;\n\n\t// Extract all needles.\n\tconst needles = Object.keys( replacePairs );\n\n\t// Loop through delimiters (elements) only.\n\tfor ( let i = 1; i < textArr.length; i += 2 ) {\n\t\tfor ( let j = 0; j < needles.length; j++ ) {\n\t\t\tconst needle = needles[ j ];\n\t\t\tif ( -1 !== textArr[ i ].indexOf( needle ) ) {\n\t\t\t\ttextArr[ i ] = textArr[ i ].replace(\n\t\t\t\t\tnew RegExp( needle, 'g' ),\n\t\t\t\t\treplacePairs[ needle ]\n\t\t\t\t);\n\t\t\t\tchanged = true;\n\t\t\t\t// After one strtr() break out of the foreach loop and look at next element.\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif ( changed ) {\n\t\thaystack = textArr.join( '' );\n\t}\n\n\treturn haystack;\n}\n\n/**\n * Replaces double line-breaks with paragraph elements.\n *\n * A group of regex replaces used to identify text formatted with newlines and\n * replace double line-breaks with HTML paragraph tags. The remaining line-\n * breaks after conversion become `<br />` tags, unless br is set to 'false'.\n *\n * @param text The text which has to be formatted.\n * @param br Optional. If set, will convert all remaining line-\n * breaks after paragraphing. Default true.\n *\n * @example\n *```js\n * import { autop } from '@wordpress/autop';\n * autop( 'my text' ); // \"<p>my text</p>\"\n * ```\n *\n * @return Text which has been converted into paragraph tags.\n */\nexport function autop( text: string, br: boolean = true ): string {\n\tconst preTags: Array< [ string, string ] > = [];\n\n\tif ( text.trim() === '' ) {\n\t\treturn '';\n\t}\n\n\t// Just to make things a little easier, pad the end.\n\ttext = text + '\\n';\n\n\t/*\n\t * Pre tags shouldn't be touched by autop.\n\t * Replace pre tags with placeholders and bring them back after autop.\n\t */\n\tif ( text.indexOf( '<pre' ) !== -1 ) {\n\t\tconst textParts = text.split( '</pre>' );\n\t\tconst lastText = textParts.pop();\n\t\ttext = '';\n\n\t\tfor ( let i = 0; i < textParts.length; i++ ) {\n\t\t\tconst textPart = textParts[ i ];\n\t\t\tconst start = textPart.indexOf( '<pre' );\n\n\t\t\t// Malformed html?\n\t\t\tif ( start === -1 ) {\n\t\t\t\ttext += textPart;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst name = '<pre wp-pre-tag-' + i + '></pre>';\n\t\t\tpreTags.push( [ name, textPart.substr( start ) + '</pre>' ] );\n\n\t\t\ttext += textPart.substr( 0, start ) + name;\n\t\t}\n\n\t\ttext += lastText;\n\t}\n\t// Change multiple <br>s into two line breaks, which will turn into paragraphs.\n\ttext = text.replace( /<br\\s*\\/?>\\s*<br\\s*\\/?>/g, '\\n\\n' );\n\n\tconst allBlocks =\n\t\t'(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';\n\n\t// Add a double line break above block-level opening tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(<' + allBlocks + '[\\\\s/>])', 'g' ),\n\t\t'\\n\\n$1'\n\t);\n\n\t// Add a double line break below block-level closing tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(</' + allBlocks + '>)', 'g' ),\n\t\t'$1\\n\\n'\n\t);\n\n\t// Standardize newline characters to \"\\n\".\n\ttext = text.replace( /\\r\\n|\\r/g, '\\n' );\n\n\t// Find newlines in all elements and add placeholders.\n\ttext = replaceInHtmlTags( text, { '\\n': ' <!-- wpnl --> ' } );\n\n\t// Collapse line breaks before and after <option> elements so they don't get autop'd.\n\tif ( text.indexOf( '<option' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*<option/g, '<option' );\n\t\ttext = text.replace( /<\\/option>\\s*/g, '</option>' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <object> elements, before <param> and <embed> elements\n\t * so they don't get autop'd.\n\t */\n\tif ( text.indexOf( '</object>' ) !== -1 ) {\n\t\ttext = text.replace( /(<object[^>]*>)\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*<\\/object>/g, '</object>' );\n\t\ttext = text.replace( /\\s*(<\\/?(?:param|embed)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <audio> and <video> elements,\n\t * before and after <source> and <track> elements.\n\t */\n\tif ( text.indexOf( '<source' ) !== -1 || text.indexOf( '<track' ) !== -1 ) {\n\t\ttext = text.replace( /([<\\[](?:audio|video)[^>\\]]*[>\\]])\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*([<\\[]\\/(?:audio|video)[>\\]])/g, '$1' );\n\t\ttext = text.replace( /\\s*(<(?:source|track)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t// Collapse line breaks before and after <figcaption> elements.\n\tif ( text.indexOf( '<figcaption' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*(<figcaption[^>]*>)/, '$1' );\n\t\ttext = text.replace( /<\\/figcaption>\\s*/, '</figcaption>' );\n\t}\n\n\t// Remove more than two contiguous line breaks.\n\ttext = text.replace( /\\n\\n+/g, '\\n\\n' );\n\n\t// Split up the contents into an array of strings, separated by double line breaks.\n\tconst texts = text.split( /\\n\\s*\\n/ ).filter( Boolean );\n\n\t// Reset text prior to rebuilding.\n\ttext = '';\n\n\t// Rebuild the content as a string, wrapping every bit with a <p>.\n\ttexts.forEach( ( textPiece ) => {\n\t\ttext += '<p>' + textPiece.replace( /^\\n*|\\n*$/g, '' ) + '</p>\\n';\n\t} );\n\n\t// Under certain strange conditions it could create a P of entirely whitespace.\n\ttext = text.replace( /<p>\\s*<\\/p>/g, '' );\n\n\t// Add a closing <p> inside <div>, <address>, or <form> tag if missing.\n\ttext = text.replace(\n\t\t/<p>([^<]+)<\\/(div|address|form)>/g,\n\t\t'<p>$1</p></$2>'\n\t);\n\n\t// If an opening or closing block element tag is wrapped in a <p>, unwrap it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// In some cases <li> may get wrapped in <p>, fix them.\n\ttext = text.replace( /<p>(<li.+?)<\\/p>/g, '$1' );\n\n\t// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.\n\ttext = text.replace( /<p><blockquote([^>]*)>/gi, '<blockquote$1><p>' );\n\ttext = text.replace( /<\\/blockquote><\\/p>/g, '</p></blockquote>' );\n\n\t// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If an opening or closing block element tag is followed by a closing <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// Optionally insert line breaks.\n\tif ( br ) {\n\t\t// Replace newlines that shouldn't be touched with a placeholder.\n\t\ttext = text.replace( /<(script|style).*?<\\/\\\\1>/g, ( match ) =>\n\t\t\tmatch[ 0 ].replace( /\\n/g, '<WPPreserveNewline />' )\n\t\t);\n\n\t\t// Normalize <br>\n\t\ttext = text.replace( /<br>|<br\\/>/g, '<br />' );\n\n\t\t// Replace any new line characters that aren't preceded by a <br /> with a <br />.\n\t\ttext = text.replace( /(<br \\/>)?\\s*\\n/g, ( a, b ) =>\n\t\t\tb ? a : '<br />\\n'\n\t\t);\n\n\t\t// Replace newline placeholders with newlines.\n\t\ttext = text.replace( /<WPPreserveNewline \\/>/g, '\\n' );\n\t}\n\n\t// If a <br /> tag is after an opening or closing block tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*<br />', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If a <br /> tag is before a subset of opening or closing block tags, remove it.\n\ttext = text.replace(\n\t\t/<br \\/>(\\s*<\\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g,\n\t\t'$1'\n\t);\n\ttext = text.replace( /\\n<\\/p>$/g, '</p>' );\n\n\t// Replace placeholder <pre> tags with their original content.\n\tpreTags.forEach( ( preTag ) => {\n\t\tconst [ name, original ] = preTag;\n\t\t// Use a function to avoid treating special replacement patterns like $' in the original content\n\t\ttext = text.replace( name, () => original );\n\t} );\n\n\t// Restore newlines in all elements.\n\tif ( -1 !== text.indexOf( '<!-- wpnl -->' ) ) {\n\t\ttext = text.replace( /\\s?<!-- wpnl -->\\s?/g, '\\n' );\n\t}\n\n\treturn text;\n}\n\n/**\n * Replaces `<p>` tags with two line breaks. \"Opposite\" of autop().\n *\n * Replaces `<p>` tags with two line breaks except where the `<p>` has attributes.\n * Unifies whitespace. Indents `<li>`, `<dt>` and `<dd>` for better readability.\n *\n * @param html The content from the editor.\n *\n * @example\n * ```js\n * import { removep } from '@wordpress/autop';\n * removep( '<p>my text</p>' ); // \"my text\"\n * ```\n *\n * @return The content with stripped paragraph tags.\n */\nexport function removep( html: string ): string {\n\tconst blocklist =\n\t\t'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';\n\tconst blocklist1 = blocklist + '|div|p';\n\tconst blocklist2 = blocklist + '|pre';\n\tconst preserve: string[] = [];\n\tlet preserveLinebreaks = false;\n\tlet preserveBr = false;\n\n\tif ( ! html ) {\n\t\treturn '';\n\t}\n\n\t// Protect script and style tags.\n\tif ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {\n\t\thtml = html.replace(\n\t\t\t/<(script|style)[^>]*>[\\s\\S]*?<\\/\\1>/g,\n\t\t\t( match ) => {\n\t\t\t\tpreserve.push( match );\n\t\t\t\treturn '<wp-preserve>';\n\t\t\t}\n\t\t);\n\t}\n\n\t// Protect pre tags.\n\tif ( html.indexOf( '<pre' ) !== -1 ) {\n\t\tpreserveLinebreaks = true;\n\t\thtml = html.replace( /<pre[^>]*>[\\s\\S]+?<\\/pre>/g, ( a ) => {\n\t\t\ta = a.replace( /<br ?\\/?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\ta = a.replace( /<\\/?p( [^>]*)?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\treturn a.replace( /\\r?\\n/g, '<wp-line-break>' );\n\t\t} );\n\t}\n\n\t// Remove line breaks but keep <br> tags inside image captions.\n\tif ( html.indexOf( '[caption' ) !== -1 ) {\n\t\tpreserveBr = true;\n\t\thtml = html.replace( /\\[caption[\\s\\S]+?\\[\\/caption\\]/g, ( a ) => {\n\t\t\treturn a\n\t\t\t\t.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' )\n\t\t\t\t.replace( /[\\r\\n\\t]+/, '' );\n\t\t} );\n\t}\n\n\t// Normalize white space characters before and after block tags.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist1 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\n\t// Mark </p> if it has any attributes.\n\thtml = html.replace( /(<p [^>]+>[\\s\\S]*?)<\\/p>/g, '$1</p#>' );\n\n\t// Preserve the first <p> inside a <div>.\n\thtml = html.replace( /<div( [^>]*)?>\\s*<p>/gi, '<div$1>\\n\\n' );\n\n\t// Remove paragraph tags.\n\thtml = html.replace( /\\s*<p>/gi, '' );\n\thtml = html.replace( /\\s*<\\/p>\\s*/gi, '\\n\\n' );\n\n\t// Normalize white space chars and remove multiple line breaks.\n\thtml = html.replace( /\\n[\\s\\u00a0]+\\n/g, '\\n\\n' );\n\n\t// Replace <br> tags with line breaks.\n\thtml = html.replace( /(\\s*)<br ?\\/?>\\s*/gi, ( _, space ) => {\n\t\tif ( space && space.indexOf( '\\n' ) !== -1 ) {\n\t\t\treturn '\\n\\n';\n\t\t}\n\n\t\treturn '\\n';\n\t} );\n\n\t// Fix line breaks around <div>.\n\thtml = html.replace( /\\s*<div/g, '\\n<div' );\n\thtml = html.replace( /<\\/div>\\s*/g, '</div>\\n' );\n\n\t// Fix line breaks around caption shortcodes.\n\thtml = html.replace(\n\t\t/\\s*\\[caption([^\\[]+)\\[\\/caption\\]\\s*/gi,\n\t\t'\\n\\n[caption$1[/caption]\\n\\n'\n\t);\n\thtml = html.replace( /caption\\]\\n\\n+\\[caption/g, 'caption]\\n\\n[caption' );\n\n\t// Pad block elements tags with a line break.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\\\s*>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist2 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\n\t// Indent <li>, <dt> and <dd> tags.\n\thtml = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \\t<$1>' );\n\n\t// Fix line breaks around <select> and <option>.\n\tif ( html.indexOf( '<option' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<option/g, '\\n<option' );\n\t\thtml = html.replace( /\\s*<\\/select>/g, '\\n</select>' );\n\t}\n\n\t// Pad <hr> with two line breaks.\n\tif ( html.indexOf( '<hr' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<hr( [^>]*)?>\\s*/g, '\\n\\n<hr$1>\\n\\n' );\n\t}\n\n\t// Remove line breaks in <object> tags.\n\tif ( html.indexOf( '<object' ) !== -1 ) {\n\t\thtml = html.replace( /<object[\\s\\S]+?<\\/object>/g, ( a ) => {\n\t\t\treturn a.replace( /[\\r\\n]+/g, '' );\n\t\t} );\n\t}\n\n\t// Unmark special paragraph closing tags.\n\thtml = html.replace( /<\\/p#>/g, '</p>\\n' );\n\n\t// Pad remaining <p> tags whit a line break.\n\thtml = html.replace( /\\s*(<p [^>]+>[\\s\\S]*?<\\/p>)/g, '\\n$1' );\n\n\t// Trim.\n\thtml = html.replace( /^\\s+/, '' );\n\thtml = html.replace( /[\\s\\u00a0]+$/, '' );\n\n\tif ( preserveLinebreaks ) {\n\t\thtml = html.replace( /<wp-line-break>/g, '\\n' );\n\t}\n\n\tif ( preserveBr ) {\n\t\thtml = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );\n\t}\n\n\t// Restore preserved tags.\n\tif ( preserve.length ) {\n\t\thtml = html.replace( /<wp-preserve>/g, () => {\n\t\t\treturn preserve.shift() as string;\n\t\t} );\n\t}\n\n\treturn html;\n}\n"], | ||
| "mappings": ";AAGA,IAAM,kBAA2B,MAAM;AACtC,QAAM,WACL;AAOD,QAAM,QACL;AAQD,QAAM,UACL;AAAA,EAMA,WACA,MACA,QACA;AAED,QAAM,QACL;AAAA,EAGA;AAAA,EACA;AAKD,SAAO,IAAI,OAAQ,KAAM;AAC1B,GAAI;AASJ,SA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D,SAAO,KAAK,QAAS,QAAQ,EAAG;AAChC,SAAO,KAAK,QAAS,gBAAgB,EAAG;AAExC,MAAK,oBAAqB;AACzB,WAAO,KAAK,QAAS,oBAAoB,IAAK;AAAA,EAC/C;AAEA,MAAK,YAAa;AACjB,WAAO,KAAK,QAAS,wBAAwB,QAAS;AAAA,EACvD;AAGA,MAAK,SAAS,QAAS;AACtB,WAAO,KAAK,QAAS,kBAAkB,MAAM;AAC5C,aAAO,SAAS,MAAM;AAAA,IACvB,CAAE;AAAA,EACH;AAEA,SAAO;AACR;", | ||
| "names": [] | ||
| } |
+285
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // packages/autop/src/index.ts | ||
| var index_exports = {}; | ||
| __export(index_exports, { | ||
| autop: () => autop, | ||
| removep: () => removep | ||
| }); | ||
| module.exports = __toCommonJS(index_exports); | ||
| var htmlSplitRegex = (() => { | ||
| const comments = "!(?:-(?!->)[^\\-]*)*(?:-->)?"; | ||
| const cdata = "!\\[CDATA\\[[^\\]]*(?:](?!]>)[^\\]]*)*?(?:]]>)?"; | ||
| const escaped = "(?=!--|!\\[CDATA\\[)((?=!-)" + // If yes, which type? | ||
| comments + "|" + cdata + ")"; | ||
| const regex = "(<(" + // Conditional expression follows. | ||
| escaped + // Find end of escaped element. | ||
| "|[^>]*>?))"; | ||
| return new RegExp(regex); | ||
| })(); | ||
| function htmlSplit(input) { | ||
| const parts = []; | ||
| let workingInput = input; | ||
| let match; | ||
| while (match = workingInput.match(htmlSplitRegex)) { | ||
| const index = match.index; | ||
| parts.push(workingInput.slice(0, index)); | ||
| parts.push(match[0]); | ||
| workingInput = workingInput.slice(index + match[0].length); | ||
| } | ||
| if (workingInput.length) { | ||
| parts.push(workingInput); | ||
| } | ||
| return parts; | ||
| } | ||
| function replaceInHtmlTags(haystack, replacePairs) { | ||
| const textArr = htmlSplit(haystack); | ||
| let changed = false; | ||
| const needles = Object.keys(replacePairs); | ||
| for (let i = 1; i < textArr.length; i += 2) { | ||
| for (let j = 0; j < needles.length; j++) { | ||
| const needle = needles[j]; | ||
| if (-1 !== textArr[i].indexOf(needle)) { | ||
| textArr[i] = textArr[i].replace( | ||
| new RegExp(needle, "g"), | ||
| replacePairs[needle] | ||
| ); | ||
| changed = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (changed) { | ||
| haystack = textArr.join(""); | ||
| } | ||
| return haystack; | ||
| } | ||
| function autop(text, br = true) { | ||
| const preTags = []; | ||
| if (text.trim() === "") { | ||
| return ""; | ||
| } | ||
| text = text + "\n"; | ||
| if (text.indexOf("<pre") !== -1) { | ||
| const textParts = text.split("</pre>"); | ||
| const lastText = textParts.pop(); | ||
| text = ""; | ||
| for (let i = 0; i < textParts.length; i++) { | ||
| const textPart = textParts[i]; | ||
| const start = textPart.indexOf("<pre"); | ||
| if (start === -1) { | ||
| text += textPart; | ||
| continue; | ||
| } | ||
| const name = "<pre wp-pre-tag-" + i + "></pre>"; | ||
| preTags.push([name, textPart.substr(start) + "</pre>"]); | ||
| text += textPart.substr(0, start) + name; | ||
| } | ||
| text += lastText; | ||
| } | ||
| text = text.replace(/<br\s*\/?>\s*<br\s*\/?>/g, "\n\n"); | ||
| const allBlocks = "(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)"; | ||
| text = text.replace( | ||
| new RegExp("(<" + allBlocks + "[\\s/>])", "g"), | ||
| "\n\n$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</" + allBlocks + ">)", "g"), | ||
| "$1\n\n" | ||
| ); | ||
| text = text.replace(/\r\n|\r/g, "\n"); | ||
| text = replaceInHtmlTags(text, { "\n": " <!-- wpnl --> " }); | ||
| if (text.indexOf("<option") !== -1) { | ||
| text = text.replace(/\s*<option/g, "<option"); | ||
| text = text.replace(/<\/option>\s*/g, "</option>"); | ||
| } | ||
| if (text.indexOf("</object>") !== -1) { | ||
| text = text.replace(/(<object[^>]*>)\s*/g, "$1"); | ||
| text = text.replace(/\s*<\/object>/g, "</object>"); | ||
| text = text.replace(/\s*(<\/?(?:param|embed)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<source") !== -1 || text.indexOf("<track") !== -1) { | ||
| text = text.replace(/([<\[](?:audio|video)[^>\]]*[>\]])\s*/g, "$1"); | ||
| text = text.replace(/\s*([<\[]\/(?:audio|video)[>\]])/g, "$1"); | ||
| text = text.replace(/\s*(<(?:source|track)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<figcaption") !== -1) { | ||
| text = text.replace(/\s*(<figcaption[^>]*>)/, "$1"); | ||
| text = text.replace(/<\/figcaption>\s*/, "</figcaption>"); | ||
| } | ||
| text = text.replace(/\n\n+/g, "\n\n"); | ||
| const texts = text.split(/\n\s*\n/).filter(Boolean); | ||
| text = ""; | ||
| texts.forEach((textPiece) => { | ||
| text += "<p>" + textPiece.replace(/^\n*|\n*$/g, "") + "</p>\n"; | ||
| }); | ||
| text = text.replace(/<p>\s*<\/p>/g, ""); | ||
| text = text.replace( | ||
| /<p>([^<]+)<\/(div|address|form)>/g, | ||
| "<p>$1</p></$2>" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace(/<p>(<li.+?)<\/p>/g, "$1"); | ||
| text = text.replace(/<p><blockquote([^>]*)>/gi, "<blockquote$1><p>"); | ||
| text = text.replace(/<\/blockquote><\/p>/g, "</p></blockquote>"); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| if (br) { | ||
| text = text.replace( | ||
| /<(script|style).*?<\/\\1>/g, | ||
| (match) => match[0].replace(/\n/g, "<WPPreserveNewline />") | ||
| ); | ||
| text = text.replace(/<br>|<br\/>/g, "<br />"); | ||
| text = text.replace( | ||
| /(<br \/>)?\s*\n/g, | ||
| (a, b) => b ? a : "<br />\n" | ||
| ); | ||
| text = text.replace(/<WPPreserveNewline \/>/g, "\n"); | ||
| } | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*<br />", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g, | ||
| "$1" | ||
| ); | ||
| text = text.replace(/\n<\/p>$/g, "</p>"); | ||
| preTags.forEach((preTag) => { | ||
| const [name, original] = preTag; | ||
| text = text.replace(name, () => original); | ||
| }); | ||
| if (-1 !== text.indexOf("<!-- wpnl -->")) { | ||
| text = text.replace(/\s?<!-- wpnl -->\s?/g, "\n"); | ||
| } | ||
| return text; | ||
| } | ||
| function removep(html) { | ||
| const blocklist = "blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure"; | ||
| const blocklist1 = blocklist + "|div|p"; | ||
| const blocklist2 = blocklist + "|pre"; | ||
| const preserve = []; | ||
| let preserveLinebreaks = false; | ||
| let preserveBr = false; | ||
| if (!html) { | ||
| return ""; | ||
| } | ||
| if (html.indexOf("<script") !== -1 || html.indexOf("<style") !== -1) { | ||
| html = html.replace( | ||
| /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, | ||
| (match) => { | ||
| preserve.push(match); | ||
| return "<wp-preserve>"; | ||
| } | ||
| ); | ||
| } | ||
| if (html.indexOf("<pre") !== -1) { | ||
| preserveLinebreaks = true; | ||
| html = html.replace(/<pre[^>]*>[\s\S]+?<\/pre>/g, (a) => { | ||
| a = a.replace(/<br ?\/?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| a = a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| return a.replace(/\r?\n/g, "<wp-line-break>"); | ||
| }); | ||
| } | ||
| if (html.indexOf("[caption") !== -1) { | ||
| preserveBr = true; | ||
| html = html.replace(/\[caption[\s\S]+?\[\/caption\]/g, (a) => { | ||
| return a.replace(/<br([^>]*)>/g, "<wp-temp-br$1>").replace(/[\r\n\t]+/, ""); | ||
| }); | ||
| } | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist1 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist1 + ")(?: [^>]*)?)>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace(/(<p [^>]+>[\s\S]*?)<\/p>/g, "$1</p#>"); | ||
| html = html.replace(/<div( [^>]*)?>\s*<p>/gi, "<div$1>\n\n"); | ||
| html = html.replace(/\s*<p>/gi, ""); | ||
| html = html.replace(/\s*<\/p>\s*/gi, "\n\n"); | ||
| html = html.replace(/\n[\s\u00a0]+\n/g, "\n\n"); | ||
| html = html.replace(/(\s*)<br ?\/?>\s*/gi, (_, space) => { | ||
| if (space && space.indexOf("\n") !== -1) { | ||
| return "\n\n"; | ||
| } | ||
| return "\n"; | ||
| }); | ||
| html = html.replace(/\s*<div/g, "\n<div"); | ||
| html = html.replace(/<\/div>\s*/g, "</div>\n"); | ||
| html = html.replace( | ||
| /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, | ||
| "\n\n[caption$1[/caption]\n\n" | ||
| ); | ||
| html = html.replace(/caption\]\n\n+\[caption/g, "caption]\n\n[caption"); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist2 + ")(?: [^>]*)?)\\s*>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist2 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace(/<((li|dt|dd)[^>]*)>/g, " <$1>"); | ||
| if (html.indexOf("<option") !== -1) { | ||
| html = html.replace(/\s*<option/g, "\n<option"); | ||
| html = html.replace(/\s*<\/select>/g, "\n</select>"); | ||
| } | ||
| if (html.indexOf("<hr") !== -1) { | ||
| html = html.replace(/\s*<hr( [^>]*)?>\s*/g, "\n\n<hr$1>\n\n"); | ||
| } | ||
| if (html.indexOf("<object") !== -1) { | ||
| html = html.replace(/<object[\s\S]+?<\/object>/g, (a) => { | ||
| return a.replace(/[\r\n]+/g, ""); | ||
| }); | ||
| } | ||
| html = html.replace(/<\/p#>/g, "</p>\n"); | ||
| html = html.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, "\n$1"); | ||
| html = html.replace(/^\s+/, ""); | ||
| html = html.replace(/[\s\u00a0]+$/, ""); | ||
| if (preserveLinebreaks) { | ||
| html = html.replace(/<wp-line-break>/g, "\n"); | ||
| } | ||
| if (preserveBr) { | ||
| html = html.replace(/<wp-temp-br([^>]*)>/g, "<br$1>"); | ||
| } | ||
| if (preserve.length) { | ||
| html = html.replace(/<wp-preserve>/g, () => { | ||
| return preserve.shift(); | ||
| }); | ||
| } | ||
| return html; | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| autop, | ||
| removep | ||
| }); | ||
| //# sourceMappingURL=index.cjs.map |
| { | ||
| "version": 3, | ||
| "sources": ["../src/index.ts"], | ||
| "sourcesContent": ["/**\n * The regular expression for an HTML element.\n */\nconst htmlSplitRegex: RegExp = ( () => {\n\tconst comments =\n\t\t'!' + // Start of comment, after the <.\n\t\t'(?:' + // Unroll the loop: Consume everything until --> is found.\n\t\t'-(?!->)' + // Dash not followed by end of comment.\n\t\t'[^\\\\-]*' + // Consume non-dashes.\n\t\t')*' + // Loop possessively.\n\t\t'(?:-->)?'; // End of comment. If not found, match all input.\n\n\tconst cdata =\n\t\t'!\\\\[CDATA\\\\[' + // Start of comment, after the <.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t'(?:' + // Unroll the loop: Consume everything until ]]> is found.\n\t\t'](?!]>)' + // One ] not followed by end of comment.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t')*?' + // Loop possessively.\n\t\t'(?:]]>)?'; // End of comment. If not found, match all input.\n\n\tconst escaped =\n\t\t'(?=' + // Is the element escaped?\n\t\t'!--' +\n\t\t'|' +\n\t\t'!\\\\[CDATA\\\\[' +\n\t\t')' +\n\t\t'((?=!-)' + // If yes, which type?\n\t\tcomments +\n\t\t'|' +\n\t\tcdata +\n\t\t')';\n\n\tconst regex =\n\t\t'(' + // Capture the entire match.\n\t\t'<' + // Find start of element.\n\t\t'(' + // Conditional expression follows.\n\t\tescaped + // Find end of escaped element.\n\t\t'|' + // ... else ...\n\t\t'[^>]*>?' + // Find end of normal element.\n\t\t')' +\n\t\t')';\n\n\treturn new RegExp( regex );\n} )();\n\n/**\n * Separate HTML elements and comments from the text.\n *\n * @param input The text which has to be formatted.\n *\n * @return The formatted text.\n */\nfunction htmlSplit( input: string ): string[] {\n\tconst parts = [];\n\tlet workingInput = input;\n\n\tlet match;\n\twhile ( ( match = workingInput.match( htmlSplitRegex ) ) ) {\n\t\t// The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.\n\t\t// If the `g` flag is omitted, `index` is included.\n\t\t// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.\n\t\t// Assert `match.index` is a number.\n\t\tconst index = match.index as number;\n\n\t\tparts.push( workingInput.slice( 0, index ) );\n\t\tparts.push( match[ 0 ] );\n\t\tworkingInput = workingInput.slice( index + match[ 0 ].length );\n\t}\n\n\tif ( workingInput.length ) {\n\t\tparts.push( workingInput );\n\t}\n\n\treturn parts;\n}\n\n/**\n * Replace characters or phrases within HTML elements only.\n *\n * @param haystack The text which has to be formatted.\n * @param replacePairs In the form {from: 'to', \u2026}.\n *\n * @return The formatted text.\n */\nfunction replaceInHtmlTags(\n\thaystack: string,\n\treplacePairs: Record< string, string >\n): string {\n\t// Find all elements.\n\tconst textArr = htmlSplit( haystack );\n\tlet changed = false;\n\n\t// Extract all needles.\n\tconst needles = Object.keys( replacePairs );\n\n\t// Loop through delimiters (elements) only.\n\tfor ( let i = 1; i < textArr.length; i += 2 ) {\n\t\tfor ( let j = 0; j < needles.length; j++ ) {\n\t\t\tconst needle = needles[ j ];\n\t\t\tif ( -1 !== textArr[ i ].indexOf( needle ) ) {\n\t\t\t\ttextArr[ i ] = textArr[ i ].replace(\n\t\t\t\t\tnew RegExp( needle, 'g' ),\n\t\t\t\t\treplacePairs[ needle ]\n\t\t\t\t);\n\t\t\t\tchanged = true;\n\t\t\t\t// After one strtr() break out of the foreach loop and look at next element.\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif ( changed ) {\n\t\thaystack = textArr.join( '' );\n\t}\n\n\treturn haystack;\n}\n\n/**\n * Replaces double line-breaks with paragraph elements.\n *\n * A group of regex replaces used to identify text formatted with newlines and\n * replace double line-breaks with HTML paragraph tags. The remaining line-\n * breaks after conversion become `<br />` tags, unless br is set to 'false'.\n *\n * @param text The text which has to be formatted.\n * @param br Optional. If set, will convert all remaining line-\n * breaks after paragraphing. Default true.\n *\n * @example\n *```js\n * import { autop } from '@wordpress/autop';\n * autop( 'my text' ); // \"<p>my text</p>\"\n * ```\n *\n * @return Text which has been converted into paragraph tags.\n */\nexport function autop( text: string, br: boolean = true ): string {\n\tconst preTags: Array< [ string, string ] > = [];\n\n\tif ( text.trim() === '' ) {\n\t\treturn '';\n\t}\n\n\t// Just to make things a little easier, pad the end.\n\ttext = text + '\\n';\n\n\t/*\n\t * Pre tags shouldn't be touched by autop.\n\t * Replace pre tags with placeholders and bring them back after autop.\n\t */\n\tif ( text.indexOf( '<pre' ) !== -1 ) {\n\t\tconst textParts = text.split( '</pre>' );\n\t\tconst lastText = textParts.pop();\n\t\ttext = '';\n\n\t\tfor ( let i = 0; i < textParts.length; i++ ) {\n\t\t\tconst textPart = textParts[ i ];\n\t\t\tconst start = textPart.indexOf( '<pre' );\n\n\t\t\t// Malformed html?\n\t\t\tif ( start === -1 ) {\n\t\t\t\ttext += textPart;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst name = '<pre wp-pre-tag-' + i + '></pre>';\n\t\t\tpreTags.push( [ name, textPart.substr( start ) + '</pre>' ] );\n\n\t\t\ttext += textPart.substr( 0, start ) + name;\n\t\t}\n\n\t\ttext += lastText;\n\t}\n\t// Change multiple <br>s into two line breaks, which will turn into paragraphs.\n\ttext = text.replace( /<br\\s*\\/?>\\s*<br\\s*\\/?>/g, '\\n\\n' );\n\n\tconst allBlocks =\n\t\t'(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';\n\n\t// Add a double line break above block-level opening tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(<' + allBlocks + '[\\\\s/>])', 'g' ),\n\t\t'\\n\\n$1'\n\t);\n\n\t// Add a double line break below block-level closing tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(</' + allBlocks + '>)', 'g' ),\n\t\t'$1\\n\\n'\n\t);\n\n\t// Standardize newline characters to \"\\n\".\n\ttext = text.replace( /\\r\\n|\\r/g, '\\n' );\n\n\t// Find newlines in all elements and add placeholders.\n\ttext = replaceInHtmlTags( text, { '\\n': ' <!-- wpnl --> ' } );\n\n\t// Collapse line breaks before and after <option> elements so they don't get autop'd.\n\tif ( text.indexOf( '<option' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*<option/g, '<option' );\n\t\ttext = text.replace( /<\\/option>\\s*/g, '</option>' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <object> elements, before <param> and <embed> elements\n\t * so they don't get autop'd.\n\t */\n\tif ( text.indexOf( '</object>' ) !== -1 ) {\n\t\ttext = text.replace( /(<object[^>]*>)\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*<\\/object>/g, '</object>' );\n\t\ttext = text.replace( /\\s*(<\\/?(?:param|embed)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <audio> and <video> elements,\n\t * before and after <source> and <track> elements.\n\t */\n\tif ( text.indexOf( '<source' ) !== -1 || text.indexOf( '<track' ) !== -1 ) {\n\t\ttext = text.replace( /([<\\[](?:audio|video)[^>\\]]*[>\\]])\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*([<\\[]\\/(?:audio|video)[>\\]])/g, '$1' );\n\t\ttext = text.replace( /\\s*(<(?:source|track)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t// Collapse line breaks before and after <figcaption> elements.\n\tif ( text.indexOf( '<figcaption' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*(<figcaption[^>]*>)/, '$1' );\n\t\ttext = text.replace( /<\\/figcaption>\\s*/, '</figcaption>' );\n\t}\n\n\t// Remove more than two contiguous line breaks.\n\ttext = text.replace( /\\n\\n+/g, '\\n\\n' );\n\n\t// Split up the contents into an array of strings, separated by double line breaks.\n\tconst texts = text.split( /\\n\\s*\\n/ ).filter( Boolean );\n\n\t// Reset text prior to rebuilding.\n\ttext = '';\n\n\t// Rebuild the content as a string, wrapping every bit with a <p>.\n\ttexts.forEach( ( textPiece ) => {\n\t\ttext += '<p>' + textPiece.replace( /^\\n*|\\n*$/g, '' ) + '</p>\\n';\n\t} );\n\n\t// Under certain strange conditions it could create a P of entirely whitespace.\n\ttext = text.replace( /<p>\\s*<\\/p>/g, '' );\n\n\t// Add a closing <p> inside <div>, <address>, or <form> tag if missing.\n\ttext = text.replace(\n\t\t/<p>([^<]+)<\\/(div|address|form)>/g,\n\t\t'<p>$1</p></$2>'\n\t);\n\n\t// If an opening or closing block element tag is wrapped in a <p>, unwrap it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// In some cases <li> may get wrapped in <p>, fix them.\n\ttext = text.replace( /<p>(<li.+?)<\\/p>/g, '$1' );\n\n\t// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.\n\ttext = text.replace( /<p><blockquote([^>]*)>/gi, '<blockquote$1><p>' );\n\ttext = text.replace( /<\\/blockquote><\\/p>/g, '</p></blockquote>' );\n\n\t// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If an opening or closing block element tag is followed by a closing <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// Optionally insert line breaks.\n\tif ( br ) {\n\t\t// Replace newlines that shouldn't be touched with a placeholder.\n\t\ttext = text.replace( /<(script|style).*?<\\/\\\\1>/g, ( match ) =>\n\t\t\tmatch[ 0 ].replace( /\\n/g, '<WPPreserveNewline />' )\n\t\t);\n\n\t\t// Normalize <br>\n\t\ttext = text.replace( /<br>|<br\\/>/g, '<br />' );\n\n\t\t// Replace any new line characters that aren't preceded by a <br /> with a <br />.\n\t\ttext = text.replace( /(<br \\/>)?\\s*\\n/g, ( a, b ) =>\n\t\t\tb ? a : '<br />\\n'\n\t\t);\n\n\t\t// Replace newline placeholders with newlines.\n\t\ttext = text.replace( /<WPPreserveNewline \\/>/g, '\\n' );\n\t}\n\n\t// If a <br /> tag is after an opening or closing block tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*<br />', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If a <br /> tag is before a subset of opening or closing block tags, remove it.\n\ttext = text.replace(\n\t\t/<br \\/>(\\s*<\\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g,\n\t\t'$1'\n\t);\n\ttext = text.replace( /\\n<\\/p>$/g, '</p>' );\n\n\t// Replace placeholder <pre> tags with their original content.\n\tpreTags.forEach( ( preTag ) => {\n\t\tconst [ name, original ] = preTag;\n\t\t// Use a function to avoid treating special replacement patterns like $' in the original content\n\t\ttext = text.replace( name, () => original );\n\t} );\n\n\t// Restore newlines in all elements.\n\tif ( -1 !== text.indexOf( '<!-- wpnl -->' ) ) {\n\t\ttext = text.replace( /\\s?<!-- wpnl -->\\s?/g, '\\n' );\n\t}\n\n\treturn text;\n}\n\n/**\n * Replaces `<p>` tags with two line breaks. \"Opposite\" of autop().\n *\n * Replaces `<p>` tags with two line breaks except where the `<p>` has attributes.\n * Unifies whitespace. Indents `<li>`, `<dt>` and `<dd>` for better readability.\n *\n * @param html The content from the editor.\n *\n * @example\n * ```js\n * import { removep } from '@wordpress/autop';\n * removep( '<p>my text</p>' ); // \"my text\"\n * ```\n *\n * @return The content with stripped paragraph tags.\n */\nexport function removep( html: string ): string {\n\tconst blocklist =\n\t\t'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';\n\tconst blocklist1 = blocklist + '|div|p';\n\tconst blocklist2 = blocklist + '|pre';\n\tconst preserve: string[] = [];\n\tlet preserveLinebreaks = false;\n\tlet preserveBr = false;\n\n\tif ( ! html ) {\n\t\treturn '';\n\t}\n\n\t// Protect script and style tags.\n\tif ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {\n\t\thtml = html.replace(\n\t\t\t/<(script|style)[^>]*>[\\s\\S]*?<\\/\\1>/g,\n\t\t\t( match ) => {\n\t\t\t\tpreserve.push( match );\n\t\t\t\treturn '<wp-preserve>';\n\t\t\t}\n\t\t);\n\t}\n\n\t// Protect pre tags.\n\tif ( html.indexOf( '<pre' ) !== -1 ) {\n\t\tpreserveLinebreaks = true;\n\t\thtml = html.replace( /<pre[^>]*>[\\s\\S]+?<\\/pre>/g, ( a ) => {\n\t\t\ta = a.replace( /<br ?\\/?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\ta = a.replace( /<\\/?p( [^>]*)?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\treturn a.replace( /\\r?\\n/g, '<wp-line-break>' );\n\t\t} );\n\t}\n\n\t// Remove line breaks but keep <br> tags inside image captions.\n\tif ( html.indexOf( '[caption' ) !== -1 ) {\n\t\tpreserveBr = true;\n\t\thtml = html.replace( /\\[caption[\\s\\S]+?\\[\\/caption\\]/g, ( a ) => {\n\t\t\treturn a\n\t\t\t\t.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' )\n\t\t\t\t.replace( /[\\r\\n\\t]+/, '' );\n\t\t} );\n\t}\n\n\t// Normalize white space characters before and after block tags.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist1 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\n\t// Mark </p> if it has any attributes.\n\thtml = html.replace( /(<p [^>]+>[\\s\\S]*?)<\\/p>/g, '$1</p#>' );\n\n\t// Preserve the first <p> inside a <div>.\n\thtml = html.replace( /<div( [^>]*)?>\\s*<p>/gi, '<div$1>\\n\\n' );\n\n\t// Remove paragraph tags.\n\thtml = html.replace( /\\s*<p>/gi, '' );\n\thtml = html.replace( /\\s*<\\/p>\\s*/gi, '\\n\\n' );\n\n\t// Normalize white space chars and remove multiple line breaks.\n\thtml = html.replace( /\\n[\\s\\u00a0]+\\n/g, '\\n\\n' );\n\n\t// Replace <br> tags with line breaks.\n\thtml = html.replace( /(\\s*)<br ?\\/?>\\s*/gi, ( _, space ) => {\n\t\tif ( space && space.indexOf( '\\n' ) !== -1 ) {\n\t\t\treturn '\\n\\n';\n\t\t}\n\n\t\treturn '\\n';\n\t} );\n\n\t// Fix line breaks around <div>.\n\thtml = html.replace( /\\s*<div/g, '\\n<div' );\n\thtml = html.replace( /<\\/div>\\s*/g, '</div>\\n' );\n\n\t// Fix line breaks around caption shortcodes.\n\thtml = html.replace(\n\t\t/\\s*\\[caption([^\\[]+)\\[\\/caption\\]\\s*/gi,\n\t\t'\\n\\n[caption$1[/caption]\\n\\n'\n\t);\n\thtml = html.replace( /caption\\]\\n\\n+\\[caption/g, 'caption]\\n\\n[caption' );\n\n\t// Pad block elements tags with a line break.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\\\s*>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist2 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\n\t// Indent <li>, <dt> and <dd> tags.\n\thtml = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \\t<$1>' );\n\n\t// Fix line breaks around <select> and <option>.\n\tif ( html.indexOf( '<option' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<option/g, '\\n<option' );\n\t\thtml = html.replace( /\\s*<\\/select>/g, '\\n</select>' );\n\t}\n\n\t// Pad <hr> with two line breaks.\n\tif ( html.indexOf( '<hr' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<hr( [^>]*)?>\\s*/g, '\\n\\n<hr$1>\\n\\n' );\n\t}\n\n\t// Remove line breaks in <object> tags.\n\tif ( html.indexOf( '<object' ) !== -1 ) {\n\t\thtml = html.replace( /<object[\\s\\S]+?<\\/object>/g, ( a ) => {\n\t\t\treturn a.replace( /[\\r\\n]+/g, '' );\n\t\t} );\n\t}\n\n\t// Unmark special paragraph closing tags.\n\thtml = html.replace( /<\\/p#>/g, '</p>\\n' );\n\n\t// Pad remaining <p> tags whit a line break.\n\thtml = html.replace( /\\s*(<p [^>]+>[\\s\\S]*?<\\/p>)/g, '\\n$1' );\n\n\t// Trim.\n\thtml = html.replace( /^\\s+/, '' );\n\thtml = html.replace( /[\\s\\u00a0]+$/, '' );\n\n\tif ( preserveLinebreaks ) {\n\t\thtml = html.replace( /<wp-line-break>/g, '\\n' );\n\t}\n\n\tif ( preserveBr ) {\n\t\thtml = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );\n\t}\n\n\t// Restore preserved tags.\n\tif ( preserve.length ) {\n\t\thtml = html.replace( /<wp-preserve>/g, () => {\n\t\t\treturn preserve.shift() as string;\n\t\t} );\n\t}\n\n\treturn html;\n}\n"], | ||
| "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,IAAM,kBAA2B,MAAM;AACtC,QAAM,WACL;AAOD,QAAM,QACL;AAQD,QAAM,UACL;AAAA,EAMA,WACA,MACA,QACA;AAED,QAAM,QACL;AAAA,EAGA;AAAA,EACA;AAKD,SAAO,IAAI,OAAQ,KAAM;AAC1B,GAAI;AASJ,SAAS,UAAW,OAA0B;AAC7C,QAAM,QAAQ,CAAC;AACf,MAAI,eAAe;AAEnB,MAAI;AACJ,SAAU,QAAQ,aAAa,MAAO,cAAe,GAAM;AAK1D,UAAM,QAAQ,MAAM;AAEpB,UAAM,KAAM,aAAa,MAA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O,KAAK,QAAS,wBAAwB,QAAS;AAAA,EACvD;AAGA,MAAK,SAAS,QAAS;AACtB,WAAO,KAAK,QAAS,kBAAkB,MAAM;AAC5C,aAAO,SAAS,MAAM;AAAA,IACvB,CAAE;AAAA,EACH;AAEA,SAAO;AACR;", | ||
| "names": [] | ||
| } |
+0
-2
@@ -5,4 +5,2 @@ <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. --> | ||
| ## 4.37.0 (2025-12-23) | ||
| ## 4.36.0 (2025-11-26) | ||
@@ -9,0 +7,0 @@ |
+1
-1
| ## Gutenberg | ||
| Copyright 2016-2025 by the contributors | ||
| Copyright 2016-2026 by the contributors | ||
@@ -5,0 +5,0 @@ **License for Contributions (on and after April 15, 2021)** |
+13
-6
| { | ||
| "name": "@wordpress/autop", | ||
| "version": "4.37.0", | ||
| "version": "4.37.1-next.06ee73755.0", | ||
| "description": "WordPress's automatic paragraph functions `autop` and `removep`.", | ||
@@ -25,9 +25,16 @@ "author": "The WordPress Contributors", | ||
| }, | ||
| "main": "build/index.js", | ||
| "module": "build-module/index.js", | ||
| "files": [ | ||
| "src", | ||
| "build", | ||
| "build-module", | ||
| "build-types", | ||
| "*.md" | ||
| ], | ||
| "main": "build/index.cjs", | ||
| "module": "build-module/index.mjs", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./build-types/index.d.ts", | ||
| "import": "./build-module/index.js", | ||
| "require": "./build/index.js" | ||
| "import": "./build-module/index.mjs", | ||
| "require": "./build/index.cjs" | ||
| }, | ||
@@ -43,3 +50,3 @@ "./package.json": "./package.json" | ||
| }, | ||
| "gitHead": "2cf13ec6cf86153c9b3cf369bf5c59046f5cd950" | ||
| "gitHead": "fd315436f44683a993d5f053789b5279d95b2872" | ||
| } |
| // packages/autop/src/index.ts | ||
| var htmlSplitRegex = (() => { | ||
| const comments = "!(?:-(?!->)[^\\-]*)*(?:-->)?"; | ||
| const cdata = "!\\[CDATA\\[[^\\]]*(?:](?!]>)[^\\]]*)*?(?:]]>)?"; | ||
| const escaped = "(?=!--|!\\[CDATA\\[)((?=!-)" + // If yes, which type? | ||
| comments + "|" + cdata + ")"; | ||
| const regex = "(<(" + // Conditional expression follows. | ||
| escaped + // Find end of escaped element. | ||
| "|[^>]*>?))"; | ||
| return new RegExp(regex); | ||
| })(); | ||
| function htmlSplit(input) { | ||
| const parts = []; | ||
| let workingInput = input; | ||
| let match; | ||
| while (match = workingInput.match(htmlSplitRegex)) { | ||
| const index = match.index; | ||
| parts.push(workingInput.slice(0, index)); | ||
| parts.push(match[0]); | ||
| workingInput = workingInput.slice(index + match[0].length); | ||
| } | ||
| if (workingInput.length) { | ||
| parts.push(workingInput); | ||
| } | ||
| return parts; | ||
| } | ||
| function replaceInHtmlTags(haystack, replacePairs) { | ||
| const textArr = htmlSplit(haystack); | ||
| let changed = false; | ||
| const needles = Object.keys(replacePairs); | ||
| for (let i = 1; i < textArr.length; i += 2) { | ||
| for (let j = 0; j < needles.length; j++) { | ||
| const needle = needles[j]; | ||
| if (-1 !== textArr[i].indexOf(needle)) { | ||
| textArr[i] = textArr[i].replace( | ||
| new RegExp(needle, "g"), | ||
| replacePairs[needle] | ||
| ); | ||
| changed = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (changed) { | ||
| haystack = textArr.join(""); | ||
| } | ||
| return haystack; | ||
| } | ||
| function autop(text, br = true) { | ||
| const preTags = []; | ||
| if (text.trim() === "") { | ||
| return ""; | ||
| } | ||
| text = text + "\n"; | ||
| if (text.indexOf("<pre") !== -1) { | ||
| const textParts = text.split("</pre>"); | ||
| const lastText = textParts.pop(); | ||
| text = ""; | ||
| for (let i = 0; i < textParts.length; i++) { | ||
| const textPart = textParts[i]; | ||
| const start = textPart.indexOf("<pre"); | ||
| if (start === -1) { | ||
| text += textPart; | ||
| continue; | ||
| } | ||
| const name = "<pre wp-pre-tag-" + i + "></pre>"; | ||
| preTags.push([name, textPart.substr(start) + "</pre>"]); | ||
| text += textPart.substr(0, start) + name; | ||
| } | ||
| text += lastText; | ||
| } | ||
| text = text.replace(/<br\s*\/?>\s*<br\s*\/?>/g, "\n\n"); | ||
| const allBlocks = "(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)"; | ||
| text = text.replace( | ||
| new RegExp("(<" + allBlocks + "[\\s/>])", "g"), | ||
| "\n\n$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</" + allBlocks + ">)", "g"), | ||
| "$1\n\n" | ||
| ); | ||
| text = text.replace(/\r\n|\r/g, "\n"); | ||
| text = replaceInHtmlTags(text, { "\n": " <!-- wpnl --> " }); | ||
| if (text.indexOf("<option") !== -1) { | ||
| text = text.replace(/\s*<option/g, "<option"); | ||
| text = text.replace(/<\/option>\s*/g, "</option>"); | ||
| } | ||
| if (text.indexOf("</object>") !== -1) { | ||
| text = text.replace(/(<object[^>]*>)\s*/g, "$1"); | ||
| text = text.replace(/\s*<\/object>/g, "</object>"); | ||
| text = text.replace(/\s*(<\/?(?:param|embed)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<source") !== -1 || text.indexOf("<track") !== -1) { | ||
| text = text.replace(/([<\[](?:audio|video)[^>\]]*[>\]])\s*/g, "$1"); | ||
| text = text.replace(/\s*([<\[]\/(?:audio|video)[>\]])/g, "$1"); | ||
| text = text.replace(/\s*(<(?:source|track)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<figcaption") !== -1) { | ||
| text = text.replace(/\s*(<figcaption[^>]*>)/, "$1"); | ||
| text = text.replace(/<\/figcaption>\s*/, "</figcaption>"); | ||
| } | ||
| text = text.replace(/\n\n+/g, "\n\n"); | ||
| const texts = text.split(/\n\s*\n/).filter(Boolean); | ||
| text = ""; | ||
| texts.forEach((textPiece) => { | ||
| text += "<p>" + textPiece.replace(/^\n*|\n*$/g, "") + "</p>\n"; | ||
| }); | ||
| text = text.replace(/<p>\s*<\/p>/g, ""); | ||
| text = text.replace( | ||
| /<p>([^<]+)<\/(div|address|form)>/g, | ||
| "<p>$1</p></$2>" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace(/<p>(<li.+?)<\/p>/g, "$1"); | ||
| text = text.replace(/<p><blockquote([^>]*)>/gi, "<blockquote$1><p>"); | ||
| text = text.replace(/<\/blockquote><\/p>/g, "</p></blockquote>"); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| if (br) { | ||
| text = text.replace( | ||
| /<(script|style).*?<\/\\1>/g, | ||
| (match) => match[0].replace(/\n/g, "<WPPreserveNewline />") | ||
| ); | ||
| text = text.replace(/<br>|<br\/>/g, "<br />"); | ||
| text = text.replace( | ||
| /(<br \/>)?\s*\n/g, | ||
| (a, b) => b ? a : "<br />\n" | ||
| ); | ||
| text = text.replace(/<WPPreserveNewline \/>/g, "\n"); | ||
| } | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*<br />", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g, | ||
| "$1" | ||
| ); | ||
| text = text.replace(/\n<\/p>$/g, "</p>"); | ||
| preTags.forEach((preTag) => { | ||
| const [name, original] = preTag; | ||
| text = text.replace(name, () => original); | ||
| }); | ||
| if (-1 !== text.indexOf("<!-- wpnl -->")) { | ||
| text = text.replace(/\s?<!-- wpnl -->\s?/g, "\n"); | ||
| } | ||
| return text; | ||
| } | ||
| function removep(html) { | ||
| const blocklist = "blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure"; | ||
| const blocklist1 = blocklist + "|div|p"; | ||
| const blocklist2 = blocklist + "|pre"; | ||
| const preserve = []; | ||
| let preserveLinebreaks = false; | ||
| let preserveBr = false; | ||
| if (!html) { | ||
| return ""; | ||
| } | ||
| if (html.indexOf("<script") !== -1 || html.indexOf("<style") !== -1) { | ||
| html = html.replace( | ||
| /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, | ||
| (match) => { | ||
| preserve.push(match); | ||
| return "<wp-preserve>"; | ||
| } | ||
| ); | ||
| } | ||
| if (html.indexOf("<pre") !== -1) { | ||
| preserveLinebreaks = true; | ||
| html = html.replace(/<pre[^>]*>[\s\S]+?<\/pre>/g, (a) => { | ||
| a = a.replace(/<br ?\/?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| a = a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| return a.replace(/\r?\n/g, "<wp-line-break>"); | ||
| }); | ||
| } | ||
| if (html.indexOf("[caption") !== -1) { | ||
| preserveBr = true; | ||
| html = html.replace(/\[caption[\s\S]+?\[\/caption\]/g, (a) => { | ||
| return a.replace(/<br([^>]*)>/g, "<wp-temp-br$1>").replace(/[\r\n\t]+/, ""); | ||
| }); | ||
| } | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist1 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist1 + ")(?: [^>]*)?)>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace(/(<p [^>]+>[\s\S]*?)<\/p>/g, "$1</p#>"); | ||
| html = html.replace(/<div( [^>]*)?>\s*<p>/gi, "<div$1>\n\n"); | ||
| html = html.replace(/\s*<p>/gi, ""); | ||
| html = html.replace(/\s*<\/p>\s*/gi, "\n\n"); | ||
| html = html.replace(/\n[\s\u00a0]+\n/g, "\n\n"); | ||
| html = html.replace(/(\s*)<br ?\/?>\s*/gi, (_, space) => { | ||
| if (space && space.indexOf("\n") !== -1) { | ||
| return "\n\n"; | ||
| } | ||
| return "\n"; | ||
| }); | ||
| html = html.replace(/\s*<div/g, "\n<div"); | ||
| html = html.replace(/<\/div>\s*/g, "</div>\n"); | ||
| html = html.replace( | ||
| /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, | ||
| "\n\n[caption$1[/caption]\n\n" | ||
| ); | ||
| html = html.replace(/caption\]\n\n+\[caption/g, "caption]\n\n[caption"); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist2 + ")(?: [^>]*)?)\\s*>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist2 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace(/<((li|dt|dd)[^>]*)>/g, " <$1>"); | ||
| if (html.indexOf("<option") !== -1) { | ||
| html = html.replace(/\s*<option/g, "\n<option"); | ||
| html = html.replace(/\s*<\/select>/g, "\n</select>"); | ||
| } | ||
| if (html.indexOf("<hr") !== -1) { | ||
| html = html.replace(/\s*<hr( [^>]*)?>\s*/g, "\n\n<hr$1>\n\n"); | ||
| } | ||
| if (html.indexOf("<object") !== -1) { | ||
| html = html.replace(/<object[\s\S]+?<\/object>/g, (a) => { | ||
| return a.replace(/[\r\n]+/g, ""); | ||
| }); | ||
| } | ||
| html = html.replace(/<\/p#>/g, "</p>\n"); | ||
| html = html.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, "\n$1"); | ||
| html = html.replace(/^\s+/, ""); | ||
| html = html.replace(/[\s\u00a0]+$/, ""); | ||
| if (preserveLinebreaks) { | ||
| html = html.replace(/<wp-line-break>/g, "\n"); | ||
| } | ||
| if (preserveBr) { | ||
| html = html.replace(/<wp-temp-br([^>]*)>/g, "<br$1>"); | ||
| } | ||
| if (preserve.length) { | ||
| html = html.replace(/<wp-preserve>/g, () => { | ||
| return preserve.shift(); | ||
| }); | ||
| } | ||
| return html; | ||
| } | ||
| export { | ||
| autop, | ||
| removep | ||
| }; | ||
| //# sourceMappingURL=index.js.map |
| { | ||
| "version": 3, | ||
| "sources": ["../src/index.ts"], | ||
| "sourcesContent": ["/**\n * The regular expression for an HTML element.\n */\nconst htmlSplitRegex: RegExp = ( () => {\n\tconst comments =\n\t\t'!' + // Start of comment, after the <.\n\t\t'(?:' + // Unroll the loop: Consume everything until --> is found.\n\t\t'-(?!->)' + // Dash not followed by end of comment.\n\t\t'[^\\\\-]*' + // Consume non-dashes.\n\t\t')*' + // Loop possessively.\n\t\t'(?:-->)?'; // End of comment. If not found, match all input.\n\n\tconst cdata =\n\t\t'!\\\\[CDATA\\\\[' + // Start of comment, after the <.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t'(?:' + // Unroll the loop: Consume everything until ]]> is found.\n\t\t'](?!]>)' + // One ] not followed by end of comment.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t')*?' + // Loop possessively.\n\t\t'(?:]]>)?'; // End of comment. If not found, match all input.\n\n\tconst escaped =\n\t\t'(?=' + // Is the element escaped?\n\t\t'!--' +\n\t\t'|' +\n\t\t'!\\\\[CDATA\\\\[' +\n\t\t')' +\n\t\t'((?=!-)' + // If yes, which type?\n\t\tcomments +\n\t\t'|' +\n\t\tcdata +\n\t\t')';\n\n\tconst regex =\n\t\t'(' + // Capture the entire match.\n\t\t'<' + // Find start of element.\n\t\t'(' + // Conditional expression follows.\n\t\tescaped + // Find end of escaped element.\n\t\t'|' + // ... else ...\n\t\t'[^>]*>?' + // Find end of normal element.\n\t\t')' +\n\t\t')';\n\n\treturn new RegExp( regex );\n} )();\n\n/**\n * Separate HTML elements and comments from the text.\n *\n * @param input The text which has to be formatted.\n *\n * @return The formatted text.\n */\nfunction htmlSplit( input: string ): string[] {\n\tconst parts = [];\n\tlet workingInput = input;\n\n\tlet match;\n\twhile ( ( match = workingInput.match( htmlSplitRegex ) ) ) {\n\t\t// The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.\n\t\t// If the `g` flag is omitted, `index` is included.\n\t\t// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.\n\t\t// Assert `match.index` is a number.\n\t\tconst index = match.index as number;\n\n\t\tparts.push( workingInput.slice( 0, index ) );\n\t\tparts.push( match[ 0 ] );\n\t\tworkingInput = workingInput.slice( index + match[ 0 ].length );\n\t}\n\n\tif ( workingInput.length ) {\n\t\tparts.push( workingInput );\n\t}\n\n\treturn parts;\n}\n\n/**\n * Replace characters or phrases within HTML elements only.\n *\n * @param haystack The text which has to be formatted.\n * @param replacePairs In the form {from: 'to', \u2026}.\n *\n * @return The formatted text.\n */\nfunction replaceInHtmlTags(\n\thaystack: string,\n\treplacePairs: Record< string, string >\n): string {\n\t// Find all elements.\n\tconst textArr = htmlSplit( haystack );\n\tlet changed = false;\n\n\t// Extract all needles.\n\tconst needles = Object.keys( replacePairs );\n\n\t// Loop through delimiters (elements) only.\n\tfor ( let i = 1; i < textArr.length; i += 2 ) {\n\t\tfor ( let j = 0; j < needles.length; j++ ) {\n\t\t\tconst needle = needles[ j ];\n\t\t\tif ( -1 !== textArr[ i ].indexOf( needle ) ) {\n\t\t\t\ttextArr[ i ] = textArr[ i ].replace(\n\t\t\t\t\tnew RegExp( needle, 'g' ),\n\t\t\t\t\treplacePairs[ needle ]\n\t\t\t\t);\n\t\t\t\tchanged = true;\n\t\t\t\t// After one strtr() break out of the foreach loop and look at next element.\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif ( changed ) {\n\t\thaystack = textArr.join( '' );\n\t}\n\n\treturn haystack;\n}\n\n/**\n * Replaces double line-breaks with paragraph elements.\n *\n * A group of regex replaces used to identify text formatted with newlines and\n * replace double line-breaks with HTML paragraph tags. The remaining line-\n * breaks after conversion become `<br />` tags, unless br is set to 'false'.\n *\n * @param text The text which has to be formatted.\n * @param br Optional. If set, will convert all remaining line-\n * breaks after paragraphing. Default true.\n *\n * @example\n *```js\n * import { autop } from '@wordpress/autop';\n * autop( 'my text' ); // \"<p>my text</p>\"\n * ```\n *\n * @return Text which has been converted into paragraph tags.\n */\nexport function autop( text: string, br: boolean = true ): string {\n\tconst preTags: Array< [ string, string ] > = [];\n\n\tif ( text.trim() === '' ) {\n\t\treturn '';\n\t}\n\n\t// Just to make things a little easier, pad the end.\n\ttext = text + '\\n';\n\n\t/*\n\t * Pre tags shouldn't be touched by autop.\n\t * Replace pre tags with placeholders and bring them back after autop.\n\t */\n\tif ( text.indexOf( '<pre' ) !== -1 ) {\n\t\tconst textParts = text.split( '</pre>' );\n\t\tconst lastText = textParts.pop();\n\t\ttext = '';\n\n\t\tfor ( let i = 0; i < textParts.length; i++ ) {\n\t\t\tconst textPart = textParts[ i ];\n\t\t\tconst start = textPart.indexOf( '<pre' );\n\n\t\t\t// Malformed html?\n\t\t\tif ( start === -1 ) {\n\t\t\t\ttext += textPart;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst name = '<pre wp-pre-tag-' + i + '></pre>';\n\t\t\tpreTags.push( [ name, textPart.substr( start ) + '</pre>' ] );\n\n\t\t\ttext += textPart.substr( 0, start ) + name;\n\t\t}\n\n\t\ttext += lastText;\n\t}\n\t// Change multiple <br>s into two line breaks, which will turn into paragraphs.\n\ttext = text.replace( /<br\\s*\\/?>\\s*<br\\s*\\/?>/g, '\\n\\n' );\n\n\tconst allBlocks =\n\t\t'(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';\n\n\t// Add a double line break above block-level opening tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(<' + allBlocks + '[\\\\s/>])', 'g' ),\n\t\t'\\n\\n$1'\n\t);\n\n\t// Add a double line break below block-level closing tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(</' + allBlocks + '>)', 'g' ),\n\t\t'$1\\n\\n'\n\t);\n\n\t// Standardize newline characters to \"\\n\".\n\ttext = text.replace( /\\r\\n|\\r/g, '\\n' );\n\n\t// Find newlines in all elements and add placeholders.\n\ttext = replaceInHtmlTags( text, { '\\n': ' <!-- wpnl --> ' } );\n\n\t// Collapse line breaks before and after <option> elements so they don't get autop'd.\n\tif ( text.indexOf( '<option' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*<option/g, '<option' );\n\t\ttext = text.replace( /<\\/option>\\s*/g, '</option>' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <object> elements, before <param> and <embed> elements\n\t * so they don't get autop'd.\n\t */\n\tif ( text.indexOf( '</object>' ) !== -1 ) {\n\t\ttext = text.replace( /(<object[^>]*>)\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*<\\/object>/g, '</object>' );\n\t\ttext = text.replace( /\\s*(<\\/?(?:param|embed)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <audio> and <video> elements,\n\t * before and after <source> and <track> elements.\n\t */\n\tif ( text.indexOf( '<source' ) !== -1 || text.indexOf( '<track' ) !== -1 ) {\n\t\ttext = text.replace( /([<\\[](?:audio|video)[^>\\]]*[>\\]])\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*([<\\[]\\/(?:audio|video)[>\\]])/g, '$1' );\n\t\ttext = text.replace( /\\s*(<(?:source|track)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t// Collapse line breaks before and after <figcaption> elements.\n\tif ( text.indexOf( '<figcaption' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*(<figcaption[^>]*>)/, '$1' );\n\t\ttext = text.replace( /<\\/figcaption>\\s*/, '</figcaption>' );\n\t}\n\n\t// Remove more than two contiguous line breaks.\n\ttext = text.replace( /\\n\\n+/g, '\\n\\n' );\n\n\t// Split up the contents into an array of strings, separated by double line breaks.\n\tconst texts = text.split( /\\n\\s*\\n/ ).filter( Boolean );\n\n\t// Reset text prior to rebuilding.\n\ttext = '';\n\n\t// Rebuild the content as a string, wrapping every bit with a <p>.\n\ttexts.forEach( ( textPiece ) => {\n\t\ttext += '<p>' + textPiece.replace( /^\\n*|\\n*$/g, '' ) + '</p>\\n';\n\t} );\n\n\t// Under certain strange conditions it could create a P of entirely whitespace.\n\ttext = text.replace( /<p>\\s*<\\/p>/g, '' );\n\n\t// Add a closing <p> inside <div>, <address>, or <form> tag if missing.\n\ttext = text.replace(\n\t\t/<p>([^<]+)<\\/(div|address|form)>/g,\n\t\t'<p>$1</p></$2>'\n\t);\n\n\t// If an opening or closing block element tag is wrapped in a <p>, unwrap it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// In some cases <li> may get wrapped in <p>, fix them.\n\ttext = text.replace( /<p>(<li.+?)<\\/p>/g, '$1' );\n\n\t// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.\n\ttext = text.replace( /<p><blockquote([^>]*)>/gi, '<blockquote$1><p>' );\n\ttext = text.replace( /<\\/blockquote><\\/p>/g, '</p></blockquote>' );\n\n\t// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If an opening or closing block element tag is followed by a closing <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// Optionally insert line breaks.\n\tif ( br ) {\n\t\t// Replace newlines that shouldn't be touched with a placeholder.\n\t\ttext = text.replace( /<(script|style).*?<\\/\\\\1>/g, ( match ) =>\n\t\t\tmatch[ 0 ].replace( /\\n/g, '<WPPreserveNewline />' )\n\t\t);\n\n\t\t// Normalize <br>\n\t\ttext = text.replace( /<br>|<br\\/>/g, '<br />' );\n\n\t\t// Replace any new line characters that aren't preceded by a <br /> with a <br />.\n\t\ttext = text.replace( /(<br \\/>)?\\s*\\n/g, ( a, b ) =>\n\t\t\tb ? a : '<br />\\n'\n\t\t);\n\n\t\t// Replace newline placeholders with newlines.\n\t\ttext = text.replace( /<WPPreserveNewline \\/>/g, '\\n' );\n\t}\n\n\t// If a <br /> tag is after an opening or closing block tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*<br />', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If a <br /> tag is before a subset of opening or closing block tags, remove it.\n\ttext = text.replace(\n\t\t/<br \\/>(\\s*<\\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g,\n\t\t'$1'\n\t);\n\ttext = text.replace( /\\n<\\/p>$/g, '</p>' );\n\n\t// Replace placeholder <pre> tags with their original content.\n\tpreTags.forEach( ( preTag ) => {\n\t\tconst [ name, original ] = preTag;\n\t\t// Use a function to avoid treating special replacement patterns like $' in the original content\n\t\ttext = text.replace( name, () => original );\n\t} );\n\n\t// Restore newlines in all elements.\n\tif ( -1 !== text.indexOf( '<!-- wpnl -->' ) ) {\n\t\ttext = text.replace( /\\s?<!-- wpnl -->\\s?/g, '\\n' );\n\t}\n\n\treturn text;\n}\n\n/**\n * Replaces `<p>` tags with two line breaks. \"Opposite\" of autop().\n *\n * Replaces `<p>` tags with two line breaks except where the `<p>` has attributes.\n * Unifies whitespace. Indents `<li>`, `<dt>` and `<dd>` for better readability.\n *\n * @param html The content from the editor.\n *\n * @example\n * ```js\n * import { removep } from '@wordpress/autop';\n * removep( '<p>my text</p>' ); // \"my text\"\n * ```\n *\n * @return The content with stripped paragraph tags.\n */\nexport function removep( html: string ): string {\n\tconst blocklist =\n\t\t'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';\n\tconst blocklist1 = blocklist + '|div|p';\n\tconst blocklist2 = blocklist + '|pre';\n\tconst preserve: string[] = [];\n\tlet preserveLinebreaks = false;\n\tlet preserveBr = false;\n\n\tif ( ! html ) {\n\t\treturn '';\n\t}\n\n\t// Protect script and style tags.\n\tif ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {\n\t\thtml = html.replace(\n\t\t\t/<(script|style)[^>]*>[\\s\\S]*?<\\/\\1>/g,\n\t\t\t( match ) => {\n\t\t\t\tpreserve.push( match );\n\t\t\t\treturn '<wp-preserve>';\n\t\t\t}\n\t\t);\n\t}\n\n\t// Protect pre tags.\n\tif ( html.indexOf( '<pre' ) !== -1 ) {\n\t\tpreserveLinebreaks = true;\n\t\thtml = html.replace( /<pre[^>]*>[\\s\\S]+?<\\/pre>/g, ( a ) => {\n\t\t\ta = a.replace( /<br ?\\/?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\ta = a.replace( /<\\/?p( [^>]*)?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\treturn a.replace( /\\r?\\n/g, '<wp-line-break>' );\n\t\t} );\n\t}\n\n\t// Remove line breaks but keep <br> tags inside image captions.\n\tif ( html.indexOf( '[caption' ) !== -1 ) {\n\t\tpreserveBr = true;\n\t\thtml = html.replace( /\\[caption[\\s\\S]+?\\[\\/caption\\]/g, ( a ) => {\n\t\t\treturn a\n\t\t\t\t.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' )\n\t\t\t\t.replace( /[\\r\\n\\t]+/, '' );\n\t\t} );\n\t}\n\n\t// Normalize white space characters before and after block tags.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist1 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\n\t// Mark </p> if it has any attributes.\n\thtml = html.replace( /(<p [^>]+>[\\s\\S]*?)<\\/p>/g, '$1</p#>' );\n\n\t// Preserve the first <p> inside a <div>.\n\thtml = html.replace( /<div( [^>]*)?>\\s*<p>/gi, '<div$1>\\n\\n' );\n\n\t// Remove paragraph tags.\n\thtml = html.replace( /\\s*<p>/gi, '' );\n\thtml = html.replace( /\\s*<\\/p>\\s*/gi, '\\n\\n' );\n\n\t// Normalize white space chars and remove multiple line breaks.\n\thtml = html.replace( /\\n[\\s\\u00a0]+\\n/g, '\\n\\n' );\n\n\t// Replace <br> tags with line breaks.\n\thtml = html.replace( /(\\s*)<br ?\\/?>\\s*/gi, ( _, space ) => {\n\t\tif ( space && space.indexOf( '\\n' ) !== -1 ) {\n\t\t\treturn '\\n\\n';\n\t\t}\n\n\t\treturn '\\n';\n\t} );\n\n\t// Fix line breaks around <div>.\n\thtml = html.replace( /\\s*<div/g, '\\n<div' );\n\thtml = html.replace( /<\\/div>\\s*/g, '</div>\\n' );\n\n\t// Fix line breaks around caption shortcodes.\n\thtml = html.replace(\n\t\t/\\s*\\[caption([^\\[]+)\\[\\/caption\\]\\s*/gi,\n\t\t'\\n\\n[caption$1[/caption]\\n\\n'\n\t);\n\thtml = html.replace( /caption\\]\\n\\n+\\[caption/g, 'caption]\\n\\n[caption' );\n\n\t// Pad block elements tags with a line break.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\\\s*>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist2 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\n\t// Indent <li>, <dt> and <dd> tags.\n\thtml = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \\t<$1>' );\n\n\t// Fix line breaks around <select> and <option>.\n\tif ( html.indexOf( '<option' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<option/g, '\\n<option' );\n\t\thtml = html.replace( /\\s*<\\/select>/g, '\\n</select>' );\n\t}\n\n\t// Pad <hr> with two line breaks.\n\tif ( html.indexOf( '<hr' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<hr( [^>]*)?>\\s*/g, '\\n\\n<hr$1>\\n\\n' );\n\t}\n\n\t// Remove line breaks in <object> tags.\n\tif ( html.indexOf( '<object' ) !== -1 ) {\n\t\thtml = html.replace( /<object[\\s\\S]+?<\\/object>/g, ( a ) => {\n\t\t\treturn a.replace( /[\\r\\n]+/g, '' );\n\t\t} );\n\t}\n\n\t// Unmark special paragraph closing tags.\n\thtml = html.replace( /<\\/p#>/g, '</p>\\n' );\n\n\t// Pad remaining <p> tags whit a line break.\n\thtml = html.replace( /\\s*(<p [^>]+>[\\s\\S]*?<\\/p>)/g, '\\n$1' );\n\n\t// Trim.\n\thtml = html.replace( /^\\s+/, '' );\n\thtml = html.replace( /[\\s\\u00a0]+$/, '' );\n\n\tif ( preserveLinebreaks ) {\n\t\thtml = html.replace( /<wp-line-break>/g, '\\n' );\n\t}\n\n\tif ( preserveBr ) {\n\t\thtml = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );\n\t}\n\n\t// Restore preserved tags.\n\tif ( preserve.length ) {\n\t\thtml = html.replace( /<wp-preserve>/g, () => {\n\t\t\treturn preserve.shift() as string;\n\t\t} );\n\t}\n\n\treturn html;\n}\n"], | ||
| "mappings": ";AAGA,IAAM,kBAA2B,MAAM;AACtC,QAAM,WACL;AAOD,QAAM,QACL;AAQD,QAAM,UACL;AAAA,EAMA,WACA,MACA,QACA;AAED,QAAM,QACL;AAAA,EAGA;AAAA,EACA;AAKD,SAAO,IAAI,OAAQ,KAAM;AAC1B,GAAI;AASJ,SAAS,UAAW,OAA0B;AAC7C,QAAM,QAAQ,CAAC;AACf,MAAI,eAAe;AAEnB,MAAI;AACJ,SAAU,QAAQ,aAAa,MAAO,cAAe,GAAM;AAK1D,UAAM,QAAQ,MAAM;AA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AEA,MAAK,YAAa;AACjB,WAAO,KAAK,QAAS,wBAAwB,QAAS;AAAA,EACvD;AAGA,MAAK,SAAS,QAAS;AACtB,WAAO,KAAK,QAAS,kBAAkB,MAAM;AAC5C,aAAO,SAAS,MAAM;AAAA,IACvB,CAAE;AAAA,EACH;AAEA,SAAO;AACR;", | ||
| "names": [] | ||
| } |
-285
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // packages/autop/src/index.ts | ||
| var index_exports = {}; | ||
| __export(index_exports, { | ||
| autop: () => autop, | ||
| removep: () => removep | ||
| }); | ||
| module.exports = __toCommonJS(index_exports); | ||
| var htmlSplitRegex = (() => { | ||
| const comments = "!(?:-(?!->)[^\\-]*)*(?:-->)?"; | ||
| const cdata = "!\\[CDATA\\[[^\\]]*(?:](?!]>)[^\\]]*)*?(?:]]>)?"; | ||
| const escaped = "(?=!--|!\\[CDATA\\[)((?=!-)" + // If yes, which type? | ||
| comments + "|" + cdata + ")"; | ||
| const regex = "(<(" + // Conditional expression follows. | ||
| escaped + // Find end of escaped element. | ||
| "|[^>]*>?))"; | ||
| return new RegExp(regex); | ||
| })(); | ||
| function htmlSplit(input) { | ||
| const parts = []; | ||
| let workingInput = input; | ||
| let match; | ||
| while (match = workingInput.match(htmlSplitRegex)) { | ||
| const index = match.index; | ||
| parts.push(workingInput.slice(0, index)); | ||
| parts.push(match[0]); | ||
| workingInput = workingInput.slice(index + match[0].length); | ||
| } | ||
| if (workingInput.length) { | ||
| parts.push(workingInput); | ||
| } | ||
| return parts; | ||
| } | ||
| function replaceInHtmlTags(haystack, replacePairs) { | ||
| const textArr = htmlSplit(haystack); | ||
| let changed = false; | ||
| const needles = Object.keys(replacePairs); | ||
| for (let i = 1; i < textArr.length; i += 2) { | ||
| for (let j = 0; j < needles.length; j++) { | ||
| const needle = needles[j]; | ||
| if (-1 !== textArr[i].indexOf(needle)) { | ||
| textArr[i] = textArr[i].replace( | ||
| new RegExp(needle, "g"), | ||
| replacePairs[needle] | ||
| ); | ||
| changed = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (changed) { | ||
| haystack = textArr.join(""); | ||
| } | ||
| return haystack; | ||
| } | ||
| function autop(text, br = true) { | ||
| const preTags = []; | ||
| if (text.trim() === "") { | ||
| return ""; | ||
| } | ||
| text = text + "\n"; | ||
| if (text.indexOf("<pre") !== -1) { | ||
| const textParts = text.split("</pre>"); | ||
| const lastText = textParts.pop(); | ||
| text = ""; | ||
| for (let i = 0; i < textParts.length; i++) { | ||
| const textPart = textParts[i]; | ||
| const start = textPart.indexOf("<pre"); | ||
| if (start === -1) { | ||
| text += textPart; | ||
| continue; | ||
| } | ||
| const name = "<pre wp-pre-tag-" + i + "></pre>"; | ||
| preTags.push([name, textPart.substr(start) + "</pre>"]); | ||
| text += textPart.substr(0, start) + name; | ||
| } | ||
| text += lastText; | ||
| } | ||
| text = text.replace(/<br\s*\/?>\s*<br\s*\/?>/g, "\n\n"); | ||
| const allBlocks = "(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)"; | ||
| text = text.replace( | ||
| new RegExp("(<" + allBlocks + "[\\s/>])", "g"), | ||
| "\n\n$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</" + allBlocks + ">)", "g"), | ||
| "$1\n\n" | ||
| ); | ||
| text = text.replace(/\r\n|\r/g, "\n"); | ||
| text = replaceInHtmlTags(text, { "\n": " <!-- wpnl --> " }); | ||
| if (text.indexOf("<option") !== -1) { | ||
| text = text.replace(/\s*<option/g, "<option"); | ||
| text = text.replace(/<\/option>\s*/g, "</option>"); | ||
| } | ||
| if (text.indexOf("</object>") !== -1) { | ||
| text = text.replace(/(<object[^>]*>)\s*/g, "$1"); | ||
| text = text.replace(/\s*<\/object>/g, "</object>"); | ||
| text = text.replace(/\s*(<\/?(?:param|embed)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<source") !== -1 || text.indexOf("<track") !== -1) { | ||
| text = text.replace(/([<\[](?:audio|video)[^>\]]*[>\]])\s*/g, "$1"); | ||
| text = text.replace(/\s*([<\[]\/(?:audio|video)[>\]])/g, "$1"); | ||
| text = text.replace(/\s*(<(?:source|track)[^>]*>)\s*/g, "$1"); | ||
| } | ||
| if (text.indexOf("<figcaption") !== -1) { | ||
| text = text.replace(/\s*(<figcaption[^>]*>)/, "$1"); | ||
| text = text.replace(/<\/figcaption>\s*/, "</figcaption>"); | ||
| } | ||
| text = text.replace(/\n\n+/g, "\n\n"); | ||
| const texts = text.split(/\n\s*\n/).filter(Boolean); | ||
| text = ""; | ||
| texts.forEach((textPiece) => { | ||
| text += "<p>" + textPiece.replace(/^\n*|\n*$/g, "") + "</p>\n"; | ||
| }); | ||
| text = text.replace(/<p>\s*<\/p>/g, ""); | ||
| text = text.replace( | ||
| /<p>([^<]+)<\/(div|address|form)>/g, | ||
| "<p>$1</p></$2>" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace(/<p>(<li.+?)<\/p>/g, "$1"); | ||
| text = text.replace(/<p><blockquote([^>]*)>/gi, "<blockquote$1><p>"); | ||
| text = text.replace(/<\/blockquote><\/p>/g, "</p></blockquote>"); | ||
| text = text.replace( | ||
| new RegExp("<p>\\s*(</?" + allBlocks + "[^>]*>)", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*</p>", "g"), | ||
| "$1" | ||
| ); | ||
| if (br) { | ||
| text = text.replace( | ||
| /<(script|style).*?<\/\\1>/g, | ||
| (match) => match[0].replace(/\n/g, "<WPPreserveNewline />") | ||
| ); | ||
| text = text.replace(/<br>|<br\/>/g, "<br />"); | ||
| text = text.replace( | ||
| /(<br \/>)?\s*\n/g, | ||
| (a, b) => b ? a : "<br />\n" | ||
| ); | ||
| text = text.replace(/<WPPreserveNewline \/>/g, "\n"); | ||
| } | ||
| text = text.replace( | ||
| new RegExp("(</?" + allBlocks + "[^>]*>)\\s*<br />", "g"), | ||
| "$1" | ||
| ); | ||
| text = text.replace( | ||
| /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g, | ||
| "$1" | ||
| ); | ||
| text = text.replace(/\n<\/p>$/g, "</p>"); | ||
| preTags.forEach((preTag) => { | ||
| const [name, original] = preTag; | ||
| text = text.replace(name, () => original); | ||
| }); | ||
| if (-1 !== text.indexOf("<!-- wpnl -->")) { | ||
| text = text.replace(/\s?<!-- wpnl -->\s?/g, "\n"); | ||
| } | ||
| return text; | ||
| } | ||
| function removep(html) { | ||
| const blocklist = "blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure"; | ||
| const blocklist1 = blocklist + "|div|p"; | ||
| const blocklist2 = blocklist + "|pre"; | ||
| const preserve = []; | ||
| let preserveLinebreaks = false; | ||
| let preserveBr = false; | ||
| if (!html) { | ||
| return ""; | ||
| } | ||
| if (html.indexOf("<script") !== -1 || html.indexOf("<style") !== -1) { | ||
| html = html.replace( | ||
| /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, | ||
| (match) => { | ||
| preserve.push(match); | ||
| return "<wp-preserve>"; | ||
| } | ||
| ); | ||
| } | ||
| if (html.indexOf("<pre") !== -1) { | ||
| preserveLinebreaks = true; | ||
| html = html.replace(/<pre[^>]*>[\s\S]+?<\/pre>/g, (a) => { | ||
| a = a.replace(/<br ?\/?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| a = a.replace(/<\/?p( [^>]*)?>(\r\n|\n)?/g, "<wp-line-break>"); | ||
| return a.replace(/\r?\n/g, "<wp-line-break>"); | ||
| }); | ||
| } | ||
| if (html.indexOf("[caption") !== -1) { | ||
| preserveBr = true; | ||
| html = html.replace(/\[caption[\s\S]+?\[\/caption\]/g, (a) => { | ||
| return a.replace(/<br([^>]*)>/g, "<wp-temp-br$1>").replace(/[\r\n\t]+/, ""); | ||
| }); | ||
| } | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist1 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist1 + ")(?: [^>]*)?)>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace(/(<p [^>]+>[\s\S]*?)<\/p>/g, "$1</p#>"); | ||
| html = html.replace(/<div( [^>]*)?>\s*<p>/gi, "<div$1>\n\n"); | ||
| html = html.replace(/\s*<p>/gi, ""); | ||
| html = html.replace(/\s*<\/p>\s*/gi, "\n\n"); | ||
| html = html.replace(/\n[\s\u00a0]+\n/g, "\n\n"); | ||
| html = html.replace(/(\s*)<br ?\/?>\s*/gi, (_, space) => { | ||
| if (space && space.indexOf("\n") !== -1) { | ||
| return "\n\n"; | ||
| } | ||
| return "\n"; | ||
| }); | ||
| html = html.replace(/\s*<div/g, "\n<div"); | ||
| html = html.replace(/<\/div>\s*/g, "</div>\n"); | ||
| html = html.replace( | ||
| /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, | ||
| "\n\n[caption$1[/caption]\n\n" | ||
| ); | ||
| html = html.replace(/caption\]\n\n+\[caption/g, "caption]\n\n[caption"); | ||
| html = html.replace( | ||
| new RegExp("\\s*<((?:" + blocklist2 + ")(?: [^>]*)?)\\s*>", "g"), | ||
| "\n<$1>" | ||
| ); | ||
| html = html.replace( | ||
| new RegExp("\\s*</(" + blocklist2 + ")>\\s*", "g"), | ||
| "</$1>\n" | ||
| ); | ||
| html = html.replace(/<((li|dt|dd)[^>]*)>/g, " <$1>"); | ||
| if (html.indexOf("<option") !== -1) { | ||
| html = html.replace(/\s*<option/g, "\n<option"); | ||
| html = html.replace(/\s*<\/select>/g, "\n</select>"); | ||
| } | ||
| if (html.indexOf("<hr") !== -1) { | ||
| html = html.replace(/\s*<hr( [^>]*)?>\s*/g, "\n\n<hr$1>\n\n"); | ||
| } | ||
| if (html.indexOf("<object") !== -1) { | ||
| html = html.replace(/<object[\s\S]+?<\/object>/g, (a) => { | ||
| return a.replace(/[\r\n]+/g, ""); | ||
| }); | ||
| } | ||
| html = html.replace(/<\/p#>/g, "</p>\n"); | ||
| html = html.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, "\n$1"); | ||
| html = html.replace(/^\s+/, ""); | ||
| html = html.replace(/[\s\u00a0]+$/, ""); | ||
| if (preserveLinebreaks) { | ||
| html = html.replace(/<wp-line-break>/g, "\n"); | ||
| } | ||
| if (preserveBr) { | ||
| html = html.replace(/<wp-temp-br([^>]*)>/g, "<br$1>"); | ||
| } | ||
| if (preserve.length) { | ||
| html = html.replace(/<wp-preserve>/g, () => { | ||
| return preserve.shift(); | ||
| }); | ||
| } | ||
| return html; | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| autop, | ||
| removep | ||
| }); | ||
| //# sourceMappingURL=index.js.map |
| { | ||
| "version": 3, | ||
| "sources": ["../src/index.ts"], | ||
| "sourcesContent": ["/**\n * The regular expression for an HTML element.\n */\nconst htmlSplitRegex: RegExp = ( () => {\n\tconst comments =\n\t\t'!' + // Start of comment, after the <.\n\t\t'(?:' + // Unroll the loop: Consume everything until --> is found.\n\t\t'-(?!->)' + // Dash not followed by end of comment.\n\t\t'[^\\\\-]*' + // Consume non-dashes.\n\t\t')*' + // Loop possessively.\n\t\t'(?:-->)?'; // End of comment. If not found, match all input.\n\n\tconst cdata =\n\t\t'!\\\\[CDATA\\\\[' + // Start of comment, after the <.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t'(?:' + // Unroll the loop: Consume everything until ]]> is found.\n\t\t'](?!]>)' + // One ] not followed by end of comment.\n\t\t'[^\\\\]]*' + // Consume non-].\n\t\t')*?' + // Loop possessively.\n\t\t'(?:]]>)?'; // End of comment. If not found, match all input.\n\n\tconst escaped =\n\t\t'(?=' + // Is the element escaped?\n\t\t'!--' +\n\t\t'|' +\n\t\t'!\\\\[CDATA\\\\[' +\n\t\t')' +\n\t\t'((?=!-)' + // If yes, which type?\n\t\tcomments +\n\t\t'|' +\n\t\tcdata +\n\t\t')';\n\n\tconst regex =\n\t\t'(' + // Capture the entire match.\n\t\t'<' + // Find start of element.\n\t\t'(' + // Conditional expression follows.\n\t\tescaped + // Find end of escaped element.\n\t\t'|' + // ... else ...\n\t\t'[^>]*>?' + // Find end of normal element.\n\t\t')' +\n\t\t')';\n\n\treturn new RegExp( regex );\n} )();\n\n/**\n * Separate HTML elements and comments from the text.\n *\n * @param input The text which has to be formatted.\n *\n * @return The formatted text.\n */\nfunction htmlSplit( input: string ): string[] {\n\tconst parts = [];\n\tlet workingInput = input;\n\n\tlet match;\n\twhile ( ( match = workingInput.match( htmlSplitRegex ) ) ) {\n\t\t// The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.\n\t\t// If the `g` flag is omitted, `index` is included.\n\t\t// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.\n\t\t// Assert `match.index` is a number.\n\t\tconst index = match.index as number;\n\n\t\tparts.push( workingInput.slice( 0, index ) );\n\t\tparts.push( match[ 0 ] );\n\t\tworkingInput = workingInput.slice( index + match[ 0 ].length );\n\t}\n\n\tif ( workingInput.length ) {\n\t\tparts.push( workingInput );\n\t}\n\n\treturn parts;\n}\n\n/**\n * Replace characters or phrases within HTML elements only.\n *\n * @param haystack The text which has to be formatted.\n * @param replacePairs In the form {from: 'to', \u2026}.\n *\n * @return The formatted text.\n */\nfunction replaceInHtmlTags(\n\thaystack: string,\n\treplacePairs: Record< string, string >\n): string {\n\t// Find all elements.\n\tconst textArr = htmlSplit( haystack );\n\tlet changed = false;\n\n\t// Extract all needles.\n\tconst needles = Object.keys( replacePairs );\n\n\t// Loop through delimiters (elements) only.\n\tfor ( let i = 1; i < textArr.length; i += 2 ) {\n\t\tfor ( let j = 0; j < needles.length; j++ ) {\n\t\t\tconst needle = needles[ j ];\n\t\t\tif ( -1 !== textArr[ i ].indexOf( needle ) ) {\n\t\t\t\ttextArr[ i ] = textArr[ i ].replace(\n\t\t\t\t\tnew RegExp( needle, 'g' ),\n\t\t\t\t\treplacePairs[ needle ]\n\t\t\t\t);\n\t\t\t\tchanged = true;\n\t\t\t\t// After one strtr() break out of the foreach loop and look at next element.\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif ( changed ) {\n\t\thaystack = textArr.join( '' );\n\t}\n\n\treturn haystack;\n}\n\n/**\n * Replaces double line-breaks with paragraph elements.\n *\n * A group of regex replaces used to identify text formatted with newlines and\n * replace double line-breaks with HTML paragraph tags. The remaining line-\n * breaks after conversion become `<br />` tags, unless br is set to 'false'.\n *\n * @param text The text which has to be formatted.\n * @param br Optional. If set, will convert all remaining line-\n * breaks after paragraphing. Default true.\n *\n * @example\n *```js\n * import { autop } from '@wordpress/autop';\n * autop( 'my text' ); // \"<p>my text</p>\"\n * ```\n *\n * @return Text which has been converted into paragraph tags.\n */\nexport function autop( text: string, br: boolean = true ): string {\n\tconst preTags: Array< [ string, string ] > = [];\n\n\tif ( text.trim() === '' ) {\n\t\treturn '';\n\t}\n\n\t// Just to make things a little easier, pad the end.\n\ttext = text + '\\n';\n\n\t/*\n\t * Pre tags shouldn't be touched by autop.\n\t * Replace pre tags with placeholders and bring them back after autop.\n\t */\n\tif ( text.indexOf( '<pre' ) !== -1 ) {\n\t\tconst textParts = text.split( '</pre>' );\n\t\tconst lastText = textParts.pop();\n\t\ttext = '';\n\n\t\tfor ( let i = 0; i < textParts.length; i++ ) {\n\t\t\tconst textPart = textParts[ i ];\n\t\t\tconst start = textPart.indexOf( '<pre' );\n\n\t\t\t// Malformed html?\n\t\t\tif ( start === -1 ) {\n\t\t\t\ttext += textPart;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst name = '<pre wp-pre-tag-' + i + '></pre>';\n\t\t\tpreTags.push( [ name, textPart.substr( start ) + '</pre>' ] );\n\n\t\t\ttext += textPart.substr( 0, start ) + name;\n\t\t}\n\n\t\ttext += lastText;\n\t}\n\t// Change multiple <br>s into two line breaks, which will turn into paragraphs.\n\ttext = text.replace( /<br\\s*\\/?>\\s*<br\\s*\\/?>/g, '\\n\\n' );\n\n\tconst allBlocks =\n\t\t'(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';\n\n\t// Add a double line break above block-level opening tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(<' + allBlocks + '[\\\\s/>])', 'g' ),\n\t\t'\\n\\n$1'\n\t);\n\n\t// Add a double line break below block-level closing tags.\n\ttext = text.replace(\n\t\tnew RegExp( '(</' + allBlocks + '>)', 'g' ),\n\t\t'$1\\n\\n'\n\t);\n\n\t// Standardize newline characters to \"\\n\".\n\ttext = text.replace( /\\r\\n|\\r/g, '\\n' );\n\n\t// Find newlines in all elements and add placeholders.\n\ttext = replaceInHtmlTags( text, { '\\n': ' <!-- wpnl --> ' } );\n\n\t// Collapse line breaks before and after <option> elements so they don't get autop'd.\n\tif ( text.indexOf( '<option' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*<option/g, '<option' );\n\t\ttext = text.replace( /<\\/option>\\s*/g, '</option>' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <object> elements, before <param> and <embed> elements\n\t * so they don't get autop'd.\n\t */\n\tif ( text.indexOf( '</object>' ) !== -1 ) {\n\t\ttext = text.replace( /(<object[^>]*>)\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*<\\/object>/g, '</object>' );\n\t\ttext = text.replace( /\\s*(<\\/?(?:param|embed)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t/*\n\t * Collapse line breaks inside <audio> and <video> elements,\n\t * before and after <source> and <track> elements.\n\t */\n\tif ( text.indexOf( '<source' ) !== -1 || text.indexOf( '<track' ) !== -1 ) {\n\t\ttext = text.replace( /([<\\[](?:audio|video)[^>\\]]*[>\\]])\\s*/g, '$1' );\n\t\ttext = text.replace( /\\s*([<\\[]\\/(?:audio|video)[>\\]])/g, '$1' );\n\t\ttext = text.replace( /\\s*(<(?:source|track)[^>]*>)\\s*/g, '$1' );\n\t}\n\n\t// Collapse line breaks before and after <figcaption> elements.\n\tif ( text.indexOf( '<figcaption' ) !== -1 ) {\n\t\ttext = text.replace( /\\s*(<figcaption[^>]*>)/, '$1' );\n\t\ttext = text.replace( /<\\/figcaption>\\s*/, '</figcaption>' );\n\t}\n\n\t// Remove more than two contiguous line breaks.\n\ttext = text.replace( /\\n\\n+/g, '\\n\\n' );\n\n\t// Split up the contents into an array of strings, separated by double line breaks.\n\tconst texts = text.split( /\\n\\s*\\n/ ).filter( Boolean );\n\n\t// Reset text prior to rebuilding.\n\ttext = '';\n\n\t// Rebuild the content as a string, wrapping every bit with a <p>.\n\ttexts.forEach( ( textPiece ) => {\n\t\ttext += '<p>' + textPiece.replace( /^\\n*|\\n*$/g, '' ) + '</p>\\n';\n\t} );\n\n\t// Under certain strange conditions it could create a P of entirely whitespace.\n\ttext = text.replace( /<p>\\s*<\\/p>/g, '' );\n\n\t// Add a closing <p> inside <div>, <address>, or <form> tag if missing.\n\ttext = text.replace(\n\t\t/<p>([^<]+)<\\/(div|address|form)>/g,\n\t\t'<p>$1</p></$2>'\n\t);\n\n\t// If an opening or closing block element tag is wrapped in a <p>, unwrap it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// In some cases <li> may get wrapped in <p>, fix them.\n\ttext = text.replace( /<p>(<li.+?)<\\/p>/g, '$1' );\n\n\t// If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.\n\ttext = text.replace( /<p><blockquote([^>]*)>/gi, '<blockquote$1><p>' );\n\ttext = text.replace( /<\\/blockquote><\\/p>/g, '</p></blockquote>' );\n\n\t// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '<p>\\\\s*(</?' + allBlocks + '[^>]*>)', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If an opening or closing block element tag is followed by a closing <p> tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*</p>', 'g' ),\n\t\t'$1'\n\t);\n\n\t// Optionally insert line breaks.\n\tif ( br ) {\n\t\t// Replace newlines that shouldn't be touched with a placeholder.\n\t\ttext = text.replace( /<(script|style).*?<\\/\\\\1>/g, ( match ) =>\n\t\t\tmatch[ 0 ].replace( /\\n/g, '<WPPreserveNewline />' )\n\t\t);\n\n\t\t// Normalize <br>\n\t\ttext = text.replace( /<br>|<br\\/>/g, '<br />' );\n\n\t\t// Replace any new line characters that aren't preceded by a <br /> with a <br />.\n\t\ttext = text.replace( /(<br \\/>)?\\s*\\n/g, ( a, b ) =>\n\t\t\tb ? a : '<br />\\n'\n\t\t);\n\n\t\t// Replace newline placeholders with newlines.\n\t\ttext = text.replace( /<WPPreserveNewline \\/>/g, '\\n' );\n\t}\n\n\t// If a <br /> tag is after an opening or closing block tag, remove it.\n\ttext = text.replace(\n\t\tnew RegExp( '(</?' + allBlocks + '[^>]*>)\\\\s*<br />', 'g' ),\n\t\t'$1'\n\t);\n\n\t// If a <br /> tag is before a subset of opening or closing block tags, remove it.\n\ttext = text.replace(\n\t\t/<br \\/>(\\s*<\\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/g,\n\t\t'$1'\n\t);\n\ttext = text.replace( /\\n<\\/p>$/g, '</p>' );\n\n\t// Replace placeholder <pre> tags with their original content.\n\tpreTags.forEach( ( preTag ) => {\n\t\tconst [ name, original ] = preTag;\n\t\t// Use a function to avoid treating special replacement patterns like $' in the original content\n\t\ttext = text.replace( name, () => original );\n\t} );\n\n\t// Restore newlines in all elements.\n\tif ( -1 !== text.indexOf( '<!-- wpnl -->' ) ) {\n\t\ttext = text.replace( /\\s?<!-- wpnl -->\\s?/g, '\\n' );\n\t}\n\n\treturn text;\n}\n\n/**\n * Replaces `<p>` tags with two line breaks. \"Opposite\" of autop().\n *\n * Replaces `<p>` tags with two line breaks except where the `<p>` has attributes.\n * Unifies whitespace. Indents `<li>`, `<dt>` and `<dd>` for better readability.\n *\n * @param html The content from the editor.\n *\n * @example\n * ```js\n * import { removep } from '@wordpress/autop';\n * removep( '<p>my text</p>' ); // \"my text\"\n * ```\n *\n * @return The content with stripped paragraph tags.\n */\nexport function removep( html: string ): string {\n\tconst blocklist =\n\t\t'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';\n\tconst blocklist1 = blocklist + '|div|p';\n\tconst blocklist2 = blocklist + '|pre';\n\tconst preserve: string[] = [];\n\tlet preserveLinebreaks = false;\n\tlet preserveBr = false;\n\n\tif ( ! html ) {\n\t\treturn '';\n\t}\n\n\t// Protect script and style tags.\n\tif ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) {\n\t\thtml = html.replace(\n\t\t\t/<(script|style)[^>]*>[\\s\\S]*?<\\/\\1>/g,\n\t\t\t( match ) => {\n\t\t\t\tpreserve.push( match );\n\t\t\t\treturn '<wp-preserve>';\n\t\t\t}\n\t\t);\n\t}\n\n\t// Protect pre tags.\n\tif ( html.indexOf( '<pre' ) !== -1 ) {\n\t\tpreserveLinebreaks = true;\n\t\thtml = html.replace( /<pre[^>]*>[\\s\\S]+?<\\/pre>/g, ( a ) => {\n\t\t\ta = a.replace( /<br ?\\/?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\ta = a.replace( /<\\/?p( [^>]*)?>(\\r\\n|\\n)?/g, '<wp-line-break>' );\n\t\t\treturn a.replace( /\\r?\\n/g, '<wp-line-break>' );\n\t\t} );\n\t}\n\n\t// Remove line breaks but keep <br> tags inside image captions.\n\tif ( html.indexOf( '[caption' ) !== -1 ) {\n\t\tpreserveBr = true;\n\t\thtml = html.replace( /\\[caption[\\s\\S]+?\\[\\/caption\\]/g, ( a ) => {\n\t\t\treturn a\n\t\t\t\t.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' )\n\t\t\t\t.replace( /[\\r\\n\\t]+/, '' );\n\t\t} );\n\t}\n\n\t// Normalize white space characters before and after block tags.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist1 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\n\t// Mark </p> if it has any attributes.\n\thtml = html.replace( /(<p [^>]+>[\\s\\S]*?)<\\/p>/g, '$1</p#>' );\n\n\t// Preserve the first <p> inside a <div>.\n\thtml = html.replace( /<div( [^>]*)?>\\s*<p>/gi, '<div$1>\\n\\n' );\n\n\t// Remove paragraph tags.\n\thtml = html.replace( /\\s*<p>/gi, '' );\n\thtml = html.replace( /\\s*<\\/p>\\s*/gi, '\\n\\n' );\n\n\t// Normalize white space chars and remove multiple line breaks.\n\thtml = html.replace( /\\n[\\s\\u00a0]+\\n/g, '\\n\\n' );\n\n\t// Replace <br> tags with line breaks.\n\thtml = html.replace( /(\\s*)<br ?\\/?>\\s*/gi, ( _, space ) => {\n\t\tif ( space && space.indexOf( '\\n' ) !== -1 ) {\n\t\t\treturn '\\n\\n';\n\t\t}\n\n\t\treturn '\\n';\n\t} );\n\n\t// Fix line breaks around <div>.\n\thtml = html.replace( /\\s*<div/g, '\\n<div' );\n\thtml = html.replace( /<\\/div>\\s*/g, '</div>\\n' );\n\n\t// Fix line breaks around caption shortcodes.\n\thtml = html.replace(\n\t\t/\\s*\\[caption([^\\[]+)\\[\\/caption\\]\\s*/gi,\n\t\t'\\n\\n[caption$1[/caption]\\n\\n'\n\t);\n\thtml = html.replace( /caption\\]\\n\\n+\\[caption/g, 'caption]\\n\\n[caption' );\n\n\t// Pad block elements tags with a line break.\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\\\s*>', 'g' ),\n\t\t'\\n<$1>'\n\t);\n\thtml = html.replace(\n\t\tnew RegExp( '\\\\s*</(' + blocklist2 + ')>\\\\s*', 'g' ),\n\t\t'</$1>\\n'\n\t);\n\n\t// Indent <li>, <dt> and <dd> tags.\n\thtml = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \\t<$1>' );\n\n\t// Fix line breaks around <select> and <option>.\n\tif ( html.indexOf( '<option' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<option/g, '\\n<option' );\n\t\thtml = html.replace( /\\s*<\\/select>/g, '\\n</select>' );\n\t}\n\n\t// Pad <hr> with two line breaks.\n\tif ( html.indexOf( '<hr' ) !== -1 ) {\n\t\thtml = html.replace( /\\s*<hr( [^>]*)?>\\s*/g, '\\n\\n<hr$1>\\n\\n' );\n\t}\n\n\t// Remove line breaks in <object> tags.\n\tif ( html.indexOf( '<object' ) !== -1 ) {\n\t\thtml = html.replace( /<object[\\s\\S]+?<\\/object>/g, ( a ) => {\n\t\t\treturn a.replace( /[\\r\\n]+/g, '' );\n\t\t} );\n\t}\n\n\t// Unmark special paragraph closing tags.\n\thtml = html.replace( /<\\/p#>/g, '</p>\\n' );\n\n\t// Pad remaining <p> tags whit a line break.\n\thtml = html.replace( /\\s*(<p [^>]+>[\\s\\S]*?<\\/p>)/g, '\\n$1' );\n\n\t// Trim.\n\thtml = html.replace( /^\\s+/, '' );\n\thtml = html.replace( /[\\s\\u00a0]+$/, '' );\n\n\tif ( preserveLinebreaks ) {\n\t\thtml = html.replace( /<wp-line-break>/g, '\\n' );\n\t}\n\n\tif ( preserveBr ) {\n\t\thtml = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );\n\t}\n\n\t// Restore preserved tags.\n\tif ( preserve.length ) {\n\t\thtml = html.replace( /<wp-preserve>/g, () => {\n\t\t\treturn preserve.shift() as string;\n\t\t} );\n\t}\n\n\treturn html;\n}\n"], | ||
| "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,IAAM,kBAA2B,MAAM;AACtC,QAAM,WACL;AAOD,QAAM,QACL;AAQD,QAAM,UACL;AAAA,EAMA,WACA,MACA,QACA;AAED,QAAM,QACL;AAAA,EAGA;AAAA,EACA;AAKD,SAAO,IAAI,OAAQ,KAAM;AAC1B,GAAI;AASJ,SAAS,UAAW,OAA0B;AAC7C,QAAM,QAAQ,CAAC;AACf,MAAI,eAAe;AAEnB,MAAI;AACJ,SAAU,QAAQ,aAAa,MAAO,cAAe,GAAM;AAK1D,UAAM,QAAQ,MAAM;AAEpB,UAAM,KAAM,aAAa,MAAO,GAAG,KAAM,CAAE;AAC3C,UAAM,KAAM,MAAO,CAAE,CAAE;AACvB,mBAAe,aAAa,MAAO,QAAQ,MAAO,CAAE,EAAE,MAAO;AAAA,EAC9D;AAEA,MAAK,aAAa,QA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AAA,EACH;AAEA,SAAO;AACR;", | ||
| "names": [] | ||
| } |
| { | ||
| "$schema": "https://json.schemastore.org/tsconfig.json", | ||
| "extends": "../../tsconfig.base.json", | ||
| "references": [ { "path": "../dom-ready" } ] | ||
| } |
Sorry, the diff of this file is not supported yet
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
144123
-9.63%12
-14.29%1453
-0.34%1
Infinity%