This repository was archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathserialize-html.js
More file actions
200 lines (175 loc) · 6.11 KB
/
serialize-html.js
File metadata and controls
200 lines (175 loc) · 6.11 KB
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
var firepad = firepad || { };
/**
* Helper to turn Firebase contents into HMTL.
* Takes a doc and an entity manager
*/
firepad.SerializeHtml = (function () {
var utils = firepad.utils;
var ATTR = firepad.AttributeConstants;
var LIST_TYPE = firepad.LineFormatting.LIST_TYPE;
var TODO_STYLE = '<style>ul.firepad-todo { list-style: none; margin-left: 0; padding-left: 0; } ul.firepad-todo > li { padding-left: 1em; text-indent: -1em; } ul.firepad-todo > li:before { content: "\\2610"; padding-right: 5px; } ul.firepad-todo > li.firepad-checked:before { content: "\\2611"; padding-right: 5px; }</style>\n';
function open(listType) {
return (listType === LIST_TYPE.ORDERED) ? '<ol>' :
(listType === LIST_TYPE.UNORDERED) ? '<ul>' :
'<ul class="firepad-todo">';
}
function close(listType) {
return (listType === LIST_TYPE.ORDERED) ? '</ol>' : '</ul>';
}
function compatibleListType(l1, l2) {
return (l1 === l2) ||
(l1 === LIST_TYPE.TODO && l2 === LIST_TYPE.TODOCHECKED) ||
(l1 === LIST_TYPE.TODOCHECKED && l2 === LIST_TYPE.TODO);
}
function textToHtml(text) {
return text.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\u00a0/g, ' ')
}
function serializeHtml(doc, entityManager) {
var html = '';
var newLine = true;
var listTypeStack = [];
var inListItem = false;
var firstLine = true;
var emptyLine = true;
var i = 0, op = doc.ops[i];
var usesTodo = false;
while(op) {
utils.assert(op.isInsert());
var attrs = op.attributes;
if (newLine) {
newLine = false;
var indent = 0, listType = null, lineAlign = 'left';
if (ATTR.LINE_SENTINEL in attrs) {
indent = attrs[ATTR.LINE_INDENT] || 0;
listType = attrs[ATTR.LIST_TYPE] || null;
lineAlign = attrs[ATTR.LINE_ALIGN] || 'left';
}
if (listType) {
indent = indent || 1; // lists are automatically indented at least 1.
}
if (inListItem) {
html += '</li>';
inListItem = false;
} else if (!firstLine) {
if (emptyLine) {
html += '<br/>';
}
html += '</div>';
}
firstLine = false;
// Close any extra lists.
utils.assert(indent >= 0, "Indent must not be negative.");
while (listTypeStack.length > indent ||
(indent === listTypeStack.length && listType !== null && !compatibleListType(listType, listTypeStack[listTypeStack.length - 1]))) {
html += close(listTypeStack.pop());
}
// Open any needed lists.
while (listTypeStack.length < indent) {
var toOpen = listType || LIST_TYPE.UNORDERED; // default to unordered lists for indenting non-list-item lines.
usesTodo = listType == LIST_TYPE.TODO || listType == LIST_TYPE.TODOCHECKED || usesTodo;
html += open(toOpen);
listTypeStack.push(toOpen);
}
var style = (lineAlign !== 'left') ? ' style="text-align:' + lineAlign + '"': '';
if (listType) {
var clazz = '';
switch (listType)
{
case LIST_TYPE.TODOCHECKED:
clazz = ' class="firepad-checked"';
break;
case LIST_TYPE.TODO:
clazz = ' class="firepad-unchecked"';
break;
}
html += "<li" + clazz + style + ">";
inListItem = true;
} else {
// start line div.
html += '<div' + style + '>';
}
emptyLine = true;
}
if (ATTR.LINE_SENTINEL in attrs) {
op = doc.ops[++i];
continue;
}
if (ATTR.ENTITY_SENTINEL in attrs) {
for(var j = 0; j < op.text.length; j++) {
var entity = firepad.Entity.fromAttributes(attrs);
var element = entityManager.exportToElement(entity);
html += element.outerHTML;
}
op = doc.ops[++i];
continue;
}
var prefix = '', suffix = '';
for(var attr in attrs) {
var value = attrs[attr];
var start, end;
if (attr === ATTR.BOLD || attr === ATTR.ITALIC || attr === ATTR.UNDERLINE || attr === ATTR.STRIKE) {
utils.assert(value === true);
start = end = attr;
} else if (attr === ATTR.FONT_SIZE) {
start = 'span style="font-size: ' + value;
start += (typeof value !== "string" || value.indexOf("px", value.length - 2) === -1) ? 'px"' : '"';
end = 'span';
} else if (attr === ATTR.FONT) {
start = 'span style="font-family: ' + value + '"';
end = 'span';
} else if (attr === ATTR.COLOR) {
start = 'span style="color: ' + value + '"';
end = 'span';
} else if (attr === ATTR.BACKGROUND_COLOR) {
start = 'span style="background-color: ' + value + '"';
end = 'span';
}
else {
utils.log(false, "Encountered unknown attribute while rendering html: " + attr);
}
if (start) prefix += '<' + start + '>';
if (end) suffix = '</' + end + '>' + suffix;
}
var text = op.text;
var newLineIndex = text.indexOf('\n');
if (newLineIndex >= 0) {
newLine = true;
if (newLineIndex < text.length - 1) {
// split op.
op = new firepad.TextOp('insert', text.substr(newLineIndex+1), attrs);
} else {
op = doc.ops[++i];
}
text = text.substr(0, newLineIndex);
} else {
op = doc.ops[++i];
}
if (text.length > 0) {
emptyLine = false;
}
html += prefix + textToHtml(text) + suffix;
}
if (inListItem) {
html += '</li>';
} else if (!firstLine) {
if (emptyLine) {
html += ' ';
}
html += '</div>';
}
// Close any extra lists.
while (listTypeStack.length > 0) {
html += close(listTypeStack.pop());
}
if (usesTodo) {
html = TODO_STYLE + html;
}
return '<pre>'+html+'</pre>';
}
return serializeHtml;
})();