Skip to content
Merged
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Here's an example of how it can be used:

The rendered view will output:
```html
<script type="text/javascript">window['key'] = 'value';</script>
<script>window['key'] = 'value';</script>
```

So in your browser you now have access to a key variable:
Expand Down Expand Up @@ -83,7 +83,7 @@ With the package installed you can make use of a `@javascript` Blade directive.

The rendered view will output:
```html
<script type="text/javascript">key = 'value';</script>
<script>key = 'value';</script>
```

You can also use a single argument:
Expand All @@ -93,13 +93,13 @@ You can also use a single argument:

This will also output:
```html
<script type="text/javascript">key = 'value';</script>
<script>key = 'value';</script>
```

When setting the namespace to eg `js` in the config file this will be the output:

```html
<script type="text/javascript">window['js'] = window['js'] || {};js['key'] = 'value';</script>
<script>window['js'] = window['js'] || {};js['key'] = 'value';</script>
```

## Changelog
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function render(...$arguments): string
{
$variables = $this->normalizeArguments($arguments);

return '<script type="text/javascript">'.$this->buildJavaScriptSyntax($variables).'</script>';
return '<script>'.$this->buildJavaScriptSyntax($variables).'</script>';
}

/**
Expand Down
74 changes: 35 additions & 39 deletions tests/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BladeTest extends TestCase
public function it_can_render_a_key_value_pair()
{
$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
$this->renderView('keyValue')
);
}
Expand All @@ -24,7 +24,7 @@ public function it_can_render_an_array()
$parameter = ['key' => 'value'];

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
$this->renderView('variable', compact('parameter'))
);
}
Expand All @@ -35,7 +35,7 @@ public function it_can_render_a_numeric_value()
$parameter = ['number' => 1];

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 1;</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 1;</script>',
$this->renderView('variable', compact('parameter'))
);
}
Expand All @@ -46,14 +46,14 @@ public function it_can_render_a_boolean()
$parameter = ['boolean' => true];

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = true;</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = true;</script>',
$this->renderView('variable', compact('parameter'))
);

$parameter = ['boolean' => false];

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = false;</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = false;</script>',
$this->renderView('variable', compact('parameter'))
);
}
Expand All @@ -64,75 +64,71 @@ public function it_can_render_null()
$parameter = ['nothing' => null];

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'nothing\'] = null;</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'nothing\'] = null;</script>',
$this->renderView('variable', compact('parameter'))
);
}

/** @test */
public function it_can_render_arrayable_objects()
{
$parameter = new class implements Arrayable
{
public function toArray()
{
return ['arrayableKey' => 'arrayableValue'];
}
};
$parameter = new class implements Arrayable {
public function toArray()
{
return ['arrayableKey' => 'arrayableValue'];
}
};

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'arrayableKey\'] = \'arrayableValue\';</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'arrayableKey\'] = \'arrayableValue\';</script>',
$this->renderView('variable', compact('parameter'))
);
}

/** @test */
public function it_can_render_json_serializable_objects()
{
$parameter = new class implements JsonSerializable
{
public function jsonSerialize()
{
return ['jsonKey' => 'jsonValue'];
}
};
$parameter = new class implements JsonSerializable {
public function jsonSerialize()
{
return ['jsonKey' => 'jsonValue'];
}
};

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
$this->renderView('variable', compact('parameter'))
);
}

/** @test */
public function it_can_render_an_object_that_implements_toJson()
{
$parameter = new class
{
public function toJson()
{
return json_encode(['jsonKey' => 'jsonValue']);
}
};
$parameter = new class {
public function toJson()
{
return json_encode(['jsonKey' => 'jsonValue']);
}
};

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
$this->renderView('variable', compact('parameter'))
);
}

/** @test */
public function it_can_render_an_object_that_implements_to_string()
{
$parameter = new class
{
public function __toString()
{
return 'string';
}
};
$parameter = new class {
public function __toString()
{
return 'string';
}
};

$this->assertEquals(
'<script type="text/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = \'string\';</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = \'string\';</script>',
$this->renderView('variable', compact('parameter'))
);
}
Expand All @@ -143,7 +139,7 @@ public function it_can_render_data_without_a_namespace()
$this->app['config']->set('laravel-blade-javascript.namespace', '');

$this->assertEquals(
'<script type="text/javascript">window[\'key\'] = \'value\';</script>',
'<script>window[\'key\'] = \'value\';</script>',
$this->renderView('keyValue')
);
}
Expand Down