0

I have a piece of code: "></li></a><script>alert(123);</script> that I would like to color using \lstset (i.e => the HTML tags like "li" "a" "script" would be dkblue color). I'm using "listings" package and \lstset extension as this:

\lstset{
language        = html,
basicstyle      = \small\ttfamily,
keywordstyle    = \color{dkblue},
stringstyle     = \color{red},
identifierstyle = \color{dkgreen},
commentstyle    = \color{gray},
emph            =[1]{html},
emphstyle       =[1]\color{black},
emph            =[2]{if,and,or,else},
emphstyle       =[2]\color{dkyellow}}

The problem is that putting "> before the rest of the code prevent LaTeX to interpret the code as HTML. Someone as a solution ?

0

Your code snippet is correctly colored: it starts with a " and therefore it is a string.

A quick and dirty (actually not very quick yet considerably dirty) solution is to copy the html driver from lstlisting without the string delimiter, and add your own preferences:

\documentclass{article}
\usepackage{listings,xcolor}
\definecolor{dkgreen}{rgb}{0,.6,0}
\definecolor{dkblue}{rgb}{0,0,.6}
\definecolor{dkyellow}{cmyk}{0,0,.8,.3}
\lstdefinelanguage{nohtml}{
  morekeywords={A,ABBR,ACRONYM,ADDRESS,APPLET,AREA,B,BASE,BASEFONT,%
      BDO,BIG,BLOCKQUOTE,BODY,BR,BUTTON,CAPTION,CENTER,CITE,CODE,COL,%
      COLGROUP,DD,DEL,DFN,DIR,DIV,DL,DOCTYPE,DT,EM,FIELDSET,FONT,FORM,%
      FRAME,FRAMESET,HEAD,HR,H1,H2,H3,H4,H5,H6,HTML,I,IFRAME,IMG,INPUT,%
      INS,ISINDEX,KBD,LABEL,LEGEND,LH,LI,LINK,LISTING,MAP,META,MENU,%
      NOFRAMES,NOSCRIPT,OBJECT,OPTGROUP,OPTION,P,PARAM,PLAINTEXT,PRE,%
      OL,Q,S,SAMP,SCRIPT,SELECT,SMALL,SPAN,STRIKE,STRING,STRONG,STYLE,%
      SUB,SUP,TABLE,TBODY,TD,TEXTAREA,TFOOT,TH,THEAD,TITLE,TR,TT,U,UL,%
      VAR,XMP,%
      accesskey,action,align,alink,alt,archive,axis,background,bgcolor,%
      border,cellpadding,cellspacing,charset,checked,cite,class,classid,%
      code,codebase,codetype,color,cols,colspan,content,coords,data,%
      datetime,defer,disabled,dir,event,error,for,frameborder,headers,%
      height,href,hreflang,hspace,http-equiv,id,ismap,label,lang,link,%
      longdesc,marginwidth,marginheight,maxlength,media,method,multiple,%
      name,nohref,noresize,noshade,nowrap,onblur,onchange,onclick,%
      ondblclick,onfocus,onkeydown,onkeypress,onkeyup,onload,onmousedown,%
      profile,readonly,onmousemove,onmouseout,onmouseover,onmouseup,%
      onselect,onunload,rel,rev,rows,rowspan,scheme,scope,scrolling,%
      selected,shape,size,src,standby,style,tabindex,text,title,type,%
      units,usemap,valign,value,valuetype,vlink,vspace,width,xmlns},%
   tag=**[s]<>,%
   sensitive=f,%
    basicstyle      = \small\ttfamily,
    keywordstyle    = \color{dkblue},
    stringstyle     = \color{red},
    identifierstyle = \color{dkgreen},
    commentstyle    = \color{gray},
    emph            =[1]{html},
    emphstyle       =[1]\color{black},
    emph            =[2]{if,and,or,else},
    emphstyle       =[2]\color{dkyellow}
}
\begin{document}
\begin{lstlisting}[language=nohtml]
"></li></a><script>alert(123);</script>
\end{lstlisting}
\end{document}

Result:

enter image description here

An advantage of this solution is that for other, well-formed html you can still use the default html definition (with strings).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.