* sax_html.pp, ignore markup in STYLE and SCRIPT elements, Mantis #18826

git-svn-id: trunk@17003 -
This commit is contained in:
sergei 2011-02-26 16:59:57 +00:00
parent 08fdb5af54
commit b7e26ed986

View File

@ -429,6 +429,18 @@ begin
WStrLower(result); WStrLower(result);
end; end;
function RightTrimmedLength(const s: SAXString): Integer;
begin
result := Length(s);
while IsXmlWhitespace(s[result]) do Dec(result);
end;
function TagPos(elTag: THTMLElementTag; s: SAXString): Integer;
begin
WStrLower(s);
Result := Pos(HTMLElementProps[elTag].Name, s);
end;
procedure THTMLReader.EnterNewScannerContext(NewContext: THTMLScannerContext); procedure THTMLReader.EnterNewScannerContext(NewContext: THTMLScannerContext);
var var
Attr: TSAXAttributes; Attr: TSAXAttributes;
@ -455,45 +467,60 @@ begin
scTag: scTag:
if Length(TokenText) > 0 then if Length(TokenText) > 0 then
begin begin
Attr := nil; { ignore possibly unescaped markup in SCRIPT and STYLE }
if TokenText[Length(fTokenText)]='/' then // handle xml/xhtml style empty tag if (FNesting > 0) and (FStack[FNesting-1] in [etScript,etStyle]) and
not (
(TokenText[1] = '/') and
(RightTrimmedLength(TokenText)=Length(HTMLElementProps[FStack[FNesting-1]].Name)+1) and
(TagPos(FStack[FNesting-1], TokenText) = 2)
)
and (TokenText[1] <> '!') then
begin begin
setlength(fTokenText,length(fTokenText)-1); FTokenText := '<'+FTokenText+'>';
// Do NOT combine to a single line, as Attr is an output value! DoCharacters(PSAXChar(TokenText), 0, Length(TokenText));
TagName := SplitTagString(TokenText, Attr);
AutoClose(TagName);
DoStartElement('', TagName, '', Attr);
DoEndElement('', TagName, '');
end end
else if TokenText[1] = '/' then else
begin begin
Delete(FTokenText, 1, 1); Attr := nil;
TagName := SplitTagString(TokenText, Attr); if TokenText[Length(fTokenText)]='/' then // handle xml/xhtml style empty tag
elTag := LookupTag(TagName); begin
i := FNesting-1; setlength(fTokenText,length(fTokenText)-1);
while (i >= 0) and (FStack[i] <> elTag) and // Do NOT combine to a single line, as Attr is an output value!
(efEndTagOptional in HTMLElementProps[FStack[i]].Flags) do TagName := SplitTagString(TokenText, Attr);
Dec(i); AutoClose(TagName);
if (i>=0) and (FStack[i] = elTag) then DoStartElement('', TagName, '', Attr);
while FStack[FNesting-1] <> elTag do DoEndElement('', TagName, '');
begin end
DoEndElement('', HTMLElementProps[FStack[FNesting-1]].Name, ''); else if TokenText[1] = '/' then
namePop; begin
end; Delete(FTokenText, 1, 1);
TagName := SplitTagString(TokenText, Attr);
elTag := LookupTag(TagName);
i := FNesting-1;
while (i >= 0) and (FStack[i] <> elTag) and
(efEndTagOptional in HTMLElementProps[FStack[i]].Flags) do
Dec(i);
if (i>=0) and (FStack[i] = elTag) then
while FStack[FNesting-1] <> elTag do
begin
DoEndElement('', HTMLElementProps[FStack[FNesting-1]].Name, '');
namePop;
end;
DoEndElement('', TagName, ''); DoEndElement('', TagName, '');
namePop; namePop;
end end
else if TokenText[1] <> '!' then else if TokenText[1] <> '!' then
begin begin
// Do NOT combine to a single line, as Attr is an output value! // Do NOT combine to a single line, as Attr is an output value!
TagName := SplitTagString(TokenText, Attr); TagName := SplitTagString(TokenText, Attr);
AutoClose(TagName); AutoClose(TagName);
namePush(TagName); namePush(TagName);
DoStartElement('', TagName, '', Attr); DoStartElement('', TagName, '', Attr);
end;
if Assigned(Attr) then
Attr.Free;
end; end;
if Assigned(Attr) then
Attr.Free;
end; end;
end; end;
FScannerContext := NewContext; FScannerContext := NewContext;