Skip to content

Commit

Permalink
fix: LEAP-580: Display non-string values in Text (HumanSignal#1675)
Browse files Browse the repository at this point in the history
* Fix: LEAP-580: Display non-string values in Text

During recent incident fix we missed conversion to string
for Text tag's `value` param.

* Fix deprecated `substr` to `slice` in `parseValue`

* Add cypress test for non-string values

---------

Co-authored-by: hlomzik <[email protected]>
  • Loading branch information
2 people authored and MasherJames committed Feb 29, 2024
1 parent 9e751b1 commit 210b3cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/tags/object/RichText/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const Model = types
// nodes count better be the same, so replace them with stubs
// we should not sanitize text tasks because we already have htmlEscape in view.js
if (isFF(FF_SAFE_TEXT) && self.type === 'text') {
self._value = val;
self._value = String(val);
} else {
self._value = sanitizeHtml(String(val));
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export const parseValue = (value, task) => {

// value can refer to structures, not only texts, so just replace wouldn't be enough
if (value.match(reVar)?.[0] === value) {
return get(task, value.substr(1)) ?? '';
return get(task, value.slice(1)) ?? '';
}

return value.replace(reVar, (v) => get(task, v.substr(1) ?? ''));
return value.replace(reVar, (v) => get(task, v.slice(1) ?? ''));
};

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/functional/specs/object_tags/text.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { LabelStudio } from '@heartexlabs/ls-test/helpers/LSF';

describe('<Text> tag', () => {
it('Display non-string values', () => {
const config = `
<View>
<Header>String — usual case</Header>
<Text name="string" value="$string"></Text>
<Header>Float number</Header>
<Text name="number" value="$number"></Text>
<Header>Boolean</Header>
<Text name="bool" value="$bool"></Text>
<Header>Array</Header>
<Text name="array" value="$array"></Text>
<Header value="Crazy header $string $number $bool $array" />
</View>
`;

const data = {
string: 'Simple text',
number: 123.45,
bool: false,
array: [1, 2, 3],
};

LabelStudio.params()
.config(config)
.data(data)
.withResult([])
.init();

cy.get('.lsf-object').contains('Simple text').should('be.visible');
cy.get('.lsf-object').contains('123.45').should('be.visible');
cy.get('.lsf-object').contains('false').should('be.visible');
cy.get('.lsf-object').contains('1,2,3').should('be.visible');
});
});

0 comments on commit 210b3cf

Please sign in to comment.