* feat: add new css parser * make selectors change * selectors and tests * media changes * remove old css references * better variable name * use postcss and port tests * fix media test * inline plugins * fix failing multiline selector * correct test result * move tests to correct file * cleanup all tests * remove unused css-tree * update bundle * cleanup dependencies * revert config files to master * remove d.ts files * update snapshot * reset rebuilt test * apply fuzzy css matching * remove extra test * Fix imports * Newer versions of nswapi break rrdom-nodejs tests. Example: FAIL test/document-nodejs.test.ts > RRDocument for nodejs environment > RRDocument API > querySelectorAll TypeError: e[api] is not a function ❯ byTag ../../node_modules/nwsapi/src/nwsapi.js:390:37 ❯ Array.<anonymous> ../../node_modules/nwsapi/src/nwsapi.js:327:113 ❯ collect ../../node_modules/nwsapi/src/nwsapi.js:1578:32 ❯ Object._querySelectorAll [as select] ../../node_modules/nwsapi/src/nwsapi.js:1533:36 ❯ RRDocument.querySelectorAll src/document-nodejs.ts:96:24 * Migrate from jest to vitest * Order of selectors has changed with postcss * Remove unused eslint --------- Co-authored-by: Justin Halsall <Juice10@users.noreply.github.com>
284 lines
8.8 KiB
TypeScript
284 lines
8.8 KiB
TypeScript
/**
|
|
* @vitest-environment jsdom
|
|
*/
|
|
import { describe, it, test, expect } from 'vitest';
|
|
import { NodeType, serializedNode } from '../src/types';
|
|
import {
|
|
escapeImportStatement,
|
|
extractFileExtension,
|
|
fixSafariColons,
|
|
isNodeMetaEqual,
|
|
} from '../src/utils';
|
|
import type { serializedNodeWithId } from 'rrweb-snapshot';
|
|
|
|
describe('utils', () => {
|
|
describe('isNodeMetaEqual()', () => {
|
|
const document1: serializedNode = {
|
|
type: NodeType.Document,
|
|
compatMode: 'CSS1Compat',
|
|
childNodes: [],
|
|
};
|
|
const document2: serializedNode = {
|
|
type: NodeType.Document,
|
|
compatMode: 'BackCompat',
|
|
childNodes: [],
|
|
};
|
|
const documentType1: serializedNode = {
|
|
type: NodeType.DocumentType,
|
|
name: 'html',
|
|
publicId: '',
|
|
systemId: '',
|
|
};
|
|
const documentType2: serializedNode = {
|
|
type: NodeType.DocumentType,
|
|
name: 'html',
|
|
publicId: '',
|
|
systemId: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd',
|
|
};
|
|
const text1: serializedNode = {
|
|
type: NodeType.Text,
|
|
textContent: 'Hello World',
|
|
};
|
|
const text2: serializedNode = {
|
|
type: NodeType.Text,
|
|
textContent: 'Hello world',
|
|
};
|
|
const comment1: serializedNode = {
|
|
type: NodeType.Comment,
|
|
textContent: 'Hello World',
|
|
};
|
|
const comment2: serializedNode = {
|
|
type: NodeType.Comment,
|
|
textContent: 'Hello world',
|
|
};
|
|
const element1: serializedNode = {
|
|
type: NodeType.Element,
|
|
tagName: 'div',
|
|
attributes: {
|
|
className: 'test',
|
|
},
|
|
childNodes: [],
|
|
};
|
|
const element2: serializedNode = {
|
|
type: NodeType.Element,
|
|
tagName: 'span',
|
|
attributes: {
|
|
'aria-label': 'Hello World',
|
|
},
|
|
childNodes: [],
|
|
};
|
|
const element3: serializedNode = {
|
|
type: NodeType.Element,
|
|
tagName: 'div',
|
|
attributes: { id: 'test' },
|
|
childNodes: [comment1 as serializedNodeWithId],
|
|
};
|
|
|
|
it('should return false if two nodes have different node types', () => {
|
|
expect(
|
|
isNodeMetaEqual(
|
|
undefined as unknown as serializedNode,
|
|
null as unknown as serializedNode,
|
|
),
|
|
).toBeFalsy();
|
|
expect(isNodeMetaEqual(document1, element1)).toBeFalsy();
|
|
expect(isNodeMetaEqual(document1, documentType1)).toBeFalsy();
|
|
expect(isNodeMetaEqual(documentType1, element1)).toBeFalsy();
|
|
expect(isNodeMetaEqual(text1, comment1)).toBeFalsy();
|
|
expect(isNodeMetaEqual(text1, element1)).toBeFalsy();
|
|
expect(isNodeMetaEqual(comment1, element1)).toBeFalsy();
|
|
});
|
|
|
|
it('should compare meta data of two document nodes', () => {
|
|
expect(
|
|
isNodeMetaEqual(document1, JSON.parse(JSON.stringify(document1))),
|
|
).toBeTruthy();
|
|
expect(
|
|
isNodeMetaEqual(JSON.parse(JSON.stringify(document2)), document2),
|
|
).toBeTruthy();
|
|
expect(isNodeMetaEqual(document1, document2)).toBeFalsy();
|
|
});
|
|
|
|
it('should compare meta data of two documentType nodes', () => {
|
|
expect(
|
|
isNodeMetaEqual(
|
|
documentType1,
|
|
JSON.parse(JSON.stringify(documentType1)),
|
|
),
|
|
).toBeTruthy();
|
|
expect(
|
|
isNodeMetaEqual(
|
|
JSON.parse(JSON.stringify(documentType2)),
|
|
documentType2,
|
|
),
|
|
).toBeTruthy();
|
|
expect(isNodeMetaEqual(documentType1, documentType2)).toBeFalsy();
|
|
});
|
|
|
|
it('should compare meta data of two text nodes', () => {
|
|
expect(
|
|
isNodeMetaEqual(text1, JSON.parse(JSON.stringify(text1))),
|
|
).toBeTruthy();
|
|
expect(
|
|
isNodeMetaEqual(JSON.parse(JSON.stringify(text2)), text2),
|
|
).toBeTruthy();
|
|
expect(isNodeMetaEqual(text1, text2)).toBeFalsy();
|
|
});
|
|
|
|
it('should compare meta data of two comment nodes', () => {
|
|
expect(
|
|
isNodeMetaEqual(comment1, JSON.parse(JSON.stringify(comment1))),
|
|
).toBeTruthy();
|
|
expect(
|
|
isNodeMetaEqual(JSON.parse(JSON.stringify(comment2)), comment2),
|
|
).toBeTruthy();
|
|
expect(isNodeMetaEqual(comment1, comment2)).toBeFalsy();
|
|
});
|
|
|
|
it('should compare meta data of two HTML elements', () => {
|
|
expect(
|
|
isNodeMetaEqual(element1, JSON.parse(JSON.stringify(element1))),
|
|
).toBeTruthy();
|
|
expect(
|
|
isNodeMetaEqual(JSON.parse(JSON.stringify(element2)), element2),
|
|
).toBeTruthy();
|
|
expect(
|
|
isNodeMetaEqual(element1, {
|
|
...element1,
|
|
childNodes: [comment2 as serializedNodeWithId],
|
|
}),
|
|
).toBeTruthy();
|
|
expect(isNodeMetaEqual(element1, element2)).toBeFalsy();
|
|
expect(isNodeMetaEqual(element1, element3)).toBeFalsy();
|
|
expect(isNodeMetaEqual(element2, element3)).toBeFalsy();
|
|
});
|
|
});
|
|
describe('extractFileExtension', () => {
|
|
test('absolute path', () => {
|
|
const path = 'https://example.com/styles/main.css';
|
|
const extension = extractFileExtension(path);
|
|
expect(extension).toBe('css');
|
|
});
|
|
|
|
test('relative path', () => {
|
|
const path = 'styles/main.css';
|
|
const baseURL = 'https://example.com/';
|
|
const extension = extractFileExtension(path, baseURL);
|
|
expect(extension).toBe('css');
|
|
});
|
|
|
|
test('path with search parameters', () => {
|
|
const path = 'https://example.com/scripts/app.js?version=1.0';
|
|
const extension = extractFileExtension(path);
|
|
expect(extension).toBe('js');
|
|
});
|
|
|
|
test('path with fragment', () => {
|
|
const path = 'https://example.com/styles/main.css#section1';
|
|
const extension = extractFileExtension(path);
|
|
expect(extension).toBe('css');
|
|
});
|
|
|
|
test('path with search parameters and fragment', () => {
|
|
const path = 'https://example.com/scripts/app.js?version=1.0#section1';
|
|
const extension = extractFileExtension(path);
|
|
expect(extension).toBe('js');
|
|
});
|
|
|
|
test('path without extension', () => {
|
|
const path = 'https://example.com/path/to/directory/';
|
|
const extension = extractFileExtension(path);
|
|
expect(extension).toBeNull();
|
|
});
|
|
|
|
test('invalid URL', () => {
|
|
const path = '!@#$%^&*()';
|
|
const baseURL = 'invalid';
|
|
const extension = extractFileExtension(path, baseURL);
|
|
expect(extension).toBeNull();
|
|
});
|
|
|
|
test('path with multiple dots', () => {
|
|
const path = 'https://example.com/scripts/app.min.js?version=1.0';
|
|
const extension = extractFileExtension(path);
|
|
expect(extension).toBe('js');
|
|
});
|
|
});
|
|
|
|
describe('escapeImportStatement', () => {
|
|
it('parses imports with quotes correctly', () => {
|
|
const out1 = escapeImportStatement({
|
|
cssText: `@import url("/foo.css;900;800"");`,
|
|
href: '/foo.css;900;800"',
|
|
media: {
|
|
length: 0,
|
|
},
|
|
layerName: null,
|
|
supportsText: null,
|
|
} as unknown as CSSImportRule);
|
|
expect(out1).toEqual(`@import url("/foo.css;900;800\\"");`);
|
|
|
|
const out2 = escapeImportStatement({
|
|
cssText: `@import url("/foo.css;900;800"") supports(display: flex);`,
|
|
href: '/foo.css;900;800"',
|
|
media: {
|
|
length: 0,
|
|
},
|
|
layerName: null,
|
|
supportsText: 'display: flex',
|
|
} as unknown as CSSImportRule);
|
|
expect(out2).toEqual(
|
|
`@import url("/foo.css;900;800\\"") supports(display: flex);`,
|
|
);
|
|
|
|
const out3 = escapeImportStatement({
|
|
cssText: `@import url("/foo.css;900;800"");`,
|
|
href: '/foo.css;900;800"',
|
|
media: {
|
|
length: 1,
|
|
mediaText: 'print, screen',
|
|
},
|
|
layerName: null,
|
|
supportsText: null,
|
|
} as unknown as CSSImportRule);
|
|
expect(out3).toEqual(`@import url("/foo.css;900;800\\"") print, screen;`);
|
|
|
|
const out4 = escapeImportStatement({
|
|
cssText: `@import url("/foo.css;900;800"") layer(layer-1);`,
|
|
href: '/foo.css;900;800"',
|
|
media: {
|
|
length: 0,
|
|
},
|
|
layerName: 'layer-1',
|
|
supportsText: null,
|
|
} as unknown as CSSImportRule);
|
|
expect(out4).toEqual(
|
|
`@import url("/foo.css;900;800\\"") layer(layer-1);`,
|
|
);
|
|
|
|
const out5 = escapeImportStatement({
|
|
cssText: `@import url("/foo.css;900;800"") layer;`,
|
|
href: '/foo.css;900;800"',
|
|
media: {
|
|
length: 0,
|
|
},
|
|
layerName: '',
|
|
supportsText: null,
|
|
} as unknown as CSSImportRule);
|
|
expect(out5).toEqual(`@import url("/foo.css;900;800\\"") layer;`);
|
|
});
|
|
});
|
|
describe('fixSafariColons', () => {
|
|
it('parses : in attribute selectors correctly', () => {
|
|
const out1 = fixSafariColons('[data-foo] { color: red; }');
|
|
expect(out1).toEqual('[data-foo] { color: red; }');
|
|
|
|
const out2 = fixSafariColons('[data-foo:other] { color: red; }');
|
|
expect(out2).toEqual('[data-foo\\:other] { color: red; }');
|
|
|
|
const out3 = fixSafariColons('[data-aa\\:other] { color: red; }');
|
|
expect(out3).toEqual('[data-aa\\:other] { color: red; }');
|
|
});
|
|
});
|
|
});
|