Browse Source

* Standalone tag fix from Dokkie884

Michaël Van Canneyt 3 years ago
parent
commit
d7ab530096
1 changed files with 37 additions and 33 deletions
  1. 37 33
      packages/fcl-mustache/src/fpmustache.pp

+ 37 - 33
packages/fcl-mustache/src/fpmustache.pp

@@ -977,14 +977,6 @@ Var
   PartialPrefix: TMustacheString;
   PartialPrefix: TMustacheString;
   Partial : TMustacheELement;
   Partial : TMustacheELement;
 
 
-  // check if the current tag is a tag that can occur standalone on a line
-  // if so, the whole line will be removed as per the specs
-  function IsStandaloneTag: Boolean;
-  begin
-    Result := (NewPos + lStart <= Total) and
-      (aTemplate[NewPos + lStart] in ['=','#','!','^','>','/']);
-  end;
-
   function IsPartialTag: Boolean;
   function IsPartialTag: Boolean;
   begin
   begin
     Result := (NewPos + lStart <= Total) and (aTemplate[NewPos + lStart] = '>');
     Result := (NewPos + lStart <= Total) and (aTemplate[NewPos + lStart] = '>');
@@ -992,33 +984,40 @@ Var
 
 
   // check if the current tag occurs standalone on a line
   // check if the current tag occurs standalone on a line
   function CheckStandalone: Boolean;
   function CheckStandalone: Boolean;
+  var
+    I,
+    pStop: Integer;
+  begin
+    // a tag is considered standalone if it is on a line that consists of:
+    // - zero or more spaces
+    // - one or more non-variable tags
+    // - zero or more spaces
+
+    // get start of current line
+    I := NewPos;
+    while (I > 1) and (aTemplate[I - 1] <> #10) do
+      Dec(I);
 
 
-    function LeftOnlyWhiteSpace: Boolean;
-    var
-      I: Integer;
-    begin
-      I := NewPos - 1;
-      while (I >= 1) and (aTemplate[I] = ' ') do
-         Dec(I);
-      Result := (I < 1) or (aTemplate[I] = #10);
-    end;
+    // skip zero or more spaces
+    while (I <= Total) and (aTemplate[I] = ' ') do
+      Inc(I);
 
 
-    function RightOnlyWhiteSpace: Boolean;
-    var
-      I: Integer;
-    begin
-      I := Pos(cStop, aTemplate, NewPos + lStart);
-      if I = 0 then
-        Exit(False);
-      Inc(I, lStop);
-      while (I <= Total) and (aTemplate[I] = ' ') do
-         Inc(I);
-      Result := (I > Total) or (aTemplate[I] = #10) or
-        (Copy(aTemplate, I, 2) = #13#10);
-    end;
+    // skip one or more non-variable tags
+    repeat
+      pStop := Pos(cStop, aTemplate, I + lStart + 1);
+      if (Copy(aTemplate, I, lStart) = cStart) and (pStop > 0)
+        and (aTemplate[I + lStart] in ['=','#','!','^','>','/']) then
+        I := pStop + lStop
+      else
+        Break;
+    until False;
 
 
-  begin
-    Result := IsStandaloneTag and LeftOnlyWhiteSpace and RightOnlyWhiteSpace;
+    // skip zero or more spaces
+    while (I <= Total) and (aTemplate[I] = ' ') do
+      Inc(I);
+
+    // now end of line must be reached if tag is standalone
+    Result := (I > Total) or (aTemplate[I] = #10) or (Copy(aTemplate, I, 2) = #13#10);
   end;
   end;
 
 
   function WhiteSpaceRightPos(const S: TMustacheString): Integer;
   function WhiteSpaceRightPos(const S: TMustacheString): Integer;
@@ -1032,6 +1031,11 @@ Var
     Result := I + 1;
     Result := I + 1;
   end;
   end;
 
 
+  function CurrentIsWhitespace: Boolean;
+  begin
+    Result := (Current <= Total) and (aTemplate[Current] in [' ',#13,#10]);
+  end;
+
   procedure SkipRestOfLine;
   procedure SkipRestOfLine;
   var
   var
     EndOfLine: Integer;
     EndOfLine: Integer;
@@ -1152,7 +1156,7 @@ begin
         CreateDefault(CurrParent,Current,aName);
         CreateDefault(CurrParent,Current,aName);
       end;
       end;
       Current:=NewPos+clStop;
       Current:=NewPos+clStop;
-      if IsStandalone then
+      if IsStandalone and CurrentIsWhitespace then
         SkipRestOfLine;
         SkipRestOfLine;
       end;
       end;
     end;
     end;