-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate from angular selectors. ENG-7482 (#1653)
* Generate from angular selectors. ENG-7482 * fmt * using angular selector parser * stringify values * expand tests * changeset
- Loading branch information
Showing
4 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@builder.io/mitosis': patch | ||
--- | ||
|
||
Angular selector support in code generation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { parse } from '../generators/angular/parse-selector'; | ||
|
||
describe('Angular selectors', () => { | ||
test('should parse gnarly selectors', () => { | ||
expect(parse('ccc.c1#wat[co].c2[counter="cool"]#wat[x=\'y\'].c3')).toEqual({ | ||
element: 'ccc', | ||
classNames: ['c1', 'c2', 'c3'], | ||
attributes: { | ||
co: '', | ||
counter: 'cool', | ||
id: 'wat', | ||
x: 'y', | ||
}, | ||
}); | ||
}); | ||
|
||
test('parsing multiple returns only the first', () => { | ||
expect(parse('dropzone, [dropzone]')).toEqual({ | ||
element: 'dropzone', | ||
classNames: [], | ||
attributes: {}, | ||
}); | ||
}); | ||
|
||
test(':not parses but is unused', () => { | ||
expect(parse('list-item:not(.foo)')).toEqual({ | ||
element: 'list-item', | ||
classNames: [], | ||
attributes: {}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { CssSelector } from '@angular/compiler'; | ||
|
||
export function parse(selector: string) { | ||
const { element, classNames, attrs } = CssSelector.parse(selector)[0]; | ||
const attributes = attrs.reduce((acc, attr, i) => { | ||
if (i % 2 === 0) { | ||
acc[attr] = attrs[i + 1]; | ||
} | ||
return acc; | ||
}, {} as Record<string, string>); | ||
|
||
return { | ||
element, | ||
classNames, | ||
attributes, | ||
}; | ||
} |