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

HTML escape key names #1108

Merged
merged 2 commits into from Apr 3, 2019
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
4 changes: 2 additions & 2 deletions src/cgame/cg_api.cpp
Expand Up @@ -44,7 +44,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

int VM::VM_API_VERSION = CGAME_API_VERSION;

void CG_Init( int serverMessageNum, int clientNum, glconfig_t gl, GameStateCSs gameState );
void CG_Init( int serverMessageNum, int clientNum, const glconfig_t& gl, const GameStateCSs& gameState );
void CG_Shutdown();

void VM::VMHandleSyscall(uint32_t id, Util::Reader reader) {
Expand All @@ -62,7 +62,7 @@ void VM::VMHandleSyscall(uint32_t id, Util::Reader reader) {
break;

case CG_INIT:
IPC::HandleMsg<CGameInitMsg>(VM::rootChannel, std::move(reader), [] (int serverMessageNum, int clientNum, glconfig_t gl, GameStateCSs gamestate) {
IPC::HandleMsg<CGameInitMsg>(VM::rootChannel, std::move(reader), [] (int serverMessageNum, int clientNum, const glconfig_t& gl, const GameStateCSs& gamestate) {
CG_Init(serverMessageNum, clientNum, gl, gamestate);
cmdBuffer.TryFlush();
});
Expand Down
2 changes: 1 addition & 1 deletion src/cgame/cg_main.cpp
Expand Up @@ -1442,7 +1442,7 @@ Will perform callbacks to make the loading info screen update.
=================
*/

void CG_Init( int serverMessageNum, int clientNum, glconfig_t gl, GameStateCSs gameState)
void CG_Init( int serverMessageNum, int clientNum, const glconfig_t& gl, const GameStateCSs& gameState)
{
const char *s;

Expand Down
11 changes: 5 additions & 6 deletions src/cgame/cg_rocket_draw.cpp
Expand Up @@ -3294,7 +3294,6 @@ static void CG_Rocket_DrawPlayerUnlockedItems()

static void CG_Rocket_DrawVote_internal( team_t team )
{
char *s;
int sec;

if ( !cgs.voteTime[ team ] )
Expand All @@ -3317,16 +3316,16 @@ static void CG_Rocket_DrawVote_internal( team_t team )
sec = 0;
}

Rocket::Core::String yeskey = CG_KeyBinding( va( "%svote yes", team == TEAM_NONE ? "" : "team" ), team );
Rocket::Core::String nokey = CG_KeyBinding( va( "%svote no", team == TEAM_NONE ? "" : "team" ), team );
std::string yeskey = CG_EscapeHTMLText( CG_KeyBinding( va( "%svote yes", team == TEAM_NONE ? "" : "team" ), team ) );
std::string nokey = CG_EscapeHTMLText( CG_KeyBinding( va( "%svote no", team == TEAM_NONE ? "" : "team" ), team ) );

s = va( "%sVOTE(%i): %s\n"
std::string s = Str::Format( "%sVOTE(%i): %s\n"
" Called by: \"%s\"\n"
" [%s][<span class='material-icon'>&#xe8dc;</span>]:%i [%s][<span class='material-icon'>&#xe8db;</span>]:%i\n",
team == TEAM_NONE ? "" : "TEAM", sec, cgs.voteString[ team ],
cgs.voteCaller[ team ], yeskey.CString(), cgs.voteYes[ team ], nokey.CString(), cgs.voteNo[ team ] );
cgs.voteCaller[ team ], yeskey, cgs.voteYes[ team ], nokey, cgs.voteNo[ team ] );

Rocket_SetInnerRML( s, 0 );
Rocket_SetInnerRML( s.c_str(), 0 );
}

static void CG_Rocket_DrawVote()
Expand Down
22 changes: 22 additions & 0 deletions src/cgame/rocket/rocket.cpp
Expand Up @@ -485,6 +485,28 @@ static bool IsInvalidEmoticon( Rocket::Core::String emoticon )
return false;
}

std::string CG_EscapeHTMLText( Str::StringRef text )
Copy link
Member

Choose a reason for hiding this comment

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

This seems useful to include in other places. noteably Rocket_QuakeToRML and rocketChatField.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I noticed there were a couple of other places doing escaping. But they also did other transformations and used different string types, so it didn't seem worth trying to combine them somehow.

{
std::string out;
for (char c : text) {
switch (c) {
case '<':
out += "&lt;";
break;
case '>':
out += "&gt;";
break;
case '&':
out += "&amp;";
break;
default:
out += c;
break;
}
}
return out;
}

// TODO: Make this take Rocket::Core::String as an input.
Rocket::Core::String Rocket_QuakeToRML( const char *in, int parseFlags = 0 )
{
Expand Down
7 changes: 6 additions & 1 deletion src/cgame/rocket/rocket.h
Expand Up @@ -41,6 +41,7 @@ Maryland 20850 USA.

#include "common/Compiler.h"
#include "common/Color.h"
#include "common/String.h"

#ifdef __GNUC__
#pragma GCC diagnostic push
Expand Down Expand Up @@ -74,8 +75,12 @@ class RocketEvent_t
Rocket::Core::String cmd;
};

// HTML-escape a string that will be used as text. Text meaning that it will be not be
// somewhere weird like inside a tag, which may require a different form of escaping.
std::string CG_EscapeHTMLText( Str::StringRef text );

Rocket::Core::String Rocket_QuakeToRML( const char *in, int parseFlags );
Rocket::Core::String CG_KeyBinding( const char *bind, int team );
std::string CG_KeyBinding( const char *bind, int team );

void Rocket_AddEvent( RocketEvent_t *event );

Expand Down
2 changes: 1 addition & 1 deletion src/cgame/rocket/rocketKeyBinder.h
Expand Up @@ -98,7 +98,7 @@ class RocketKeyBinder : public Rocket::Core::Element, public Rocket::Core::Event
if ( rocketInfo.realtime >= nextKeyUpdateTime && team >= 0 && !cmd.Empty() && !waitingForKeypress )
{
nextKeyUpdateTime = rocketInfo.realtime + KEY_BINDING_REFRESH_INTERVAL_MS;
SetInnerRML( CG_KeyBinding( cmd.CString(), team ) );
SetInnerRML( CG_EscapeHTMLText( CG_KeyBinding( cmd.CString(), team ) ).c_str() );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cgame/rocket/rocket_keys.cpp
Expand Up @@ -313,7 +313,7 @@ void Rocket_MouseMove( int x, int y )
CG_KeyBinding
================
*/
Rocket::Core::String CG_KeyBinding( const char* bind, int team )
std::string CG_KeyBinding( const char* bind, int team )
{
std::vector<Keyboard::Key> keys = trap_Key_GetKeysForBinds( team, {bind} )[0];

Expand All @@ -322,7 +322,7 @@ Rocket::Core::String CG_KeyBinding( const char* bind, int team )
return "Unbound";
}

Rocket::Core::String keyNames = CG_KeyDisplayName( keys[0] ).c_str();
std::string keyNames = CG_KeyDisplayName( keys[0] );

for ( size_t i = 1; i < keys.size(); i++ )
{
Expand Down