From f1aec14c49e38c6075204478c3596c489587e5cf Mon Sep 17 00:00:00 2001 From: Sanjay Kholiya Date: Mon, 16 Nov 2026 13:34:43 +0530 Subject: [PATCH 01/40] Read clipboard HTML into a paste tree --- lib/paste/clipboard_snapshot.dart | 193 ++++ lib/paste/html_reader.dart | 1516 +++++++++++++++++++++++++++++ lib/paste/paste_tree.dart | 180 ++++ pubspec.lock | 2 +- pubspec.yaml | 4 + 5 files changed, 1894 insertions(+), 1 deletion(-) create mode 100644 lib/paste/clipboard_snapshot.dart create mode 100644 lib/paste/html_reader.dart create mode 100644 lib/paste/paste_tree.dart diff --git a/lib/paste/clipboard_snapshot.dart b/lib/paste/clipboard_snapshot.dart new file mode 100644 index 0000000..ad9ce1a --- /dev/null +++ b/lib/paste/clipboard_snapshot.dart @@ -0,0 +1,193 @@ +import 'dart:convert'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; + +import 'html_reader.dart' show PasteResources; + +/// Everything the clipboard held at one moment. +/// +/// Read in one native call, so its representations agree with each other +/// (a dictation app can't swap the text out between two reads) and iOS asks +/// for permission at most once. +class ClipboardSnapshot { + const ClipboardSnapshot({ + this.types = const [], + this.text, + this.html, + this.sourceUrl, + this.rtfHtml, + this.resources = const {}, + this.vscode, + this.kapy, + this.files = const [], + this.image, + this.imageMime, + }); + + /// The platform's own names for what was there. + final List types; + final String? text; + final String? html; + + /// The page the copy came from, for resolving its relative links. + final String? sourceUrl; + + /// Rich text (RTF, RTFD) converted to HTML by the platform. + final String? rtfHtml; + + /// Picture bytes the clipboard carried beside its markup, by the name or + /// URL the markup refers to them by. + final PasteResources resources; + + /// VS Code's description of a copy, as JSON. + final String? vscode; + + /// Kapy Notes' own fragment, from its private clipboard type. + final String? kapy; + + /// Paths of copied files. + final List files; + final Uint8List? image; + final String? imageMime; + + /// The language VS Code says the copied code is in. + String? get vscodeMode { + final raw = vscode; + if (raw == null) return null; + try { + final decoded = jsonDecode(raw); + if (decoded is Map && decoded['mode'] is String) { + final mode = (decoded['mode'] as String).trim(); + return mode.isEmpty || mode == 'plaintext' ? null : mode; + } + } on FormatException { + return null; + } + return null; + } + + static const _channel = MethodChannel('kapynotes/rich_clipboard'); + + /// Reads the clipboard, or the private pasteboard named [pasteboard] on + /// Apple platforms, which is how tests avoid the user's own clipboard. + static Future read({String? pasteboard}) async { + try { + final raw = await _channel.invokeMethod('read', { + 'pasteboard': ?pasteboard, + }); + return fromChannel(raw); + } on MissingPluginException { + return _readLegacy(); + } on PlatformException catch (error) { + debugPrint('KapyNotes: could not read the clipboard: $error'); + return _readLegacy(); + } + } + + /// What an older native side offers: the text and the HTML, read apart. + static Future _readLegacy() async { + String? html; + try { + html = await _channel.invokeMethod('readHtml'); + } on Object { + html = null; + } + final text = (await Clipboard.getData(Clipboard.kTextPlain))?.text; + return fromChannel({'text': text, 'html': html}); + } + + @visibleForTesting + static ClipboardSnapshot fromChannel(Object? raw) { + if (raw is! Map) return const ClipboardSnapshot(); + String? string(String key) { + final value = raw[key]; + return value is String && value.isNotEmpty ? value : null; + } + + var html = string('html'); + var sourceUrl = string('sourceUrl'); + if (html != null && html.startsWith('Version:')) { + final parsed = _parseWindowsHtml(html); + html = parsed.html; + sourceUrl ??= parsed.sourceUrl; + } + final resources = {}; + final rawResources = raw['resources']; + if (rawResources is List) { + for (final entry in rawResources) { + if (entry is! Map) continue; + final name = entry['name']; + final data = entry['data']; + final mime = entry['mime']; + if (name is String && data is Uint8List && data.isNotEmpty) { + resources[name] = ( + bytes: data, + mime: mime is String && mime.isNotEmpty + ? mime.toLowerCase() + : 'application/octet-stream', + ); + } + } + } + final types = raw['types']; + final files = raw['files']; + final image = raw['image']; + return ClipboardSnapshot( + types: types is List ? [...types.whereType()] : const [], + text: string('text')?.replaceAll('\r\n', '\n'), + html: html, + sourceUrl: sourceUrl, + rtfHtml: string('rtfHtml'), + resources: resources, + vscode: string('vscode'), + kapy: string('kapy'), + files: files is List ? [...files.whereType()] : const [], + image: image is Uint8List && image.isNotEmpty ? image : null, + imageMime: string('imageMime'), + ); + } + + /// Windows' "HTML Format": a header of byte offsets and the page's + /// address, then the markup. + static ({String html, String? sourceUrl}) _parseWindowsHtml(String raw) { + String? sourceUrl; + int? startHtml; + int? endHtml; + final markup = raw.indexOf('<'); + for (final line in const LineSplitter().convert( + markup < 0 ? raw : raw.substring(0, markup), + )) { + final colon = line.indexOf(':'); + if (colon <= 0) continue; + final name = line.substring(0, colon); + final value = line.substring(colon + 1).trim(); + switch (name) { + case 'StartHTML': + startHtml = int.tryParse(value); + case 'EndHTML': + endHtml = int.tryParse(value); + case 'SourceURL': + if (value.isNotEmpty) sourceUrl = value; + } + } + final bytes = utf8.encode(raw); + if (startHtml != null && + endHtml != null && + startHtml >= 0 && + startHtml < endHtml && + endHtml <= bytes.length) { + return ( + html: utf8.decode( + bytes.sublist(startHtml, endHtml), + allowMalformed: true, + ), + sourceUrl: sourceUrl, + ); + } + return ( + html: markup < 0 ? '' : raw.substring(markup), + sourceUrl: sourceUrl, + ); + } +} diff --git a/lib/paste/html_reader.dart b/lib/paste/html_reader.dart new file mode 100644 index 0000000..c4021f8 --- /dev/null +++ b/lib/paste/html_reader.dart @@ -0,0 +1,1516 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:html/dom.dart' as dom; +import 'package:html/parser.dart' as html_parser; + +import 'paste_tree.dart'; + +/// Image bytes a clipboard carried alongside its markup, by the URL or file +/// name the markup refers to them by: Safari's web archive, an RTFD's +/// attachments. +typedef PasteResources = Map; + +/// Reads clipboard HTML into [PasteBlock]s. +/// +/// HTML on a clipboard is whatever the source app thought would survive a +/// trip into another app: a browser's serialization of the selection with +/// computed styles inlined, Google Docs' spans with weights in their style +/// attributes, Word's paragraphs pretending to be lists, KaTeX's three copies +/// of every formula. This keeps what was meant — structure, emphasis, links, +/// math, code, pictures — and throws away how it was drawn. +class HtmlPasteReader { + HtmlPasteReader({this.baseUrl, this.resources = const {}}); + + /// The page the selection came from, for its relative links and pictures. + final Uri? baseUrl; + final PasteResources resources; + + final _CssSheet _css = _CssSheet(); + + List read(String html) { + final document = html_parser.parse(_withoutDownlevelConditionals(html)); + for (final style in document.querySelectorAll('style')) { + _css.add(style.text); + } + final root = document.body ?? document.documentElement; + if (root == null) return const []; + _rewriteWordLists(root); + final out = _Builder(); + _walkChildren(root, const _Context(), out); + out.flush(); + return _mergeAdjacentCode(out.blocks); + } + + // --------------------------------------------------------------------- + // Walking + + void _walkChildren(dom.Node parent, _Context context, _Builder out) { + for (final node in parent.nodes) { + _walk(node, context, out); + } + } + + void _walk(dom.Node node, _Context context, _Builder out) { + if (node is dom.Text) { + out.line.addText(node.data, context); + return; + } + if (node is! dom.Element) return; + final element = node; + final tag = element.localName ?? ''; + if (_dropped.contains(tag) || _droppedClass(element)) return; + final declarations = _css.declarationsFor(element); + if (_isHidden(element, declarations) && !_isMath(element)) return; + + if (_isMath(element)) { + final math = _readMath(element); + if (math != null) { + if (math.display) { + out.flush(); + out.blocks.add(PasteMathBlock(math.tex)); + } else { + out.line.add(PasteMath(math.tex)); + } + return; + } + } + + final inner = _styled(element, declarations, context); + + final wordTitle = tag == 'p' ? _wordTitleLevel(element) : null; + if (wordTitle != null) { + out.flush(); + final inlines = _inlinesOf(element, inner); + if (_hasWords(inlines)) out.blocks.add(PasteHeading(wordTitle, inlines)); + return; + } + + switch (tag) { + case 'h1' || 'h2' || 'h3' || 'h4' || 'h5' || 'h6': + out.flush(); + final inlines = _inlinesOf(element, inner); + if (_hasWords(inlines)) { + out.blocks.add(PasteHeading(int.parse(tag.substring(1)), inlines)); + } + return; + case 'ul' || 'ol' || 'menu' || 'dir': + out.flush(); + final list = _readList(element, inner); + if (list.items.isNotEmpty) out.blocks.add(list); + return; + case 'li': + // An item with no list around it: what a selection that starts + // inside a list serialises as. + out.flush(); + out.blocks.add( + PasteList(ordered: false, items: [_readItem(element, inner)]), + ); + return; + case 'blockquote': + out.flush(); + final blocks = _blocksOf(element, inner); + if (blocks.isNotEmpty) out.blocks.add(PasteQuote(blocks)); + return; + case 'pre': + out.flush(); + final code = _readPre(element); + if (code != null) out.blocks.add(code); + return; + case 'code' || 'kbd' || 'samp' || 'tt': + final text = _codeText(element); + if (text.contains('\n') && text.trim().isNotEmpty) { + out.flush(); + out.blocks.add( + PasteCode(_trimCode(text), language: _languageOf(element)), + ); + } else if (text.isNotEmpty) { + out.line.add(PasteCodeSpan(text, href: inner.href)); + } + return; + case 'table': + out.flush(); + _readTable(element, inner, out); + return; + case 'hr': + out.flush(); + out.blocks.add(const PasteRule()); + return; + case 'br': + out.line.addBreak(inner); + return; + case 'img': + _readImage(element, inner, out); + return; + case 'input': + final type = element.attributes['type']?.toLowerCase(); + if (type == 'checkbox') { + final checked = element.attributes.containsKey('checked'); + out.line.addText(checked ? '☑ ' : '☐ ', inner); + } + return; + case 'rt': + out.line.addText('(', inner); + _walkChildren(element, inner, out); + out.line.addText(')', inner); + return; + case 'dt' || 'summary': + out.flush(); + _walkChildren(element, inner.withStyle(PasteStyle.bold), out); + out.flush(); + return; + } + + if (_isBlock(tag, declarations)) { + out.flush(); + _walkChildren(element, inner, out); + out.flush(); + return; + } + _walkChildren(element, inner, out); + } + + List _blocksOf(dom.Element element, _Context context) { + final out = _Builder(); + _walkChildren(element, context, out); + out.flush(); + return out.blocks; + } + + /// A heading's or a cell's words: its blocks flattened into one run, each + /// block after the first on a line of its own. + List _inlinesOf(dom.Element element, _Context context) { + final out = _Builder(); + _walkChildren(element, context, out); + out.flush(); + return _flatten(out.blocks); + } + + List _flatten(List blocks, {String bullet = ''}) { + final result = []; + void lineBreak() { + if (result.isNotEmpty && result.last is! PasteBreak) { + result.add(const PasteBreak()); + } + } + + for (final block in blocks) { + switch (block) { + case PasteParagraph(:final inlines) || PasteHeading(:final inlines): + lineBreak(); + if (bullet.isNotEmpty) result.add(PasteText(bullet)); + result.addAll(inlines); + case PasteList(:final items, :final ordered, :final start): + for (var i = 0; i < items.length; i++) { + final item = items[i]; + final marker = item.checked != null + ? (item.checked! ? '☑ ' : '☐ ') + : ordered + ? '${start + i}. ' + : '• '; + lineBreak(); + result.addAll(_flatten(item.blocks, bullet: marker)); + } + case PasteQuote(:final blocks): + lineBreak(); + result.addAll(_flatten(blocks)); + case PasteCode(:final text): + lineBreak(); + result.add(PasteCodeSpan(text.replaceAll('\n', ' '))); + case PasteMathBlock(:final tex): + lineBreak(); + result.add(PasteMath(tex)); + case PasteTable(:final rows): + for (final row in rows) { + lineBreak(); + for (var c = 0; c < row.length; c++) { + if (c > 0) result.add(const PasteText(' · ')); + result.addAll(row[c]); + } + } + case PasteRule(): + lineBreak(); + } + } + while (result.isNotEmpty && result.last is PasteBreak) { + result.removeLast(); + } + return result; + } + + // --------------------------------------------------------------------- + // Styles + + _Context _styled( + dom.Element element, + Map declarations, + _Context context, + ) { + final styles = {...context.styles}; + switch (element.localName) { + case 'b' || 'strong': + styles.add(PasteStyle.bold); + case 'i' || 'em' || 'cite' || 'var' || 'dfn': + styles.add(PasteStyle.italic); + case 'u' || 'ins': + styles.add(PasteStyle.underline); + case 's' || 'strike' || 'del': + styles.add(PasteStyle.strike); + case 'sup': + styles.add(PasteStyle.superscript); + case 'sub': + styles.add(PasteStyle.subscript); + case 'th': + styles.add(PasteStyle.bold); + } + final weight = declarations['font-weight']; + if (weight != null) { + final numeric = int.tryParse(weight); + if (weight == 'bold' || + weight == 'bolder' || + (numeric != null && numeric >= 600)) { + styles.add(PasteStyle.bold); + } else if (weight == 'normal' || + weight == 'lighter' || + (numeric != null && numeric < 600)) { + styles.remove(PasteStyle.bold); + } + } + final fontStyle = declarations['font-style']; + if (fontStyle == 'italic' || fontStyle == 'oblique') { + styles.add(PasteStyle.italic); + } else if (fontStyle == 'normal') { + styles.remove(PasteStyle.italic); + } + final decoration = + '${declarations['text-decoration'] ?? ''} ' + '${declarations['text-decoration-line'] ?? ''}'; + if (decoration.contains('underline')) styles.add(PasteStyle.underline); + if (decoration.contains('line-through')) styles.add(PasteStyle.strike); + final vertical = declarations['vertical-align']; + if (vertical == 'super') styles.add(PasteStyle.superscript); + if (vertical == 'sub') styles.add(PasteStyle.subscript); + + var href = context.href; + if (element.localName == 'a') { + href = _resolveLink(element.attributes['href']); + } + // A link says it is one by being a link; the underline a page draws under + // it is not emphasis the writer chose. + if (href != null) styles.remove(PasteStyle.underline); + + final whiteSpace = declarations['white-space']; + var pre = context.pre; + if (whiteSpace != null) { + pre = + whiteSpace.startsWith('pre') && whiteSpace != 'pre-line' || + whiteSpace == 'break-spaces'; + } + return _Context(styles: styles, href: href, pre: pre); + } + + bool _isHidden(dom.Element element, Map declarations) { + if (element.attributes.containsKey('hidden')) return true; + if (element.attributes['aria-hidden'] == 'true') return true; + if (declarations['display'] == 'none') return true; + if (declarations['visibility'] == 'hidden') return true; + for (final name in element.classes) { + if (_screenReaderOnly.contains(name)) return true; + } + return false; + } + + static bool _isBlock(String tag, Map declarations) { + final display = declarations['display']; + if (display != null) { + if (display.startsWith('inline')) return false; + if (display == 'block' || + display == 'flex' || + display == 'grid' || + display == 'list-item' || + display.startsWith('table') || + display == 'flow-root') { + return true; + } + } + return _blockTags.contains(tag); + } + + // --------------------------------------------------------------------- + // Lists + + PasteList _readList(dom.Element list, _Context context) { + final ordered = list.localName == 'ol'; + final start = int.tryParse(list.attributes['start'] ?? '') ?? 1; + final items = []; + final levels = []; + for (final child in list.nodes) { + if (child is dom.Text) { + if (child.data.trim().isEmpty) continue; + items.add( + PasteListItem([ + PasteParagraph([PasteText(child.data.trim())]), + ]), + ); + levels.add(null); + continue; + } + if (child is! dom.Element) continue; + final declarations = _css.declarationsFor(child); + if (_isHidden(child, declarations)) continue; + final tag = child.localName; + if (tag == 'ul' || tag == 'ol') { + // A list straight inside a list — Google Docs and hand-written HTML + // both do it — belongs to the item before it. + final nested = _readList(child, _styled(child, declarations, context)); + if (nested.items.isEmpty) continue; + if (items.isEmpty) { + items.add(PasteListItem([nested])); + levels.add(null); + } else { + final last = items.removeLast(); + items.add( + PasteListItem([...last.blocks, nested], checked: last.checked), + ); + } + continue; + } + items.add(_readItem(child, _styled(child, declarations, context))); + levels.add(int.tryParse(child.attributes['aria-level'] ?? '')); + } + final nestedByLevel = _nestByLevel(items, levels, ordered); + return PasteList( + ordered: ordered, + start: start, + items: nestedByLevel ?? items, + ); + } + + /// Google Docs writes one flat list and says each item's depth in + /// `aria-level`. Rebuilds the nesting it means, or returns null when the + /// levels say nothing. + List? _nestByLevel( + List items, + List levels, + bool ordered, + ) { + final known = levels.whereType().toSet(); + if (known.length < 2 || levels.contains(null)) return null; + final base = known.reduce((a, b) => a < b ? a : b); + List build(int from, int to, int level) { + final result = []; + var i = from; + while (i < to) { + final item = items[i]; + var j = i + 1; + while (j < to && levels[j]! > level) { + j++; + } + if (j > i + 1) { + final children = build(i + 1, j, levels[i + 1]!); + result.add( + PasteListItem([ + ...item.blocks, + PasteList(ordered: ordered, items: children), + ], checked: item.checked), + ); + } else { + result.add(item); + } + i = j; + } + return result; + } + + return build(0, items.length, base); + } + + PasteListItem _readItem(dom.Element item, _Context context) { + bool? checked; + final role = item.attributes['role']; + if (role == 'checkbox') { + checked = item.attributes['aria-checked'] == 'true'; + } + final box = item.querySelector( + 'input[type=checkbox], input[type="checkbox"]', + ); + if (box != null && _belongsToItem(box, item)) { + checked = box.attributes.containsKey('checked'); + box.remove(); + } + final notionBox = item.querySelector('.checkbox'); + if (checked == null && notionBox != null) { + checked = notionBox.classes.contains('checkbox-on'); + notionBox.remove(); + } + var blocks = _blocksOf(item, context); + if (checked == null && blocks.isNotEmpty) { + final (marker, rest) = _leadingTaskMarker(blocks.first); + if (marker != null) { + checked = marker; + blocks = [?rest, ...blocks.skip(1)]; + } + } + return PasteListItem(blocks, checked: checked); + } + + /// Whether a checkbox is this item's own rather than one in a list nested + /// inside it. + static bool _belongsToItem(dom.Element box, dom.Element item) { + for (var parent = box.parent; parent != null; parent = parent.parent) { + if (identical(parent, item)) return true; + if (parent.localName == 'li') return false; + } + return false; + } + + /// An item whose words open with a box drawn as a character — `☐`, `☑`, + /// `[ ]`, `[x]` — which is how many apps export a checklist. + static (bool?, PasteBlock?) _leadingTaskMarker(PasteBlock block) { + if (block is! PasteParagraph || block.inlines.isEmpty) return (null, block); + final first = block.inlines.first; + if (first is! PasteText) return (null, block); + final match = RegExp( + r'^\s*(☐|☑|☒|✅|\[ \]|\[[xX]\])\s+', + ).firstMatch(first.text); + if (match == null) return (null, block); + final marker = match.group(1)!; + final checked = marker != '☐' && marker != '[ ]'; + final remaining = first.text.substring(match.end); + return ( + checked, + PasteParagraph([ + if (remaining.isNotEmpty) + PasteText(remaining, styles: first.styles, href: first.href), + ...block.inlines.skip(1), + ]), + ); + } + + // --------------------------------------------------------------------- + // Code + + PasteCode? _readPre(dom.Element pre) { + final codes = pre.querySelectorAll('code'); + final code = codes.length == 1 ? codes.single : null; + final text = _codeText(code ?? pre); + if (text.trim().isEmpty) return null; + return PasteCode( + _trimCode(text), + language: + (code == null ? null : _languageOf(code)) ?? + _languageOf(pre) ?? + _languageOfAncestors(pre) ?? + _toolbarLanguage(pre, code), + ); + } + + /// The text of a code element as it reads: line breaks kept, toolbars and + /// line numbers left out. + String _codeText(dom.Element element) { + final buffer = StringBuffer(); + void visit(dom.Node node) { + if (node is dom.Text) { + buffer.write(node.data); + return; + } + if (node is! dom.Element) return; + final tag = node.localName; + if (tag == 'br') { + buffer.write('\n'); + return; + } + if (_dropped.contains(tag) || _droppedClass(node)) return; + if (_isHidden(node, _css.declarationsFor(node))) return; + if (_lineNumberClass(node)) return; + final block = + _isBlock(tag ?? '', _css.declarationsFor(node)) && tag != 'code'; + if (block && buffer.isNotEmpty && !_endsWithNewline(buffer)) { + buffer.write('\n'); + } + for (final child in node.nodes) { + visit(child); + } + if (block && !_endsWithNewline(buffer)) buffer.write('\n'); + } + + for (final child in element.nodes) { + visit(child); + } + return buffer.toString().replaceAll('\r\n', '\n').replaceAll('\u00a0', ' '); + } + + static bool _endsWithNewline(StringBuffer buffer) => + buffer.isNotEmpty && buffer.toString().endsWith('\n'); + + static String _trimCode(String text) { + var result = text; + while (result.startsWith('\n')) { + result = result.substring(1); + } + return result.trimRight(); + } + + static final _languageClass = RegExp( + r'(?:^|\s)(?:language|lang|highlight-source|brush:?)-?([A-Za-z0-9_+#.-]+)', + ); + + String? _languageOf(dom.Element element) { + final declared = + element.attributes['data-language'] ?? + element.attributes['data-lang'] ?? + element.attributes['lang']; + if (declared != null && _plausibleLanguage(declared)) { + return declared.toLowerCase(); + } + final match = _languageClass.firstMatch(element.className); + final language = match?.group(1); + if (language == null) return null; + return _normalizedLanguage(language); + } + + String? _languageOfAncestors(dom.Element element) { + var parent = element.parent; + for (var depth = 0; parent != null && depth < 3; depth++) { + final language = _languageOf(parent); + if (language != null) return language; + parent = parent.parent; + } + return null; + } + + /// The label above a code block in ChatGPT, Claude and Gemini: a short + /// word on its own in the block's header, outside the code itself. + String? _toolbarLanguage(dom.Element pre, dom.Element? code) { + final container = code == null ? pre : _blockContainerOf(pre); + for (final candidate in container.querySelectorAll('div, span')) { + if (code != null && _contains(candidate, code)) continue; + if (code != null && _contains(code, candidate)) continue; + if (candidate.children.isNotEmpty) continue; + final text = candidate.text.trim(); + if (_plausibleLanguage(text)) return _normalizedLanguage(text); + } + return null; + } + + static dom.Element _blockContainerOf(dom.Element pre) { + final parent = pre.parent; + if (parent != null && parent.localName != 'body') { + final label = parent.querySelectorAll('pre'); + if (label.length == 1) return parent; + } + return pre; + } + + static bool _contains(dom.Element outer, dom.Element inner) { + for (dom.Node? node = inner; node != null; node = node.parent) { + if (identical(node, outer)) return true; + } + return false; + } + + static bool _plausibleLanguage(String text) => + text.isNotEmpty && + text.length <= 24 && + RegExp(r'^[A-Za-z][A-Za-z0-9_+#.\- ]*$').hasMatch(text) && + !_notLanguages.contains(text.toLowerCase()); + + static String _normalizedLanguage(String raw) { + final lower = raw.trim().toLowerCase(); + return switch (lower) { + 'c++' => 'cpp', + 'c#' => 'csharp', + 'f#' => 'fsharp', + 'objective-c' => 'objectivec', + 'shell' || 'bash' || 'sh' || 'zsh' || 'console' => lower, + _ => lower.replaceAll(' ', '-'), + }; + } + + static bool _lineNumberClass(dom.Element element) { + for (final name in element.classes) { + if (name == 'linenumber' || + name == 'line-number' || + name == 'lineno' || + name == 'gutter' || + name == 'react-syntax-highlighter-line-number') { + return true; + } + } + return false; + } + + /// Code a source splits into one `pre` per line — Notion, some + /// highlighters — read back as one block. + static List _mergeAdjacentCode(List blocks) { + final merged = []; + for (final block in blocks) { + final last = merged.isEmpty ? null : merged.last; + if (block is PasteCode && + last is PasteCode && + last.language == block.language && + !last.text.contains('\n') && + !block.text.contains('\n') && + last.language == null) { + merged.removeLast(); + merged.add(PasteCode('${last.text}\n${block.text}')); + continue; + } + merged.add(block); + } + return merged; + } + + // --------------------------------------------------------------------- + // Tables + + void _readTable(dom.Element table, _Context context, _Builder out) { + final rows = _ownRows(table); + if (_isLayoutTable(table, rows)) { + for (final row in rows) { + for (final cell in row.children) { + if (cell.localName != 'td' && cell.localName != 'th') continue; + out.flush(); + _walkChildren(cell, context, out); + out.flush(); + } + } + return; + } + + final grid = >>[]; + final aligns = []; + final pending = value})>{}; + for (var r = 0; r < rows.length; r++) { + final cells = >[]; + var column = 0; + void fillSpans() { + while (pending.containsKey(column)) { + final span = pending[column]!; + cells.add(const []); + if (span.rows <= 1) { + pending.remove(column); + } else { + pending[column] = (rows: span.rows - 1, value: span.value); + } + column++; + } + } + + for (final cell in rows[r].children) { + if (cell.localName != 'td' && cell.localName != 'th') continue; + final declarations = _css.declarationsFor(cell); + if (_isHidden(cell, declarations)) continue; + fillSpans(); + final inlines = _inlinesOf(cell, _styled(cell, declarations, context)); + final colspan = (int.tryParse(cell.attributes['colspan'] ?? '') ?? 1) + .clamp(1, 64); + final rowspan = (int.tryParse(cell.attributes['rowspan'] ?? '') ?? 1) + .clamp(1, 512); + while (aligns.length <= column + colspan - 1) { + aligns.add(PasteAlign.none); + } + if (r == 0 || aligns[column] == PasteAlign.none) { + final align = _alignOf(cell, declarations); + if (align != PasteAlign.none) aligns[column] = align; + } + for (var c = 0; c < colspan; c++) { + cells.add(c == 0 ? inlines : const []); + if (rowspan > 1) { + pending[column] = (rows: rowspan - 1, value: inlines); + } + column++; + } + } + fillSpans(); + grid.add(cells); + } + while (grid.isNotEmpty && grid.last.every((cell) => !_hasWords(cell))) { + grid.removeLast(); + } + if (grid.isEmpty) return; + final width = grid.fold( + 0, + (most, row) => row.length > most ? row.length : most, + ); + if (width == 0) return; + for (final row in grid) { + while (row.length < width) { + row.add(const []); + } + } + while (aligns.length < width) { + aligns.add(PasteAlign.none); + } + final caption = table.querySelector('caption'); + if (caption != null) { + final words = _inlinesOf(caption, context.withStyle(PasteStyle.bold)); + if (_hasWords(words)) out.blocks.add(PasteParagraph(words)); + } + out.blocks.add(PasteTable(grid, aligns.sublist(0, width))); + } + + /// Rows that belong to this table, not to one nested in it. + static List _ownRows(dom.Element table) { + final rows = []; + void collect(dom.Element parent) { + for (final child in parent.children) { + switch (child.localName) { + case 'tr': + rows.add(child); + case 'thead' || 'tbody' || 'tfoot': + collect(child); + } + } + } + + collect(table); + return rows; + } + + /// A table used to lay a page out — an email's columns, a site's sidebar — + /// rather than to hold data. Its cells are read as the blocks they hold. + bool _isLayoutTable(dom.Element table, List rows) { + final role = table.attributes['role']; + if (role == 'presentation' || role == 'none') return true; + if (rows.isEmpty) return true; + if (table.querySelector('table') != null) return true; + var widest = 0; + for (final row in rows) { + final count = row.children + .where((cell) => cell.localName == 'td' || cell.localName == 'th') + .length; + if (count > widest) widest = count; + } + if (widest <= 1) return true; + // A cell holding headings or several paragraphs of prose is a column of + // a page, not a value. + for (final cell in table.querySelectorAll('td')) { + if (cell.querySelector('h1, h2, h3, h4, blockquote, pre') != null) { + return true; + } + } + return false; + } + + static PasteAlign _alignOf( + dom.Element cell, + Map declarations, + ) { + final value = (declarations['text-align'] ?? cell.attributes['align'] ?? '') + .toLowerCase(); + return switch (value) { + 'center' => PasteAlign.center, + 'right' || 'end' => PasteAlign.right, + 'left' || 'start' => PasteAlign.left, + _ => PasteAlign.none, + }; + } + + // --------------------------------------------------------------------- + // Images + + void _readImage(dom.Element image, _Context context, _Builder out) { + final alt = (image.attributes['alt'] ?? '').trim(); + if (_isEmojiImage(image, alt)) { + out.line.addText(alt, context); + return; + } + final declarations = _css.declarationsFor(image); + final width = + _pixels(image.attributes['width']) ?? _pixels(declarations['width']); + final height = + _pixels(image.attributes['height']) ?? _pixels(declarations['height']); + // Tracking pixels, spacers, favicons and the little icons beside a + // citation: furniture, not content. + if ((width != null && width <= 24) || (height != null && height <= 24)) { + return; + } + final source = _imageSource(image); + if (source == null) { + if (alt.isNotEmpty) out.line.addText(alt, context); + return; + } + if (source.contains('favicon')) return; + PasteImage picture; + if (source.startsWith('data:')) { + final decoded = _decodeDataUri(source); + if (decoded == null) { + if (alt.isNotEmpty) out.line.addText(alt, context); + return; + } + picture = PasteImage( + bytes: decoded.bytes, + mime: decoded.mime, + alt: alt, + width: width, + height: height, + ); + } else { + final resolved = _resolve(source); + final carried = + resources[source] ?? (resolved == null ? null : resources[resolved]); + if (carried != null) { + picture = PasteImage( + src: resolved, + bytes: carried.bytes, + mime: carried.mime, + alt: alt, + width: width, + height: height, + ); + } else if (resolved != null) { + picture = PasteImage( + src: resolved, + alt: alt, + width: width, + height: height, + ); + } else { + if (alt.isNotEmpty) out.line.addText(alt, context); + return; + } + } + out.line.add(PasteImageInline(picture, href: context.href)); + } + + String? _imageSource(dom.Element image) { + final attributes = image.attributes; + final src = attributes['src']; + // A lazy-loaded picture keeps a 1x1 stand-in in src and the real one in + // a data attribute until it scrolls into view. + final lazy = + attributes['data-src'] ?? + attributes['data-original'] ?? + attributes['data-lazy-src']; + if (lazy != null && + (src == null || src.startsWith('data:image/gif') || src.isEmpty)) { + return lazy; + } + if (src != null && src.isNotEmpty) return src; + final srcset = attributes['srcset']; + if (srcset != null && srcset.isNotEmpty) { + return srcset.split(',').last.trim().split(RegExp(r'\s+')).first; + } + return null; + } + + static bool _isEmojiImage(dom.Element image, String alt) { + final classes = image.className.toLowerCase(); + if (classes.contains('emoji') || + image.attributes.containsKey('data-stringify-emoji')) { + return alt.isNotEmpty; + } + // A picture whose alt text is a single emoji, or a :shortcode:, is an + // emoji drawn as a picture. + if (RegExp(r'^:[a-z0-9_+\-]+:$').hasMatch(alt)) return true; + return alt.isNotEmpty && + alt.runes.length <= 2 && + alt.runes.every((rune) => rune >= 0x2190); + } + + static ({Uint8List bytes, String mime})? _decodeDataUri(String uri) { + final comma = uri.indexOf(','); + if (comma < 0) return null; + final header = uri.substring(5, comma); + final mime = header.split(';').first.toLowerCase(); + if (!mime.startsWith('image/')) return null; + try { + final payload = uri.substring(comma + 1); + final bytes = header.contains(';base64') + ? base64.decode(payload.replaceAll(RegExp(r'\s'), '')) + : Uint8List.fromList(utf8.encode(Uri.decodeComponent(payload))); + if (bytes.isEmpty) return null; + return (bytes: bytes, mime: mime); + } on FormatException { + return null; + } + } + + static double? _pixels(String? value) { + if (value == null) return null; + final match = RegExp(r'^\s*([0-9.]+)\s*(px)?\s*$').firstMatch(value); + return match == null ? null : double.tryParse(match.group(1)!); + } + + // --------------------------------------------------------------------- + // Links + + String? _resolveLink(String? raw) { + if (raw == null) return null; + final href = raw.trim(); + if (href.isEmpty || href.startsWith('#')) return null; + final lower = href.toLowerCase(); + if (lower.startsWith('javascript:') || + lower.startsWith('data:') || + lower.startsWith('vbscript:')) { + return null; + } + final resolved = _resolve(href); + if (resolved == null) return null; + final uri = Uri.tryParse(resolved); + // Google wraps every link in a document in a redirect through itself. + if (uri != null && + (uri.host == 'www.google.com' || uri.host == 'google.com') && + uri.path == '/url') { + final target = uri.queryParameters['q'] ?? uri.queryParameters['url']; + if (target != null && target.startsWith('http')) return target; + } + return resolved; + } + + /// An absolute address for [raw], or null when it is relative and there is + /// nothing to resolve it against. + String? _resolve(String raw) { + final uri = Uri.tryParse(raw.trim()); + if (uri == null) return null; + if (uri.hasScheme) { + const allowed = {'http', 'https', 'mailto', 'tel', 'file', 'ftp'}; + return allowed.contains(uri.scheme.toLowerCase()) ? uri.toString() : null; + } + final base = baseUrl; + if (base == null) return null; + return base.resolveUri(uri).toString(); + } + + // --------------------------------------------------------------------- + // Math + + static bool _isMath(dom.Element element) { + final tag = element.localName; + if (tag == 'math' || tag == 'mjx-container') return true; + if (tag == 'script') { + final type = element.attributes['type'] ?? ''; + return type.startsWith('math/tex'); + } + final classes = element.classes; + return classes.contains('katex') || + classes.contains('katex-display') || + classes.contains('mwe-math-element') || + classes.contains('MathJax_Display') || + classes.contains('math-display') && + element.attributes.containsKey('data-latex') || + classes.contains('math-inline') && + element.attributes.containsKey('data-latex'); + } + + ({String tex, bool display})? _readMath(dom.Element element) { + final tag = element.localName; + final classes = element.classes; + if (tag == 'script') { + final tex = element.text.trim(); + if (tex.isEmpty) return null; + return ( + tex: tex, + display: (element.attributes['type'] ?? '').contains('display'), + ); + } + final latex = element.attributes['data-latex']; + if (latex != null && latex.trim().isNotEmpty) { + return (tex: latex.trim(), display: classes.contains('math-display')); + } + final annotation = element.querySelector( + 'annotation[encoding="application/x-tex"], annotation[encoding="TeX"]', + ); + final display = + classes.contains('katex-display') || + element.querySelector('.katex-display') != null || + element.attributes['display'] == 'true' || + element.attributes['display'] == 'block' || + element.querySelector('math[display="block"]') != null || + element.querySelector('.mwe-math-fallback-image-display') != null || + classes.contains('MathJax_Display'); + if (annotation != null && annotation.text.trim().isNotEmpty) { + return (tex: _cleanTex(annotation.text), display: display); + } + final math = tag == 'math' ? element : element.querySelector('math'); + final alttext = math?.attributes['alttext']; + if (alttext != null && alttext.trim().isNotEmpty) { + return (tex: _cleanTex(alttext), display: display); + } + final fallback = element.querySelector('img[alt]'); + final alt = fallback?.attributes['alt']; + if (alt != null && alt.trim().isNotEmpty) { + return (tex: _cleanTex(alt), display: display); + } + // MathML with no TeX anywhere: its text is the nearest honest thing. + final text = (math ?? element).text.replaceAll(RegExp(r'\s+'), ' ').trim(); + if (text.isEmpty) return null; + return (tex: text, display: display); + } + + static String _cleanTex(String raw) { + var tex = raw.trim(); + final wrapper = RegExp( + r'^\{\\(?:displaystyle|textstyle)\s*(.*)\}$', + dotAll: true, + ).firstMatch(tex); + if (wrapper != null) tex = wrapper.group(1)!.trim(); + return tex; + } + + // --------------------------------------------------------------------- + // Word + + /// Word writes a list as paragraphs, each saying in its style which list + /// and level it is on and carrying its bullet as text. Turns runs of them + /// into real lists before the walk, so the rest of the reader never has to + /// know. + void _rewriteWordLists(dom.Element root) { + final paragraphs = root + .querySelectorAll('p, h1, h2, h3, h4, h5, h6, div') + .where((p) => _msoListLevel(p) != null) + .toList(); + if (paragraphs.isEmpty) return; + var index = 0; + while (index < paragraphs.length) { + final first = paragraphs[index]; + final run = [first]; + var next = index + 1; + while (next < paragraphs.length && + identical(_nextElementSibling(run.last), paragraphs[next])) { + run.add(paragraphs[next]); + next++; + } + _replaceWithList(run); + index = next; + } + } + + static dom.Element? _nextElementSibling(dom.Element element) { + final parent = element.parent; + if (parent == null) return null; + final siblings = parent.children; + final at = siblings.indexOf(element); + return at >= 0 && at + 1 < siblings.length ? siblings[at + 1] : null; + } + + static int? _msoListLevel(dom.Element element) { + final style = element.attributes['style'] ?? ''; + final match = RegExp(r'mso-list:\s*l\d+\s+level(\d+)').firstMatch(style); + if (match == null) return null; + return int.tryParse(match.group(1)!); + } + + void _replaceWithList(List paragraphs) { + final parent = paragraphs.first.parent; + if (parent == null) return; + // (list element, level) from the outermost in. + final stack = <(dom.Element, int)>[]; + dom.Element? outermost; + for (final paragraph in paragraphs) { + final level = _msoListLevel(paragraph) ?? 1; + final marker = _takeWordMarker(paragraph); + final ordered = + marker != null && RegExp(r'^[0-9A-Za-z]{1,4}[.)]$').hasMatch(marker); + while (stack.isNotEmpty && stack.last.$2 > level) { + stack.removeLast(); + } + if (stack.isEmpty || stack.last.$2 < level) { + final list = dom.Element.tag(ordered ? 'ol' : 'ul'); + if (ordered) { + final number = int.tryParse(marker.substring(0, marker.length - 1)); + if (number != null && number != 1) { + list.attributes['start'] = '$number'; + } + } + if (stack.isEmpty) { + outermost = list; + } else { + final parentList = stack.last.$1; + final lastItem = parentList.children.isEmpty + ? null + : parentList.children.last; + (lastItem ?? parentList).append(list); + } + stack.add((list, level)); + } + final item = dom.Element.tag('li'); + for (final node in [...paragraph.nodes]) { + item.append(node); + } + stack.last.$1.append(item); + } + parent.insertBefore(outermost!, paragraphs.first); + for (final paragraph in paragraphs) { + paragraph.remove(); + } + } + + /// Removes the bullet or number Word wrote as text in front of an item, and + /// returns it. + static String? _takeWordMarker(dom.Element paragraph) { + for (final span in paragraph.querySelectorAll('span')) { + final style = span.attributes['style'] ?? ''; + if (style.contains('mso-list:Ignore') || + style.contains('mso-list: Ignore')) { + final text = span.text.replaceAll('\u00a0', ' ').trim(); + // The span that holds the marker can sit inside one that styles it. + var outer = span; + while (outer.parent != null && + !identical(outer.parent, paragraph) && + outer.parent!.text.replaceAll('\u00a0', ' ').trim() == text) { + outer = outer.parent!; + } + outer.remove(); + return text; + } + } + return null; + } + + /// A Word title or subtitle paragraph, which Word does not write as a + /// heading element. + static int? _wordTitleLevel(dom.Element paragraph) { + for (final name in paragraph.classes) { + if (name == 'MsoTitle') return 1; + if (name == 'MsoSubtitle') return 2; + final match = RegExp(r'^MsoHeading([1-6])$').firstMatch(name); + if (match != null) return int.parse(match.group(1)!); + } + return null; + } + + /// `` and its kind are not comments to an HTML5 + /// parser, which keeps the text between them; that text is the marker the + /// list rewrite reads, so only the conditional markers themselves go. + static String _withoutDownlevelConditionals(String html) => + html.replaceAll(RegExp(r''), ''); + + // --------------------------------------------------------------------- + + bool _droppedClass(dom.Element element) { + for (final name in element.classes) { + if (_droppedClasses.contains(name)) return true; + } + final testId = element.attributes['data-testid'] ?? ''; + if (testId.contains('copy') || testId.contains('citation-pill')) { + return element.localName == 'button' || testId.contains('copy'); + } + return false; + } + + static bool _hasWords(List inlines) => inlines.any( + (inline) => switch (inline) { + PasteText(:final text) => text.trim().isNotEmpty, + PasteBreak() => false, + _ => true, + }, + ); +} + +const _dropped = { + 'script', + 'style', + 'noscript', + 'template', + 'iframe', + 'object', + 'embed', + 'canvas', + 'video', + 'audio', + 'button', + 'select', + 'option', + 'textarea', + 'svg', + 'head', + 'title', + 'meta', + 'link', + 'base', + 'map', + 'area', + 'track', + 'source', + 'rp', + 'wbr', + 'clipboard-copy', + 'tool-tip', + 'o:p', + 'xml', + 'v:shape', + 'v:imagedata', +}; + +const _droppedClasses = { + // Wikipedia: section edit links, footnote marks, jump links. + 'mw-editsection', + 'reference', + 'mw-cite-backlink', + 'mw-jump-link', + 'noprint', + // GitHub: the link icon beside every heading, copy buttons. + 'anchor', + 'octicon', + 'zeroclipboard-container', + // Copy buttons and code-block toolbars of AI chats and docs sites. + 'copy-button', + 'copy-code-button', + 'code-block-decoration', +}; + +const _screenReaderOnly = { + 'sr-only', + 'visually-hidden', + 'visuallyhidden', + 'screen-reader-text', + 'screenreader-only', + 'a11y-hidden', + 'assistive-text', +}; + +const _blockTags = { + 'address', + 'article', + 'aside', + 'blockquote', + 'body', + 'caption', + 'center', + 'dd', + 'details', + 'dialog', + 'div', + 'dl', + 'dt', + 'fieldset', + 'figcaption', + 'figure', + 'footer', + 'form', + 'h1', + 'h2', + 'h3', + 'h4', + 'h5', + 'h6', + 'header', + 'hgroup', + 'hr', + 'html', + 'legend', + 'li', + 'main', + 'nav', + 'ol', + 'p', + 'pre', + 'section', + 'summary', + 'table', + 'tbody', + 'td', + 'tfoot', + 'th', + 'thead', + 'tr', + 'ul', +}; + +const _notLanguages = { + 'copy', + 'copy code', + 'copied', + 'copied!', + 'code', + 'edit', + 'run', + 'download', + 'wrap', + 'expand', + 'collapse', + 'preview', +}; + +/// What the walk carries down the tree: the emphasis in force, the link the +/// words are inside, and whether white space is kept as written. +class _Context { + const _Context({this.styles = const {}, this.href, this.pre = false}); + + final Set styles; + final String? href; + final bool pre; + + _Context withStyle(PasteStyle style) => + _Context(styles: {...styles, style}, href: href, pre: pre); +} + +class _Builder { + final List blocks = []; + final _Line line = _Line(); + + void flush() { + final inlines = line.take(); + if (inlines.isNotEmpty) blocks.add(PasteParagraph(inlines)); + } +} + +/// The words of the block being read, with HTML's white space rules applied +/// as they arrive: runs of spaces and line breaks in the markup are one +/// space, and none at all at the start of a line. +class _Line { + final List _inlines = []; + + bool get _atLineStart => + _inlines.isEmpty || + _inlines.last is PasteBreak || + (_inlines.last is PasteText && + (_inlines.last as PasteText).text.endsWith(' ')); + + void addText(String raw, _Context context) { + var text = raw.replaceAll('\r\n', '\n').replaceAll('\r', '\n'); + text = text.replaceAll(RegExp('[\u200b\ufeff\u00ad\ufffc]'), ''); + if (context.pre) { + final lines = text.split('\n'); + for (var i = 0; i < lines.length; i++) { + if (i > 0) _inlines.add(const PasteBreak()); + _append(lines[i].replaceAll('\u00a0', ' '), context); + } + return; + } + text = text.replaceAll(RegExp(r'[ \t\n\f]+'), ' '); + if (text.startsWith(' ') && _atLineStart) text = text.substring(1); + text = text.replaceAll('\u00a0', ' '); + if (text.isEmpty) return; + _append(text, context); + } + + void _append(String text, _Context context) { + if (text.isEmpty) return; + final last = _inlines.isEmpty ? null : _inlines.last; + if (last is PasteText && + last.href == context.href && + _sameStyles(last.styles, context.styles)) { + _inlines[_inlines.length - 1] = PasteText( + last.text + text, + styles: last.styles, + href: last.href, + ); + return; + } + _inlines.add(PasteText(text, styles: context.styles, href: context.href)); + } + + void addBreak(_Context context) { + _trimTrailingSpace(); + _inlines.add(const PasteBreak()); + } + + void add(PasteInline inline) => _inlines.add(inline); + + void _trimTrailingSpace() { + if (_inlines.isEmpty) return; + final last = _inlines.last; + if (last is PasteText && last.text.endsWith(' ')) { + final trimmed = last.text.trimRight(); + if (trimmed.isEmpty) { + _inlines.removeLast(); + } else { + _inlines[_inlines.length - 1] = PasteText( + trimmed, + styles: last.styles, + href: last.href, + ); + } + } + } + + List take() { + _trimTrailingSpace(); + while (_inlines.isNotEmpty && _inlines.last is PasteBreak) { + _inlines.removeLast(); + _trimTrailingSpace(); + } + while (_inlines.isNotEmpty && _inlines.first is PasteBreak) { + _inlines.removeAt(0); + } + if (_inlines.isNotEmpty && _inlines.first is PasteText) { + final first = _inlines.first as PasteText; + final trimmed = first.text.trimLeft(); + if (trimmed.isEmpty) { + _inlines.removeAt(0); + } else if (trimmed.length != first.text.length) { + _inlines[0] = PasteText( + trimmed, + styles: first.styles, + href: first.href, + ); + } + } + final hasContent = _inlines.any( + (inline) => + inline is! PasteBreak && + (inline is! PasteText || inline.text.trim().isNotEmpty), + ); + final result = hasContent + ? List.of(_inlines) + : const []; + _inlines.clear(); + return result; + } + + static bool _sameStyles(Set a, Set b) => + a.length == b.length && a.containsAll(b); +} + +/// The few CSS rules clipboard HTML relies on: Apple's RTF conversion, Excel +/// and Word put bold and italic in classes defined in a style block. +class _CssSheet { + final Map> _byClass = {}; + final Map> _byTagClass = {}; + final Map> _cache = {}; + + static final _rule = RegExp(r'([^{}]+)\{([^}]*)\}'); + + void add(String css) { + final source = css.replaceAll(RegExp(r'/\*.*?\*/', dotAll: true), ''); + for (final match in _rule.allMatches(source)) { + final declarations = parseDeclarations(match.group(2)!); + if (declarations.isEmpty) continue; + for (final raw in match.group(1)!.split(',')) { + final selector = raw.trim(); + final simple = RegExp( + r'^([a-zA-Z0-9]*)\.([A-Za-z0-9_-]+)$', + ).firstMatch(selector); + if (simple == null) continue; + final tag = simple.group(1)!.toLowerCase(); + final name = simple.group(2)!; + final target = tag.isEmpty + ? _byClass.putIfAbsent(name, () => {}) + : _byTagClass.putIfAbsent('$tag.$name', () => {}); + target.addAll(declarations); + } + } + } + + Map declarationsFor(dom.Element element) { + final cached = _cache[element]; + if (cached != null) return cached; + final result = {}; + if (_byClass.isNotEmpty || _byTagClass.isNotEmpty) { + for (final name in element.classes) { + result.addAll(_byClass[name] ?? const {}); + result.addAll(_byTagClass['${element.localName}.$name'] ?? const {}); + } + } + final inline = element.attributes['style']; + if (inline != null) result.addAll(parseDeclarations(inline)); + return _cache[element] = result; + } + + static Map parseDeclarations(String block) { + final result = {}; + for (final part in block.split(';')) { + final colon = part.indexOf(':'); + if (colon <= 0) continue; + final name = part.substring(0, colon).trim().toLowerCase(); + var value = part.substring(colon + 1).trim().toLowerCase(); + value = value.replaceAll('!important', '').trim(); + if (name.isEmpty || value.isEmpty) continue; + result[name] = value; + } + return result; + } +} diff --git a/lib/paste/paste_tree.dart b/lib/paste/paste_tree.dart new file mode 100644 index 0000000..3cc6f7f --- /dev/null +++ b/lib/paste/paste_tree.dart @@ -0,0 +1,180 @@ +import 'dart:typed_data'; + +/// What a paste reads as, between the clipboard's markup and the note. +/// +/// Every source — a page's HTML, the HTML Apple builds from RTF, the +/// markdown an AI's copy button writes — is read into this one shape, and +/// the note's text is written from it: as markdown in a markdown note, as +/// the editor's own plain format in one that is not. Keeping the reading and +/// the writing apart is what lets the two writers agree on what a paste +/// contains while disagreeing about how to say it. +sealed class PasteBlock { + const PasteBlock(); +} + +final class PasteParagraph extends PasteBlock { + const PasteParagraph(this.inlines); + + final List inlines; +} + +final class PasteHeading extends PasteBlock { + const PasteHeading(this.level, this.inlines); + + /// 1 to 6. + final int level; + final List inlines; +} + +final class PasteList extends PasteBlock { + const PasteList({required this.ordered, required this.items, this.start = 1}); + + final bool ordered; + + /// The first item's number, for an ordered list. + final int start; + final List items; +} + +final class PasteListItem { + const PasteListItem(this.blocks, {this.checked}); + + final List blocks; + + /// Null for an ordinary item; true or false for a to-do. + final bool? checked; +} + +final class PasteQuote extends PasteBlock { + const PasteQuote(this.blocks); + + final List blocks; +} + +final class PasteCode extends PasteBlock { + const PasteCode(this.text, {this.language}); + + final String text; + + /// The info string a fence carries: `python`, `ts`. Null when the source + /// did not say. + final String? language; +} + +enum PasteAlign { none, left, center, right } + +final class PasteTable extends PasteBlock { + const PasteTable(this.rows, this.aligns); + + /// The first row is the header. Every row has [aligns].length cells; a + /// merged cell's value is in its first cell and the rest are empty. + final List>> rows; + final List aligns; +} + +final class PasteRule extends PasteBlock { + const PasteRule(); +} + +final class PasteMathBlock extends PasteBlock { + const PasteMathBlock(this.tex); + + final String tex; +} + +/// One run of a block's words. +sealed class PasteInline { + const PasteInline(); +} + +enum PasteStyle { bold, italic, underline, strike, superscript, subscript } + +final class PasteText extends PasteInline { + const PasteText(this.text, {this.styles = const {}, this.href}); + + final String text; + final Set styles; + + /// Where the words link to, already resolved against the page. + final String? href; +} + +final class PasteCodeSpan extends PasteInline { + const PasteCodeSpan(this.code, {this.href}); + + final String code; + final String? href; +} + +final class PasteMath extends PasteInline { + const PasteMath(this.tex); + + final String tex; +} + +/// A line break inside a block. +final class PasteBreak extends PasteInline { + const PasteBreak(); +} + +final class PasteImageInline extends PasteInline { + const PasteImageInline(this.image, {this.href}); + + final PasteImage image; + final String? href; +} + +/// A picture the paste carries, before it is anything in the note. +/// +/// Either its bytes came with the clipboard — a data URI, an RTFD attachment, +/// a Safari web archive's resource — or [src] says where to fetch them. +class PasteImage { + const PasteImage({ + this.src, + this.bytes, + this.mime, + this.alt = '', + this.width, + this.height, + }); + + /// An absolute http(s) or file URL, or null when [bytes] are here. + final String? src; + final Uint8List? bytes; + final String? mime; + final String alt; + + /// The size the source displayed it at, in CSS pixels, when it said. + final double? width; + final double? height; + + bool get hasBytes => bytes != null && bytes!.isNotEmpty; + + /// How much of the writing column the picture takes: its displayed width + /// against a 720px column, so a logo stays a logo and a figure stays wide. + double get widthFactor { + final shown = width; + if (shown == null || shown <= 0) return 1; + return (shown / referenceColumnWidth).clamp(0.25, 1.0); + } + + static const double referenceColumnWidth = 720; + + Map toMessage() => { + 'src': src, + 'bytes': bytes, + 'mime': mime, + 'alt': alt, + 'width': width, + 'height': height, + }; + + static PasteImage fromMessage(Map raw) => PasteImage( + src: raw['src'] as String?, + bytes: raw['bytes'] as Uint8List?, + mime: raw['mime'] as String?, + alt: raw['alt'] as String? ?? '', + width: (raw['width'] as num?)?.toDouble(), + height: (raw['height'] as num?)?.toDouble(), + ); +} diff --git a/pubspec.lock b/pubspec.lock index db5ba9f..7f70e61 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -568,7 +568,7 @@ packages: source: hosted version: "0.2.0" html: - dependency: transitive + dependency: "direct main" description: name: html sha256: "43b67b8f43321ab066817dfac5619596c98bb1b61624e77203bb4351785f9699" diff --git a/pubspec.yaml b/pubspec.yaml index 727ae05..d9a484a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,6 +52,10 @@ dependencies: # user hand-edited from one it wrote itself. Already present transitively; # named here because this is a direct use. crypto: ^3.0.7 + # Reading the HTML other apps put on the clipboard, so a paste keeps its + # headings, lists, tables, links and pictures. Already present + # transitively; named here because this is a direct use. + html: ^0.15.7 # The save and open dialogs export and import need. Flutter-team federated # plugin; `getSaveLocation` is desktop-only, and the mobile path writes into # the app's own documents directory instead. From 0216d16d778badd93089cf4bf1e7305eb0af73e5 Mon Sep 17 00:00:00 2001 From: Sanjay Kholiya Date: Mon, 16 Nov 2026 14:02:22 +0530 Subject: [PATCH 02/40] Add paste fixture corpus and capture tooling Real headless-Chrome captures (Wikipedia, ChatGPT, Gemini, Perplexity, GitHub, Google Docs/Sheets, MDN, Ask Ubuntu, emoji), synthesized native-app payloads (Word, Outlook, Excel, Numbers, Apple Notes, Pages, Safari, Slack, Gmail, VS Code, Finder, images), and the scripts that produced them, including a read-only macOS pasteboard capture script. --- test/fixtures/paste/INDEX.md | 66 + .../com.apple.flat-rtfd.rtfd | Bin 0 -> 4312 bytes .../fixture.json | 31 + .../public.html.html | 43 + .../public.rtf.rtf | 38 + .../public.utf8-plain-text.txt | 12 + .../code-c-spanish-copy-button/fixture.json | 24 + .../public.html.html | 224 ++ .../public.utf8-plain-text.txt | 296 +++ .../code-c-spanish-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../code-c-spanish-selection/public.html.html | 224 ++ .../public.utf8-plain-text.txt | 262 +++ .../code-tables-php-copy-button/fixture.json | 24 + .../public.html.html | 2 + .../public.utf8-plain-text.txt | 203 ++ .../code-tables-php-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 176 ++ .../public.utf8-plain-text.txt | 185 ++ .../fixture.json | 24 + .../public.html.html | 266 +++ .../public.utf8-plain-text.txt | 1627 ++++++++++++++ .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 179 ++ .../public.utf8-plain-text.txt | 1919 ++++++++++++++++ .../mac-app-copy-button-guess/fixture.json | 19 + .../public.utf8-plain-text.txt | 22 + .../fixture.json | 24 + .../public.html.html | 47 + .../public.utf8-plain-text.txt | 235 ++ .../math-number-theory-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 1011 +++++++++ .../fixture.json | 24 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 15 + .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 46 + .../fixture.json | 24 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 126 ++ .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 111 + .../fixture.json | 24 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 475 ++++ .../nested-lists-emoji-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 374 ++++ .../fixture.json | 24 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 124 ++ .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 131 ++ .../mac-app-copy-button-guess/fixture.json | 19 + .../public.utf8-plain-text.txt | 22 + .../paste/excel/budget-range-mac/fixture.json | 27 + .../excel/budget-range-mac/public.html.html | 146 ++ .../excel/budget-range-mac/public.png.png | Bin 0 -> 893 bytes .../public.utf8-plain-text.txt | 6 + .../budget-range-windows/CF_UNICODETEXT.txt | 6 + .../paste/excel/budget-range-windows/Csv.csv | 6 + .../budget-range-windows/HTML_Format.html | 151 ++ .../paste/excel/budget-range-windows/PNG.png | Bin 0 -> 893 bytes .../excel/budget-range-windows/fixture.json | 31 + .../NSFilenamesPboardType.plist | 8 + .../finder/two-files-copied/fixture.json | 43 + .../item1.public.file-url.txt | 1 + .../item1.public.utf8-plain-text.txt | 1 + .../two-files-copied/kapy-sample-chart.png | Bin 0 -> 452 bytes .../two-files-copied/kapy-sample-notes.txt | 2 + .../two-files-copied/public.file-url.txt | 1 + .../public.utf8-plain-text.txt | 1 + .../gemini/code-tables-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../code-tables-selection/public.html.html | 1 + .../public.utf8-plain-text.txt | 57 + .../gemini/math-heavy-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../math-heavy-selection/public.html.html | 7 + .../public.utf8-plain-text.txt | 219 ++ .../gemini/math-tables-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../math-tables-selection/public.html.html | 1 + .../public.utf8-plain-text.txt | 103 + .../nested-lists-long-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 3 + .../public.utf8-plain-text.txt | 85 + .../github/issue-body-task-list/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../issue-body-task-list/public.html.html | 1 + .../public.utf8-plain-text.txt | 65 + .../github/pr-body-checklist/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../github/pr-body-checklist/public.html.html | 1 + .../public.utf8-plain-text.txt | 6 + .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 19 + .../public.utf8-plain-text.txt | 154 ++ .../fixture.json | 27 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 19 + .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 57 + .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 638 ++++++ .../vmware-com1-images-table/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../vmware-com1-images-table/public.html.html | 1 + .../public.utf8-plain-text.txt | 370 ++++ .../class-data-plain-view-only/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 6 + .../fixture.json | 27 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 5 + .../merged-cells-view-only/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../merged-cells-view-only/public.html.html | 1 + .../public.utf8-plain-text.txt | 14 + .../styled-list-view-only/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../styled-list-view-only/public.html.html | 1 + .../public.utf8-plain-text.txt | 10 + .../image/single-image-png-tiff/fixture.json | 23 + .../single-image-png-tiff/public.png.png | Bin 0 -> 2046 bytes .../single-image-png-tiff/public.tiff.tiff | Bin 0 -> 259382 bytes .../paste/numbers/table-cells/fixture.json | 27 + .../numbers/table-cells/public.html.html | 67 + .../paste/numbers/table-cells/public.rtf.rtf | 18 + .../table-cells/public.utf8-plain-text.txt | 4 + .../paste/outlook/email-body-mac/fixture.json | 23 + .../outlook/email-body-mac/public.html.html | 96 + .../email-body-mac/public.utf8-plain-text.txt | 11 + .../email-body-windows/CF_UNICODETEXT.txt | 11 + .../email-body-windows/HTML_Format.html | 101 + .../email-body-windows/Rich_Text_Format.rtf | 1 + .../outlook/email-body-windows/fixture.json | 27 + .../com.apple.flat-rtfd.rtfd | Bin 0 -> 4331 bytes .../fixture.json | 27 + .../public.rtf.rtf | 38 + .../public.utf8-plain-text.txt | 12 + .../perplexity/code-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../code-selection/public.html.html | 18 + .../code-selection/public.utf8-plain-text.txt | 54 + .../nested-lists-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../nested-lists-selection/public.html.html | 1 + .../public.utf8-plain-text.txt | 48 + .../table-links-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../table-links-selection/public.html.html | 1 + .../public.utf8-plain-text.txt | 23 + .../table-math-selection/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../table-math-selection/public.html.html | 4 + .../public.utf8-plain-text.txt | 65 + .../com.apple.flat-rtfd.rtfd | Bin 0 -> 2200 bytes .../com.apple.webarchive.webarchive | Bin 0 -> 3370 bytes .../fixture.json | 35 + .../public.html.html | 1 + .../public.rtf.rtf | 14 + .../public.utf8-plain-text.txt | 4 + .../fixture.json | 23 + .../public.html.html | 2 + .../public.utf8-plain-text.txt | 10 + .../askubuntu-answer-code-blocks/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 52 + .../vscode/python-selection/fixture.json | 31 + .../org.chromium.web-custom-data.bin | Bin 0 -> 216 bytes .../vscode/python-selection/public.html.html | 1 + .../public.utf8-plain-text.txt | 9 + .../python-selection/vscode-editor-data.json | 1 + .../vscode/typescript-selection/fixture.json | 31 + .../org.chromium.web-custom-data.bin | Bin 0 -> 224 bytes .../typescript-selection/public.html.html | 1 + .../public.utf8-plain-text.txt | 5 + .../vscode-editor-data.json | 1 + .../getemoji-people-skin-tones/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 645 ++++++ .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 72 + .../public.utf8-plain-text.txt | 328 +++ .../wikipedia/cairo-arabic-rtl/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../cairo-arabic-rtl/public.html.html | 1 + .../public.utf8-plain-text.txt | 10 + .../einstein-infobox-lead/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../einstein-infobox-lead/public.html.html | 1 + .../public.utf8-plain-text.txt | 70 + .../fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 23 + .../periodic-table-merged-cells/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 11 + .../quadratic-formula-derivation/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../public.html.html | 1 + .../public.utf8-plain-text.txt | 1971 +++++++++++++++++ .../quadratic-formula-math/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../quadratic-formula-math/public.html.html | 1 + .../public.utf8-plain-text.txt | 48 + .../roman-emperors-long/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../roman-emperors-long/public.html.html | 2 + .../public.utf8-plain-text.txt | 1660 ++++++++++++++ .../tokyo-climate-table-cjk/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../tokyo-climate-table-cjk/public.html.html | 1 + .../public.utf8-plain-text.txt | 120 + .../wikipedia/tokyo-japanese/fixture.json | 28 + .../org.chromium.source-url.txt | 1 + .../wikipedia/tokyo-japanese/public.html.html | 1 + .../tokyo-japanese/public.utf8-plain-text.txt | 11 + .../clip_image001.png | Bin 0 -> 607 bytes .../mixed-lists-table-image-mac/fixture.json | 30 + .../public.html.html | 286 +++ .../public.rtf.rtf | 37 + .../public.utf8-plain-text.txt | 13 + .../CF_UNICODETEXT.txt | 13 + .../HTML_Format.html | 291 +++ .../Rich_Text_Format.rtf | 37 + .../clip_image001.png | Bin 0 -> 607 bytes .../fixture.json | 30 + tool/paste_fixtures/README.md | 81 + tool/paste_fixtures/capture_macos.swift | 142 ++ tool/paste_fixtures/check_cf_html.mjs | 23 + tool/paste_fixtures/headless/.gitignore | 1 + tool/paste_fixtures/headless/apply_notes.mjs | 17 + tool/paste_fixtures/headless/capture.mjs | 63 + tool/paste_fixtures/headless/gcapture.mjs | 43 + tool/paste_fixtures/headless/lib.mjs | 78 + tool/paste_fixtures/headless/notes.json | 47 + tool/paste_fixtures/headless/package.json | 9 + tool/paste_fixtures/headless/run_google.sh | 15 + tool/paste_fixtures/headless/scan.mjs | 32 + .../headless/specs/ai-helpers.mjs | 19 + .../paste_fixtures/headless/specs/chatgpt.mjs | 33 + .../headless/specs/gemini-perplexity.mjs | 34 + .../paste_fixtures/headless/specs/helpers.mjs | 14 + tool/paste_fixtures/headless/specs/web.mjs | 40 + .../headless/specs/wikipedia.mjs | 53 + .../headless/watch_changecount.sh | 9 + tool/paste_fixtures/synthesize/apple.swift | 198 ++ tool/paste_fixtures/synthesize/apps.mjs | 134 ++ tool/paste_fixtures/synthesize/common.mjs | 100 + tool/paste_fixtures/synthesize/sheets.mjs | 293 +++ tool/paste_fixtures/synthesize/word.mjs | 484 ++++ tool/paste_fixtures/test_capture_macos.swift | 28 + tool/paste_fixtures/validate.mjs | 40 + 280 files changed, 20808 insertions(+) create mode 100644 test/fixtures/paste/INDEX.md create mode 100644 test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/com.apple.flat-rtfd.rtfd create mode 100644 test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/fixture.json create mode 100644 test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.html.html create mode 100644 test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.rtf.rtf create mode 100644 test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/code-c-spanish-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/code-c-spanish-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/code-c-spanish-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/code-c-spanish-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/code-c-spanish-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/code-tables-php-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/code-tables-php-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/code-tables-php-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/code-tables-php-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/code-tables-php-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/long-math-code-tables-images-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/long-math-code-tables-images-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/long-math-code-tables-images-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/long-math-code-tables-images-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/long-math-code-tables-images-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/long-math-code-tables-images-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/long-math-code-tables-images-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/mac-app-copy-button-guess/fixture.json create mode 100644 test/fixtures/paste/chatgpt/mac-app-copy-button-guess/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/math-number-theory-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/math-number-theory-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/math-number-theory-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/math-number-theory-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/math-number-theory-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/math-number-theory-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/math-number-theory-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/math-stochastic-processes-german-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/math-stochastic-processes-german-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/math-stochastic-processes-german-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/math-stochastic-processes-german-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/math-stochastic-processes-german-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/math-stochastic-processes-german-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/math-stochastic-processes-german-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-2-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-2-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-2-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-2-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-2-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-2-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-2-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/nested-lists-emoji-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/table-math-headings-copy-button/fixture.json create mode 100644 test/fixtures/paste/chatgpt/table-math-headings-copy-button/public.html.html create mode 100644 test/fixtures/paste/chatgpt/table-math-headings-copy-button/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/chatgpt/table-math-headings-selection/fixture.json create mode 100644 test/fixtures/paste/chatgpt/table-math-headings-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/chatgpt/table-math-headings-selection/public.html.html create mode 100644 test/fixtures/paste/chatgpt/table-math-headings-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/claude/mac-app-copy-button-guess/fixture.json create mode 100644 test/fixtures/paste/claude/mac-app-copy-button-guess/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/excel/budget-range-mac/fixture.json create mode 100644 test/fixtures/paste/excel/budget-range-mac/public.html.html create mode 100644 test/fixtures/paste/excel/budget-range-mac/public.png.png create mode 100644 test/fixtures/paste/excel/budget-range-mac/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/excel/budget-range-windows/CF_UNICODETEXT.txt create mode 100644 test/fixtures/paste/excel/budget-range-windows/Csv.csv create mode 100644 test/fixtures/paste/excel/budget-range-windows/HTML_Format.html create mode 100644 test/fixtures/paste/excel/budget-range-windows/PNG.png create mode 100644 test/fixtures/paste/excel/budget-range-windows/fixture.json create mode 100644 test/fixtures/paste/finder/two-files-copied/NSFilenamesPboardType.plist create mode 100644 test/fixtures/paste/finder/two-files-copied/fixture.json create mode 100644 test/fixtures/paste/finder/two-files-copied/item1.public.file-url.txt create mode 100644 test/fixtures/paste/finder/two-files-copied/item1.public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/finder/two-files-copied/kapy-sample-chart.png create mode 100644 test/fixtures/paste/finder/two-files-copied/kapy-sample-notes.txt create mode 100644 test/fixtures/paste/finder/two-files-copied/public.file-url.txt create mode 100644 test/fixtures/paste/finder/two-files-copied/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/gemini/code-tables-selection/fixture.json create mode 100644 test/fixtures/paste/gemini/code-tables-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/gemini/code-tables-selection/public.html.html create mode 100644 test/fixtures/paste/gemini/code-tables-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/gemini/math-heavy-selection/fixture.json create mode 100644 test/fixtures/paste/gemini/math-heavy-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/gemini/math-heavy-selection/public.html.html create mode 100644 test/fixtures/paste/gemini/math-heavy-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/gemini/math-tables-selection/fixture.json create mode 100644 test/fixtures/paste/gemini/math-tables-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/gemini/math-tables-selection/public.html.html create mode 100644 test/fixtures/paste/gemini/math-tables-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/gemini/nested-lists-long-selection/fixture.json create mode 100644 test/fixtures/paste/gemini/nested-lists-long-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/gemini/nested-lists-long-selection/public.html.html create mode 100644 test/fixtures/paste/gemini/nested-lists-long-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/github/issue-body-task-list/fixture.json create mode 100644 test/fixtures/paste/github/issue-body-task-list/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/github/issue-body-task-list/public.html.html create mode 100644 test/fixtures/paste/github/issue-body-task-list/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/github/pr-body-checklist/fixture.json create mode 100644 test/fixtures/paste/github/pr-body-checklist/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/github/pr-body-checklist/public.html.html create mode 100644 test/fixtures/paste/github/pr-body-checklist/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/github/readme-got-headings-tables-code/fixture.json create mode 100644 test/fixtures/paste/github/readme-got-headings-tables-code/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/github/readme-got-headings-tables-code/public.html.html create mode 100644 test/fixtures/paste/github/readme-got-headings-tables-code/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/gmail/html-email-layout-tables-quote-signature/fixture.json create mode 100644 test/fixtures/paste/gmail/html-email-layout-tables-quote-signature/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/gmail/html-email-layout-tables-quote-signature/public.html.html create mode 100644 test/fixtures/paste/gmail/html-email-layout-tables-quote-signature/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/google-docs/trace-event-format-toc-and-intro/fixture.json create mode 100644 test/fixtures/paste/google-docs/trace-event-format-toc-and-intro/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/google-docs/trace-event-format-toc-and-intro/public.html.html create mode 100644 test/fixtures/paste/google-docs/trace-event-format-toc-and-intro/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/google-docs/trace-event-format-whole-document/fixture.json create mode 100644 test/fixtures/paste/google-docs/trace-event-format-whole-document/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/google-docs/trace-event-format-whole-document/public.html.html create mode 100644 test/fixtures/paste/google-docs/trace-event-format-whole-document/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/google-docs/vmware-com1-images-table/fixture.json create mode 100644 test/fixtures/paste/google-docs/vmware-com1-images-table/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/google-docs/vmware-com1-images-table/public.html.html create mode 100644 test/fixtures/paste/google-docs/vmware-com1-images-table/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/google-sheets/class-data-plain-view-only/fixture.json create mode 100644 test/fixtures/paste/google-sheets/class-data-plain-view-only/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/google-sheets/class-data-plain-view-only/public.html.html create mode 100644 test/fixtures/paste/google-sheets/class-data-plain-view-only/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/google-sheets/merged-cells-formulas-synthesized/fixture.json create mode 100644 test/fixtures/paste/google-sheets/merged-cells-formulas-synthesized/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/google-sheets/merged-cells-formulas-synthesized/public.html.html create mode 100644 test/fixtures/paste/google-sheets/merged-cells-formulas-synthesized/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/google-sheets/merged-cells-view-only/fixture.json create mode 100644 test/fixtures/paste/google-sheets/merged-cells-view-only/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/google-sheets/merged-cells-view-only/public.html.html create mode 100644 test/fixtures/paste/google-sheets/merged-cells-view-only/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/google-sheets/styled-list-view-only/fixture.json create mode 100644 test/fixtures/paste/google-sheets/styled-list-view-only/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/google-sheets/styled-list-view-only/public.html.html create mode 100644 test/fixtures/paste/google-sheets/styled-list-view-only/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/image/single-image-png-tiff/fixture.json create mode 100644 test/fixtures/paste/image/single-image-png-tiff/public.png.png create mode 100644 test/fixtures/paste/image/single-image-png-tiff/public.tiff.tiff create mode 100644 test/fixtures/paste/numbers/table-cells/fixture.json create mode 100644 test/fixtures/paste/numbers/table-cells/public.html.html create mode 100644 test/fixtures/paste/numbers/table-cells/public.rtf.rtf create mode 100644 test/fixtures/paste/numbers/table-cells/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/outlook/email-body-mac/fixture.json create mode 100644 test/fixtures/paste/outlook/email-body-mac/public.html.html create mode 100644 test/fixtures/paste/outlook/email-body-mac/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/outlook/email-body-windows/CF_UNICODETEXT.txt create mode 100644 test/fixtures/paste/outlook/email-body-windows/HTML_Format.html create mode 100644 test/fixtures/paste/outlook/email-body-windows/Rich_Text_Format.rtf create mode 100644 test/fixtures/paste/outlook/email-body-windows/fixture.json create mode 100644 test/fixtures/paste/pages/document-selection-lists-image/com.apple.flat-rtfd.rtfd create mode 100644 test/fixtures/paste/pages/document-selection-lists-image/fixture.json create mode 100644 test/fixtures/paste/pages/document-selection-lists-image/public.rtf.rtf create mode 100644 test/fixtures/paste/pages/document-selection-lists-image/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/perplexity/code-selection/fixture.json create mode 100644 test/fixtures/paste/perplexity/code-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/perplexity/code-selection/public.html.html create mode 100644 test/fixtures/paste/perplexity/code-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/perplexity/nested-lists-selection/fixture.json create mode 100644 test/fixtures/paste/perplexity/nested-lists-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/perplexity/nested-lists-selection/public.html.html create mode 100644 test/fixtures/paste/perplexity/nested-lists-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/perplexity/table-links-selection/fixture.json create mode 100644 test/fixtures/paste/perplexity/table-links-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/perplexity/table-links-selection/public.html.html create mode 100644 test/fixtures/paste/perplexity/table-links-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/perplexity/table-math-selection/fixture.json create mode 100644 test/fixtures/paste/perplexity/table-math-selection/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/perplexity/table-math-selection/public.html.html create mode 100644 test/fixtures/paste/perplexity/table-math-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/safari/selection-with-image-webarchive/com.apple.flat-rtfd.rtfd create mode 100644 test/fixtures/paste/safari/selection-with-image-webarchive/com.apple.webarchive.webarchive create mode 100644 test/fixtures/paste/safari/selection-with-image-webarchive/fixture.json create mode 100644 test/fixtures/paste/safari/selection-with-image-webarchive/public.html.html create mode 100644 test/fixtures/paste/safari/selection-with-image-webarchive/public.rtf.rtf create mode 100644 test/fixtures/paste/safari/selection-with-image-webarchive/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/slack/message-formatting-emoji-code/fixture.json create mode 100644 test/fixtures/paste/slack/message-formatting-emoji-code/public.html.html create mode 100644 test/fixtures/paste/slack/message-formatting-emoji-code/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/stackexchange/askubuntu-answer-code-blocks/fixture.json create mode 100644 test/fixtures/paste/stackexchange/askubuntu-answer-code-blocks/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/stackexchange/askubuntu-answer-code-blocks/public.html.html create mode 100644 test/fixtures/paste/stackexchange/askubuntu-answer-code-blocks/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/vscode/python-selection/fixture.json create mode 100644 test/fixtures/paste/vscode/python-selection/org.chromium.web-custom-data.bin create mode 100644 test/fixtures/paste/vscode/python-selection/public.html.html create mode 100644 test/fixtures/paste/vscode/python-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/vscode/python-selection/vscode-editor-data.json create mode 100644 test/fixtures/paste/vscode/typescript-selection/fixture.json create mode 100644 test/fixtures/paste/vscode/typescript-selection/org.chromium.web-custom-data.bin create mode 100644 test/fixtures/paste/vscode/typescript-selection/public.html.html create mode 100644 test/fixtures/paste/vscode/typescript-selection/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/vscode/typescript-selection/vscode-editor-data.json create mode 100644 test/fixtures/paste/web/getemoji-people-skin-tones/fixture.json create mode 100644 test/fixtures/paste/web/getemoji-people-skin-tones/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/web/getemoji-people-skin-tones/public.html.html create mode 100644 test/fixtures/paste/web/getemoji-people-skin-tones/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/web/mdn-html-images-relative-urls/fixture.json create mode 100644 test/fixtures/paste/web/mdn-html-images-relative-urls/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/web/mdn-html-images-relative-urls/public.html.html create mode 100644 test/fixtures/paste/web/mdn-html-images-relative-urls/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/cairo-arabic-rtl/fixture.json create mode 100644 test/fixtures/paste/wikipedia/cairo-arabic-rtl/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/cairo-arabic-rtl/public.html.html create mode 100644 test/fixtures/paste/wikipedia/cairo-arabic-rtl/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/einstein-infobox-lead/fixture.json create mode 100644 test/fixtures/paste/wikipedia/einstein-infobox-lead/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/einstein-infobox-lead/public.html.html create mode 100644 test/fixtures/paste/wikipedia/einstein-infobox-lead/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/einstein-section-with-edit-links/fixture.json create mode 100644 test/fixtures/paste/wikipedia/einstein-section-with-edit-links/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/einstein-section-with-edit-links/public.html.html create mode 100644 test/fixtures/paste/wikipedia/einstein-section-with-edit-links/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/periodic-table-merged-cells/fixture.json create mode 100644 test/fixtures/paste/wikipedia/periodic-table-merged-cells/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/periodic-table-merged-cells/public.html.html create mode 100644 test/fixtures/paste/wikipedia/periodic-table-merged-cells/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-derivation/fixture.json create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-derivation/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-derivation/public.html.html create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-derivation/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-math/fixture.json create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-math/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-math/public.html.html create mode 100644 test/fixtures/paste/wikipedia/quadratic-formula-math/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/roman-emperors-long/fixture.json create mode 100644 test/fixtures/paste/wikipedia/roman-emperors-long/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/roman-emperors-long/public.html.html create mode 100644 test/fixtures/paste/wikipedia/roman-emperors-long/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/tokyo-climate-table-cjk/fixture.json create mode 100644 test/fixtures/paste/wikipedia/tokyo-climate-table-cjk/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/tokyo-climate-table-cjk/public.html.html create mode 100644 test/fixtures/paste/wikipedia/tokyo-climate-table-cjk/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/wikipedia/tokyo-japanese/fixture.json create mode 100644 test/fixtures/paste/wikipedia/tokyo-japanese/org.chromium.source-url.txt create mode 100644 test/fixtures/paste/wikipedia/tokyo-japanese/public.html.html create mode 100644 test/fixtures/paste/wikipedia/tokyo-japanese/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-mac/clip_image001.png create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-mac/fixture.json create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-mac/public.html.html create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-mac/public.rtf.rtf create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-mac/public.utf8-plain-text.txt create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-windows/CF_UNICODETEXT.txt create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-windows/HTML_Format.html create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-windows/Rich_Text_Format.rtf create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-windows/clip_image001.png create mode 100644 test/fixtures/paste/word/mixed-lists-table-image-windows/fixture.json create mode 100644 tool/paste_fixtures/README.md create mode 100644 tool/paste_fixtures/capture_macos.swift create mode 100644 tool/paste_fixtures/check_cf_html.mjs create mode 100644 tool/paste_fixtures/headless/.gitignore create mode 100644 tool/paste_fixtures/headless/apply_notes.mjs create mode 100644 tool/paste_fixtures/headless/capture.mjs create mode 100644 tool/paste_fixtures/headless/gcapture.mjs create mode 100644 tool/paste_fixtures/headless/lib.mjs create mode 100644 tool/paste_fixtures/headless/notes.json create mode 100644 tool/paste_fixtures/headless/package.json create mode 100755 tool/paste_fixtures/headless/run_google.sh create mode 100644 tool/paste_fixtures/headless/scan.mjs create mode 100644 tool/paste_fixtures/headless/specs/ai-helpers.mjs create mode 100644 tool/paste_fixtures/headless/specs/chatgpt.mjs create mode 100644 tool/paste_fixtures/headless/specs/gemini-perplexity.mjs create mode 100644 tool/paste_fixtures/headless/specs/helpers.mjs create mode 100644 tool/paste_fixtures/headless/specs/web.mjs create mode 100644 tool/paste_fixtures/headless/specs/wikipedia.mjs create mode 100755 tool/paste_fixtures/headless/watch_changecount.sh create mode 100644 tool/paste_fixtures/synthesize/apple.swift create mode 100644 tool/paste_fixtures/synthesize/apps.mjs create mode 100644 tool/paste_fixtures/synthesize/common.mjs create mode 100644 tool/paste_fixtures/synthesize/sheets.mjs create mode 100644 tool/paste_fixtures/synthesize/word.mjs create mode 100644 tool/paste_fixtures/test_capture_macos.swift create mode 100644 tool/paste_fixtures/validate.mjs diff --git a/test/fixtures/paste/INDEX.md b/test/fixtures/paste/INDEX.md new file mode 100644 index 0000000..494b329 --- /dev/null +++ b/test/fixtures/paste/INDEX.md @@ -0,0 +1,66 @@ +| fixture | capture | platform | items | representations | size | +| --- | --- | --- | --- | --- | --- | +| apple-notes/rich-note-title-lists-checklist-image | synthesized | macos | 1 | public.rtf, com.apple.flat-rtfd, public.utf8-plain-text, public.html | 10 KB | +| chatgpt/code-c-spanish-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 23 KB | +| chatgpt/code-c-spanish-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 56 KB | +| chatgpt/code-tables-php-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 37 KB | +| chatgpt/code-tables-php-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 49 KB | +| chatgpt/long-math-code-tables-images-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 308 KB | +| chatgpt/long-math-code-tables-images-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 397 KB | +| chatgpt/mac-app-copy-button-guess | synthesized | macos | 1 | public.utf8-plain-text | 1 KB | +| chatgpt/math-number-theory-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 63 KB | +| chatgpt/math-number-theory-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 66 KB | +| chatgpt/math-stochastic-processes-german-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 15 KB | +| chatgpt/math-stochastic-processes-german-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 18 KB | +| chatgpt/nested-lists-emoji-2-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 56 KB | +| chatgpt/nested-lists-emoji-2-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 67 KB | +| chatgpt/nested-lists-emoji-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 117 KB | +| chatgpt/nested-lists-emoji-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 172 KB | +| chatgpt/table-math-headings-copy-button | real | macos | 1 | public.utf8-plain-text, public.html | 47 KB | +| chatgpt/table-math-headings-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 59 KB | +| claude/mac-app-copy-button-guess | synthesized | macos | 1 | public.utf8-plain-text | 1 KB | +| excel/budget-range-mac | synthesized | macos | 1 | public.html, public.utf8-plain-text, public.png | 7 KB | +| excel/budget-range-windows | synthesized | windows | 1 | HTML Format, CF_UNICODETEXT, Csv, PNG | 7 KB | +| finder/two-files-copied | synthesized | macos | 2 | public.file-url, public.utf8-plain-text, NSFilenamesPboardType | 2 KB | +| gemini/code-tables-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 86 KB | +| gemini/math-heavy-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 125 KB | +| gemini/math-tables-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 92 KB | +| gemini/nested-lists-long-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 51 KB | +| github/issue-body-task-list | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 108 KB | +| github/pr-body-checklist | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 8 KB | +| github/readme-got-headings-tables-code | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 170 KB | +| gmail/html-email-layout-tables-quote-signature | synthesized | macos | 1 | public.html, public.utf8-plain-text, org.chromium.source-url | 5 KB | +| google-docs/trace-event-format-toc-and-intro | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 43 KB | +| google-docs/trace-event-format-whole-document | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 714 KB | +| google-docs/vmware-com1-images-table | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 993 KB | +| google-sheets/class-data-plain-view-only | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 3 KB | +| google-sheets/merged-cells-formulas-synthesized | synthesized | macos | 1 | public.html, public.utf8-plain-text, org.chromium.source-url | 6 KB | +| google-sheets/merged-cells-view-only | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 8 KB | +| google-sheets/styled-list-view-only | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 9 KB | +| image/single-image-png-tiff | synthesized | macos | 1 | public.png, public.tiff | 256 KB | +| numbers/table-cells | synthesized | macos | 1 | public.html, public.rtf, public.utf8-plain-text | 4 KB | +| outlook/email-body-mac | synthesized | macos | 1 | public.html, public.utf8-plain-text | 6 KB | +| outlook/email-body-windows | synthesized | windows | 1 | HTML Format, CF_UNICODETEXT, Rich Text Format | 7 KB | +| pages/document-selection-lists-image | synthesized | macos | 1 | public.rtf, com.apple.flat-rtfd, public.utf8-plain-text | 8 KB | +| perplexity/code-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 24 KB | +| perplexity/nested-lists-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 29 KB | +| perplexity/table-links-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 26 KB | +| perplexity/table-math-selection | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 43 KB | +| safari/selection-with-image-webarchive | synthesized | macos | 1 | com.apple.webarchive, public.rtf, com.apple.flat-rtfd, public.html, public.utf8-plain-text | 8 KB | +| slack/message-formatting-emoji-code | synthesized | macos | 1 | public.html, public.utf8-plain-text | 5 KB | +| stackexchange/askubuntu-answer-code-blocks | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 1000 KB | +| vscode/python-selection | synthesized | macos | 1 | public.html, public.utf8-plain-text, org.chromium.web-custom-data, vscode-editor-data | 4 KB | +| vscode/typescript-selection | synthesized | macos | 1 | public.html, public.utf8-plain-text, org.chromium.web-custom-data, vscode-editor-data | 4 KB | +| web/getemoji-people-skin-tones | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 1.1 MB | +| web/mdn-html-images-relative-urls | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 195 KB | +| wikipedia/cairo-arabic-rtl | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 27 KB | +| wikipedia/einstein-infobox-lead | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 109 KB | +| wikipedia/einstein-section-with-edit-links | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 52 KB | +| wikipedia/periodic-table-merged-cells | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 30 KB | +| wikipedia/quadratic-formula-derivation | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 697 KB | +| wikipedia/quadratic-formula-math | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 42 KB | +| wikipedia/roman-emperors-long | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 2.5 MB | +| wikipedia/tokyo-climate-table-cjk | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 32 KB | +| wikipedia/tokyo-japanese | real | macos | 1 | public.utf8-plain-text, public.html, org.chromium.source-url | 29 KB | +| word/mixed-lists-table-image-mac | synthesized | macos | 1 | public.html, public.rtf, public.utf8-plain-text | 23 KB | +| word/mixed-lists-table-image-windows | synthesized | windows | 1 | HTML Format, Rich Text Format, CF_UNICODETEXT | 23 KB | diff --git a/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/com.apple.flat-rtfd.rtfd b/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/com.apple.flat-rtfd.rtfd new file mode 100644 index 0000000000000000000000000000000000000000..72b12b9b7fd086e0186174ef3ac7104d3c9a1e34 GIT binary patch literal 4312 zcmds34Qv}%9k(xTV|%2{URi>^hO$*Jml{8Vd@_*-# zWK$P0L07>^cmMbQc<=x5d+&eGVBwGro}%1a479VWv)o`|I}FQR@KK_&!OG)W)_Yi|NRg~8N=WzD44M~O!~Qk8@iUD07$SgI9$?kvYhl9`IEQY|88C2>_| zVcm#{DIC@{#)2vii#TldQPad>?$;ztL@h+^C5$Ta=76q-{22UwxDBYKCDfaD1Xa2> zLHzEtI|Wgwx&dze*dQUPstQL8LNq=IstE$rQh3CyytcX^=94#Kl8nn*h=dBu__1P| zMkI(8$k1Ek^HyTTBk~6;nj9m@>^B*S1z_>7aM%9qIx008lY=TrVLr4zV8+WsXvkyn z(f*)8LskUdWm z5aqC60cpt+emK%OfS@>eP_GWoM4>TRUAiJ30GiiigD8FwWiDSD>GzjIN-rD(y_XoU z&&i_WkQe2PXVJV+(G##&v1)p9n<%fyS!YX{7Z$9!OB|Pt5N63L*!V2z@xXorQtjhJ zk^n;>J~+87S;==SkXp;c>$Oa(M9Tzbb6vH^GO6afny*U@O=bxLst6pTktictSXP*B zh;BFUazSWPULV3rSVUh-fDJL!egsEe)1X1(YZ{#g2YDNoa^dY%WvU^5-Zvo@B^t^m zfP&DCVaTn~CN7jPMU+8@S1d8*Vw_M>0y>HTXPtzA({zn+xycOLL-JG+5eO1W4|p*2 zL?oKii#Zw+5rSTeWCZ^TN>u2C8*x|-QO#tjmOFMfb==n4-iFph8H<}+Dk?})hNeWy z6+KpA=yE6~$Jg-I1yc-DWxVc^6&pmPNC<-KNvF#f%AQ3$+oSjw+Sb1VE#HWks=X>S zd;m6k%6a8ni@KsKeS4I~c_|T%#o-4;j0MNs3v5FX_Lxd7@=;xTuI$?CTdwpj-GC}W zC7Y58{6un%Xiea_P)4k>7(^wqNs#v6at}N6N}zaLY4{JT zL}4gQlW<;nvWq~Ipk9Tb;2@hEsYUuqSGt4rQqqU|D2t*HF=<3wDA&i{SuErOiGCEL zY7chbP5@_Xn`yl|Rme4r3>tzdd9&>u4d@ssyMN*1)~F3%wJ&)BLL32(JL| zqWksxuGXcX<8RaRb9;{R3AyKkJXayOe&4sVy}87_$_;EuYfEDX&}n!NI+w#Y@YA(7 z1F@SO%>mny->g0hjE{7+hPQ9GZG^GYX1|x(9NYxnRq#S^BSJwkJyKK{ALPnk3Crqj zHpg&lSR#o8*Hn+lRWp-1U7QiAGPUF@tbenRabxFtuG4BXV*?2Jv}~jt8MVI!*RZ;VZ>v%*9@Owz})!1 z&Yawq8C&OQpLwZmbbe}gD%JPg+|2p;l4Y;`<>ZQ=)z>+b#pi#0=$%{M`Q*2Yoqybb zCg+Cmx5mDc$$U4uwPB>rZlCCTBj6ZzIF2`-vI`eo&gVvN`plZA*P_XFt5%jiv7=$^ z;*G~j2iMu{6|W>S4QW9*a^dBBoSR(ES_{8PkUwF9g&dU#JcQw}!)=VbvZb-nQ+RJID zT3R~2eD$HyC)yjv#!HT^94rxplbKW33*!*h&&y`Yp5E3l_WAhMz{nQ6eOJvJb&hJM zGg&m$?CgZZrVhMz@7VIey#rsJJ#%h!V9(6!ubzAF$-f>*9TJ?ET&vM5cOP>&woaTl zZFe}X9{1wAsmZCQcFer=jUC5N_|MPH*3CC;D$@P$9W&@BinhI_sa@SpJ?6~C9s>_7eeVZeHJ zX!9r5qsjk^X@d{FJv;wE{T~7j$#uzf`0V&oWq^O + + + + + + + + + + +

Quarterly planning

+

Plain text, then bold, italic, underlined, struck through and a link.

+
    +
  • Ship the importer
  • +
      +
    • Word and Excel
    • +
    • Apple Notes
    • +
    +
  • Write the release notes
  • +
+
    +
  1. Collect fixtures
  2. +
  3. Write the parser
  4. +
  5. Review with design
  6. +
+

Buy milk

+

Send the invoice

+

chart.png

+ + diff --git a/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.rtf.rtf b/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.rtf.rtf new file mode 100644 index 0000000..664569f --- /dev/null +++ b/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.rtf.rtf @@ -0,0 +1,38 @@ +{\rtf1\ansi\ansicpg1252\cocoartf2908 +\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica-Bold;\f1\fswiss\fcharset0 Helvetica;\f2\fswiss\fcharset0 Helvetica-Oblique; +} +{\colortbl;\red255\green255\blue255;\red10\green96\blue255;} +{\*\expandedcolortbl;;\cssrgb\c0\c47843\c100000\cname systemBlueColor;} +{\*\listtable{\list\listtemplateid1\listhybrid{\listlevel\levelnfc23\levelnfcn23\leveljc0\leveljcn0\levelfollow0\levelstartat1\levelspace360\levelindent0{\*\levelmarker \{disc\}}{\leveltext\leveltemplateid1\'01\uc0\u8226 ;}{\levelnumbers;}\fi-360\li720\lin720 }{\listlevel\levelnfc23\levelnfcn23\leveljc0\leveljcn0\levelfollow0\levelstartat1\levelspace360\levelindent0{\*\levelmarker \{circle\}}{\leveltext\leveltemplateid2\'01\uc0\u9702 ;}{\levelnumbers;}\fi-360\li1440\lin1440 }{\listname ;}\listid1} +{\list\listtemplateid2\listhybrid{\listlevel\levelnfc0\levelnfcn0\leveljc0\leveljcn0\levelfollow0\levelstartat1\levelspace360\levelindent0{\*\levelmarker \{decimal\}}{\leveltext\leveltemplateid101\'01\'00;}{\levelnumbers\'01;}\fi-360\li720\lin720 }{\listname ;}\listid2}} +{\*\listoverridetable{\listoverride\listid1\listoverridecount0\ls1}{\listoverride\listid2\listoverridecount0\ls2}} +\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0 + +\f0\b\fs48 \cf0 Quarterly planning\ +\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0 + +\f1\b0\fs28 \cf0 Plain text, then +\f0\b bold +\f1\b0 , +\f2\i italic +\f1\i0 , \ul underlined\ulnone , \strike \strikec0 struck through\strike0\striked0 and a {\field{\*\fldinst{HYPERLINK "https://example.com/roadmap"}}{\fldrslt \cf2 \ul \ulc2 link}}.\ +\pard\tx360\tx720\li720\fi-720\pardirnatural\partightenfactor0 +\ls1\ilvl0\cf0 {\listtext \uc0\u8226 }Ship the importer\ +\pard\tx720\tx1080\li1080\fi-720\pardirnatural\partightenfactor0 +\ls1\ilvl1\cf0 {\listtext \uc0\u9702 }Word and Excel\ +{\listtext \uc0\u9702 }Apple Notes\ +\pard\tx360\tx720\li720\fi-720\pardirnatural\partightenfactor0 +\ls1\ilvl0\cf0 {\listtext \uc0\u8226 }Write the release notes\ +\pard\tx360\tx720\li720\fi-720\pardirnatural\partightenfactor0 +\ls2\ilvl0\cf0 {\listtext 1 }Collect fixtures\ +{\listtext 2 }Write the parser\ +{\listtext 3 }Review with design\ +\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0 +\cf0 Buy milk\ +Send the invoice\ +\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0 + +\fs24 \cf0 \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0 + +\fs28 \cf0 \ +} \ No newline at end of file diff --git a/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.utf8-plain-text.txt b/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.utf8-plain-text.txt new file mode 100644 index 0000000..c990107 --- /dev/null +++ b/test/fixtures/paste/apple-notes/rich-note-title-lists-checklist-image/public.utf8-plain-text.txt @@ -0,0 +1,12 @@ +Quarterly planning +Plain text, then bold, italic, underlined, struck through and a link. + • Ship the importer + ◦ Word and Excel + ◦ Apple Notes + • Write the release notes + 1 Collect fixtures + 2 Write the parser + 3 Review with design +Buy milk +Send the invoice + diff --git a/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/fixture.json b/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/fixture.json new file mode 100644 index 0000000..2e6321b --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/fixture.json @@ -0,0 +1,24 @@ +{ + "source": "chatgpt", + "case": "code-c-spanish-copy-button", + "app": "Chrome/157.0.8103.78 (chrome-headless-shell)", + "platform": "macos", + "capture": "real", + "how": "Opened the public ChatGPT share page and clicked the site's own \"Copy response\" button under the longest assistant answer (C code blocks in a Spanish answer).", + "url": "https://chatgpt.com/share/ccc23d98-6d4f-4be5-b9b6-b4464ff1bf9d", + "notes": "Copy-button capture. The site writes through navigator.clipboard.write, so there is no org.chromium.source-url. text/plain is the answer as markdown source (pipe tables, ``` fences, $...$ / $$...$$ math, hard line breaks as a trailing backslash); text/html is the app's rendered DOM full of hydration comments, wrapped in , with no inline styles. Spanish-language answer with several C code blocks (```c fences in plain text), headings, nested lists.", + "items": [ + { + "representations": [ + { + "type": "public.utf8-plain-text", + "file": "public.utf8-plain-text.txt" + }, + { + "type": "public.html", + "file": "public.html.html" + } + ] + } + ] +} diff --git a/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.html.html b/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.html.html new file mode 100644 index 0000000..c496b54 --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.html.html @@ -0,0 +1,224 @@ +

Writing a Lexer and Parser in C

Here's a basic lexer and parser in C for a hypothetical programming language that supports integer types constrained to specific integer ranges. I'll provide the relevant code for lexing and parsing integer types with constraints.

Lexer

Header (lexer.h)

c
#ifndef LEXER_H
+#define LEXER_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+typedef enum {
+    TOKEN_INT,
+    TOKEN_RANGE,
+    TOKEN_IDENTIFIER,
+    TOKEN_LPAREN,
+    TOKEN_RPAREN,
+    TOKEN_COLON,
+    TOKEN_COMMA,
+    TOKEN_EOF,
+    TOKEN_UNKNOWN
+} TokenType;
+
+typedef struct {
+    TokenType type;
+    char* value;
+} Token;
+
+typedef struct {
+    const char* source;
+    size_t length;
+    size_t position;
+} Lexer;
+
+Lexer* create_lexer(const char* source);
+void destroy_lexer(Lexer* lexer);
+Token next_token(Lexer* lexer);
+void destroy_token(Token token);
+
+#endif // LEXER_H
+

Implementation (lexer.c)

c
#include "lexer.h"
+
+Lexer* create_lexer(const char* source) {
+    Lexer* lexer = (Lexer*)malloc(sizeof(Lexer));
+    lexer->source = source;
+    lexer->length = strlen(source);
+    lexer->position = 0;
+    return lexer;
+}
+
+void destroy_lexer(Lexer* lexer) {
+    free(lexer);
+}
+
+static char peek_char(Lexer* lexer) {
+    if (lexer->position >= lexer->length) {
+        return '\0';
+    }
+    return lexer->source[lexer->position];
+}
+
+static char advance_char(Lexer* lexer) {
+    if (lexer->position >= lexer->length) {
+        return '\0';
+    }
+    return lexer->source[lexer->position++];
+}
+
+static Token make_token(TokenType type, const char* value) {
+    Token token;
+    token.type = type;
+    token.value = strdup(value);
+    return token;
+}
+
+static void skip_whitespace(Lexer* lexer) {
+    while (isspace(peek_char(lexer))) {
+        advance_char(lexer);
+    }
+}
+
+static Token scan_number(Lexer* lexer) {
+    size_t start = lexer->position - 1;
+    while (isdigit(peek_char(lexer))) {
+        advance_char(lexer);
+    }
+    char* value = strndup(lexer->source + start, lexer->position - start);
+    return make_token(TOKEN_INT, value);
+}
+
+static Token scan_identifier(Lexer* lexer) {
+    size_t start = lexer->position - 1;
+    while (isalnum(peek_char(lexer)) || peek_char(lexer) == '_') {
+        advance_char(lexer);
+    }
+    char* value = strndup(lexer->source + start, lexer->position - start);
+    return make_token(TOKEN_IDENTIFIER, value);
+}
+
+Token next_token(Lexer* lexer) {
+    skip_whitespace(lexer);
+    char current = advance_char(lexer);
+
+    if (isdigit(current)) {
+        return scan_number(lexer);
+    }
+
+    if (isalpha(current) || current == '_') {
+        return scan_identifier(lexer);
+    }
+
+    switch (current) {
+        case '\0': return make_token(TOKEN_EOF, "");
+        case '(': return make_token(TOKEN_LPAREN, "(");
+        case ')': return make_token(TOKEN_RPAREN, ")");
+        case ':': return make_token(TOKEN_COLON, ":");
+        case ',': return make_token(TOKEN_COMMA, ",");
+        case '-':
+            if (peek_char(lexer) == '>') {
+                advance_char(lexer);
+                return make_token(TOKEN_RANGE, "->");
+            }
+            break;
+    }
+
+    char unknown[2] = { current, '\0' };
+    return make_token(TOKEN_UNKNOWN, unknown);
+}
+
+void destroy_token(Token token) {
+    free(token.value);
+}
+

Parser

Header (parser.h)

c
#ifndef PARSER_H
+#define PARSER_H
+
+#include "lexer.h"
+
+typedef struct {
+    char* name;
+    int min_value;
+    int max_value;
+} ConstrainedIntType;
+
+typedef struct {
+    Lexer* lexer;
+    Token current_token;
+} Parser;
+
+Parser* create_parser(const char* source);
+void destroy_parser(Parser* parser);
+ConstrainedIntType* parse_constrained_int_type(Parser* parser);
+
+#endif // PARSER_H
+

Implementation (parser.c)

c
#include "parser.h"
+
+Parser* create_parser(const char* source) {
+    Parser* parser = (Parser*)malloc(sizeof(Parser));
+    parser->lexer = create_lexer(source);
+    parser->current_token = next_token(parser->lexer);
+    return parser;
+}
+
+void destroy_parser(Parser* parser) {
+    destroy_token(parser->current_token);
+    destroy_lexer(parser->lexer);
+    free(parser);
+}
+
+static void consume_token(Parser* parser, TokenType expected) {
+    if (parser->current_token.type == expected) {
+        destroy_token(parser->current_token);
+        parser->current_token = next_token(parser->lexer);
+    } else {
+        fprintf(stderr, "Error: Expected token of type %d but got %d\n", expected, parser->current_token.type);
+        exit(1);
+    }
+}
+
+static int parse_integer(Parser* parser) {
+    int value = atoi(parser->current_token.value);
+    consume_token(parser, TOKEN_INT);
+    return value;
+}
+
+ConstrainedIntType* parse_constrained_int_type(Parser* parser) {
+    ConstrainedIntType* result = (ConstrainedIntType*)malloc(sizeof(ConstrainedIntType));
+
+    if (parser->current_token.type != TOKEN_IDENTIFIER) {
+        fprintf(stderr, "Error: Expected identifier for type name\n");
+        exit(1);
+    }
+
+    result->name = strdup(parser->current_token.value);
+    consume_token(parser, TOKEN_IDENTIFIER);
+
+    consume_token(parser, TOKEN_LPAREN);
+    result->min_value = parse_integer(parser);
+    consume_token(parser, TOKEN_RANGE);
+    result->max_value = parse_integer(parser);
+    consume_token(parser, TOKEN_RPAREN);
+
+    return result;
+}
+

Main Program

To demonstrate the lexer and parser:

c
#include <stdio.h>
+#include "parser.h"
+
+int main() {
+    const char* source_code = "ConstrainedInt(0->100)";
+    Parser* parser = create_parser(source_code);
+
+    ConstrainedIntType* int_type = parse_constrained_int_type(parser);
+    printf("Parsed constrained int type:\n");
+    printf("Name: %s\n", int_type->name);
+    printf("Range: %d -> %d\n", int_type->min_value, int_type->max_value);
+
+    destroy_parser(parser);
+    free(int_type->name);
+    free(int_type);
+
+    return 0;
+}
+

Compilation and Execution

  1. Create files for the above code:
    • lexer.h
    • lexer.c
    • parser.h
    • parser.c
    • main.c
  2. Compile using:
bash
gcc -o parser lexer.c parser.c main.c
+
  1. Run:
bash
./parser
+

Output

Parsed constrained int type:
+Name: ConstrainedInt
+Range: 0 -> 100
+

This provides a basic foundation for a lexer and parser in C that can recognize integer types with specific ranges. You can extend this further by adding more language features.

\ No newline at end of file diff --git a/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.utf8-plain-text.txt b/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.utf8-plain-text.txt new file mode 100644 index 0000000..cdb0279 --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-c-spanish-copy-button/public.utf8-plain-text.txt @@ -0,0 +1,296 @@ +### Writing a Lexer and Parser in C + +Here's a basic lexer and parser in C for a hypothetical programming language that supports integer types constrained to specific integer ranges. I'll provide the relevant code for lexing and parsing integer types with constraints. + +### Lexer + +#### Header (lexer.h) + +c + +```c +#ifndef LEXER_H +#define LEXER_H + +#include +#include +#include +#include + +typedef enum { + TOKEN_INT, + TOKEN_RANGE, + TOKEN_IDENTIFIER, + TOKEN_LPAREN, + TOKEN_RPAREN, + TOKEN_COLON, + TOKEN_COMMA, + TOKEN_EOF, + TOKEN_UNKNOWN +} TokenType; + +typedef struct { + TokenType type; + char* value; +} Token; + +typedef struct { + const char* source; + size_t length; + size_t position; +} Lexer; + +Lexer* create_lexer(const char* source); +void destroy_lexer(Lexer* lexer); +Token next_token(Lexer* lexer); +void destroy_token(Token token); + +#endif // LEXER_H +``` + +#### Implementation (lexer.c) + +c + +```c +#include "lexer.h" + +Lexer* create_lexer(const char* source) { + Lexer* lexer = (Lexer*)malloc(sizeof(Lexer)); + lexer->source = source; + lexer->length = strlen(source); + lexer->position = 0; + return lexer; +} + +void destroy_lexer(Lexer* lexer) { + free(lexer); +} + +static char peek_char(Lexer* lexer) { + if (lexer->position >= lexer->length) { + return '\0'; + } + return lexer->source[lexer->position]; +} + +static char advance_char(Lexer* lexer) { + if (lexer->position >= lexer->length) { + return '\0'; + } + return lexer->source[lexer->position++]; +} + +static Token make_token(TokenType type, const char* value) { + Token token; + token.type = type; + token.value = strdup(value); + return token; +} + +static void skip_whitespace(Lexer* lexer) { + while (isspace(peek_char(lexer))) { + advance_char(lexer); + } +} + +static Token scan_number(Lexer* lexer) { + size_t start = lexer->position - 1; + while (isdigit(peek_char(lexer))) { + advance_char(lexer); + } + char* value = strndup(lexer->source + start, lexer->position - start); + return make_token(TOKEN_INT, value); +} + +static Token scan_identifier(Lexer* lexer) { + size_t start = lexer->position - 1; + while (isalnum(peek_char(lexer)) || peek_char(lexer) == '_') { + advance_char(lexer); + } + char* value = strndup(lexer->source + start, lexer->position - start); + return make_token(TOKEN_IDENTIFIER, value); +} + +Token next_token(Lexer* lexer) { + skip_whitespace(lexer); + char current = advance_char(lexer); + + if (isdigit(current)) { + return scan_number(lexer); + } + + if (isalpha(current) || current == '_') { + return scan_identifier(lexer); + } + + switch (current) { + case '\0': return make_token(TOKEN_EOF, ""); + case '(': return make_token(TOKEN_LPAREN, "("); + case ')': return make_token(TOKEN_RPAREN, ")"); + case ':': return make_token(TOKEN_COLON, ":"); + case ',': return make_token(TOKEN_COMMA, ","); + case '-': + if (peek_char(lexer) == '>') { + advance_char(lexer); + return make_token(TOKEN_RANGE, "->"); + } + break; + } + + char unknown[2] = { current, '\0' }; + return make_token(TOKEN_UNKNOWN, unknown); +} + +void destroy_token(Token token) { + free(token.value); +} +``` + +### Parser + +#### Header (parser.h) + +c + +```c +#ifndef PARSER_H +#define PARSER_H + +#include "lexer.h" + +typedef struct { + char* name; + int min_value; + int max_value; +} ConstrainedIntType; + +typedef struct { + Lexer* lexer; + Token current_token; +} Parser; + +Parser* create_parser(const char* source); +void destroy_parser(Parser* parser); +ConstrainedIntType* parse_constrained_int_type(Parser* parser); + +#endif // PARSER_H +``` + +#### Implementation (parser.c) + +c + +```c +#include "parser.h" + +Parser* create_parser(const char* source) { + Parser* parser = (Parser*)malloc(sizeof(Parser)); + parser->lexer = create_lexer(source); + parser->current_token = next_token(parser->lexer); + return parser; +} + +void destroy_parser(Parser* parser) { + destroy_token(parser->current_token); + destroy_lexer(parser->lexer); + free(parser); +} + +static void consume_token(Parser* parser, TokenType expected) { + if (parser->current_token.type == expected) { + destroy_token(parser->current_token); + parser->current_token = next_token(parser->lexer); + } else { + fprintf(stderr, "Error: Expected token of type %d but got %d\n", expected, parser->current_token.type); + exit(1); + } +} + +static int parse_integer(Parser* parser) { + int value = atoi(parser->current_token.value); + consume_token(parser, TOKEN_INT); + return value; +} + +ConstrainedIntType* parse_constrained_int_type(Parser* parser) { + ConstrainedIntType* result = (ConstrainedIntType*)malloc(sizeof(ConstrainedIntType)); + + if (parser->current_token.type != TOKEN_IDENTIFIER) { + fprintf(stderr, "Error: Expected identifier for type name\n"); + exit(1); + } + + result->name = strdup(parser->current_token.value); + consume_token(parser, TOKEN_IDENTIFIER); + + consume_token(parser, TOKEN_LPAREN); + result->min_value = parse_integer(parser); + consume_token(parser, TOKEN_RANGE); + result->max_value = parse_integer(parser); + consume_token(parser, TOKEN_RPAREN); + + return result; +} +``` + +### Main Program + +To demonstrate the lexer and parser: + +c + +```c +#include +#include "parser.h" + +int main() { + const char* source_code = "ConstrainedInt(0->100)"; + Parser* parser = create_parser(source_code); + + ConstrainedIntType* int_type = parse_constrained_int_type(parser); + printf("Parsed constrained int type:\n"); + printf("Name: %s\n", int_type->name); + printf("Range: %d -> %d\n", int_type->min_value, int_type->max_value); + + destroy_parser(parser); + free(int_type->name); + free(int_type); + + return 0; +} +``` + +### Compilation and Execution + +1. Create files for the above code: + - `lexer.h` + - `lexer.c` + - `parser.h` + - `parser.c` + - `main.c` +2. Compile using: + +bash + +```bash +gcc -o parser lexer.c parser.c main.c +``` + +3. Run: + +bash + +```bash +./parser +``` + +### Output + +``` +Parsed constrained int type: +Name: ConstrainedInt +Range: 0 -> 100 +``` + +This provides a basic foundation for a lexer and parser in C that can recognize integer types with specific ranges. You can extend this further by adding more language features. \ No newline at end of file diff --git a/test/fixtures/paste/chatgpt/code-c-spanish-selection/fixture.json b/test/fixtures/paste/chatgpt/code-c-spanish-selection/fixture.json new file mode 100644 index 0000000..add6c37 --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-c-spanish-selection/fixture.json @@ -0,0 +1,28 @@ +{ + "source": "chatgpt", + "case": "code-c-spanish-selection", + "app": "Chrome/157.0.8103.78 (chrome-headless-shell)", + "platform": "macos", + "capture": "real", + "how": "Opened the public ChatGPT share page and selected the longest assistant answer (C code blocks in a Spanish answer) from its first block to its last with a DOM range (what a mouse drag over the answer yields), then ran document.execCommand(\"copy\").", + "url": "https://chatgpt.com/share/ccc23d98-6d4f-4be5-b9b6-b4464ff1bf9d", + "notes": "Selection capture (DOM range + execCommand('copy')), so it is Chrome's serialization: org.chromium.source-url is the page URL, every element carries a computed inline style=, the first block starts with
, and plain text is the rendered text (UI labels such as 'Thought for ...' / 'Worked for ...' can lead it; tokens of rendered math sit on separate lines). Code blocks lose their language-* classes in this path. 8
 blocks, 10 headings, lists.",
+  "items": [
+    {
+      "representations": [
+        {
+          "type": "public.utf8-plain-text",
+          "file": "public.utf8-plain-text.txt"
+        },
+        {
+          "type": "public.html",
+          "file": "public.html.html"
+        },
+        {
+          "type": "org.chromium.source-url",
+          "file": "org.chromium.source-url.txt"
+        }
+      ]
+    }
+  ]
+}
diff --git a/test/fixtures/paste/chatgpt/code-c-spanish-selection/org.chromium.source-url.txt b/test/fixtures/paste/chatgpt/code-c-spanish-selection/org.chromium.source-url.txt
new file mode 100644
index 0000000..cfbe5bc
--- /dev/null
+++ b/test/fixtures/paste/chatgpt/code-c-spanish-selection/org.chromium.source-url.txt
@@ -0,0 +1 @@
+https://chatgpt.com/share/ccc23d98-6d4f-4be5-b9b6-b4464ff1bf9d
\ No newline at end of file
diff --git a/test/fixtures/paste/chatgpt/code-c-spanish-selection/public.html.html b/test/fixtures/paste/chatgpt/code-c-spanish-selection/public.html.html
new file mode 100644
index 0000000..f19b04e
--- /dev/null
+++ b/test/fixtures/paste/chatgpt/code-c-spanish-selection/public.html.html
@@ -0,0 +1,224 @@
+


Writing a Lexer and Parser in C

Here's a basic lexer and parser in C for a hypothetical programming language that supports integer types constrained to specific integer ranges. I'll provide the relevant code for lexing and parsing integer types with constraints.

Lexer

Header (lexer.h)

c
#ifndef LEXER_H
+#define LEXER_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+typedef enum {
+    TOKEN_INT,
+    TOKEN_RANGE,
+    TOKEN_IDENTIFIER,
+    TOKEN_LPAREN,
+    TOKEN_RPAREN,
+    TOKEN_COLON,
+    TOKEN_COMMA,
+    TOKEN_EOF,
+    TOKEN_UNKNOWN
+} TokenType;
+
+typedef struct {
+    TokenType type;
+    char* value;
+} Token;
+
+typedef struct {
+    const char* source;
+    size_t length;
+    size_t position;
+} Lexer;
+
+Lexer* create_lexer(const char* source);
+void destroy_lexer(Lexer* lexer);
+Token next_token(Lexer* lexer);
+void destroy_token(Token token);
+
+#endif // LEXER_H
+

Implementation (lexer.c)

c
#include "lexer.h"
+
+Lexer* create_lexer(const char* source) {
+    Lexer* lexer = (Lexer*)malloc(sizeof(Lexer));
+    lexer->source = source;
+    lexer->length = strlen(source);
+    lexer->position = 0;
+    return lexer;
+}
+
+void destroy_lexer(Lexer* lexer) {
+    free(lexer);
+}
+
+static char peek_char(Lexer* lexer) {
+    if (lexer->position >= lexer->length) {
+        return '\0';
+    }
+    return lexer->source[lexer->position];
+}
+
+static char advance_char(Lexer* lexer) {
+    if (lexer->position >= lexer->length) {
+        return '\0';
+    }
+    return lexer->source[lexer->position++];
+}
+
+static Token make_token(TokenType type, const char* value) {
+    Token token;
+    token.type = type;
+    token.value = strdup(value);
+    return token;
+}
+
+static void skip_whitespace(Lexer* lexer) {
+    while (isspace(peek_char(lexer))) {
+        advance_char(lexer);
+    }
+}
+
+static Token scan_number(Lexer* lexer) {
+    size_t start = lexer->position - 1;
+    while (isdigit(peek_char(lexer))) {
+        advance_char(lexer);
+    }
+    char* value = strndup(lexer->source + start, lexer->position - start);
+    return make_token(TOKEN_INT, value);
+}
+
+static Token scan_identifier(Lexer* lexer) {
+    size_t start = lexer->position - 1;
+    while (isalnum(peek_char(lexer)) || peek_char(lexer) == '_') {
+        advance_char(lexer);
+    }
+    char* value = strndup(lexer->source + start, lexer->position - start);
+    return make_token(TOKEN_IDENTIFIER, value);
+}
+
+Token next_token(Lexer* lexer) {
+    skip_whitespace(lexer);
+    char current = advance_char(lexer);
+
+    if (isdigit(current)) {
+        return scan_number(lexer);
+    }
+
+    if (isalpha(current) || current == '_') {
+        return scan_identifier(lexer);
+    }
+
+    switch (current) {
+        case '\0': return make_token(TOKEN_EOF, "");
+        case '(': return make_token(TOKEN_LPAREN, "(");
+        case ')': return make_token(TOKEN_RPAREN, ")");
+        case ':': return make_token(TOKEN_COLON, ":");
+        case ',': return make_token(TOKEN_COMMA, ",");
+        case '-':
+            if (peek_char(lexer) == '>') {
+                advance_char(lexer);
+                return make_token(TOKEN_RANGE, "->");
+            }
+            break;
+    }
+
+    char unknown[2] = { current, '\0' };
+    return make_token(TOKEN_UNKNOWN, unknown);
+}
+
+void destroy_token(Token token) {
+    free(token.value);
+}
+

Parser

Header (parser.h)

c
#ifndef PARSER_H
+#define PARSER_H
+
+#include "lexer.h"
+
+typedef struct {
+    char* name;
+    int min_value;
+    int max_value;
+} ConstrainedIntType;
+
+typedef struct {
+    Lexer* lexer;
+    Token current_token;
+} Parser;
+
+Parser* create_parser(const char* source);
+void destroy_parser(Parser* parser);
+ConstrainedIntType* parse_constrained_int_type(Parser* parser);
+
+#endif // PARSER_H
+

Implementation (parser.c)

c
#include "parser.h"
+
+Parser* create_parser(const char* source) {
+    Parser* parser = (Parser*)malloc(sizeof(Parser));
+    parser->lexer = create_lexer(source);
+    parser->current_token = next_token(parser->lexer);
+    return parser;
+}
+
+void destroy_parser(Parser* parser) {
+    destroy_token(parser->current_token);
+    destroy_lexer(parser->lexer);
+    free(parser);
+}
+
+static void consume_token(Parser* parser, TokenType expected) {
+    if (parser->current_token.type == expected) {
+        destroy_token(parser->current_token);
+        parser->current_token = next_token(parser->lexer);
+    } else {
+        fprintf(stderr, "Error: Expected token of type %d but got %d\n", expected, parser->current_token.type);
+        exit(1);
+    }
+}
+
+static int parse_integer(Parser* parser) {
+    int value = atoi(parser->current_token.value);
+    consume_token(parser, TOKEN_INT);
+    return value;
+}
+
+ConstrainedIntType* parse_constrained_int_type(Parser* parser) {
+    ConstrainedIntType* result = (ConstrainedIntType*)malloc(sizeof(ConstrainedIntType));
+
+    if (parser->current_token.type != TOKEN_IDENTIFIER) {
+        fprintf(stderr, "Error: Expected identifier for type name\n");
+        exit(1);
+    }
+
+    result->name = strdup(parser->current_token.value);
+    consume_token(parser, TOKEN_IDENTIFIER);
+
+    consume_token(parser, TOKEN_LPAREN);
+    result->min_value = parse_integer(parser);
+    consume_token(parser, TOKEN_RANGE);
+    result->max_value = parse_integer(parser);
+    consume_token(parser, TOKEN_RPAREN);
+
+    return result;
+}
+

Main Program

To demonstrate the lexer and parser:

c
#include <stdio.h>
+#include "parser.h"
+
+int main() {
+    const char* source_code = "ConstrainedInt(0->100)";
+    Parser* parser = create_parser(source_code);
+
+    ConstrainedIntType* int_type = parse_constrained_int_type(parser);
+    printf("Parsed constrained int type:\n");
+    printf("Name: %s\n", int_type->name);
+    printf("Range: %d -> %d\n", int_type->min_value, int_type->max_value);
+
+    destroy_parser(parser);
+    free(int_type->name);
+    free(int_type);
+
+    return 0;
+}
+

Compilation and Execution

  1. Create files for the above code:
    • lexer.h
    • lexer.c
    • parser.h
    • parser.c
    • main.c
  2. Compile using:
bash
gcc -o parser lexer.c parser.c main.c
+
  1. Run:
bash
./parser
+

Output

Parsed constrained int type:
+Name: ConstrainedInt
+Range: 0 -> 100
+

This provides a basic foundation for a lexer and parser in C that can recognize integer types with specific ranges. You can extend this further by adding more language features.

\ No newline at end of file diff --git a/test/fixtures/paste/chatgpt/code-c-spanish-selection/public.utf8-plain-text.txt b/test/fixtures/paste/chatgpt/code-c-spanish-selection/public.utf8-plain-text.txt new file mode 100644 index 0000000..c98dc99 --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-c-spanish-selection/public.utf8-plain-text.txt @@ -0,0 +1,262 @@ + +Writing a Lexer and Parser in C +Here's a basic lexer and parser in C for a hypothetical programming language that supports integer types constrained to specific integer ranges. I'll provide the relevant code for lexing and parsing integer types with constraints. + +Lexer +Header (lexer.h) + +c +#ifndef LEXER_H +#define LEXER_H + +#include +#include +#include +#include + +typedef enum { + TOKEN_INT, + TOKEN_RANGE, + TOKEN_IDENTIFIER, + TOKEN_LPAREN, + TOKEN_RPAREN, + TOKEN_COLON, + TOKEN_COMMA, + TOKEN_EOF, + TOKEN_UNKNOWN +} TokenType; + +typedef struct { + TokenType type; + char* value; +} Token; + +typedef struct { + const char* source; + size_t length; + size_t position; +} Lexer; + +Lexer* create_lexer(const char* source); +void destroy_lexer(Lexer* lexer); +Token next_token(Lexer* lexer); +void destroy_token(Token token); + +#endif // LEXER_H +Implementation (lexer.c) + +c +#include "lexer.h" + +Lexer* create_lexer(const char* source) { + Lexer* lexer = (Lexer*)malloc(sizeof(Lexer)); + lexer->source = source; + lexer->length = strlen(source); + lexer->position = 0; + return lexer; +} + +void destroy_lexer(Lexer* lexer) { + free(lexer); +} + +static char peek_char(Lexer* lexer) { + if (lexer->position >= lexer->length) { + return '\0'; + } + return lexer->source[lexer->position]; +} + +static char advance_char(Lexer* lexer) { + if (lexer->position >= lexer->length) { + return '\0'; + } + return lexer->source[lexer->position++]; +} + +static Token make_token(TokenType type, const char* value) { + Token token; + token.type = type; + token.value = strdup(value); + return token; +} + +static void skip_whitespace(Lexer* lexer) { + while (isspace(peek_char(lexer))) { + advance_char(lexer); + } +} + +static Token scan_number(Lexer* lexer) { + size_t start = lexer->position - 1; + while (isdigit(peek_char(lexer))) { + advance_char(lexer); + } + char* value = strndup(lexer->source + start, lexer->position - start); + return make_token(TOKEN_INT, value); +} + +static Token scan_identifier(Lexer* lexer) { + size_t start = lexer->position - 1; + while (isalnum(peek_char(lexer)) || peek_char(lexer) == '_') { + advance_char(lexer); + } + char* value = strndup(lexer->source + start, lexer->position - start); + return make_token(TOKEN_IDENTIFIER, value); +} + +Token next_token(Lexer* lexer) { + skip_whitespace(lexer); + char current = advance_char(lexer); + + if (isdigit(current)) { + return scan_number(lexer); + } + + if (isalpha(current) || current == '_') { + return scan_identifier(lexer); + } + + switch (current) { + case '\0': return make_token(TOKEN_EOF, ""); + case '(': return make_token(TOKEN_LPAREN, "("); + case ')': return make_token(TOKEN_RPAREN, ")"); + case ':': return make_token(TOKEN_COLON, ":"); + case ',': return make_token(TOKEN_COMMA, ","); + case '-': + if (peek_char(lexer) == '>') { + advance_char(lexer); + return make_token(TOKEN_RANGE, "->"); + } + break; + } + + char unknown[2] = { current, '\0' }; + return make_token(TOKEN_UNKNOWN, unknown); +} + +void destroy_token(Token token) { + free(token.value); +} +Parser +Header (parser.h) + +c +#ifndef PARSER_H +#define PARSER_H + +#include "lexer.h" + +typedef struct { + char* name; + int min_value; + int max_value; +} ConstrainedIntType; + +typedef struct { + Lexer* lexer; + Token current_token; +} Parser; + +Parser* create_parser(const char* source); +void destroy_parser(Parser* parser); +ConstrainedIntType* parse_constrained_int_type(Parser* parser); + +#endif // PARSER_H +Implementation (parser.c) + +c +#include "parser.h" + +Parser* create_parser(const char* source) { + Parser* parser = (Parser*)malloc(sizeof(Parser)); + parser->lexer = create_lexer(source); + parser->current_token = next_token(parser->lexer); + return parser; +} + +void destroy_parser(Parser* parser) { + destroy_token(parser->current_token); + destroy_lexer(parser->lexer); + free(parser); +} + +static void consume_token(Parser* parser, TokenType expected) { + if (parser->current_token.type == expected) { + destroy_token(parser->current_token); + parser->current_token = next_token(parser->lexer); + } else { + fprintf(stderr, "Error: Expected token of type %d but got %d\n", expected, parser->current_token.type); + exit(1); + } +} + +static int parse_integer(Parser* parser) { + int value = atoi(parser->current_token.value); + consume_token(parser, TOKEN_INT); + return value; +} + +ConstrainedIntType* parse_constrained_int_type(Parser* parser) { + ConstrainedIntType* result = (ConstrainedIntType*)malloc(sizeof(ConstrainedIntType)); + + if (parser->current_token.type != TOKEN_IDENTIFIER) { + fprintf(stderr, "Error: Expected identifier for type name\n"); + exit(1); + } + + result->name = strdup(parser->current_token.value); + consume_token(parser, TOKEN_IDENTIFIER); + + consume_token(parser, TOKEN_LPAREN); + result->min_value = parse_integer(parser); + consume_token(parser, TOKEN_RANGE); + result->max_value = parse_integer(parser); + consume_token(parser, TOKEN_RPAREN); + + return result; +} +Main Program +To demonstrate the lexer and parser: + + +c +#include +#include "parser.h" + +int main() { + const char* source_code = "ConstrainedInt(0->100)"; + Parser* parser = create_parser(source_code); + + ConstrainedIntType* int_type = parse_constrained_int_type(parser); + printf("Parsed constrained int type:\n"); + printf("Name: %s\n", int_type->name); + printf("Range: %d -> %d\n", int_type->min_value, int_type->max_value); + + destroy_parser(parser); + free(int_type->name); + free(int_type); + + return 0; +} +Compilation and Execution +Create files for the above code: +lexer.h +lexer.c +parser.h +parser.c +main.c +Compile using: + +bash +gcc -o parser lexer.c parser.c main.c +Run: + +bash +./parser +Output + +Parsed constrained int type: +Name: ConstrainedInt +Range: 0 -> 100 +This provides a basic foundation for a lexer and parser in C that can recognize integer types with specific ranges. You can extend this further by adding more language features. \ No newline at end of file diff --git a/test/fixtures/paste/chatgpt/code-tables-php-copy-button/fixture.json b/test/fixtures/paste/chatgpt/code-tables-php-copy-button/fixture.json new file mode 100644 index 0000000..9484148 --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-tables-php-copy-button/fixture.json @@ -0,0 +1,24 @@ +{ + "source": "chatgpt", + "case": "code-tables-php-copy-button", + "app": "Chrome/157.0.8103.78 (chrome-headless-shell)", + "platform": "macos", + "capture": "real", + "how": "Opened the public ChatGPT share page and clicked the site's own \"Copy response\" button under the longest assistant answer (PHP and other code blocks plus tables).", + "url": "https://chatgpt.com/share/6ad8ad3d-3f6c-83e9-abf0-41da8a0d6573", + "notes": "Copy-button capture. The site writes through navigator.clipboard.write, so there is no org.chromium.source-url. text/plain is the answer as markdown source (pipe tables, ``` fences, $...$ / $$...$$ math, hard line breaks as a trailing backslash); text/html is the app's rendered DOM full of hydration comments, wrapped in , with no inline styles. Fenced code (3
, classes language-text and language-c, so languages survive here), 3 tables, nested lists.",
+  "items": [
+    {
+      "representations": [
+        {
+          "type": "public.utf8-plain-text",
+          "file": "public.utf8-plain-text.txt"
+        },
+        {
+          "type": "public.html",
+          "file": "public.html.html"
+        }
+      ]
+    }
+  ]
+}
diff --git a/test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.html.html b/test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.html.html
new file mode 100644
index 0000000..a2070d2
--- /dev/null
+++ b/test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.html.html
@@ -0,0 +1,2 @@
+

Yes—Chainguard’s published packages contain substantive security backports. The changes address both request-startup/session state and PHP’s handling of memory exhaustion. They go considerably further than disabling upload progress.

For your WHM/cPanel server, the practical conclusion is that session.upload_progress.enabled=Off remains a useful precaution, but you should still install PHP packages that your vendor explicitly identifies as fixing both CVEs.

I examined the image metadata, compared the previous and updated packages, recovered their build recipes, and compared shipped development headers and VM code with the corresponding PHP release source. Here is what I found.

1. What Chainguard actually published

At the time of my checks on 21 October 2026, the image SBOMs reported these PHP package revisions:

Image tags checkedPHP package revision
latest, latest-fpm8.5.11-r4
8.4, 8.4-fpm8.4.26-r4
8.3, 8.3-fpm8.3.35-r4
8.28.2.34-r4

The links above are the registry’s machine-readable SBOM attestations.

For PHP 8.5, I confirmed the transition from 8.5.11-r3 to 8.5.11-r4. The updated package retains the PHP 8.5.11 source base and adds these two patch files:

GHSA-w8hw-w9fx-j79w.patch
+GHSA-w8hw-w9fx-j79w-fixup.patch

It also adds a step to regenerate PHP’s VM executor, rebuild its compiled execution objects, and test the new memory-limit behavior. Those steps are recorded in the build recipe embedded in the published r4 package; they are absent from the previous r3 package.

The -r4 suffix matters. This is a downstream backport: an upstream-looking version such as 8.5.11 does not, by itself, establish whether the fix is installed. The same principle applies when you inspect an EasyApache package—check its complete vendor version and release.

How I obtained the source for review

The two original patch files were not retrievable through the source location referenced by the package. However, Fedora had made the PHP 8.5.12 source archive available. I downloaded it and checked its SHA-512 against Fedora’s published source manifest.

That archive’s NEWS explicitly groups:

  • CVE-2026-109618 and CVE-2026-109619;

  • GHSA-w8hw-w9fx-j79w and GHSA-6wmf-4rv5-p3rp;

under “Avoid state corruption via bailout.” This is in the PHP 8.5.12 source archive available through Fedora.

I then corroborated the relevant changes against Chainguard’s actual shipped headers and VM code. The implementation details below come from that source comparison. I cannot separately attribute individual changes to the main patch versus its -fixup file.

2. There is a direct fix for the updated blog’s stale-serialization-state finding

This is the part most directly relevant to your original question.

PHP now initializes certain request state before request-data processing can invoke the session upload-progress handler. The new helper, php_basic_early_rinit(), resets:

  • BG(serialize_lock);

  • BG(serialize);

  • BG(unserialize);

  • related URL-rewriter state.

The source comment explicitly explains why: the upload-progress handler can serialize or unserialize session data before normal module request initialization, usually called RINIT. The helper is now called early enough to precede that processing.

Why that matters: if an earlier request terminated while serialization bookkeeping was still populated, a subsequent request’s early upload-progress processing must not reuse that stale state. Moving initialization earlier directly addresses that lifecycle problem.

This is consistent with the updated article’s surviving concern about cross-request serialization state. It is more specific than a generic reference-count adjustment; the existing Z_TRY_ADDREF(progress->data) is still present.

Source locations: main/main.c, ext/standard/basic_functions.c, and the URL-scanner initialization code in the reviewed release source.

3. Upload-progress session parsing is restricted during startup

The corresponding source also introduces a special mode for reading and rewriting sessions during the early upload-progress pass.

In that mode, objects are represented internally as __PHP_Incomplete_Class, without resolving their application classes. That prevents this early processing from invoking:

  • Autoloaders.

  • unserialize_callback_func.

  • __wakeup().

  • __unserialize().

The serializer preserves the necessary object and enum representations so the session data can be written back without running those callbacks. This restriction is scoped to the early upload-progress pass.

There are several associated fixes:

  • Session globals are reset when multipart processing starts.

  • Upload-progress update and cleanup check whether session initialization actually succeeded.

  • The callback no longer runs through request_parse_body() after modules have been activated; the source specifically identifies that case as corrupting the session.

  • The temporary $_SESSION populated during the restricted early pass is removed afterward.

These changes make the startup path’s assumptions explicit: it can maintain progress information, but it must do so with correctly initialized state and without invoking application object behavior prematurely.

Source locations: ext/session/session.c, ext/standard/var.c, and ext/standard/var_unserializer.re in the release source.

4. The larger change: memory exhaustion is handled at safer boundaries

The engine changes are broader than session handling.

Previously, exceeding PHP’s memory limit could cause the allocator to report a fatal error and call zend_bailout(). A bailout is a nonlocal jump out of the current C execution path. If it interrupts an operation partway through updating internal state, ordinary cleanup and state restoration can be skipped.

The reviewed implementation introduces deferred out-of-memory handling:

  1. Record that memory exhaustion occurred, including the original error location.

  2. Mark an out-of-memory condition as pending.

  3. Signal the VM that it needs attention.

  4. Allow relevant internal routines to reach cleanup paths.

  5. Handle the fatal error and unwind execution at a controlled boundary.

The VM changes include handling failures that occur between opcodes, cleaning up unfinished call frames, and checking for pending memory errors around internal function calls. The actual Chainguard VM header contains these new handling paths.

This does not make memory exhaustion an ordinary recoverable application exception. The request still fails. It also does not eliminate every zend_bailout() in PHP: startup without an application execution frame, timeouts, and other fatal-error paths have their own behavior.

Source locations: Zend/zend_alloc.c, Zend/zend_execute_API.c, Zend/zend_vm_def.h, and generated Zend/zend_vm_execute.h; corroborated against the Chainguard r4 development package.

A new hard memory limit provides bounded cleanup headroom

To make deferred failure possible, ordinary allocations can temporarily go beyond memory_limit while the engine reaches a suitable stopping point.

A new directive controls a separate ceiling:

memory_hard_limit_percent

In the reviewed PHP 8.5 implementation, its default internal mode is -1. For a finite memory_limit, that means:

Existing memory_limitDefault hard threshold
128M256 MiB
256M512 MiB
1G2 GiB
2G3 GiB

The rule is twice the soft limit up to 1 GiB, then the soft limit plus 1 GiB, subject to the allocator’s maximum. An explicitly configured percentage must be at least 100.

These thresholds describe PHP-managed allocation accounting. They are not total worker RSS limits, and ordinary requests do not automatically consume the additional allowance. The extra headroom exists for exhaustion handling.

If an ordinary allocation would exceed the hard limit, the implementation logs a hard-limit error and calls:

_exit(137);

That immediately terminates the process, bypassing the normal PHP shutdown path. Consequently, exit status 137 accompanied by this new message can be PHP’s own deliberate exit; it does not, by itself, prove that the kernel’s OOM killer intervened.

Source locations: zend_mm_update_hard_limit() and zend_mm_hard_limit_error() in Zend/zend_alloc.c, plus the directive registration in main/main.c, in the reviewed release source.

Parsers and unserializers also gain explicit failure handling

The source adds allocation functions that can return failure when the soft limit would be exceeded. Updated callers can then release partially constructed data and return through their normal cleanup paths.

For example, multipart-buffer growth can preserve the original buffer when reallocation fails. Multipart parsing, ordinary POST-variable processing, and unserialization gain checks for pending memory exhaustion and cleanup of partially constructed values.

This is another reason the upload-progress switch cannot be equated with the complete fix: some changed request-processing paths operate independently of upload progress.

Source locations: Zend/zend_alloc.h, main/rfc1867.c, main/php_variables.c, main/SAPI.c, and ext/standard/var_unserializer.re in the reviewed source.

5. What the package tests establish—and what I checked about the backport

Chainguard’s new package-level regression test is designed to require the following behavior:

CaseRequired result in the test definition
Soft-limit exhaustion with memory_limit=8MFatal memory error and exit status 255, while reaching a marker after the allocation and a registered shutdown function
Allocation above the hard threshold with the same soft limitHard-limit error identifying a 16 MiB threshold and exit status 137
Configuration inspectionThe new memory_hard_limit_percent directive exists

These assertions are embedded in the published package’s build metadata. I inspected the definitions; I did not independently execute the tests.

The corresponding release source also adds regressions for stale unserialization bookkeeping, partially built call frames, request parsing under memory pressure, and preventing user-code execution during upload-progress processing.

The comparison supports a security backport, with a clear review limit

Six relevant headers in Chainguard’s shipped development package are byte-for-byte identical to the staged PHP 8.5.12 source, including the allocator, executor globals, session globals, early-initialization declaration, and serialization interfaces.

The VM template and generated executor are not completely identical to the full 8.5.12 release. I investigated that difference rather than assuming it was a missing security change:

Those differences are consistent with backporting the security changes onto 8.5.11 while the full 8.5.12 release also contains other bug fixes.

My confidence is strong that the published r4 packages contain the substantive patch family described here. The limits are that I did not retrieve the two original standalone patch files, independently execute their regression suite, or validate exploit resistance on your server. The evidence also does not justify assigning one patch file to each CVE—the release notes group the two vulnerabilities together.

6. What this means for your WHM/cPanel server

The patch review strengthens the case for updating the actual PHP runtime.

  1. Keep upload progress disabled while you obtain the vendor fix. That reduces exposure to the specific early session-processing path. It does not reproduce the engine, parser, and lifecycle changes above.

  2. Use a cPanel/EasyApache package explicitly documented to cover both CVEs, for every PHP branch serving your sites. Chainguard’s images are separate container runtimes; their publication does not update your installed ea-phpXX packages. cPanel manages its PHP packages through EasyApache 4 and the server’s package manager. (Chainguard)

  3. Verify the complete vendor package revision and recycle the affected PHP workers after updating. A backport can retain the upstream version number. Verify the runtime used by the websites, as well as any separate CLI jobs.

  4. Do not try to obtain this protection by adding memory_hard_limit_percent to an unpatched php.ini. The protection requires new compiled PHP code. I would initially retain the patched vendor’s default behavior rather than reduce the headroom as an improvised mitigation.

  5. Account for the changed exhaustion behavior operationally. After patching, review PHP worker memory budgets and logs with the temporary headroom and deliberate hard-limit exits in mind. That is particularly relevant if your FPM concurrency was sized using memory_limit as though it were a strict total-memory ceiling.

Finally, the updated blog’s observations about whether particular workers survive failed startup should not be used to exempt FPM or another handler from this update. The official PHP notice concerns two critical vulnerabilities and encourages all PHP users to upgrade; the source changes show why focusing exclusively on the upload-progress demonstration is too narrow. (2026)


If you want, I can:

  • Explain implications of Chainguard PHP patch for cPanel security

  • Detail the new PHP memory exhaustion handling introduced by the patches

  • Summarize the fixes related to PHP upload-progress session handling

\ No newline at end of file diff --git a/test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.utf8-plain-text.txt b/test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.utf8-plain-text.txt new file mode 100644 index 0000000..f16a222 --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-tables-php-copy-button/public.utf8-plain-text.txt @@ -0,0 +1,203 @@ +**Yes—Chainguard’s published packages contain substantive security backports. The changes address both request-startup/session state and PHP’s handling of memory exhaustion.** They go considerably further than disabling upload progress. + +For your WHM/cPanel server, the practical conclusion is that **`session.upload_progress.enabled=Off` remains a useful precaution, but you should still install PHP packages that your vendor explicitly identifies as fixing both CVEs.** + +I examined the image metadata, compared the previous and updated packages, recovered their build recipes, and compared shipped development headers and VM code with the corresponding PHP release source. Here is what I found. + +## 1. What Chainguard actually published + +At the time of my checks on **21 October 2026**, the image SBOMs reported these PHP package revisions: + +| Image tags checked | PHP package revision | +| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------- | +| `latest`, `latest-fpm` | [8.5.11-r4](https://cgr.dev/v2/chainguard/php/blobs/sha256:8ae239bb82996bae9cbf28d9aac3ede5a37eb47c6decfcfa4d41dd3eb1e79202) | +| `8.4`, `8.4-fpm` | [8.4.26-r4](https://cgr.dev/v2/chainguard/php/blobs/sha256:ad15d4f251666381cfa7c08f77228841ed1b489ccee57efcbfa54f5e2db3127b) | +| `8.3`, `8.3-fpm` | [8.3.35-r4](https://cgr.dev/v2/chainguard/php/blobs/sha256:bc776fd9d24d3293fccd4c2228af525be1f9732435a4c7376e00e8b3bc0709dc) | +| `8.2` | [8.2.34-r4](https://cgr.dev/v2/chainguard/php/blobs/sha256:e42aa373672f2732cda622e02b8afc3cf1a1e85dac64b25f595988ac45e9b9f4) | + +The links above are the registry’s machine-readable SBOM attestations. + +For PHP 8.5, I confirmed the transition from **`8.5.11-r3` to `8.5.11-r4`**. The updated package retains the PHP 8.5.11 source base and adds these two patch files: + +```text +GHSA-w8hw-w9fx-j79w.patch +GHSA-w8hw-w9fx-j79w-fixup.patch +``` + +It also adds a step to regenerate PHP’s VM executor, rebuild its compiled execution objects, and test the new memory-limit behavior. Those steps are recorded in the build recipe embedded in the [published r4 package](https://packages.wolfi.dev/os/x86_64/php-8.5-8.5.11-r4.apk); they are absent from the [previous r3 package](https://packages.wolfi.dev/os/x86_64/php-8.5-8.5.11-r3.apk). + +**The `-r4` suffix matters.** This is a downstream backport: an upstream-looking version such as `8.5.11` does not, by itself, establish whether the fix is installed. The same principle applies when you inspect an EasyApache package—check its complete vendor version and release. + +### How I obtained the source for review + +The two original patch files were not retrievable through the source location referenced by the package. However, Fedora had made the **PHP 8.5.12 source archive** available. I downloaded it and checked its SHA-512 against [Fedora’s published source manifest](https://src.fedoraproject.org/rpms/php/raw/rawhide/f/sources). + +That archive’s `NEWS` explicitly groups: + +* `CVE-2026-109618` and `CVE-2026-109619`; +* `GHSA-w8hw-w9fx-j79w` and `GHSA-6wmf-4rv5-p3rp`; + +under **“Avoid state corruption via bailout.”** This is in the [PHP 8.5.12 source archive available through Fedora](https://src.fedoraproject.org/repo/pkgs/rpms/php/php-8.5.12.tar.xz/sha512/0a6615373cb4842a3e99632e0903f062f89410ffdc63fc754218ac64de7263119df3af2ec2570457fdc1c3108e5eecd9569dd3bab2769d8d280361bd5e25025b/php-8.5.12.tar.xz). + +I then corroborated the relevant changes against Chainguard’s actual shipped headers and VM code. The implementation details below come from that source comparison. I cannot separately attribute individual changes to the main patch versus its `-fixup` file. + +## 2. There is a direct fix for the updated blog’s stale-serialization-state finding + +This is the part most directly relevant to your original question. + +PHP now initializes certain request state **before request-data processing can invoke the session upload-progress handler**. The new helper, `php_basic_early_rinit()`, resets: + +* `BG(serialize_lock)`; +* `BG(serialize)`; +* `BG(unserialize)`; +* related URL-rewriter state. + +The source comment explicitly explains why: the upload-progress handler can serialize or unserialize session data **before normal module request initialization**, usually called RINIT. The helper is now called early enough to precede that processing. + +**Why that matters:** if an earlier request terminated while serialization bookkeeping was still populated, a subsequent request’s early upload-progress processing must not reuse that stale state. Moving initialization earlier directly addresses that lifecycle problem. + +This is consistent with the updated article’s surviving concern about cross-request serialization state. It is more specific than a generic reference-count adjustment; the existing `Z_TRY_ADDREF(progress->data)` is still present. + +*Source locations: `main/main.c`, `ext/standard/basic_functions.c`, and the URL-scanner initialization code in the reviewed [release source](https://src.fedoraproject.org/repo/pkgs/rpms/php/php-8.5.12.tar.xz/sha512/0a6615373cb4842a3e99632e0903f062f89410ffdc63fc754218ac64de7263119df3af2ec2570457fdc1c3108e5eecd9569dd3bab2769d8d280361bd5e25025b/php-8.5.12.tar.xz).* + +## 3. Upload-progress session parsing is restricted during startup + +The corresponding source also introduces a special mode for reading and rewriting sessions during the early upload-progress pass. + +In that mode, objects are represented internally as `__PHP_Incomplete_Class`, without resolving their application classes. That prevents this early processing from invoking: + +* Autoloaders. +* `unserialize_callback_func`. +* `__wakeup()`. +* `__unserialize()`. + +The serializer preserves the necessary object and enum representations so the session data can be written back without running those callbacks. **This restriction is scoped to the early upload-progress pass.** + +There are several associated fixes: + +* Session globals are reset when multipart processing starts. +* Upload-progress update and cleanup check whether session initialization actually succeeded. +* The callback no longer runs through `request_parse_body()` after modules have been activated; the source specifically identifies that case as corrupting the session. +* The temporary `$_SESSION` populated during the restricted early pass is removed afterward. + +These changes make the startup path’s assumptions explicit: it can maintain progress information, but it must do so with correctly initialized state and without invoking application object behavior prematurely. + +*Source locations: `ext/session/session.c`, `ext/standard/var.c`, and `ext/standard/var_unserializer.re` in the [release source](https://src.fedoraproject.org/repo/pkgs/rpms/php/php-8.5.12.tar.xz/sha512/0a6615373cb4842a3e99632e0903f062f89410ffdc63fc754218ac64de7263119df3af2ec2570457fdc1c3108e5eecd9569dd3bab2769d8d280361bd5e25025b/php-8.5.12.tar.xz).* + +## 4. The larger change: memory exhaustion is handled at safer boundaries + +The engine changes are broader than session handling. + +Previously, exceeding PHP’s memory limit could cause the allocator to report a fatal error and call `zend_bailout()`. A bailout is a nonlocal jump out of the current C execution path. If it interrupts an operation partway through updating internal state, ordinary cleanup and state restoration can be skipped. + +The reviewed implementation introduces **deferred out-of-memory handling**: + +1. Record that memory exhaustion occurred, including the original error location. +2. Mark an out-of-memory condition as pending. +3. Signal the VM that it needs attention. +4. Allow relevant internal routines to reach cleanup paths. +5. Handle the fatal error and unwind execution at a controlled boundary. + +The VM changes include handling failures that occur **between opcodes**, cleaning up unfinished call frames, and checking for pending memory errors around internal function calls. The actual Chainguard VM header contains these new handling paths. + +This does **not** make memory exhaustion an ordinary recoverable application exception. The request still fails. It also does not eliminate every `zend_bailout()` in PHP: startup without an application execution frame, timeouts, and other fatal-error paths have their own behavior. + +*Source locations: `Zend/zend_alloc.c`, `Zend/zend_execute_API.c`, `Zend/zend_vm_def.h`, and generated `Zend/zend_vm_execute.h`; corroborated against the [Chainguard r4 development package](https://packages.wolfi.dev/os/x86_64/php-8.5-dev-8.5.11-r4.apk).* + +### A new hard memory limit provides bounded cleanup headroom + +To make deferred failure possible, ordinary allocations can temporarily go beyond `memory_limit` while the engine reaches a suitable stopping point. + +A new directive controls a separate ceiling: + +```text +memory_hard_limit_percent +``` + +In the reviewed PHP 8.5 implementation, its default internal mode is `-1`. For a finite `memory_limit`, that means: + +| Existing `memory_limit` | Default hard threshold | +| ----------------------- | ---------------------: | +| `128M` | 256 MiB | +| `256M` | 512 MiB | +| `1G` | 2 GiB | +| `2G` | 3 GiB | + +The rule is twice the soft limit up to 1 GiB, then the soft limit plus 1 GiB, subject to the allocator’s maximum. An explicitly configured percentage must be at least 100. + +These thresholds describe **PHP-managed allocation accounting**. They are not total worker RSS limits, and ordinary requests do not automatically consume the additional allowance. The extra headroom exists for exhaustion handling. + +If an ordinary allocation would exceed the hard limit, the implementation logs a hard-limit error and calls: + +```c +_exit(137); +``` + +That immediately terminates the process, bypassing the normal PHP shutdown path. Consequently, **exit status 137 accompanied by this new message can be PHP’s own deliberate exit**; it does not, by itself, prove that the kernel’s OOM killer intervened. + +*Source locations: `zend_mm_update_hard_limit()` and `zend_mm_hard_limit_error()` in `Zend/zend_alloc.c`, plus the directive registration in `main/main.c`, in the reviewed [release source](https://src.fedoraproject.org/repo/pkgs/rpms/php/php-8.5.12.tar.xz/sha512/0a6615373cb4842a3e99632e0903f062f89410ffdc63fc754218ac64de7263119df3af2ec2570457fdc1c3108e5eecd9569dd3bab2769d8d280361bd5e25025b/php-8.5.12.tar.xz).* + +### Parsers and unserializers also gain explicit failure handling + +The source adds allocation functions that can return failure when the soft limit would be exceeded. Updated callers can then release partially constructed data and return through their normal cleanup paths. + +For example, multipart-buffer growth can preserve the original buffer when reallocation fails. Multipart parsing, ordinary POST-variable processing, and unserialization gain checks for pending memory exhaustion and cleanup of partially constructed values. + +This is another reason the upload-progress switch cannot be equated with the complete fix: **some changed request-processing paths operate independently of upload progress.** + +*Source locations: `Zend/zend_alloc.h`, `main/rfc1867.c`, `main/php_variables.c`, `main/SAPI.c`, and `ext/standard/var_unserializer.re` in the reviewed source.* + +## 5. What the package tests establish—and what I checked about the backport + +Chainguard’s new package-level regression test is designed to require the following behavior: + +| Case | Required result in the test definition | +| ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- | +| Soft-limit exhaustion with `memory_limit=8M` | Fatal memory error and exit status `255`, while reaching a marker after the allocation and a registered shutdown function | +| Allocation above the hard threshold with the same soft limit | Hard-limit error identifying a 16 MiB threshold and exit status `137` | +| Configuration inspection | The new `memory_hard_limit_percent` directive exists | + +These assertions are embedded in the [published package’s build metadata](https://packages.wolfi.dev/os/x86_64/php-8.5-8.5.11-r4.apk). I inspected the definitions; I did not independently execute the tests. + +The corresponding release source also adds regressions for stale unserialization bookkeeping, partially built call frames, request parsing under memory pressure, and preventing user-code execution during upload-progress processing. + +### The comparison supports a security backport, with a clear review limit + +Six relevant headers in Chainguard’s shipped development package are **byte-for-byte identical** to the staged PHP 8.5.12 source, including the allocator, executor globals, session globals, early-initialization declaration, and serialization interfaces. + +The VM template and generated executor are not completely identical to the full 8.5.12 release. I investigated that difference rather than assuming it was a missing security change: + +* The VM template differs by changes corresponding exactly to an earlier [include/eval exception-handling fix](https://github.com/php/php-src/commit/e7b225697ef2251e3ecfdd21984489c633f94101). +* It also differs by an earlier [constant-comparison fix](https://github.com/php/php-src/commit/7c94b351dbb44f51cf210540f49cddae64fe2037). +* The new delayed-memory-error handling and unfinished-call cleanup are present in Chainguard’s generated VM. + +Those differences are consistent with backporting the security changes onto 8.5.11 while the full 8.5.12 release also contains other bug fixes. + +**My confidence is strong that the published r4 packages contain the substantive patch family described here.** The limits are that I did not retrieve the two original standalone patch files, independently execute their regression suite, or validate exploit resistance on your server. The evidence also does not justify assigning one patch file to each CVE—the release notes group the two vulnerabilities together. + +## 6. What this means for your WHM/cPanel server + +The patch review strengthens the case for updating the actual PHP runtime. + +1. **Keep upload progress disabled while you obtain the vendor fix.** That reduces exposure to the specific early session-processing path. It does not reproduce the engine, parser, and lifecycle changes above. + +2. **Use a cPanel/EasyApache package explicitly documented to cover both CVEs, for every PHP branch serving your sites.** Chainguard’s images are separate container runtimes; their publication does not update your installed `ea-phpXX` packages. cPanel manages its PHP packages through EasyApache 4 and the server’s package manager. ([Chainguard][1]) + +3. **Verify the complete vendor package revision and recycle the affected PHP workers after updating.** A backport can retain the upstream version number. Verify the runtime used by the websites, as well as any separate CLI jobs. + +4. **Do not try to obtain this protection by adding `memory_hard_limit_percent` to an unpatched `php.ini`.** The protection requires new compiled PHP code. I would initially retain the patched vendor’s default behavior rather than reduce the headroom as an improvised mitigation. + +5. **Account for the changed exhaustion behavior operationally.** After patching, review PHP worker memory budgets and logs with the temporary headroom and deliberate hard-limit exits in mind. That is particularly relevant if your FPM concurrency was sized using `memory_limit` as though it were a strict total-memory ceiling. + +Finally, the updated blog’s observations about whether particular workers survive failed startup should not be used to exempt FPM or another handler from this update. The official PHP notice concerns two critical vulnerabilities and encourages all PHP users to upgrade; the source changes show why focusing exclusively on the upload-progress demonstration is too narrow. ([2026][2]) + +--- + +If you want, I can: + +* Explain implications of Chainguard PHP patch for cPanel security +* Detail the new PHP memory exhaustion handling introduced by the patches +* Summarize the fixes related to PHP upload-progress session handling + +[1]: https://images.chainguard.dev/directory/image/php/overview?utm_source=chatgpt.com "php Secure-by-Default Container Image" +[2]: https://www.php.net/archive/2026.php?utm_source=chatgpt.com "PHP: News Archive" diff --git a/test/fixtures/paste/chatgpt/code-tables-php-selection/fixture.json b/test/fixtures/paste/chatgpt/code-tables-php-selection/fixture.json new file mode 100644 index 0000000..6eef874 --- /dev/null +++ b/test/fixtures/paste/chatgpt/code-tables-php-selection/fixture.json @@ -0,0 +1,28 @@ +{ + "source": "chatgpt", + "case": "code-tables-php-selection", + "app": "Chrome/157.0.8103.78 (chrome-headless-shell)", + "platform": "macos", + "capture": "real", + "how": "Opened the public ChatGPT share page and selected the longest assistant answer (PHP and other code blocks plus tables) from its first block to its last with a DOM range (what a mouse drag over the answer yields), then ran document.execCommand(\"copy\").", + "url": "https://chatgpt.com/share/6ad8ad3d-3f6c-83e9-abf0-41da8a0d6573", + "notes": "Selection capture (DOM range + execCommand('copy')), so it is Chrome's serialization: org.chromium.source-url is the page URL, every element carries a computed inline style=, the first block starts with
, and plain text is the rendered text (UI labels such as 'Thought for ...' / 'Worked for ...' can lead it; tokens of rendered math sit on separate lines). Code blocks lose their language-* classes in this path. 6
, 3 tables, 6