-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathparseStorageHtml.js
180 lines (158 loc) · 5.5 KB
/
parseStorageHtml.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
import { getContentTypeConfig } from './config';
/**
* Create a basic object representing a content type in our tree
*
* @param type
* @param node
* @returns {{appearance: any, children: Array, contentType: *}}
*/
const createContentTypeObject = (type, node) => {
return {
contentType: type,
appearance: node ? node.getAttribute('data-appearance') : null,
children: []
};
};
/**
* Walk over tree nodes extracting each content types configuration
*
* @param {Node} rootEl
* @param {Object} contentTypeStructureObj
* @returns {Object}
*/
const walk = (rootEl, contentTypeStructureObj) => {
const tree = document.createTreeWalker(
rootEl,
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT
);
let currentNode = tree.nextNode();
while (currentNode) {
if (currentNode.nodeType !== Node.ELEMENT_NODE) {
currentNode = tree.nextNode();
continue;
}
const contentType = currentNode.getAttribute('data-content-type');
if (!contentType) {
currentNode = tree.nextNode();
continue;
}
const props = createContentTypeObject(contentType, currentNode);
const contentTypeConfig = getContentTypeConfig(contentType);
if (
contentTypeConfig &&
typeof contentTypeConfig.configAggregator === 'function'
) {
try {
Object.assign(
props,
contentTypeConfig.configAggregator(currentNode, props)
);
} catch (e) {
console.error(
`Failed to aggregate config for content type ${contentType}.`,
e
);
}
} else {
console.warn(
`Page Builder ${contentType} content type is not supported, this content will not be rendered.`
);
}
contentTypeStructureObj.children.push(props);
walk(currentNode, props);
currentNode = tree.nextSibling();
}
return contentTypeStructureObj;
};
const pbStyleAttribute = 'data-pb-style';
const bodyId = 'html-body';
/**
* Convert styles block to inline styles.
* @param {HTMLDocument} document
*/
const convertToInlineStyles = document => {
const styleBlocks = document.getElementsByTagName('style');
const styles = {};
const mediaStyles = {};
if (styleBlocks.length > 0) {
Array.from(styleBlocks).forEach(styleBlock => {
const cssRules = styleBlock.sheet.cssRules;
Array.from(cssRules).forEach(rule => {
if (rule instanceof CSSStyleRule) {
const selectors = rule.selectorText
.split(',')
.map(selector => selector.trim());
selectors.forEach(selector => {
if (!styles[selector]) {
styles[selector] = [];
}
styles[selector].push(rule.style);
});
} else if (rule instanceof CSSMediaRule) {
Array.from(rule.media).forEach(media => {
const styles = Array.from(rule.cssRules).map(rule => {
return {
selectors: rule.selectorText
.split(',')
.map(selector => selector.trim()),
css: rule.style.cssText
};
});
const existingMediaStyles = mediaStyles[media]
? mediaStyles[media]
: [];
mediaStyles[media] = [
...existingMediaStyles,
...styles
];
});
}
});
});
}
Object.keys(mediaStyles).map((media, i) => {
mediaStyles[media].forEach(style => {
style.selectors.forEach(selector => {
const element = document.querySelector(selector);
if (element) {
element.setAttribute(`data-media-${i}`, media);
const savedStyles = element.getAttribute(
`data-media-style-${i}`
);
// avoids overwriting previously saved styles
element.setAttribute(
`data-media-style-${i}`,
`${savedStyles ? `${savedStyles} ` : ''}${style.css}`
);
}
});
});
});
Object.keys(styles).map(selector => {
const element = document.querySelector(selector);
if (!element) {
return;
}
styles[selector].map(style => {
element.setAttribute(
'style',
element.style.cssText + style.cssText
);
});
element.removeAttribute(pbStyleAttribute);
});
};
/**
* Parse the master format storage HTML
*
* @param {String} htmlStr
* @returns {Object}
*/
const parseStorageHtml = htmlStr => {
const container = new DOMParser().parseFromString(htmlStr, 'text/html');
const stageContentType = createContentTypeObject('root-container');
container.body.id = bodyId;
convertToInlineStyles(container);
return walk(container.body, stageContentType);
};
export default parseStorageHtml;