📄 cssparser.pas
字号:
unit CSSParser;
// Version 1.01
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
// specific language governing rights and limitations under the License.
//
// Site: http://www.mythcode.org
// Author: Dzianis Koshkin
// E-mail: k5@yandex.ru
//
// (C) 2005 MYTHcode.org
interface
type
TCSSItemType = (itHead, itSimple, itLast, itEmptyLast, itComment);
TCSSParser = class
private
Final: Boolean;
FItemType: TCSSItemType;
PInitial: PChar;
PFinal: PChar;
PRun: PChar;
FItem: string;
FComment: string;
public
constructor Create(S: string);
function Next: Boolean;
property ItemType: TCSSItemType read FItemType;
property Item: string read FItem;
property Comment: string read FComment;
end;
implementation
function GetTrim(From,Before: PChar): string;
begin
Assert(From<>nil);
Assert(Before<>nil);
while (From<Before) and (From^<#33) do Inc(From);
while (From<Before) and (Before[-1]<#33) do Dec(Before);
SetLength(Result, Before-From);
Move(From^, Pointer(Result)^, Length(Result));
end;
constructor TCSSParser.Create(S: string);
begin
PInitial:=Pointer(S);
PRun:=PInitial;
PFinal:=PInitial+Length(S);
Final:=False;
end;
function TCSSParser.Next: boolean;
var
PLabel: PChar;
PComment: PChar;
label
Loop;
begin
PLabel:=PRun;
Result:=False;
if (PRun=nil) or (PRun>PFinal) then Exit;
Loop:
case PRun^ of
'{':
begin
FItem:=GetTrim(PLabel,PRun);
FItemType:=itHead;
Result:=True;
Inc(PRun);
end;
';':
begin
FItem:=GetTrim(PLabel,PRun);
FItemType:=itSimple;
Result:=True;
Inc(PRun);
end;
'}':
begin
FItem:=GetTrim(PLabel,PRun);
if FItem<>''
then FItemType:=itLast
else FItemType:=itEmptyLast;
Result:=True;
Inc(PRun);
end;
'/': if PRun[1]='*' then
begin
PComment:=PRun;
Inc(PComment);
repeat
Inc(PComment)
until (PComment[1]=#0) or ((PComment[-1]='*') and (PComment[0]='/'));
FItem:=GetTrim(PRun, @PComment[1]);
FItemType:=itComment;
Result:=True;
Inc(PComment);
Move(PComment^, PRun^, Succ(PFinal-PComment));
Dec(PFinal, PComment-PRun);
PRun:=PLabel;
end else
begin
Inc(PRun);
goto Loop;
end;
'<': if (PRun[1]='!') and (PRun[2]='-') and (PRun[3]='-') then
begin
FItem:=GetTrim(PLabel,PRun);
FItemType:=itSimple;
Result:=True;
Inc(PRun,4);
end else
begin
Inc(PRun);
goto Loop;
end;
'-': if (PRun[1]='-') and (PRun[2]='>') then
begin
FItem:=GetTrim(PLabel,PRun);
FItemType:=itSimple;
Result:=True;
Inc(PRun,3);
end else
begin
Inc(PRun);
goto Loop;
end;
#0: if Final then Result:=False else
begin
FItem:=GetTrim(PLabel,PRun);
if FItem=''
then FItemType:=itEmptylast
else FItemType:=itLast;
Result:=true;
Final:=true;
end;
else
begin
Inc(PRun);
goto Loop;
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -