add the HACK_CSS flag to bypass css parsing (#16)

This commit is contained in:
yz-yu
2019-08-27 13:42:07 +08:00
committed by GitHub
parent f6b93ee27c
commit 215aeeb6b1
2 changed files with 18 additions and 9 deletions

View File

@@ -75,7 +75,11 @@ export function addHoverClass(cssText: string): string {
return cssText; return cssText;
} }
function buildNode(n: serializedNodeWithId, doc: Document): Node | null { function buildNode(
n: serializedNodeWithId,
doc: Document,
HACK_CSS: boolean,
): Node | null {
switch (n.type) { switch (n.type) {
case NodeType.Document: case NodeType.Document:
return doc.implementation.createDocument(null, '', null); return doc.implementation.createDocument(null, '', null);
@@ -101,7 +105,7 @@ function buildNode(n: serializedNodeWithId, doc: Document): Node | null {
const isTextarea = tagName === 'textarea' && name === 'value'; const isTextarea = tagName === 'textarea' && name === 'value';
const isRemoteOrDynamicCss = const isRemoteOrDynamicCss =
tagName === 'style' && name === '_cssText'; tagName === 'style' && name === '_cssText';
if (isRemoteOrDynamicCss) { if (isRemoteOrDynamicCss && HACK_CSS) {
value = addHoverClass(value); value = addHoverClass(value);
} }
if (isTextarea || isRemoteOrDynamicCss) { if (isTextarea || isRemoteOrDynamicCss) {
@@ -141,7 +145,7 @@ function buildNode(n: serializedNodeWithId, doc: Document): Node | null {
return node; return node;
case NodeType.Text: case NodeType.Text:
return doc.createTextNode( return doc.createTextNode(
n.isStyle ? addHoverClass(n.textContent) : n.textContent, n.isStyle && HACK_CSS ? addHoverClass(n.textContent) : n.textContent,
); );
case NodeType.CDATA: case NodeType.CDATA:
return doc.createCDATASection(n.textContent); return doc.createCDATASection(n.textContent);
@@ -157,8 +161,9 @@ export function buildNodeWithSN(
doc: Document, doc: Document,
map: idNodeMap, map: idNodeMap,
skipChild = false, skipChild = false,
HACK_CSS = true,
): INode | null { ): INode | null {
let node = buildNode(n, doc); let node = buildNode(n, doc, HACK_CSS);
if (!node) { if (!node) {
return null; return null;
} }
@@ -177,7 +182,7 @@ export function buildNodeWithSN(
!skipChild !skipChild
) { ) {
for (const childN of n.childNodes) { for (const childN of n.childNodes) {
const childNode = buildNodeWithSN(childN, doc, map); const childNode = buildNodeWithSN(childN, doc, map, false, HACK_CSS);
if (!childNode) { if (!childNode) {
console.warn('Failed to rebuild', childN); console.warn('Failed to rebuild', childN);
} else { } else {
@@ -191,9 +196,13 @@ export function buildNodeWithSN(
function rebuild( function rebuild(
n: serializedNodeWithId, n: serializedNodeWithId,
doc: Document, doc: Document,
/**
* This is not a public API yet, just for POC
*/
HACK_CSS: boolean = true,
): [Node | null, idNodeMap] { ): [Node | null, idNodeMap] {
const idNodeMap: idNodeMap = {}; const idNodeMap: idNodeMap = {};
return [buildNodeWithSN(n, doc, idNodeMap), idNodeMap]; return [buildNodeWithSN(n, doc, idNodeMap, false, HACK_CSS), idNodeMap];
} }
export default rebuild; export default rebuild;

View File

@@ -1,5 +1,5 @@
import { serializedNodeWithId, idNodeMap, INode } from './types'; import { serializedNodeWithId, idNodeMap, INode } from './types';
export declare function addHoverClass(cssText: string): string; export declare function addHoverClass(cssText: string): string;
export declare function buildNodeWithSN(n: serializedNodeWithId, doc: Document, map: idNodeMap, skipChild?: boolean): INode | null; export declare function buildNodeWithSN(n: serializedNodeWithId, doc: Document, map: idNodeMap, skipChild?: boolean, HACK_CSS?: boolean): INode | null;
declare function rebuild(n: serializedNodeWithId, doc: Document): [Node | null, idNodeMap]; declare function rebuild(n: serializedNodeWithId, doc: Document, HACK_CSS?: boolean): [Node | null, idNodeMap];
export default rebuild; export default rebuild;