Skip to content

Commit 9c3de46

Browse files
committed
Fix an issue with unescaped quotemarks in strings.
1 parent dcd2ac0 commit 9c3de46

7 files changed

Lines changed: 45 additions & 11 deletions

File tree

build-tests/localization-plugin-test/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"serve": "node serve.js"
99
},
1010
"dependencies": {
11+
"@microsoft/set-webpack-public-path-plugin": "2.3.3",
1112
"@rushstack/localization-plugin": "0.0.0",
13+
"@types/webpack-env": "1.13.0",
1214
"fs-extra": "~7.0.1",
1315
"@microsoft/rush-stack-compiler-3.5": "0.4.2",
1416
"ts-loader": "6.0.0",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import { string2 } from './strings3.loc.json';
2+
const strings4: string = require('./strings4.loc.json');
23

34
console.log(string2);
5+
6+
console.log(strings4);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"string1": {
3+
"value": "\"String with quotemarks\"",
4+
"comment": "string with quotemarks"
5+
}
6+
}

build-tests/localization-plugin-test/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"rootDirs": [
55
"./src",
66
"./temp/loc-json-ts/"
7+
],
8+
"types": [
9+
"webpack-env"
710
]
811
}
912
}

build-tests/localization-plugin-test/webpack.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const webpack = require('webpack');
55

66
const { LocalizationPlugin } = require('@rushstack/localization-plugin');
7+
const { SetPublicPathPlugin } = require('@microsoft/set-webpack-public-path-plugin');
78
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
89

910
module.exports = function(env) {
@@ -53,6 +54,9 @@ module.exports = function(env) {
5354
"string1": "the third string",
5455
"string2": "the fourth string",
5556
"string3": "UNUSED STRING!"
57+
},
58+
"./src/strings4.loc.json": {
59+
"string1": "\"String with quotemarks\""
5660
}
5761
},
5862
"es-es": {
@@ -66,6 +70,9 @@ module.exports = function(env) {
6670
"string1": "la tercera cadena",
6771
"string2": "la cuarta cadena",
6872
"string3": "UNUSED STRING!"
73+
},
74+
"./src/strings4.loc.json": {
75+
"string1": "\"Cadena con comillas\""
6976
}
7077
}
7178
},
@@ -84,6 +91,12 @@ module.exports = function(env) {
8491
generateStatsFile: true,
8592
statsFilename: path.resolve(__dirname, 'temp', 'stats.json'),
8693
logLevel: 'error'
94+
}),
95+
new SetPublicPathPlugin({
96+
scriptName: {
97+
name: '[name]_[locale]_[contenthash].js',
98+
isTokenized: true
99+
}
87100
})
88101
]
89102
};

common/config/rush/nonbrowser-approved-packages.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
"name": "@microsoft/gulp-core-build-serve",
3939
"allowedCategories": [ "libraries" ]
4040
},
41-
{
42-
"name": "webpack-dev-server",
43-
"allowedCategories": [ "tests" ]
44-
},
4541
{
4642
"name": "@microsoft/gulp-core-build-typescript",
4743
"allowedCategories": [ "libraries" ]
@@ -128,7 +124,7 @@
128124
},
129125
{
130126
"name": "@microsoft/set-webpack-public-path-plugin",
131-
"allowedCategories": [ "libraries" ]
127+
"allowedCategories": [ "libraries", "tests" ]
132128
},
133129
{
134130
"name": "@microsoft/sp-tslint-rules",
@@ -594,12 +590,16 @@
594590
"name": "webpack",
595591
"allowedCategories": [ "libraries", "tests" ]
596592
},
593+
{
594+
"name": "webpack-bundle-analyzer",
595+
"allowedCategories": [ "tests" ]
596+
},
597597
{
598598
"name": "webpack-cli",
599599
"allowedCategories": [ "tests" ]
600600
},
601601
{
602-
"name": "webpack-bundle-analyzer",
602+
"name": "webpack-dev-server",
603603
"allowedCategories": [ "tests" ]
604604
},
605605
{

webpack/localization-plugin/src/LocalizationPlugin.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,10 @@ export class LocalizationPlugin implements Webpack.Plugin {
309309
kind: 'localized';
310310
values: { [locale: string]: string };
311311
size: number;
312+
quotemarkCharacter: string | undefined;
312313
}
313314

314-
const placeholderRegex: RegExp = new RegExp(`${lodash.escapeRegExp(STRING_PLACEHOLDER_PREFIX)}_(\\d+)`, 'g');
315+
const placeholderRegex: RegExp = new RegExp(`${lodash.escapeRegExp(STRING_PLACEHOLDER_PREFIX)}_(.+)_(\\d+)`, 'g');
315316
const result: Map<string, IProcessAssetResult> = new Map<string, IProcessAssetResult>();
316317
const assetSource: string = asset.source();
317318

@@ -326,7 +327,7 @@ export class LocalizationPlugin implements Webpack.Plugin {
326327
};
327328
reconstructionSeries.push(staticElement);
328329

329-
const [placeholder, placeholderSerialNumber] = regexResult;
330+
const [placeholder, quotemark, placeholderSerialNumber] = regexResult;
330331

331332
const values: { [locale: string]: string } | undefined = this._stringPlaceholderMap.get(placeholderSerialNumber);
332333
if (!values) {
@@ -340,7 +341,8 @@ export class LocalizationPlugin implements Webpack.Plugin {
340341
const localizedElement: ILocalizedReconstructionElement = {
341342
kind: 'localized',
342343
values: values,
343-
size: placeholder.length
344+
size: placeholder.length,
345+
quotemarkCharacter: quotemark !== '"' ? quotemark : undefined
344346
};
345347
reconstructionSeries.push(localizedElement);
346348
lastIndex = regexResult.index + placeholder.length;
@@ -362,7 +364,12 @@ export class LocalizationPlugin implements Webpack.Plugin {
362364
reconstruction.push((element as IStaticReconstructionElement).staticString);
363365
} else {
364366
const localizedElement: ILocalizedReconstructionElement = element as ILocalizedReconstructionElement;
365-
const newValue: string = localizedElement.values[locale];
367+
let newValue: string = localizedElement.values[locale];
368+
if (localizedElement.quotemarkCharacter) {
369+
// Replace the quotemark character with the correctly-escaped character
370+
newValue = newValue.replace(/\"/g, localizedElement.quotemarkCharacter)
371+
}
372+
366373
reconstruction.push(newValue);
367374
sizeDiff += (newValue.length - localizedElement.size);
368375
}
@@ -718,7 +725,7 @@ export class LocalizationPlugin implements Webpack.Plugin {
718725

719726
const suffix: string = (this._stringPlaceholderCounter++).toString();
720727
return {
721-
value: `${STRING_PLACEHOLDER_PREFIX}_${suffix}`,
728+
value: `${STRING_PLACEHOLDER_PREFIX}_"_${suffix}`,
722729
suffix: suffix
723730
};
724731
}

0 commit comments

Comments
 (0)