-
-
Notifications
You must be signed in to change notification settings - Fork 8
Enhance demo with character visibility and copy features #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { escapeHtml } from './utils.js'; | ||
|
|
||
| // Mark characters that carry meaning but show nothing—or no more than a gap—so that | ||
| // the demo shows what options like `decodeEntities` and whitespace collapsing do to them, | ||
| // kept apart from the page so that it can be read without one | ||
| // | ||
| // The characters are written as escapes: spelled out, they would be as invisible in this | ||
| // file as they are in the demo | ||
|
|
||
| // Characters that take up room of their own, where a tint alone shows them | ||
| const SPACES = new Map([ | ||
| ['\u00A0', 'NO-BREAK SPACE'], | ||
| ['\u1680', 'OGHAM SPACE MARK'], | ||
| ['\u2000', 'EN QUAD'], | ||
| ['\u2001', 'EM QUAD'], | ||
| ['\u2002', 'EN SPACE'], | ||
| ['\u2003', 'EM SPACE'], | ||
| ['\u2004', 'THREE-PER-EM SPACE'], | ||
| ['\u2005', 'FOUR-PER-EM SPACE'], | ||
| ['\u2006', 'SIX-PER-EM SPACE'], | ||
| ['\u2007', 'FIGURE SPACE'], | ||
| ['\u2008', 'PUNCTUATION SPACE'], | ||
| ['\u2009', 'THIN SPACE'], | ||
| ['\u200A', 'HAIR SPACE'], | ||
| ['\u202F', 'NARROW NO-BREAK SPACE'], | ||
| ['\u205F', 'MEDIUM MATHEMATICAL SPACE'], | ||
| ['\u3000', 'IDEOGRAPHIC SPACE'] | ||
| ]); | ||
|
|
||
| // Characters of no width at all, which need a marker to show up | ||
| const ZERO_WIDTH = new Map([ | ||
| ['\u00AD', ['SOFT HYPHEN', 'SHY']], | ||
| ['\u0085', ['NEXT LINE', 'NEL']], | ||
| ['\u180E', ['MONGOLIAN VOWEL SEPARATOR', 'MVS']], | ||
| ['\u200B', ['ZERO WIDTH SPACE', 'ZWSP']], | ||
| ['\u200C', ['ZERO WIDTH NON-JOINER', 'ZWNJ']], | ||
| ['\u200D', ['ZERO WIDTH JOINER', 'ZWJ']], | ||
| ['\u200E', ['LEFT-TO-RIGHT MARK', 'LRM']], | ||
| ['\u200F', ['RIGHT-TO-LEFT MARK', 'RLM']], | ||
| ['\u2028', ['LINE SEPARATOR', 'LS']], | ||
| ['\u2029', ['PARAGRAPH SEPARATOR', 'PS']], | ||
| ['\u2060', ['WORD JOINER', 'WJ']], | ||
| ['\uFEFF', ['ZERO WIDTH NO-BREAK SPACE', 'BOM']] | ||
| ]); | ||
|
|
||
| // Control characters, but for the tab, line feed, and carriage return that text is made of | ||
| const CONTROLS = '\\u0000-\\u0008\\u000B\\u000C\\u000E-\\u001F\\u007F-\\u009F'; | ||
|
|
||
| /** @param {string} char */ | ||
| const codePoint = (char) => `U+${(char.codePointAt(0) ?? 0).toString(16).toUpperCase().padStart(4, '0')}`; | ||
|
|
||
| /** @param {Iterable<string>} chars */ | ||
| const classOf = (chars) => [...chars].map(char => `\\u${(char.codePointAt(0) ?? 0).toString(16).padStart(4, '0')}`).join(''); | ||
|
|
||
| const RE_INVISIBLE = new RegExp(`[${CONTROLS}${classOf(SPACES.keys())}${classOf(ZERO_WIDTH.keys())}]`, 'g'); | ||
|
|
||
| /** | ||
| * Wrap every invisible character in a span that names it, keeping the character itself so | ||
| * that the marked-up text still copies as what it is | ||
| * | ||
| * @param {unknown} str | ||
| * @returns {string} | ||
| */ | ||
| export const annotateInvisibles = (str) => { | ||
| if (typeof str !== 'string') return ''; | ||
|
|
||
| return escapeHtml(str).replace(RE_INVISIBLE, (char) => { | ||
| const space = SPACES.get(char); | ||
| if (space) { | ||
| return `<span class="invisible" title="${codePoint(char)} ${space}">${char}</span>`; | ||
| } | ||
|
|
||
| const [name, marker] = ZERO_WIDTH.get(char) ?? ['CONTROL CHARACTER', codePoint(char)]; | ||
| return `<span class="invisible zero-width" title="${codePoint(char)} ${name}" data-marker="${marker}">${char}</span>`; | ||
| }); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.