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

Data-bound shorthand-if with <= breaks extraction #108

Open
rubenswieringa opened this issue Sep 2, 2015 · 0 comments
Open

Data-bound shorthand-if with <= breaks extraction #108

rubenswieringa opened this issue Sep 2, 2015 · 0 comments

Comments

@rubenswieringa
Copy link
Contributor

tl;dr: The following breaks string-extraction: <p translate>{{ n <= 1 ? 'a' : 'b' }}</p>

When I did a quick scan through already-filed issues I couldn't find any similar to this – sorry if I missed it and this is a duplicate.

Steps to reproduction

  1. In an html-element with a translate-attribute, use a smaller-than-or-equal-to operator (<=) in a data-bounds ({{ }}) expression.
  2. Run the nggettext_extract grunt-task

Expected:

  • Only the inner contents of the html-element should be extracted by the grunt-task. ({{ n <= 1 ? 'a' : 'b' }})

Actual:

  • Everything after the opening tag of the html-element is extracted (the extracted string might look something like {{ n <= 1 ? 'a' : 'b' }}</p></div></section>)

Tests/code

The following will break on the 4th <p>:

<!-- test/fixtures/shorthand_if.html -->
<html>
    <body>
        <p translate>1 start {{ n > 1 ? 'if' : 'else' }} end</p>
        <!-- comment after 1st line -->
        <span>after first</span>
        <p translate>2 start {{ n >= 1 ? 'if' : 'else' }} end</p>
        <!-- comment after 2nd line -->
        <span>after second</span>
        <p translate>3 start {{ n < 1 ? 'if' : 'else' }} end</p>
        <!-- comment after 3rd line -->
        <span>after third</span>
        <p translate>4 start {{ n <= 1 ? 'if' : 'else' }} end</p>
        <!-- comment after 4th line -->
        <span>after fourth</span>
    </body>
</html>
// test/extract_shorthand_if.js

var assert = require('assert');
var fs = require('fs');
var Extractor = require('..').Extractor;
var testExtract = require('./utils').testExtract;

describe('String extractor', function () {
    it('extracts strings with data-bound shorthand-if statements containing smaller-than-or-equal-to operators', function () {
        var files = [
            'test/fixtures/shorthand_if.html'
        ];

        var paragraphs = [
            "1 start {{ n > 1 ? 'if' : 'else' }} end",
            "2 start {{ n >= 1 ? 'if' : 'else' }} end",
            "3 start {{ n < 1 ? 'if' : 'else' }} end",
            "4 start {{ n <= 1 ? 'if' : 'else' }} end"
        ];

        var catalog = testExtract(files);
        assert.equal(catalog.items.length, paragraphs.length);

        for ( var i = 0; i < paragraphs.length; i++ ) {
            assert.equal(catalog.items[i].msgid, paragraphs[i]);
            assert.equal(catalog.items[i].msgstr, '');
            assert.deepEqual(catalog.items[i].references, ['test/fixtures/shorthand_if.html:' + ( 3 + i * 3 )]);
        }
    });
});

Result/output

AssertionError: "4 start {{ n <= 1=\"\" ?=\"\" 'if'=\"\" :=\"\" 'else'=\"\" }}=\"\" end<=\"\" p=\"\">\n        <!-- comment after 4th line -->\n == "4 start {{ n <= 1 ? 'if' : 'else' }} end"
+ expected - actual

-4 start {{ n <= 1="" ?="" 'if'="" :="" 'else'="" }}="" end<="" p="">
-        <!-- comment after 4th line -->
-        <span>after fourth</span>
-    </=>
+4 start {{ n <= 1 ? 'if' : 'else' }} end

Workaround

Use a greater-than operator (>) and swap your ‘if’ and ‘else’ values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant