Make block css class optional & fix test (#6)
* Make block class optional * Fix test by removing unexpected space space
This commit is contained in:
@@ -86,8 +86,11 @@ function isSVGElement(el: Element): boolean {
|
|||||||
return el.tagName === 'svg' || el instanceof SVGElement;
|
return el.tagName === 'svg' || el instanceof SVGElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
const BLOCK_CLASS = 'rr-block';
|
function serializeNode(
|
||||||
function serializeNode(n: Node, doc: Document): serializedNode | false {
|
n: Node,
|
||||||
|
doc: Document,
|
||||||
|
blockClass: string,
|
||||||
|
): serializedNode | false {
|
||||||
switch (n.nodeType) {
|
switch (n.nodeType) {
|
||||||
case n.DOCUMENT_NODE:
|
case n.DOCUMENT_NODE:
|
||||||
return {
|
return {
|
||||||
@@ -102,7 +105,7 @@ function serializeNode(n: Node, doc: Document): serializedNode | false {
|
|||||||
systemId: (n as DocumentType).systemId,
|
systemId: (n as DocumentType).systemId,
|
||||||
};
|
};
|
||||||
case n.ELEMENT_NODE:
|
case n.ELEMENT_NODE:
|
||||||
const needBlock = (n as HTMLElement).classList.contains(BLOCK_CLASS);
|
const needBlock = (n as HTMLElement).classList.contains(blockClass);
|
||||||
const tagName = (n as HTMLElement).tagName.toLowerCase();
|
const tagName = (n as HTMLElement).tagName.toLowerCase();
|
||||||
let attributes: attributes = {};
|
let attributes: attributes = {};
|
||||||
for (const { name, value } of Array.from((n as HTMLElement).attributes)) {
|
for (const { name, value } of Array.from((n as HTMLElement).attributes)) {
|
||||||
@@ -216,9 +219,10 @@ export function serializeNodeWithId(
|
|||||||
n: Node,
|
n: Node,
|
||||||
doc: Document,
|
doc: Document,
|
||||||
map: idNodeMap,
|
map: idNodeMap,
|
||||||
|
blockClass: string,
|
||||||
skipChild = false,
|
skipChild = false,
|
||||||
): serializedNodeWithId | null {
|
): serializedNodeWithId | null {
|
||||||
const _serializedNode = serializeNode(n, doc);
|
const _serializedNode = serializeNode(n, doc, blockClass);
|
||||||
if (!_serializedNode) {
|
if (!_serializedNode) {
|
||||||
// TODO: dev only
|
// TODO: dev only
|
||||||
console.warn(n, 'not serialized');
|
console.warn(n, 'not serialized');
|
||||||
@@ -241,7 +245,12 @@ export function serializeNodeWithId(
|
|||||||
recordChild
|
recordChild
|
||||||
) {
|
) {
|
||||||
for (const childN of Array.from(n.childNodes)) {
|
for (const childN of Array.from(n.childNodes)) {
|
||||||
const serializedChildNode = serializeNodeWithId(childN, doc, map);
|
const serializedChildNode = serializeNodeWithId(
|
||||||
|
childN,
|
||||||
|
doc,
|
||||||
|
map,
|
||||||
|
blockClass,
|
||||||
|
);
|
||||||
if (serializedChildNode) {
|
if (serializedChildNode) {
|
||||||
serializedNode.childNodes.push(serializedChildNode);
|
serializedNode.childNodes.push(serializedChildNode);
|
||||||
}
|
}
|
||||||
@@ -250,10 +259,13 @@ export function serializeNodeWithId(
|
|||||||
return serializedNode;
|
return serializedNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap] {
|
function snapshot(
|
||||||
|
n: Document,
|
||||||
|
blockClass = 'rr-block',
|
||||||
|
): [serializedNodeWithId | null, idNodeMap] {
|
||||||
resetId();
|
resetId();
|
||||||
const idNodeMap: idNodeMap = {};
|
const idNodeMap: idNodeMap = {};
|
||||||
return [serializeNodeWithId(n, n, idNodeMap), idNodeMap];
|
return [serializeNodeWithId(n, n, idNodeMap, blockClass), idNodeMap];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default snapshot;
|
export default snapshot;
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ describe('absolute url to stylesheet', () => {
|
|||||||
it('can handle multiple no quote paths', () => {
|
it('can handle multiple no quote paths', () => {
|
||||||
expect(
|
expect(
|
||||||
absoluteToStylesheet(
|
absoluteToStylesheet(
|
||||||
'background-image: url(images/b.jpg); background: #aabbcc url(images/a.jpg) 50% 50% repeat;',
|
'background-image: url(images/b.jpg);background: #aabbcc url(images/a.jpg) 50% 50% repeat;',
|
||||||
href,
|
href,
|
||||||
),
|
),
|
||||||
).to.equal(
|
).to.equal(
|
||||||
|
|||||||
Reference in New Issue
Block a user