Skip to content

Strange behavior of "greedy" option #1039

@romanvm

Description

@romanvm

First, I must admit that I have little experience in JavaScript. But I like Prism and now I'm working on a language definition for Django/Jinja2 template languages. Those are 2 popular template languages in Python world with similar syntax, so I'm making one definition for both. You can see my working code here.

I'd say that it works OK in 95% cases and now I try to improve handling of borderline cases. One approach that I've tried is using the greedy options for matching template tags {{ ... }} and {% ... %}. Indeed, it improved matching, but on some testing samples a strange bug happens: the first string is copy-pasted all over the code snippet.

Here's my testing code snippet:

{% load i18n %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
  <!-- This is the problem string -->
  <script src="{% static "cerulean_skin/js/ie10-viewport-bug-workaround.js" %}"></script>
</head>
<body>
  <script type="text/javascript">
    var DOCUMENTATION_OPTIONS = {
      URL_ROOT:    '{{ url_root }}',
      VERSION:     '{{ release|e }}',
      COLLAPSE_INDEX: false,
      FILE_SUFFIX: '{{ '' if no_search_suffix else file_suffix }}', // Difficult case
      HAS_SOURCE:  {{ has_source|lower }}
    };
  </script>
</body>
</html>

(GitHub has some support for Django/Jinja2 too, BTW)

And here's what I get with my language definition:
image jpg 3350-17

As you can see, it works OK except for the "difficult case". Now here's what I get with greedy option:

var _django_template = {
  'property': {
    pattern: /(?:{{|{%)[\w\W]*?(?:%}|}})/g,
    greedy: true,
inside: {
...

image jpg 3350-18
As you can see, the "difficult case" is now painted correctly, but the first string {% load i18n %} is copy-pasted all over the place.

By elimination I found out that "the problem string" with <script> tag is causing all this. If I remove it, the copy-pasting problem disappears.

I'd like to know: am I doing something wrong or this is a bug in Prism?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions