TWPXMLSyntax

<< Click to Display Table of Contents >>

Navigation:  Programming > Syntax Highlighting >

TWPXMLSyntax

The XML highlighter TWPXMLSyntax descends from TWPCustomSyntax.

 

It is used to highlight XML and HTML code. It uses a fixed pitch fonts.

 

It only overrides two methods:

 

constructor TWPXMLSyntax.Create(aOwner: TComponent);

begin

inherited Create(aOwner);

 FReservedColor := clBlue;

end;

 

and NextChar which detects the meaning of certain parts of the text. The caller takes care that "FMode" is updated with the mode which was active when the previous paragraph ended.

 

procedure TWPXMLSyntax.NextChar(par: TParagraph; CPos: Integer);

var C: WideChar;

function CC(i: Integer): WideChar;

begin if (CPos + i < 0) or (CPos + i >= par.CharCount) then Result := #0 else Result := par.CharItem[CPos + i]; end;

begin

if FRTFProps <> nil then

begin

   C := par.CharItem[CPos];

  if not (FMode in [wpsynMLComment, wpsynComment, wpsynReserved, wpsynString]) then

  begin

    if (C = '&') then FMode := wpsynChar

    else if (C = '<') then

    begin

      if (CC(1) = '!') and (CC(2) = '-') and (CC(3) = '-') then

         FMode := wpsynMLComment

      else FMode := wpsynReserved;

    end;

    if C = '>' then Ferror := true;

  end

  else

  begin

    if (FMode = wpsynReserved) and (c = #32) then FMode := wpsynString

    else if (FMode = wpsynString) and (c = '>') then FMode := wpsynReserved;

    if C = '<' then Ferror := true;

  end;

  inherited NextChar(par, CPos);

  if (FMode = wpsynMLComment) and (C = '>') and (CC(-1) = '-') then

     FMode := wpsynNormal

  else if (FMode = wpsynReserved) and (C = '>') then

     FMode := wpsynNormal

  else if (FMode = wpsynChar) and ((C = ';') or (C = #32)) then

     FMode := wpsynNormal;

   Ferror := false;

end;

end;