GetVal() in HTMLUtils returning wrong attribute value when '=' is surrounded by spaces
Original Reporter info from Mantis: wp @wpam
-
Reporter name:
Original Reporter info from Mantis: wp @wpam
- Reporter name:
Description:
The function GetVal() in unit HTMLUtils (folder packages/chm/src) is a useful helper to extract the value of attributes in html tags. For example, when the tag is '&LtPos;div id="1">' then GetVal(tag, 'id') should return the string '1' which is the value of the attribute 'id'.
However, this works only when no spaces are used around the '=' character.
Steps to reproduce:
Run this program:
program Project1;
uses HtmlUtil;
begin
WriteLn('"', GetVal('<div id="1">', 'id'), '"');
WriteLn('"', GetVal('<div id ="2">', 'id'), '"'); // space before '='
WriteLn('"', GetVal('<div id= "3">', 'id'), '"'); // space after '='
WriteLn('"', GetVal('<div id = "4">', 'id'), '"'); // space before and after '='
end.
It will output
"1"
"1"
""
""
The first "1" is correct. The "" in the third and forth line demonstrate the issue that the attribute value is not detected when the '=' is surrounded by at least one space. The second "1" should be "", too - this is due to the fact that the function GetValFromNameVal() does not provide a valid return value when S = nil:
function GetValFromNameVal(namevalpair: string): string;
var
P: Pchar;
S: Pchar;
C: Char;
begin
P:= Pchar(namevalpair);
S:= StrPos(P, '=');
if S <> nil then
begin
....
end;
end;
The attached patch fixes this undefined return value issue as well as the main issue of this report, the spaces around the '=' character.
After application of the patch the output of above demo is as expected:
"1"
"2"
"3"
"4"
Mantis conversion info:
- Mantis ID: 36141
- Fixed in version: 3.3.1
- Fixed in revision: 43141 (#0ccb7d95)
- Target version: 3.2.0