Skip to content

Commit

Permalink
Bugfix for #321: Twig builder should load JSON strings as Twig "safe …
Browse files Browse the repository at this point in the history
…strings".

Empty strings were getting converted to empty objects.
  • Loading branch information
JohnAlbin committed May 23, 2016
1 parent d733583 commit 4dc32e3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion builder/base/twig/kss_builder_base_twig.js
Expand Up @@ -147,7 +147,7 @@ class KssBuilderBaseTwig extends KssBuilderBase {
this.Twig.extend(Twig => {
safeMarkup = function(input) {
if (typeof input === 'string' || typeof input === 'number' || typeof input === 'boolean') {
return new Twig.Markup(input);
return Twig.Markup(input);
} else if (Array.isArray(input)) {
return input.map(safeMarkup);
} else if (input && typeof input === 'object') {
Expand Down
9 changes: 7 additions & 2 deletions test/fixtures/builder-twig-with-assets/index.twig
Expand Up @@ -56,8 +56,8 @@ MENU-ITEM-CHILD:[referenceURI:{{ menu_child.referenceURI }}, referenceNumber:{{
{% endif %}
{% if section.modifiers %}modifiers:yes{% else %}modifiers:none{% endif %}

{% if section.markup %}
ref:{{ section.reference }}:markup:{{ section.markup|raw }}
{% if section.example %}
ref:{{ section.reference }}:example:{{ section.example|raw }}
{% if section.modifiers %}
modifiers:
{% for modifier in section.modifiers %}
Expand All @@ -67,6 +67,11 @@ MENU-ITEM-CHILD:[referenceURI:{{ menu_child.referenceURI }}, referenceNumber:{{
ref:{{ section.reference }}:modifier:{{ modifier.className }}:markup:{{ modifier.markup }}
{% endfor %}
{% endif %}
{% else %}
ref:{{ section.reference }}:no-example:{{ section.example|raw }}
{% endif %}
{% if section.markup %}
ref:{{ section.reference }}:markup:{{ section.markup|raw }}
{% else %}
ref:{{ section.reference }}:no-markup:{{ section.markup|raw }}
{% endif %}
Expand Down
10 changes: 1 addition & 9 deletions test/fixtures/source-twig-builder-test/1c.json
@@ -1,12 +1,4 @@
{
"modifier_class": "one-cee-from-json",
"content": "This is the content.",
"sampleArray": [
"An item",
"Another item"
],
"sampleNull": null,
"sampleObject": {
"sampleProperty": "value"
}
"content": "This is the content."
}
10 changes: 9 additions & 1 deletion test/fixtures/source-twig-builder-test/kss-example-1c.json
@@ -1,4 +1,12 @@
{
"modifier_class": "one-cee-example-from-json",
"content": "This is the content."
"content": "This is the content.",
"sampleArray": [
"An item in SampleArray",
"Another item in SampleArray"
],
"sampleNull": null,
"sampleObject": {
"sampleProperty": "sampleProperty value"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/source-twig-builder-test/kss-example-1c.twig
Expand Up @@ -3,4 +3,9 @@
<p>
{{ content }}
</p>
{% for item in sampleArray %}
{{ item }}
{% endfor %}
<p>Empty sampleNull:{{ sampleNull }}:</p>
<p>{{ sampleObject.sampleProperty }}</p>
</div>
14 changes: 10 additions & 4 deletions test/test_kss_builder_base_twig.js
Expand Up @@ -351,16 +351,16 @@ describe('KssBuilderBaseTwig object API', function() {

describe('Twig markup data', function() {
it('should render {{ markup }}', function() {
expect(this.files['section-1']).to.include('ref:1.B:markup:<span>inline</span>');
expect(this.files['section-1']).to.include('ref:1.D:markup:missing-file.twig NOT FOUND!');
expect(this.files['section-1']).to.include('ref:1.B:example:<span>inline</span>');
expect(this.files['section-1']).to.include('ref:1.D:example:missing-file.twig NOT FOUND!');
});

it('should render kss-example-* {{ markup }}', function() {
expect(this.files['section-1']).to.include('<h1>Example 1c</h1>');
});

it('should render {{ markup }} when there is no markup', function() {
expect(this.files['section-1']).to.include('ref:1.A:no-markup:\n');
expect(this.files['section-1']).to.include('ref:1.A:no-example:\n');
});

it('should add modifier_class from the JSON data', function() {
Expand All @@ -377,7 +377,13 @@ describe('KssBuilderBaseTwig object API', function() {
});

it('should add modifier_class from the placeholder option if used on section', function() {
expect(this.files['section-1']).to.include('ref:1.E:markup:<div class="[modifier class]"></div>');
expect(this.files['section-1']).to.include('ref:1.E:example:<div class="[modifier class]"></div>');
});

it('should add safe markup from the example JSON data', function() {
expect(this.files['section-1']).to.include('An item in SampleArray');
expect(this.files['section-1']).to.include('Empty sampleNull::');
expect(this.files['section-1']).to.include('sampleProperty value');
});
});

Expand Down

0 comments on commit 4dc32e3

Please sign in to comment.