Skip to content

Commit

Permalink
Merge pull request #1637 from wordpress-mobile/update/simplify-androi…
Browse files Browse the repository at this point in the history
…d-strings-xml

Do no escape strings with double quotes in android strings.xml
  • Loading branch information
Tug authored Jan 9, 2020
2 parents 5a3cbc7 + cea513c commit 1aa50ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 12 additions & 4 deletions bin/po2android.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ const indent = ' ';
* @return {string} Escaped string to be copied into the XML <string></string> node
*/
function escapeResourceXML( unsafeXMLValue ) {
// Let's first replace XML special characters that JSON.stringify does not escape: <, > and &
// Then let's use JSON.stringify to handle pre and post spaces as well as escaping ", \, \t and \n
return JSON.stringify( unsafeXMLValue.replace( /[<>&]/g, function( character ) {
// See: https://tekeye.uk/android/examples/android-string-resources-gotchas
// Let's replace XML special characters <, >, &, ", ', \, \t and \n
// Note that this does not support android:textstyle attributes (<b></b>...)
return unsafeXMLValue.replace( /(\r?\n|\r|\t|<|>|&|'|"|\\)/gm, function( character ) {
switch ( character ) {
case '<': return '&lt;';
case '>': return '&gt;';
case '&': return '&amp;';
case '\'': return '\\\'';
case '"': return '\\\"';
case '\r':
case '\n':
case '\r\n': return '\\n';
case '\t': return '\\t';
case '\\': return '\\\\';
}
} ) );
} );
}

/**
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
"pregenstrings": "test -f gutenberg.pot || yarn makepot:gutenberg",
"genstrings": "yarn genstrings:android && yarn genstrings:ios",
"genstrings:android": "yarn makepot:android && ./bin/po2android.js gutenberg-android.pot bundle/android/strings.xml",
"genstrings:android:check": "xmllint --noout bundle/android/strings.xml",
"genstrings:ios": "yarn makepot:ios && ./bin/po2swift.js gutenberg-ios.pot bundle/ios/GutenbergNativeTranslations.swift",
"postgenstrings:android": "yarn genstrings:android:check",
"android": "react-native run-android",
"prewpandroid": "rm -Rf $TMPDIR/gbmobile-wpandroidfakernroot && mkdir $TMPDIR/gbmobile-wpandroidfakernroot && ln -s $(cd \"$(dirname \"../../../\")\"; pwd) $TMPDIR/gbmobile-wpandroidfakernroot/android",
"wpandroid": "yarn android --root $TMPDIR/gbmobile-wpandroidfakernroot --variant wasabiDebug --appIdSuffix beta --appFolder WordPress --main-activity=ui.WPLaunchActivity",
Expand Down

0 comments on commit 1aa50ea

Please sign in to comment.