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

gherkin/javascript: Escape regex characters in table header #1077

Merged
merged 2 commits into from Jun 23, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion gherkin/CHANGELOG.md
Expand Up @@ -30,14 +30,14 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
* Fixed Estonian translation of "Rule" ([#973](https://github.com/cucumber/cucumber/pull/973) [ookull])
* Fixed Estonian translation of "Scenario Outline" ([#972](https://github.com/cucumber/cucumber/pull/972) [ookull])


### Deprecated

### Removed

### Fixed

* [Ruby] Use `require_relative` for internal requires ([#1010](https://github.com/cucumber/cucumber/pull/1010) [deivid-rodriguez])
* [Javascript] Escape regex characters in table header ([#1077](https://github.com/cucumber/cucumber/pull/1077) [mpkorstanje])

## [13.0.0] - 2020-04-14

Expand Down
6 changes: 4 additions & 2 deletions gherkin/javascript/src/pickles/compile.ts
Expand Up @@ -222,11 +222,13 @@ function interpolate(
) {
variableCells.forEach((variableCell, n) => {
const valueCell = valueCells[n]
const search = new RegExp('<' + variableCell.value + '>', 'g')
const valuePattern = '<' + variableCell.value + '>'
const escapedPattern = valuePattern.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
const regexp = new RegExp(escapedPattern, 'g')
// JS Specific - dollar sign needs to be escaped with another dollar sign
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
const replacement = valueCell.value.replace(new RegExp('\\$', 'g'), '$$$$')
name = name.replace(search, replacement)
name = name.replace(regexp, replacement)
})
return name
}
Expand Down
22 changes: 22 additions & 0 deletions gherkin/javascript/test/ParserTest.ts
Expand Up @@ -3,6 +3,7 @@ import { messages, IdGenerator } from '@cucumber/messages'
import AstBuilder from '../src/AstBuilder'
import Parser from '../src/Parser'
import TokenMatcher from '../src/TokenMatcher'
import { parseAndCompile } from './utils/parse'

describe('Parser', function () {
it('parses a simple feature', function () {
Expand Down Expand Up @@ -122,6 +123,27 @@ describe('Parser', function () {
)
})

it('it interpolates data tables', function () {
const pickles: messages.IPickle[] = []
parseAndCompile(
'Feature: Foo\n' +
' Scenario Outline: Parenthesis\n' +
' Given the thing <is (not) triggered> and has <value>\n' +
' Examples:\n' +
' | is (not) triggered | value |\n' +
' | is triggered | foo |\n ',
(envelope) => {
if (envelope.pickle) {
pickles.push(envelope.pickle)
}
}
)
assert.strictEqual(
pickles[0].steps[0].text,
'the thing is triggered and has foo'
)
})

it('can change the default language', function () {
const parser = new Parser(new AstBuilder(IdGenerator.incrementing()))
const matcher = new TokenMatcher('no')
Expand Down