Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@
</scope->
</scope->
<console- log(="Hi, my name is {name} again" )></console->

<alert- alert(="Hello World" )></alert->
</body>
</html>
21 changes: 21 additions & 0 deletions src/components/alert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Variable } from '../variable';
import { BaseHtmlangElement } from './htmlangElement';

export class AlertDash extends BaseHtmlangElement {
static getTagName = () => 'alert' as const;

execute = (): void => {
let alertVal = this.getAttribute('alert(');
if (!alertVal) {
return;
}

Variable.forEach(alertVal, (varName) => {
const result = this.parentScope.getVariable(varName);
const value = result.found ? result.variable.value : undefined;
alertVal = alertVal!.replaceAll(`{${varName}}`, value);
});

alert(alertVal);
};
}
59 changes: 59 additions & 0 deletions src/components/console.test copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ElementGraph } from '../elementGraph';

describe('console', () => {
afterEach(() => {
document.body.innerHTML = '';
});

it('should call console.log()', async () => {
const spy = vi.spyOn(console, 'log');

const container = document.createElement('div');
container.innerHTML = `<console- log(="Hello, world!" )></console->`;
document.body.appendChild(container);
ElementGraph.build().execute();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('Hello, world!');
});

it('should not call console.log() if attribute is empty', async () => {
const spy = vi.spyOn(console, 'log');

const container = document.createElement('div');
container.innerHTML = `<console- log(="" )></console->`;
document.body.appendChild(container);
ElementGraph.build().execute();

expect(spy).not.toHaveBeenCalled();
});

it('should expand variables before logging', () => {
const spy = vi.spyOn(console, 'log');

const container = document.createElement('div');
container.innerHTML = `
<const- i="2"></const->
<console- log(="{i} should be 2" )></console->
`;
document.body.appendChild(container);
ElementGraph.build().execute();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('2 should be 2');
});

it('should log undefined variables', () => {
const spy = vi.spyOn(console, 'log');

const container = document.createElement('div');
container.innerHTML = `
<console- log(="{i} should be undefined" )></console->
`;
document.body.appendChild(container);
ElementGraph.build().execute();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('undefined should be undefined');
});
});
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './style.css';

import { AlertDash } from './components/alert';
import { ConsoleDash } from './components/console';
import { ConstDash } from './components/const';
import { ElseDash } from './components/else';
Expand All @@ -18,6 +19,7 @@ export { globalScope };

export function defineElements(): void {
const elements: Array<typeof BaseHtmlangElement> = [
AlertDash,
ConsoleDash,
ConstDash,
ElseDash,
Expand Down