{"version":3,"file":"clean-for-slug.cjs","names":["removeAccents"],"sources":["../src/clean-for-slug.ts"],"sourcesContent":["import { removeAccents } from './remove-accents';\n\n/**\n * Strips HTML tags like PHP's `strip_tags()`: a `<` not followed by ASCII\n * whitespace opens a tag, even an unclosed one, nested `<` need as many `>`,\n * a `>` inside quotes doesn't close a tag, and a comment ends at `-->`.\n *\n * @param string The string to process.\n *\n * @return The string without HTML tags.\n */\nfunction stripTags( string: string ): string {\n\tlet result = '';\n\tlet depth = 0;\n\tlet quote = '';\n\tfor ( let i = 0; i < string.length; i++ ) {\n\t\tconst char = string[ i ];\n\t\tif ( depth === 0 ) {\n\t\t\t// Like PHP's `isspace()`, only ASCII whitespace keeps a `<`.\n\t\t\tif (\n\t\t\t\tchar === '<' &&\n\t\t\t\t! /[ \\t\\n\\v\\f\\r]/.test( string[ i + 1 ] ?? '' )\n\t\t\t) {\n\t\t\t\t// A comment only ends at `-->`, or at the end of the string.\n\t\t\t\tif ( string.startsWith( '<!--', i ) ) {\n\t\t\t\t\tconst end = string.indexOf( '-->', i + 2 );\n\t\t\t\t\tif ( end === -1 ) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\ti = end + 2;\n\t\t\t\t} else {\n\t\t\t\t\tdepth = 1;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tresult += char;\n\t\t\t}\n\t\t} else if ( quote ) {\n\t\t\tif ( char === quote ) {\n\t\t\t\tquote = '';\n\t\t\t}\n\t\t} else if ( char === '\"' || char === \"'\" ) {\n\t\t\tquote = char;\n\t\t} else if ( char === '<' ) {\n\t\t\tdepth++;\n\t\t} else if ( char === '>' ) {\n\t\t\tdepth--;\n\t\t}\n\t}\n\treturn result;\n}\n\n/**\n * Truncates a string like PHP's `utf8_uri_encode()` with a length: keeps the\n * characters whose percent-encoded form fits in the length, counting each\n * ASCII character as one and any other character as three per UTF-8 byte,\n * without splitting a character.\n *\n * @param string The string to process.\n * @param length The maximum length of the percent-encoded string.\n *\n * @return The truncated string.\n */\nfunction truncateEncoded( string: string, length: number ): string {\n\tlet result = '';\n\tlet encodedLength = 0;\n\tfor ( const char of string ) {\n\t\tconst codePoint = char.codePointAt( 0 ) ?? 0;\n\t\tlet charLength = 12;\n\t\tif ( codePoint < 0x80 ) {\n\t\t\tcharLength = 1;\n\t\t} else if ( codePoint < 0x800 ) {\n\t\t\tcharLength = 6;\n\t\t} else if ( codePoint < 0x10000 ) {\n\t\t\tcharLength = 9;\n\t\t}\n\t\tif ( encodedLength + charLength > length ) {\n\t\t\tbreak;\n\t\t}\n\t\tresult += char;\n\t\tencodedLength += charLength;\n\t}\n\treturn result;\n}\n\n/**\n * Performs some basic cleanup of a string for use as a post slug.\n *\n * This replicates what `sanitize_title()` does in WordPress core when saving\n * a post, without the `sanitize_title` filters and the locale-specific\n * transliterations of `remove_accents()`, so it can only approximate what the\n * slug will be. It works on decoded characters, while WordPress returns the\n * slug percent-encoded, and keeps percent-encoded octets as they are. Like\n * WordPress, it keeps what fits in 200 percent-encoded characters.\n *\n * Converts accented letters to basic Latin letters, and strips HTML tags and\n * entities. Converts spaces, periods, forward slashes, dashes and wide spaces\n * to hyphens. Strips the punctuation\n * and invisible characters WordPress strips, and any other ASCII character\n * that isn't a letter, a number, an underscore or a hyphen. Keeps other\n * characters, such as letters of other scripts and emoji. Converts the\n * string to lowercase.\n *\n * @param string Title or slug to be processed.\n *\n * @return Processed string.\n */\nexport function cleanForSlug( string: string ): string {\n\tif ( ! string ) {\n\t\treturn '';\n\t}\n\tconst lowercase = stripTags( removeAccents( string ) )\n\t\t// Remove percent signs that aren't part of an octet.\n\t\t.replace( /%(?![0-9a-fA-F]{2})/g, '' )\n\t\t.toLowerCase();\n\treturn (\n\t\t// Like WordPress, keep what fits in 200 percent-encoded characters,\n\t\t// the length of the slug column.\n\t\ttruncateEncoded( lowercase, 200 )\n\t\t\t// Convert &nbsp, non-breaking hyphens, &ndash, &mdash and their HTML\n\t\t\t// entities, and forward slashes to hyphens.\n\t\t\t.replace( /[\\u00a0\\u2011\\u2013\\u2014/]/g, '-' )\n\t\t\t.replace(\n\t\t\t\t/&nbsp;|&#8209;|&#160;|&ndash;|&#8211;|&mdash;|&#8212;/g,\n\t\t\t\t'-'\n\t\t\t)\n\t\t\t// Strip soft hyphens, &iexcl and &iquest, angle and curly quotes,\n\t\t\t// bullets, &copy, &reg, &deg, &hellip, &trade, accents, and\n\t\t\t// invisible characters without a width.\n\t\t\t.replace(\n\t\t\t\t/[\\u00ad\\u00a1\\u00bf\\u00ab\\u00bb\\u2039\\u203a\\u2018-\\u201f\\u2022\\u00a9\\u00ae\\u00b0\\u2026\\u2122\\u00b4\\u02ca\\u0301\\u0341\\u0300\\u0304\\u030c\\u200b-\\u200f\\u202a-\\u202e\\ufeff\\ufffc]/g,\n\t\t\t\t''\n\t\t\t)\n\t\t\t// Convert invisible characters with a width to hyphens.\n\t\t\t.replace( /[\\u2000-\\u200a\\u2028\\u2029\\u202f]/g, '-' )\n\t\t\t// Convert &times to 'x'.\n\t\t\t.replace( /\\u00d7/g, 'x' )\n\t\t\t// Remove HTML entities.\n\t\t\t.replace( /&.+?;/g, '' )\n\t\t\t.replace( /\\./g, '-' )\n\t\t\t// Remove any ASCII character that isn't a letter, number, space,\n\t\t\t// underscore, hyphen or the percent sign of an octet.\n\t\t\t.replace( /[^%a-z0-9 _\\-\\u0080-\\uffff]/g, '' )\n\t\t\t// Convert spaces to hyphens.\n\t\t\t.replace( / +/g, '-' )\n\t\t\t// Replace multiple hyphens with a single one.\n\t\t\t.replace( /-+/g, '-' )\n\t\t\t// Remove any remaining leading or trailing hyphens.\n\t\t\t.replace( /(^-+)|(-+$)/g, '' )\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAWA,SAAS,UAAW,QAAyB;CAC5C,IAAI,SAAS;CACb,IAAI,QAAQ;CACZ,IAAI,QAAQ;CACZ,KAAM,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAM;EACzC,MAAM,OAA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