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

Map camle cased attributes to lower case #1122

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions packages/core/jsx-runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ export declare namespace JSX {
y?: number | string;
width?: number | string;
height?: number | string;
fill?: number | string;
}

interface ViewSVGAttributes<T>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Vitest Snapshot v1

exports[`map camle cased > map camle cased 1`] = `
"<script>
import React from \\"@builder.io/react\\";

let name = \\"Steve\\";
</script>

<div>
<img srcSet=\\"http://example.com\\" />
</div>"
`;
11 changes: 11 additions & 0 deletions packages/core/src/__tests__/camle-case.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import MyComponent from './data/map-camle-cased-attributes.raw?raw'
import { parseJsx } from '../parsers/jsx';
import { componentToSvelte } from '../generators/svelte';

describe('map camle cased', () => {
test('map camle cased', () => {
const component = parseJsx(MyComponent);
const svelteComponent = componentToSvelte()({component})
expect(svelteComponent).toMatchSnapshot();
})
})
moaoa marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState } from "@builder.io/mitosis";
moaoa marked this conversation as resolved.
Show resolved Hide resolved
import React from '@builder.io/react'

export default function MyComponent(props) {
const [name, setName] = useState("Steve");

return (
<div>
<img srcSet="http://example.com" />
</div>
);
}
7 changes: 6 additions & 1 deletion packages/core/src/generators/svelte/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { stripStateAndProps } from './helpers';
import { ToSvelteOptions } from './types';
import { blockToSvelte } from './blocks';
import { stripGetter } from '../../helpers/patterns';
import { isFirstLetterLowerCase } from '../../helpers/is-first-letter-lower-case'
import { mapCamelCasedHtmlAttributes } from 'src/helpers/map-camel-cased-html-attributes';
import { MitosisNode } from 'src/types/mitosis-node';

const getContextCode = (json: MitosisComponent) => {
const contextGetters = json.context.get;
Expand Down Expand Up @@ -146,7 +149,9 @@ export const componentToSvelte: TranspilerGenerator<ToSvelteOptions> =
.filter((x) => !props.includes(x));

json = runPostJsonPlugins(json, options.plugins);

//@ts-ignore
mapCamelCasedHtmlAttributes(json)
moaoa marked this conversation as resolved.
Show resolved Hide resolved

const css = collectCss(json);
stripMetaProperties(json);

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/helpers/is-first-letter-lower-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const isFirstLetterLowerCase = (elementName: string) => {
const firstLetter = elementName[0]

return firstLetter >= 'a' && firstLetter <= 'z'
}
33 changes: 33 additions & 0 deletions packages/core/src/helpers/map-camel-cased-html-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {isFirstLetterLowerCase} from './is-first-letter-lower-case'
import { MitosisNode } from '../types/mitosis-node';
/*
* This function does side-effects on `json`
*/
export const mapCamelCasedHtmlAttributes = (json: MitosisNode) => {
moaoa marked this conversation as resolved.
Show resolved Hide resolved
const isBuiltInHtmlElement = isFirstLetterLowerCase(json.name) && !json.name.includes('-');

if (!isBuiltInHtmlElement) {
return;
}
for (let key in json.bindings) {
const isBuiltInHtmlAttrName = !key.includes('-')
if (isBuiltInHtmlAttrName) {
const newKey = key.toLowerCase() as keyof MitosisNode['bindings']
// if json[newKey] already exists: show a warning and don't override
json.bindings[newKey] = json.bindings[key];
delete json.bindings[key];
}
}

// repeat the above loop for json.properties too
for (let key in json.properties) {
const isBuiltInHtmlAttrName = !key.includes('-')
if (isBuiltInHtmlAttrName) {
const newKey = key.toLowerCase()
// if json[newKey] already exists: show a warning and don't override

json.properties[newKey] = json.properties[key];
delete json.properties[key];
}
}
}