bem / bem-xjst

bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
https://bem.github.io/bem-xjst
Other
116 stars 48 forks source link

Single quotes in js attrs #518

Closed miripiruni closed 6 years ago

miripiruni commented 6 years ago

Fixes #504

Changes proposed in this pull request

New option singleQuotesForDataAttrs.

By default data-attributes values will be marked with double quotes. For BEMJSON:

{
  block: 'b',
  attrs: {
    id: 'without-changes',
    'data-test': '{"reqid":"42"}'
  }
}

Result of templating:

<div class="b" id="without-changes" data-test="{&quot;reqid&quot;:&quot;42&quot;}"></div>

All double quotes inside data-test value was escaped. If you want to avoid such behaviour you can use singleQuotesForDataAttrs option.

var bemxjst = require('bem-xjst');
var templates = bemxjst.bemhtml.compile(() => {
    // In this example we will add no templates.
    // Default behaviour is used for HTML rendering.
    }, {
        // use single quotes for data-* attributes
        singleQuotesForDataAttrs: true
    });

var bemjson = { block: 'b', attrs: {
  id: 'without-changes',
  'data-test': '{"reqid":"42"}'
} };

var html = templates.apply(bemjson);

Result of templating with option:

<div class="b" id="without-changes" data-test='{"reqid":"42"}'></div>

Checklist

Benchmark result

miripiruni-osx:bench miripiruni$ rm -fr dat-0c15b-4ee9b && node runner.js --rev1 0c15b96ddcd899eb861e633d68432770b73c47b8 --rev2 4ee9bbd2059c6e8dc6627785e591bf9fa3a27a06  --bemjson 2000 --dataPath ~/Documents/www/web-data/data --templatePath ./template-8x.js
Test started…
{ _: [],
  h: false,
  help: false,
  rev1: '0c15b96ddcd899eb861e633d68432770b73c47b8',
  rev2: '4ee9bbd2059c6e8dc6627785e591bf9fa3a27a06',
  bemjson: 2000,
  dataPath: '/Users/miripiruni/Documents/www/web-data/data',
  templatePath: './template-8x.js',
  '$0': 'runner.js',
  repeat: undefined,
  verbose: undefined }
2018-04-09T14:27:54.695Z
Total test time:  29056.104578
./lib/compare.py ./dat-0c15b-4ee9b ./dat-0c15b-4ee9b/0c15b96ddcd899eb861e633d68432770b73c47b8-2000-1523284089260.dat ./dat-0c15b-4ee9b/4ee9bbd2059c6e8dc6627785e591bf9fa3a27a06-2000-1523284103744.dat
Percentile:  0.5
{ rev1: 4.170607,
  rev2: 4.148633,
  'diff abs': 0.02197400000000016,
  'diff percent': 0.5268777422567039 }

Percentile:  0.9
{ rev1: 6.28738,
  rev2: 6.243454,
  'diff abs': 0.04392599999999991,
  'diff percent': 0.6986375883118234 }

Percentile:  0.95
{ rev1: 7.04883,
  rev2: 7.197721,
  'diff abs': 0.14889099999999988,
  'diff percent': 2.068585320270122 }

The result within measurement error. Looks like no speed degradation.

miripiruni commented 6 years ago

cc @sbmaxx @tadatuta @zxqfox

coveralls commented 6 years ago

Coverage Status

Coverage increased (+0.001%) to 99.404% when pulling 0c15b96ddcd899eb861e633d68432770b73c47b8 on singleQuotesInJsAttrs into 4ee9bbd2059c6e8dc6627785e591bf9fa3a27a06 on master.

qfox commented 6 years ago

Но почему у нас вообще есть деградация по скорости? Мы же теперь можем не запускать экранирование для &quot; ?

miripiruni commented 6 years ago

@zxqfox деградации нет. Это погрешность изменения на локальной машине. Считай, что ничего не изменилось. Можешь попробовать позапускать у себя несколько раз и посмотреть как варьируются результаты.

miripiruni commented 6 years ago

Мы же теперь можем не запускать экранирование для " ?

Я же измерял без опции. Соответственно, по приборам ничего даже шолохнуться не должно было. А в результате видно только погрешность изменения. Ни больше ни меньше.

qfox commented 6 years ago

Ага, я неправильно трактовал результаты. Спасибо)

miripiruni commented 6 years ago

Integration test: https://nda.ya.ru/3UDtXQ