All files / json-crdt-extensions/peritext/transfer import-html.ts

77.24% Statements 112/145
77.41% Branches 48/62
58.33% Functions 7/12
77.31% Lines 92/119

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 1987x 7x 7x 7x 7x 7x 7x 7x                                           42x 42x     346x 346x 157x 155x 155x   189x 189x 189x 189x 189x 189x 189x 189x 78x   78x     78x 78x 78x 78x   304x 189x 51x 51x 51x 51x 51x 51x       51x 51x 51x 51x     189x       42x 42x 42x       42x     7x     7x             7x   7x 433x 241x 241x 241x 241x 241x 238x 238x   238x 238x 2x 2x       238x 238x 238x 380x 380x 371x   238x 238x 183x 183x   55x 55x 55x 55x         55x 55x   238x 238x 232x     7x 53x 53x     7x 26x 26x     7x   7x         7x                                               7x               7x        
import {html as _html} from 'very-small-parser/lib/html';
import {fromHast as _fromHast} from 'very-small-parser/lib/html/json-ml/fromHast';
import {SliceTypeName} from '../slice';
import {SliceStacking, SliceHeaderShift} from '../slice/constants';
import {Anchor} from '../rga/constants';
import {toPlainText} from 'very-small-parser/lib/toPlainText';
import {walk} from 'very-small-parser/lib/html/json-ml/walk';
import {fromBase64} from '@jsonjoy.com/base64/lib/fromBase64';
import type {JsonMlNode} from 'very-small-parser/lib/html/json-ml/types';
import type {THtmlToken} from 'very-small-parser/lib/html/types';
import type {PeritextMlNode} from '../block/types';
import type {SliceRegistry} from '../registry/SliceRegistry';
import type {ViewStyle, ViewRange, ViewSlice} from '../editor/types';
import type {ClipboardData} from './export-html';
 
/**
 * @todo Implement HTML normalization function, ensure that:
 *
 * - <blockquote> and <p> nodes are treated correctly, especially when sole node
 *   is nested.
 * - list nodes are treated correctly.
 * - <svg> nodes are converted to Base64 and inlined as data URL images.
 */
 
/**
 * Flattens a {@link PeritextMlNode} tree structure into a {@link ViewRange}
 * flat string with annotation ranges.
 */
class ViewRangeBuilder {
  private text = '';
  private slices: ViewSlice[] = [];
 
  private build0(node: PeritextMlNode, path: (string | number)[]): boolean {
    const skipWhitespace = path.length < 2;
    if (typeof node === 'string') {
      if (skipWhitespace && !node.trim()) return false;
      this.text += node;
      return false;
    }
    const [type, attr] = node;
    const start = this.text.length;
    const length = node.length;
    const inline = !!attr?.inline;
    const hasType = type === 0 || !!type;
    const firstChild = node[2] as PeritextMlNode;
    const isFirstChildInline = firstChild && (typeof firstChild === 'string' || firstChild[1]?.inline);
    if (hasType && !inline && isFirstChildInline) {
      this.text += '\n';
      const header =
        (SliceStacking.Marker << SliceHeaderShift.Stacking) +
        (Anchor.Before << SliceHeaderShift.X1Anchor) +
        (Anchor.Before << SliceHeaderShift.X2Anchor);
      const slice: ViewSlice = [header, start, start, path.length ? [...path, type] : type];
      const data = attr?.data;
      Iif (data) slice.push(data);
      this.slices.push(slice);
    }
    for (let i = 2; i < length; i++) this.build0(node[i] as PeritextMlNode, type === '' ? path : [...path, type]);
    if (hasType && inline) {
      let end: number = 0,
        header: number = 0;
      if (inline) {
        end = this.text.length;
        const stacking: SliceStacking = attr?.stacking ?? SliceStacking.Many;
        header =
          (stacking << SliceHeaderShift.Stacking) +
          (Anchor.Before << SliceHeaderShift.X1Anchor) +
          (Anchor.After << SliceHeaderShift.X2Anchor);
        const slice: ViewSlice = [header, start, end, type];
        const data = attr?.data;
        Iif (data) slice.push(data);
        this.slices.push(slice);
      }
    }
    return false;
  }
 
  public build(node: PeritextMlNode): ViewRange {
    this.build0(node, []);
    const view: ViewRange = [this.text, 0, this.slices];
    return view;
  }
}
 
export const toViewRange = (node: PeritextMlNode): ViewRange => new ViewRangeBuilder().build(node);
 
// HTML elements to completely ignore.
const IGNORE_TAGS = new Set<string>(['meta', 'style', 'script', 'link', 'head']);
 
// HTML elements to rewrite as different block elements.
const BLOCK_TAGS_REWRITE = new Map<string, string>([
  ['html', ''],
  ['body', ''],
  ['div', ''],
]);
 
// HTML elements to rewrite as different inline elements.
const INLINE_TAGS_REWRITE = new Map<string, string>([['span', '']]);
 
export const fromJsonMl = (jsonml: JsonMlNode, registry: SliceRegistry): PeritextMlNode => {
  if (typeof jsonml === 'string') return jsonml;
  let tag = jsonml[0];
  let inlineHtmlTag = false;
  if (typeof tag === 'string') {
    tag = tag.toLowerCase();
    if (IGNORE_TAGS.has(tag)) return '';
    const mapped = BLOCK_TAGS_REWRITE.get(tag);
    Iif (mapped !== undefined) tag = mapped;
    else {
      const mapped = INLINE_TAGS_REWRITE.get(tag);
      if (mapped !== undefined) {
        tag = mapped;
        inlineHtmlTag = true;
      }
    }
  }
  const length = jsonml.length;
  const node: PeritextMlNode = [tag, null];
  for (let i = 2; i < length; i++) {
    const peritextNode = fromJsonMl(jsonml[i] as JsonMlNode, registry);
    if (!peritextNode) continue;
    node.push(peritextNode);
  }
  const res = registry.fromHtml(jsonml);
  if (res) {
    node[0] = res[0];
    node[1] = res[1];
  } else {
    if (typeof tag === 'string') node[0] = SliceTypeName[tag as any] ?? tag;
    const attr = jsonml[1] || {};
    let data = null;
    Iif (attr['data-attr'] !== void 0) {
      try {
        data = JSON.parse(attr['data-attr']);
      } catch {}
    }
    const inline = inlineHtmlTag || attr['data-inline'] === 'true';
    if (data || inline) node[1] = {data, inline};
  }
  if (typeof node[0] === 'number' && node[0] < 0) (node[1] ||= {}).inline = true;
  if (node.length < 3 && (node[1] || {}).inline) return '';
  return node;
};
 
export const fromHast = (hast: THtmlToken, registry: SliceRegistry): PeritextMlNode => {
  const jsonml = _fromHast(hast);
  return fromJsonMl(jsonml, registry);
};
 
export const fromHtml = (html: string, registry: SliceRegistry): PeritextMlNode => {
  const hast = _html.parsef(html);
  return fromHast(hast, registry);
};
 
export const htmlToHast = (html: string): THtmlToken => _html.parsef(html);
 
export const textFromHtml = (html: string): string => {
  const hast = _html.parsef(html);
  return toPlainText(hast);
};
 
const getExportData = (html: string): [jsonml: undefined | JsonMlNode, exportData?: ClipboardData] => {
  const attrName = 'data-json-joy-peritext';
  const maybeHasPeritextExport = html.includes(attrName);
  const hast = _html.parsef(html);
  const jsonml = _fromHast(hast);
  Iif (maybeHasPeritextExport) {
    const iterator = walk(jsonml);
    let node: JsonMlNode | undefined;
    while ((node = iterator())) {
      Iif (node && typeof node === 'object') {
        const [tag, attr] = node;
        Iif (attr?.[attrName]) {
          const jsonBase64 = attr[attrName];
          const buffer = fromBase64(jsonBase64);
          const json = new TextDecoder().decode(buffer);
          const data: ClipboardData = JSON.parse(json);
          return [void 0, data];
        }
      }
    }
  }
  return [jsonml];
};
 
export const importHtml = (html: string, registry: SliceRegistry): [view?: ViewRange, style?: ViewStyle[]] => {
  const [jsonml, data] = getExportData(html);
  Iif (data?.style) return [void 0, data.style];
  Iif (data?.view) return [data.view];
  const node = fromJsonMl(jsonml!, registry);
  return [toViewRange(node)];
};
 
export const importStyle = (html: string): ViewStyle[] | undefined => {
  const [, data] = getExportData(html);
  return data?.style;
};