|
1 | 1 | type Combinator = '>' | '~' | '+'; |
2 | 2 |
|
3 | 3 | interface Options { |
| 4 | + duplicates?: 'preserve' | 'remove'; |
4 | 5 | } |
5 | 6 |
|
6 | 7 | /** |
@@ -92,17 +93,19 @@ export function cssToHtml(css: CSSRuleList | string, options: Options = {}): HTM |
92 | 93 | }; |
93 | 94 |
|
94 | 95 | function addElementToOutput (): void { |
95 | | - // If an identical element already exists, skip adding the new element. |
96 | | - const existingElements = Array.from( |
97 | | - (descriptor.combinator === '>' ? |
98 | | - descriptor.previousElement?.children : |
99 | | - descriptor.previousElement?.parentElement?.children) ?? output.children |
100 | | - ); |
101 | | - const matchingElement = existingElements.find((element) => descriptor.matchesElement(element)); |
102 | | - if (matchingElement) { |
103 | | - // Reference the matching element so properties such as `content` can cascade. |
104 | | - descriptor.previousElement = matchingElement as HTMLElement; |
105 | | - return; |
| 96 | + if (options.duplicates !== 'preserve') { |
| 97 | + // If an identical element already exists, skip adding the new element. |
| 98 | + const existingElements = Array.from( |
| 99 | + (descriptor.combinator === '>' ? |
| 100 | + descriptor.previousElement?.children : |
| 101 | + descriptor.previousElement?.parentElement?.children) ?? output.children |
| 102 | + ); |
| 103 | + const matchingElement = existingElements.find((element) => descriptor.matchesElement(element)); |
| 104 | + if (matchingElement) { |
| 105 | + // Reference the matching element so properties such as `content` can cascade. |
| 106 | + descriptor.previousElement = matchingElement as HTMLElement; |
| 107 | + return; |
| 108 | + } |
106 | 109 | } |
107 | 110 | // Create the new element. |
108 | 111 | const newElement = document.createElement(descriptor.tag || 'div'); |
|
0 commit comments