diff --git a/packages/iconify/src/scanner/index.ts b/packages/iconify/src/scanner/index.ts index 4266447..f7456b7 100644 --- a/packages/iconify/src/scanner/index.ts +++ b/packages/iconify/src/scanner/index.ts @@ -15,13 +15,13 @@ import { import { scanRootNode } from './find'; import type { IconifyIconName } from '../iconify'; import type { FullIconifyIcon } from '@iconify/utils/lib/icon'; -import { renderInlineSVG } from '../render/svg'; import { observe, pauseObservingNode, resumeObservingNode, stopObserving, } from '../observer'; +import { renderInlineSVG } from '../render/svg'; import { renderBackground } from '../render/bg'; /** @@ -122,8 +122,8 @@ export function scanDOM(rootNode?: ObservedNode, addTempNode = false): void { paused = true; pauseObservingNode(observedNode); } - // renderInlineSVG(element, props, iconData); - renderBackground(element, props, iconData); + renderInlineSVG(element, props, iconData); + // renderBackground(element, props, iconData); } // Find all elements diff --git a/packages/iconify/tests/render-bg-test.ts b/packages/iconify/tests/render-bg-test.ts new file mode 100644 index 0000000..87e8789 --- /dev/null +++ b/packages/iconify/tests/render-bg-test.ts @@ -0,0 +1,165 @@ +import { iconDefaults } from '@iconify/utils/lib/icon'; +import { cleanupGlobals, setupDOM, waitDOMReady } from './helpers'; +import { scanRootNode } from '../src/scanner/find'; +import { renderBackground } from '../src/render/bg'; +import type { IconifyIcon } from '@iconify/utils/lib/icon'; +import { elementDataProperty, IconifyElement } from '../src/scanner/config'; + +describe('Testing rendering nodes as background', () => { + afterEach(cleanupGlobals); + + const expectedBackgroundStyles: string[] = [ + '--svg', + 'width', + 'height', + 'display', + 'background-color', + 'background-image', + 'background-repeat', + 'background-size', + ]; + + const testIcon = async ( + placeholder: string, + data: IconifyIcon, + expected: string + ): Promise => { + setupDOM(placeholder); + + await waitDOMReady(); + + // Find node + const root = document.body; + const items = scanRootNode(root); + + expect(items.length).toBe(1); + + // Get node and render it + const { node, props } = items[0]; + const result = renderBackground(node, props, { + ...iconDefaults, + ...data, + }); + + // Make sure node did not change + expect(result).toBe(node); + + // Get HTML + const html = root.innerHTML; + expect(html).toBe(expected); + + return node; + }; + + it('Rendering simple icon', async () => { + const svg = await testIcon( + '', + { + body: '', + width: 24, + height: 24, + }, + `` + ); + + const data = svg[elementDataProperty]; + expect(data.status).toBe('loaded'); + expect(data.addedClasses).toEqual(['iconify--mdi']); + expect(data.addedStyles).toEqual([...expectedBackgroundStyles]); + }); + + it('Inline icon and transformation', async () => { + const svg = await testIcon( + '', + { + body: '', + width: 24, + height: 24, + }, + `` + ); + + const data = svg[elementDataProperty]; + expect(data.status).toBe('loaded'); + expect(data.addedClasses).toEqual(['iconify', 'iconify--mdi']); + expect(data.addedStyles).toEqual([ + ...expectedBackgroundStyles, + 'vertical-align', + ]); + }); + + it('Passing attributes and style', async () => { + const svg = await testIcon( + '', + { + body: '', + width: 24, + height: 24, + }, + `` + ); + + const data = svg[elementDataProperty]; + expect(data.status).toBe('loaded'); + expect(data.addedClasses).toEqual([]); // All classes already existed on placeholder + expect(data.addedStyles).toEqual([...expectedBackgroundStyles]); // Overwritten by entry in placeholder + }); + + it('Inline icon and vertical-align', async () => { + const svg = await testIcon( + '', + { + body: '', + width: 24, + height: 24, + }, + `` + ); + + const data = svg[elementDataProperty]; + expect(data.status).toBe('loaded'); + expect(data.addedClasses).toEqual(['iconify', 'iconify--mdi']); + expect(data.addedStyles).toEqual([...expectedBackgroundStyles]); + }); + + it('Inline icon and custom style without ;', async () => { + const svg = await testIcon( + '', + { + body: '', + width: 24, + height: 24, + }, + `` + ); + + const data = svg[elementDataProperty]; + expect(data.status).toBe('loaded'); + expect(data.addedClasses).toEqual([ + 'iconify', + 'iconify--provider', + 'iconify--mdi-light', + ]); + expect(data.addedStyles).toEqual([ + ...expectedBackgroundStyles, + 'vertical-align', + ]); + }); + + it('Identical prefix and provider', async () => { + const svg = await testIcon( + '', + { + body: '', + width: 24, + height: 24, + }, + `` + ); + + const data = svg[elementDataProperty]; + expect(data.status).toBe('loaded'); + expect(data.addedClasses).toEqual(['iconify--test']); + expect(data.addedStyles).toEqual([...expectedBackgroundStyles]); + }); +});