-
Notifications
You must be signed in to change notification settings - Fork 144
/
fragmentHandler.ts
47 lines (44 loc) · 2.28 KB
/
fragmentHandler.ts
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
import convertPastedContentFromExcel from './excelConverter/convertPastedContentFromExcel';
import convertPastedContentFromWord from './wordConverter/convertPastedContentFromWord';
import convertPastedContentFromWac, { isWAC } from './wacConverter/convertPastedContentFromWac';
import { getTagOfNode } from 'roosterjs-editor-dom';
import { splitWithFragment } from 'roosterjs-html-sanitizer';
const WORD_ATTRIBUTE_NAME = 'xmlns:w';
const WORD_ATTRIBUTE_VALUE = 'urn:schemas-microsoft-com:office:word';
const EXCEL_ATTRIBUTE_NAME = 'xmlns:x';
const EXCEL_ATTRIBUTE_VALUE = 'urn:schemas-microsoft-com:office:excel';
const LAST_TD_END_REGEX = /<\/\s*td\s*>((?!<\/\s*tr\s*>)[\s\S])*$/i;
const LAST_TR_END_REGEX = /<\/\s*tr\s*>((?!<\/\s*table\s*>)[\s\S])*$/i;
const LAST_TR_REGEX = /<tr[^>]*>[^<]*/i;
const LAST_TABLE_REGEX = /<table[^>]*>[^<]*/i;
export default function fragmentHandler(doc: HTMLDocument, source: string) {
let [html, before] = splitWithFragment(source);
let firstNode = doc && doc.body && (doc.querySelector('html') as HTMLElement);
if (getTagOfNode(firstNode) == 'HTML') {
if (firstNode.getAttribute(WORD_ATTRIBUTE_NAME) == WORD_ATTRIBUTE_VALUE) {
// Handle HTML copied from MS Word
doc.body.innerHTML = html;
convertPastedContentFromWord(doc);
} else if (isWAC(firstNode)) {
// The WAC converter only supports word for now.
convertPastedContentFromWac(doc);
} else if (firstNode.getAttribute(EXCEL_ATTRIBUTE_NAME) == EXCEL_ATTRIBUTE_VALUE) {
// Handle HTML copied from MS Excel
if (html.match(LAST_TD_END_REGEX)) {
let trMatch = before.match(LAST_TR_REGEX);
let tr = trMatch ? trMatch[0] : '<TR>';
html = tr + html + '</TR>';
}
if (html.match(LAST_TR_END_REGEX)) {
let tableMatch = before.match(LAST_TABLE_REGEX);
let table = tableMatch ? tableMatch[0] : '<TABLE>';
html = table + html + '</TABLE>';
}
doc.body.innerHTML = html;
convertPastedContentFromExcel(doc);
} else {
// Handle HTML copied from other places
doc.body.innerHTML = html;
}
}
}