Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make block css class optional & fix test #6

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ function isSVGElement(el: Element): boolean {
return el.tagName === 'svg' || el instanceof SVGElement;
}

const BLOCK_CLASS = 'rr-block';
function serializeNode(n: Node, doc: Document): serializedNode | false {
function serializeNode(
n: Node,
doc: Document,
blockClass: string,
): serializedNode | false {
switch (n.nodeType) {
case n.DOCUMENT_NODE:
return {
Expand All @@ -99,7 +102,7 @@ function serializeNode(n: Node, doc: Document): serializedNode | false {
systemId: (n as DocumentType).systemId,
};
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();
let attributes: attributes = {};
for (const { name, value } of Array.from((n as HTMLElement).attributes)) {
Expand Down Expand Up @@ -197,9 +200,10 @@ export function serializeNodeWithId(
n: Node,
doc: Document,
map: idNodeMap,
blockClass: string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now serializeNodeWithId has 5 params which I think it is a little hard for non-typescript users to use. How about refactoring it to one or two params which use object?

skipChild = false,
): serializedNodeWithId | null {
const _serializedNode = serializeNode(n, doc);
const _serializedNode = serializeNode(n, doc, blockClass);
if (!_serializedNode) {
// TODO: dev only
console.warn(n, 'not serialized');
Expand All @@ -222,7 +226,12 @@ export function serializeNodeWithId(
recordChild
) {
for (const childN of Array.from(n.childNodes)) {
const serializedChildNode = serializeNodeWithId(childN, doc, map);
const serializedChildNode = serializeNodeWithId(
childN,
doc,
map,
blockClass,
);
if (serializedChildNode) {
serializedNode.childNodes.push(serializedChildNode);
}
Expand All @@ -231,10 +240,13 @@ export function serializeNodeWithId(
return serializedNode;
}

function snapshot(n: Document): [serializedNodeWithId | null, idNodeMap] {
function snapshot(
n: Document,
blockClass = 'rr-block',
): [serializedNodeWithId | null, idNodeMap] {
resetId();
const idNodeMap: idNodeMap = {};
return [serializeNodeWithId(n, n, idNodeMap), idNodeMap];
return [serializeNodeWithId(n, n, idNodeMap, blockClass), idNodeMap];
}

export default snapshot;
2 changes: 1 addition & 1 deletion test/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('absolute url to stylesheet', () => {
it('can handle multiple no quote paths', () => {
expect(
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,
),
).to.equal(
Expand Down