-
Notifications
You must be signed in to change notification settings - Fork 156
/
dom.js
220 lines (186 loc) · 6.06 KB
/
dom.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import {
NO_BREAK_SPACE,
TAB_CHARACTER
} from '../renderers/editor-dom';
import {
MARKUP_SECTION_TYPE,
LIST_SECTION_TYPE,
LIST_ITEM_TYPE
} from '../models/types';
import {
isTextNode,
normalizeTagName
} from '../utils/dom-utils';
import {
detect,
forEach,
} from '../utils/array-utils';
import { TAB } from 'mobiledoc-kit/utils/characters';
import SectionParser from 'mobiledoc-kit/parsers/section';
import { getAttributes, walkTextNodes } from '../utils/dom-utils';
import Markup from 'mobiledoc-kit/models/markup';
const GOOGLE_DOCS_CONTAINER_ID_REGEX = /^docs\-internal\-guid/;
const NO_BREAK_SPACE_REGEX = new RegExp(NO_BREAK_SPACE, 'g');
const TAB_CHARACTER_REGEX = new RegExp(TAB_CHARACTER, 'g');
export function transformHTMLText(textContent) {
let text = textContent;
text = text.replace(NO_BREAK_SPACE_REGEX, ' ');
text = text.replace(TAB_CHARACTER_REGEX, TAB);
return text;
}
function isGoogleDocsContainer(element) {
return !isTextNode(element) &&
normalizeTagName(element.tagName) === normalizeTagName('b') &&
GOOGLE_DOCS_CONTAINER_ID_REGEX.test(element.id);
}
function detectRootElement(element) {
let childNodes = element.childNodes || [];
let googleDocsContainer = detect(childNodes, isGoogleDocsContainer);
if (googleDocsContainer) {
return googleDocsContainer;
} else {
return element;
}
}
const TAG_REMAPPING = {
'b': 'strong',
'i': 'em'
};
function remapTagName(tagName) {
let normalized = normalizeTagName(tagName);
let remapped = TAG_REMAPPING[normalized];
return remapped || normalized;
}
function trim(str) {
return str.replace(/^\s+/, '').replace(/\s+$/, '');
}
/**
* Parses DOM element -> Post
*/
export default class DOMParser {
constructor(builder, options={}) {
this.builder = builder;
this.sectionParser = new SectionParser(this.builder, options);
}
parse(element) {
const post = this.builder.createPost();
let rootElement = detectRootElement(element);
this._eachChildNode(rootElement, child => {
let sections = this.parseSections(child);
this.appendSections(post, sections);
});
return post;
}
appendSections(post, sections) {
forEach(sections, section => this.appendSection(post, section));
}
appendSection(post, section) {
if (section.isBlank || (section.isMarkerable && trim(section.text) === '')) {
return;
}
let lastSection = post.sections.tail;
if (lastSection &&
lastSection._inferredTagName &&
section._inferredTagName &&
lastSection.tagName === section.tagName) {
lastSection.join(section);
} else {
post.sections.append(section);
}
}
_eachChildNode(element, callback) {
let nodes = isTextNode(element) ? [element] : element.childNodes;
forEach(nodes, node => callback(node));
}
parseSections(element) {
return this.sectionParser.parse(element);
}
// walk up from the textNode until the rootNode, converting each
// parentNode into a markup
collectMarkups(textNode, rootNode) {
let markups = [];
let currentNode = textNode.parentNode;
while (currentNode && currentNode !== rootNode) {
let markup = this.markupFromNode(currentNode);
if (markup) {
markups.push(markup);
}
currentNode = currentNode.parentNode;
}
return markups;
}
// Turn an element node into a markup
markupFromNode(node) {
if (Markup.isValidElement(node)) {
let tagName = remapTagName(node.tagName);
let attributes = getAttributes(node);
return this.builder.createMarkup(tagName, attributes);
}
}
// FIXME should move to the section parser?
// FIXME the `collectMarkups` logic could simplify the section parser?
reparseSection(section, renderTree) {
switch (section.type) {
case LIST_SECTION_TYPE:
return this.reparseListSection(section, renderTree);
case LIST_ITEM_TYPE:
return this.reparseListItem(section, renderTree);
case MARKUP_SECTION_TYPE:
return this.reparseMarkupSection(section, renderTree);
default:
return; // can only parse the above types
}
}
reparseMarkupSection(section, renderTree) {
return this._reparseSectionContainingMarkers(section, renderTree);
}
reparseListItem(listItem, renderTree) {
return this._reparseSectionContainingMarkers(listItem, renderTree);
}
reparseListSection(listSection, renderTree) {
listSection.items.forEach(li => this.reparseListItem(li, renderTree));
}
_reparseSectionContainingMarkers(section, renderTree) {
const element = section.renderNode.element;
let seenRenderNodes = [];
let previousMarker;
walkTextNodes(element, (textNode) => {
const text = transformHTMLText(textNode.textContent);
let markups = this.collectMarkups(textNode, element);
let marker;
let renderNode = renderTree.getElementRenderNode(textNode);
if (renderNode) {
if (text.length) {
marker = renderNode.postNode;
marker.value = text;
marker.markups = markups;
} else {
renderNode.scheduleForRemoval();
}
} else {
marker = this.builder.createMarker(text, markups);
renderNode = renderTree.buildRenderNode(marker);
renderNode.element = textNode;
renderNode.markClean();
let previousRenderNode = previousMarker && previousMarker.renderNode;
section.markers.insertAfter(marker, previousMarker);
section.renderNode.childNodes.insertAfter(renderNode, previousRenderNode);
let parentNodeCount = marker.closedMarkups.length;
let nextMarkerElement = textNode.parentNode;
while (parentNodeCount--) {
nextMarkerElement = nextMarkerElement.parentNode;
}
renderNode.nextMarkerElement = nextMarkerElement;
}
seenRenderNodes.push(renderNode);
previousMarker = marker;
});
let renderNode = section.renderNode.childNodes.head;
while (renderNode) {
if (seenRenderNodes.indexOf(renderNode) === -1) {
renderNode.scheduleForRemoval();
}
renderNode = renderNode.next;
}
}
}