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

fix: js and json lang quotes escape #9822

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ sub process_template ($template_filename, $template_data_ref, $result_content_re
$template_data_ref->{sep} = separator_before_colon($lc);
$template_data_ref->{lang} = \⟨
$template_data_ref->{f_lang} = \&f_lang;
# escaping quotes for use in javascript or json
# using short names to favour readability
$template_data_ref->{esq} = sub { escape_char(@_, "\'") }; # esq as escape_single_quote
$template_data_ref->{edq} = sub { escape_char(@_, '"') }; # edq as escape_double_quote
$template_data_ref->{lang_sprintf} = \&lang_sprintf;
$template_data_ref->{lc} = $lc;
$template_data_ref->{cc} = $cc;
Expand Down Expand Up @@ -5736,18 +5740,28 @@ sub search_and_export_products ($request_ref, $query_ref, $sort_by) {
return;
}

sub escape_char($s, $char) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Maybe this function (and escape_single_quote) could be moved to Text.pm

if ($s && $char) {
# normalize already escaped chars to avoid double escaping
$s =~ s/\\$char/$char/g;
$s =~ s/$char/\\$char/g;
}
return $s;
}

sub escape_single_quote ($s) {

# some app escape single quotes already, so we have \' already
if (not defined $s) {
return '';
}
# some app escape single quotes already, so we have \' already
$s =~ s/\\'/'/g;
$s =~ s/'/\\'/g;
$s =~ s/\n/ /g;
return $s;
}


@search_series = (qw/organic fairtrade with_sweeteners default/);

my %search_series_colors = (
Expand Down
21 changes: 1 addition & 20 deletions lib/ProductOpener/Lang.pm
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,7 @@ In the .po translation files, we use the msgctxt field for the string id.
=cut

sub lang ($stringid) {

my $short_l = undef;
if ($lang =~ /_/) {
$short_l = $`; # pt_pt
}

# English values have been copied to languages that do not have defined values

if (not defined $Lang{$stringid}) {
return '';
}
elsif (defined $Lang{$stringid}{$lang}) {
return $Lang{$stringid}{$lang};
}
elsif ((defined $short_l) and (defined $Lang{$stringid}{$short_l}) and ($Lang{$stringid}{$short_l} ne '')) {
return $Lang{$stringid}{$short_l};
}
else {
return '';
}
return lang_in_other_lc($lang, $stringid);
}

=head2 f_lang( $stringid, $variables_ref )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"contribution"
],
"title_element": {
"title": "[% lang('contribution_panel_title') %]",
"title": "[% edq(lang('contribution_panel_title')) %]",
},
"elements": [
[%# Panels displaying data_quality errors / warnings / info %]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
[% icon_name = "data-info" %]
[% END %]
"title_element": {
"title": "[% lang(panel.tags_type _ '_panel_title') %]",
"subtitle": "[% lang(panel.tags_type _ '_panel_subtitle') %]",
"title": "[% edq(lang(panel.tags_type _ '_panel_title')) %]",
"subtitle": "[% edq(lang(panel.tags_type _ '_panel_subtitle')) %]",
"icon_url": "[% static_subdomain %]/images/icons/dist/[% icon_name %].svg",
"icon_color_from_evaluation": true,
},
Expand Down Expand Up @@ -58,7 +58,7 @@
{
"element_type": "action",
"action_element": {
"html": `[% lang(action_name) %]`,
"html": `[% edq(lang(action_name)) %]`,
"actions": [
"[% fix_action %]",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
[% END %]
"title_element": {
[% SET driving_100g_rounded = sprintf('%.1f', driving_100g) %]
"title": "[% f_lang('f_equal_to_driving_km_in_a_petrol_car', { 'kilometers' => driving_100g_rounded } ) %]",
"title": "[% edq(f_lang('f_equal_to_driving_km_in_a_petrol_car', { 'kilometers' => driving_100g_rounded } )) %]",
[% SET co2_100g_rounded = sprintf('%.0f', co2_100g * 1000) %]
"subtitle": "[% f_lang('f_carbon_footprint_per_100g_of_product', { 'grams' => co2_100g_rounded }) %]",
"subtitle": "[% edq(f_lang('f_carbon_footprint_per_100g_of_product', { 'grams' => co2_100g_rounded })) %]",
"icon_url": "[% static_subdomain %]/images/icons/dist/car.svg",
"icon_color_from_evaluation": true,
},
Expand All @@ -40,14 +40,14 @@
"table_element": {
"id": "ecoscore_carbon_impact_by_stages_table",
"table_type": "percents",
"title": "[% lang('ecoscore_impact_detail_by_stages') %]",
"title": "[% edq(lang('ecoscore_impact_detail_by_stages')) %]",
"columns": [
{
"text": "[% lang('ecoscore_stage') %]",
"text": "[% edq(lang('ecoscore_stage')) %]",
"type": "text",
},
{
"text": "[% lang('ecoscore_impact') %]",
"text": "[% edq(lang('ecoscore_impact')) %]",
"type": "percent",
}
],
Expand All @@ -58,7 +58,7 @@
"values": [
{
"icon_url": "[% static_subdomain %]/images/icons/dist/[% step %].svg",
"text": "[% lang("ecoscore_$step") %]"
"text": "[% edq(lang("ecoscore_$step")) %]"
},
{
[% co2_step = "co2_$step" %]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"icon_url": "[% static_subdomain %]/images/icons/dist/lca.svg",
"icon_color_from_evaluation": true,
"icon_size": "small",
"title": "[% lang('average_impact_of_the_category') %][% sep %]: [% panel.agribalyse_grade FILTER upper %] (Score: [% panel.agribalyse_score %]/100)",
"subtitle": "[% lang('categories_s') FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name.dquote %]",
"title": "[% edq(lang('average_impact_of_the_category')) %][% sep %]: [% panel.agribalyse_grade FILTER upper %] (Score: [% panel.agribalyse_score %]/100)",
"subtitle": "[% edq(lang('categories_s')) FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name.dquote %]",
"type": "grade",
"grade": "[% panel.agribalyse_grade %]",
},
Expand Down Expand Up @@ -39,14 +39,14 @@
"table_element": {
"id": "ecoscore_lca_impacts_by_stages_table",
"table_type": "percents",
"title": "[% lang('ecoscore_impact_detail_by_stages') %]",
"title": "[% edq(lang('ecoscore_impact_detail_by_stages')) %]",
"columns": [
{
"text": "[% lang('ecoscore_stage') %]",
"text": "[% edq(lang('ecoscore_stage')) %]",
"type": "text",
},
{
"text": "[% lang('ecoscore_impact') %]",
"text": "[% edq(lang('ecoscore_impact')) %]",
"type": "percent",
}
],
Expand All @@ -57,7 +57,7 @@
"values": [
{
"icon_url": "[% static_subdomain %]/images/icons/dist/[% step %].svg",
"text": "[% lang("ecoscore_$step") %]"
"text": "[% edq(lang("ecoscore_$step")) %]"
},
{
[% ef_step = "ef_$step" %]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{
"element_type": "panel_group",
"panel_group_element": {
"title": "[% lang('life_cycle_analysis') %]",
"title": "[% edq(lang('life_cycle_analysis')) %]",
"panel_ids": [
"ecoscore_agribalyse",
],
Expand All @@ -50,7 +50,7 @@
{
"element_type": "panel_group",
"panel_group_element": {
"title": "[% lang('ecoscore_bonuses_and_maluses') %]",
"title": "[% edq(lang('ecoscore_bonuses_and_maluses')) %]",
"panel_ids": [
[% FOREACH adjustment IN ["production_system", "origins_of_ingredients", "threatened_species", "packaging"] %]
[% IF (adjustment == "origins_of_ingredients") or (adjustment == "packaging") or (product.ecoscore_data.adjustments.$adjustment.value != 0) %]
Expand All @@ -63,7 +63,7 @@
{
"element_type": "panel_group",
"panel_group_element": {
"title": "[% lang('ecoscore_for_this_product') %]",
"title": "[% edq(lang('ecoscore_for_this_product')) %]",
"panel_ids": [ "ecoscore_total"],
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[% SET climate_change_ratio_to_category = panel.climate_change / panel.ecoscore_extended_data_for_category.climate_change %]


// "title": "[% f_lang('f_equal_to_driving_km_in_a_petrol_car', { 'kilometers' => driving_100g_rounded } ) %]",
// "title": "[% edq(f_lang('f_equal_to_driving_km_in_a_petrol_car', { 'kilometers' => driving_100g_rounded } )) %]",
{
"level" :"info",
"topics": [
Expand All @@ -25,7 +25,7 @@
[% SET climate_change_ratio_percent_less = sprintf('%d', (1 - climate_change_ratio_to_category) * 100) %]
"title": "Environmental impact of ingredients [% climate_change_ratio_percent_less %]% smaller than similar products",
[% END %]
"subtitle": "Compared to: [% lang('categories_s') FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name.dquote %]",
"subtitle": "Compared to: [% edq(lang('categories_s')) FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name.dquote %]",
"type": "grade",
"icon_url": "[% static_subdomain %]/images/icons/dist/scale-balance.svg",
"icon_color_from_evaluation": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"title_element": {
"icon_url": "[% static_subdomain %]/images/attributes/dist/ecoscore-not-applicable.svg",
"title": "[% lang("attribute_ecoscore_not_applicable_title") %]",
"title": "[% edq(lang('attribute_ecoscore_not_applicable_title')) %]",
"subtitle": "[% panel.subtitle %]",
"type": "grade",
"grade": "unknown",
Expand All @@ -15,7 +15,7 @@
"element_type": "text",
"text_element": {
"type": "summary",
"html": "[% lang("ecoscore_not_applicable_coming_soon") %]"
"html": "[% edq(lang('ecoscore_not_applicable_coming_soon')) %]"
}
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"title_element": {
"icon_url": "[% static_subdomain %]/images/attributes/dist/ecoscore-unknown.svg",
"title": "[% lang("attribute_ecoscore_unknown_title") %] - [% lang("attribute_ecoscore_unknown_description_short") %]",
"title": "[% eqq(lang('attribute_ecoscore_unknown_title')) %] - [% edq(lang('attribute_ecoscore_unknown_description_short')) %]",
"type": "grade",
"grade": "unknown",
},
Expand All @@ -14,7 +14,7 @@
"element_type": "text",
"text_element": {
"type": "summary",
"html": "[% lang("ecoscore_unknown_call_to_help") %]"
"html": "[% edq(lang('ecoscore_unknown_call_to_help')) %]"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
[% IF product.ecoscore_data.adjustments.origins_of_ingredients.warning == "origins_are_100_percent_unknown" %]
"evaluation": "unknown",
"title_element": {
"title": "[% lang('ecoscore_origins_of_ingredients_missing_information') %]",
"subtitle": "[% lang('malus') %][% sep %]: [% product.ecoscore_data.adjustments.origins_of_ingredients.value %]",
"title": "[% edq(lang('ecoscore_origins_of_ingredients_missing_information')) %]",
"subtitle": "[% edq(lang('malus')) %][% sep %]: [% product.ecoscore_data.adjustments.origins_of_ingredients.value %]",
"icon_color_from_evaluation": true,
"icon_url": "[% static_subdomain %]/images/icons/dist/public.svg",
"icon_size": "small",
Expand All @@ -28,20 +28,20 @@
[% IF product.ecoscore_data.adjustments.origins_of_ingredients.value <= 0 %]
"evaluation": "bad",
"title_element": {
"title": "[% lang('ecoscore_origins_of_ingredients_impact_high') %]",
"title": "[% edq(lang('ecoscore_origins_of_ingredients_impact_high')) %]",
[% ELSIF product.ecoscore_data.adjustments.origins_of_ingredients.value <= 15 %]
"evaluation": "average",
"title_element": {
"title": "[% lang('ecoscore_origins_of_ingredients_impact_medium') %]",
"title": "[% edq(lang('ecoscore_origins_of_ingredients_impact_medium')) %]",
[% ELSE %]
"evaluation": "good",
"title_element": {
"title": "[% lang('ecoscore_origins_of_ingredients_impact_low') %]",
"title": "[% edq(lang('ecoscore_origins_of_ingredients_impact_low')) %]",
[% END %]
[% IF product.ecoscore_data.adjustments.origins_of_ingredients.value > 0 %]
"subtitle": "[% lang('bonus') %][% sep %]: +[% product.ecoscore_data.adjustments.origins_of_ingredients.value %]",
"subtitle": "[% edq(lang('bonus')) %][% sep %]: +[% product.ecoscore_data.adjustments.origins_of_ingredients.value %]",
[% ELSE %]
"subtitle": "[% lang('malus') %][% sep %]: [% product.ecoscore_data.adjustments.origins_of_ingredients.value %]",
"subtitle": "[% edq(lang('malus')) %][% sep %]: [% product.ecoscore_data.adjustments.origins_of_ingredients.value %]",
[% END %]
"icon_color_from_evaluation": true,
"icon_url": "[% static_subdomain %]/images/icons/dist/public.svg",
Expand All @@ -62,18 +62,18 @@
"element_type": "table",
"table_element": {
"id": "ecoscore_origins_of_ingredients_table",
"title": "[% lang('ecoscore_origins_of_ingredients') %]",
"title": "[% edq(lang('ecoscore_origins_of_ingredients')) %]",
"columns": [
{
"text": "[% lang('origin') %]",
"text": "[% edq(lang('origin')) %]",
"type": "text",
},
{
"text": "[% lang('percent_of_ingredients') %]",
"text": "[% edq(lang('percent_of_ingredients')) %]",
"type": "percent",
},
{
"text": "[% lang('ecoscore_impact') %]",
"text": "[% edq(lang('ecoscore_impact')) %]",
"type": "text",
}
],
Expand All @@ -100,13 +100,13 @@
},
{
[% IF score >= 15 %]
"text": "[% lang('low') FILTER ucfirst %]",
"text": "[% edq(lang('low')) FILTER ucfirst %]",
"evaluation": "good",
[% ELSIF score <= 0 %]
"text": "[% lang('high') FILTER ucfirst %]",
"text": "[% edq(lang('high')) FILTER ucfirst %]",
"evaluation": "bad",
[% ELSE %]
"text": "[% lang('medium') FILTER ucfirst %]",
"text": "[% edq(lang('medium')) FILTER ucfirst %]",
"evaluation": "neutral",
[% END %]
}
Expand Down
Loading
Loading