[yunqa.de] Re: missing the '=' in tag attribute with empty value after deletion another attribute

  • From: Delphi Inspiration <delphi@xxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Thu, 26 Apr 2012 13:47:16 +0200

On 25.04.2012 05:18, Кудрявцев Дмитрий wrote:

> I  use  TDIHtmlParser with TDIHtmlWriterPlugin and found the following
> strange result when processing tags:
> if  tag  has two attributes: first is empty (e.g. title=""), second is
> not  empty  (e.g.  href="1.htm"),  then  deletion  of second attribute
> causes that 1st (empty) attribute loses the ="" part and become in our
> example simple "title" (without quotes).
> May be it is not correct?

Yes, this is correct in HTML terms. An attribute without a value is just
like the attribute with an empty value:

  http://www.w3.org/TR/html-markup/syntax.html#syntax-attr-empty

> The DIHtmlWriterPlugin1.QuoteHtmlTags := qtAlways; does not helps.

You can force to write out empty attribute values by specifying

  DIHtmlWriterPlugin1.EmptyHtmlAttribValues := True;

To demonstrate this, I have copied an example project below.

Ralf

{-------------------------------------------------------------------------------

  DIHtmlParser project to demonstrate how to force TDIHtmlWriterPlugin
  to always write out quoted empty attribute values.

  Visit the DIHtmlParser homepage for latest information and updates:

    http://www.yunqa.de/delphi/

  Copyright (c) 2012 Ralf Junker, The Delphi Inspiration <delphi@xxxxxxxx>

-------------------------------------------------------------------------------}

program WriterPlugin_2;

{$APPTYPE Console}

uses
  {$IFDEF FastMM}{$I FastMM_uses.inc}{$ENDIF}
  DISystemCompat, DIHtmlMisc, DIHtmlParser, DIHtmlWriterPlugin;

const
  HTML: RawByteString = '<a title="" href="1.htm">';
var
  HP: TDIHtmlParser;
  HW: TDIHtmlWriterPlugin;
begin
  {$IFDEF FastMM}{$I FastMM_init.inc}{$ENDIF}

  HP := TDIHtmlParser.Create(nil);
  HW := TDIHtmlWriterPlugin.Create(nil);
  try
    HW.HtmlParser := HP;

    HP.SetAllFilters(fishow);
    HP.SourceBufferAsStrA := HTML;

    { Always write out quoted empty attribute values. }
    HW.EmptyHtmlAttribValues := True;
    HW.QuoteHtmlTags := qtAlways;

    while HP.ParseNextPiece do
      case HP.PieceType of
        ptHtmlTag:
          begin
            if HP.HtmlTag.Count > 1 then
              HP.HtmlTag.DeleteAt(1);
            HW.WriteCurrentHtmlTag;
          end;
      end;

    WriteLn(HW.Writer.DataToStrA);

  finally
    HW.Free;
    HP.Free;
  end;

  WriteLn;
  WriteLn('Done - Press ENTER to Exit');
  ReadLn;
end.
_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: