Skip to content
GitLab
Explore
Sign in
Commit
d37dfda4
authored
7 years ago
by
Daniel Espinosa
Browse files
Options
Downloads
Patches
Plain Diff
Renamed CSS clasess appending Css prefix
parent
e821de2e
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
gxml/css-selector-parser.vala
+42
-41
42 additions, 41 deletions
gxml/css-selector-parser.vala
with
42 additions
and
41 deletions
gxml/css-selector-parser.vala
+
42
−
41
View file @
d37dfda4
...
...
@@ -2,6 +2,7 @@
/*
*
* Copyright (C) 2017 Yannick Inizan <inizan.yannick@gmail.com>
* Copyright (C) 2017 Daniel Espinosa <esodan@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
...
...
@@ -18,8 +19,9 @@
*
* Authors:
* Yannick Inizan <inizan.yannick@gmail.com>
* Daniel Espinosa <esodan@gmail.com>
*/
public
errordomain
GXml
.
SelectorError
{
public
errordomain
GXml
.
Css
SelectorError
{
NULL
,
ATTRIBUTE
,
INVALID
,
...
...
@@ -28,7 +30,7 @@ public errordomain GXml.SelectorError {
TYPE
}
public
enum
GXml
.
SelectorType
{
public
enum
GXml
.
Css
SelectorType
{
CLASS
,
ID
,
ALL
,
...
...
@@ -47,17 +49,17 @@ public enum GXml.SelectorType {
BEFORE
}
public
struct
GXml
.
SelectorData
{
public
SelectorType
selector_type
;
public
struct
GXml
.
Css
SelectorData
{
public
Css
SelectorType
selector_type
;
public
string
data
;
public
string
value
;
}
public
class
GXml
.
SelectorParser
:
GLib
.
Object
{
Gee
.
ArrayList
<
SelectorData
?>
list
;
public
class
GXml
.
CssCss
SelectorParser
:
GLib
.
Object
{
Gee
.
ArrayList
<
Css
SelectorData
?>
list
;
construct
{
list
=
new
Gee
.
ArrayList
<
SelectorData
?>();
list
=
new
Gee
.
ArrayList
<
Css
SelectorData
?>();
}
void
parse_class
(
string
css
,
ref
int
position
)
{
...
...
@@ -66,7 +68,7 @@ public class GXml.SelectorParser : GLib.Object {
unichar
u
=
0
;
while
(
css
.
get_next_char
(
ref
position
,
out
u
)
&&
(
u
.
isalnum
()
||
u
==
'-'
))
sb
.
append_unichar
(
u
);
SelectorData
data
=
{
SelectorType
.
CLASS
,
sb
.
str
};
Css
SelectorData
data
=
{
Css
SelectorType
.
CLASS
,
sb
.
str
};
list
.
add
(
data
);
}
...
...
@@ -76,13 +78,13 @@ public class GXml.SelectorParser : GLib.Object {
unichar
u
=
0
;
while
(
css
.
get_next_char
(
ref
position
,
out
u
)
&&
(
u
.
isalnum
()
||
u
==
'-'
))
sb
.
append_unichar
(
u
);
SelectorData
data
=
{
SelectorType
.
ID
,
sb
.
str
};
Css
SelectorData
data
=
{
Css
SelectorType
.
ID
,
sb
.
str
};
list
.
add
(
data
);
}
void
parse_all
(
string
css
,
ref
int
position
)
{
position
++;
SelectorData
data
=
{
SelectorType
.
ALL
,
"*"
};
Css
SelectorData
data
=
{
Css
SelectorType
.
ALL
,
"*"
};
list
.
add
(
data
);
}
...
...
@@ -91,7 +93,7 @@ public class GXml.SelectorParser : GLib.Object {
unichar
u
=
0
;
while
(
css
.
get_next_char
(
ref
position
,
out
u
)
&&
(
u
.
isalnum
()
||
u
==
'-'
))
sb
.
append_unichar
(
u
);
SelectorData
data
=
{
SelectorType
.
ELEMENT
,
sb
.
str
};
Css
SelectorData
data
=
{
Css
SelectorType
.
ELEMENT
,
sb
.
str
};
list
.
add
(
data
);
}
...
...
@@ -103,14 +105,14 @@ public class GXml.SelectorParser : GLib.Object {
while
(
u
.
isspace
())
css
.
get_next_char
(
ref
position
,
out
u
);
if
(!
u
.
isalnum
())
throw
new
SelectorError
.
ATTRIBUTE
(
"invalid attribute character"
);
throw
new
Css
SelectorError
.
ATTRIBUTE
(
"invalid attribute character"
);
sb
.
append_unichar
(
u
);
while
(
css
.
get_next_char
(
ref
position
,
out
u
)
&&
u
.
isalnum
())
sb
.
append_unichar
(
u
);
while
(
u
.
isspace
())
css
.
get_next_char
(
ref
position
,
out
u
);
if
(
u
==
']'
)
{
SelectorData
data
=
{
SelectorType
.
ATTRIBUTE
,
sb
.
str
};
Css
SelectorData
data
=
{
Css
SelectorType
.
ATTRIBUTE
,
sb
.
str
};
list
.
add
(
data
);
return
;
}
...
...
@@ -125,21 +127,21 @@ public class GXml.SelectorParser : GLib.Object {
css
.
get_next_char
(
ref
position
,
out
u
);
}
if
(!
u
.
isalnum
())
throw
new
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character"
);
throw
new
Css
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character"
);
sb1
.
append_unichar
(
u
);
while
(
css
.
get_next_char
(
ref
position
,
out
u
)
&&
(
u
.
isalnum
()
||
u
.
isspace
()))
sb1
.
append_unichar
(
u
);
if
(
s
!=
0
)
{
if
(
u
!=
s
)
throw
new
SelectorError
.
STRING
(
"invalid end of attribute value"
);
throw
new
Css
SelectorError
.
STRING
(
"invalid end of attribute value"
);
css
.
get_next_char
(
ref
position
,
out
u
);
}
while
(
u
.
isspace
())
css
.
get_next_char
(
ref
position
,
out
u
);
if
(
u
!=
']'
)
throw
new
SelectorError
.
ATTRIBUTE
(
"invalid end of attribute selector"
);
SelectorData
data
=
{
SelectorType
.
ATTRIBUTE_EQUAL
,
throw
new
Css
SelectorError
.
ATTRIBUTE
(
"invalid end of attribute selector"
);
Css
SelectorData
data
=
{
Css
SelectorType
.
ATTRIBUTE_EQUAL
,
sb
.
str
,
sb1
.
str
};
...
...
@@ -149,20 +151,20 @@ public class GXml.SelectorParser : GLib.Object {
return
;
}
StringBuilder
sb1
=
new
StringBuilder
();
SelectorData
data
=
{
SelectorType
.
ATTRIBUTE
,
sb
.
str
,
""
};
Css
SelectorData
data
=
{
Css
SelectorType
.
ATTRIBUTE
,
sb
.
str
,
""
};
if
(
u
==
'~'
)
data
.
selector_type
=
SelectorType
.
ATTRIBUTE_CONTAINS
;
data
.
selector_type
=
Css
SelectorType
.
ATTRIBUTE_CONTAINS
;
else
if
(
u
==
'*'
)
data
.
selector_type
=
SelectorType
.
ATTRIBUTE_SUBSTRING
;
data
.
selector_type
=
Css
SelectorType
.
ATTRIBUTE_SUBSTRING
;
else
if
(
u
==
'|'
||
u
==
'^'
)
data
.
selector_type
=
SelectorType
.
ATTRIBUTE_START_WITH
;
data
.
selector_type
=
Css
SelectorType
.
ATTRIBUTE_START_WITH
;
else
if
(
u
==
'$'
)
data
.
selector_type
=
SelectorType
.
ATTRIBUTE_END_WITH
;
data
.
selector_type
=
Css
SelectorType
.
ATTRIBUTE_END_WITH
;
else
throw
new
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character"
);
throw
new
Css
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character"
);
css
.
get_next_char
(
ref
position
,
out
u
);
if
(
u
!=
'='
)
throw
new
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character : can't find '=' character"
);
throw
new
Css
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character : can't find '=' character"
);
css
.
get_next_char
(
ref
position
,
out
u
);
while
(
u
.
isspace
())
css
.
get_next_char
(
ref
position
,
out
u
);
...
...
@@ -172,19 +174,19 @@ public class GXml.SelectorParser : GLib.Object {
css
.
get_next_char
(
ref
position
,
out
u
);
}
if
(!
u
.
isalnum
())
throw
new
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character 2 (%s)"
.
printf
(
u
.
to_string
()));
throw
new
Css
SelectorError
.
ATTRIBUTE
(
"invalid attribute selector character 2 (%s)"
.
printf
(
u
.
to_string
()));
sb1
.
append_unichar
(
u
);
while
(
css
.
get_next_char
(
ref
position
,
out
u
)
&&
(
u
.
isalnum
()
||
u
.
isspace
()))
sb1
.
append_unichar
(
u
);
if
(
s
!=
0
)
{
if
(
u
!=
s
)
throw
new
SelectorError
.
STRING
(
"invalid end of attribute value"
);
throw
new
Css
SelectorError
.
STRING
(
"invalid end of attribute value"
);
css
.
get_next_char
(
ref
position
,
out
u
);
}
while
(
u
.
isspace
())
css
.
get_next_char
(
ref
position
,
out
u
);
if
(
u
!=
']'
)
throw
new
SelectorError
.
ATTRIBUTE
(
"invalid end of attribute selector"
);
throw
new
Css
SelectorError
.
ATTRIBUTE
(
"invalid end of attribute selector"
);
if
(
s
==
0
)
data
.
value
=
sb1
.
str
.
strip
();
else
...
...
@@ -211,16 +213,15 @@ public class GXml.SelectorParser : GLib.Object {
"last-of-type"
};
if
(!(
sb
.
str
in
valid_selectors
))
throw
new
SelectorError
.
INVALID
(
"invalid pseudo class selector"
);
SelectorData
data
=
{
SelectorType
.
PSEUDO
,
sb
.
str
};
throw
new
Css
SelectorError
.
INVALID
(
"invalid pseudo class selector"
);
Css
SelectorData
data
=
{
Css
SelectorType
.
PSEUDO
,
sb
.
str
};
list
.
add
(
data
);
}
public
void
parse
(
string
query
)
throws
GLib
.
Error
{
string
css
=
query
.
strip
();
if
(
css
.
length
==
0
)
throw
new
SelectorError
.
LENGTH
(
"invalid string length."
);
bool
space
=
false
;
throw
new
CssSelectorError
.
LENGTH
(
"invalid string length."
);
int
position
=
0
;
while
(
position
<
css
.
length
)
{
print
(
"position : %d (%c)\n"
,
position
,
css
[
position
]);
...
...
@@ -238,22 +239,22 @@ public class GXml.SelectorParser : GLib.Object {
parse_element
(
css
,
ref
position
);
else
if
(
css
[
position
]
==
','
)
{
position
++;
SelectorData
data
=
{
SelectorType
.
AND
,
","
};
Css
SelectorData
data
=
{
Css
SelectorType
.
AND
,
","
};
list
.
add
(
data
);
}
else
if
(
css
[
position
]
==
'+'
)
{
position
++;
SelectorData
data
=
{
SelectorType
.
AFTER
,
"+"
};
Css
SelectorData
data
=
{
Css
SelectorType
.
AFTER
,
"+"
};
list
.
add
(
data
);
}
else
if
(
css
[
position
]
==
'~'
)
{
position
++;
SelectorData
data
=
{
SelectorType
.
BEFORE
,
"~"
};
Css
SelectorData
data
=
{
Css
SelectorType
.
BEFORE
,
"~"
};
list
.
add
(
data
);
}
else
if
(
css
[
position
]
==
'>'
)
{
position
++;
SelectorData
data
=
{
SelectorType
.
PARENT
,
">"
};
Css
SelectorData
data
=
{
Css
SelectorType
.
PARENT
,
">"
};
list
.
add
(
data
);
}
else
if
(
css
[
position
].
isspace
())
{
...
...
@@ -262,22 +263,22 @@ public class GXml.SelectorParser : GLib.Object {
while
(
u
.
isspace
())
css
.
get_next_char
(
ref
position
,
out
u
);
position
--;
if
(
list
.
size
>
0
&&
list
[
list
.
size
-
1
].
selector_type
!=
SelectorType
.
AND
&&
list
[
list
.
size
-
1
].
selector_type
!=
SelectorType
.
PARENT
&&
list
[
list
.
size
-
1
].
selector_type
!=
SelectorType
.
BEFORE
&&
list
[
list
.
size
-
1
].
selector_type
!=
SelectorType
.
AFTER
)
if
(
list
.
size
>
0
&&
list
[
list
.
size
-
1
].
selector_type
!=
Css
SelectorType
.
AND
&&
list
[
list
.
size
-
1
].
selector_type
!=
Css
SelectorType
.
PARENT
&&
list
[
list
.
size
-
1
].
selector_type
!=
Css
SelectorType
.
BEFORE
&&
list
[
list
.
size
-
1
].
selector_type
!=
Css
SelectorType
.
AFTER
)
{
SelectorData
data
=
{
SelectorType
.
INSIDE
,
" "
};
Css
SelectorData
data
=
{
Css
SelectorType
.
INSIDE
,
" "
};
list
.
add
(
data
);
}
}
else
throw
new
SelectorError
.
TYPE
(
"invalid '%c' character."
.
printf
(
css
[
position
]));
throw
new
Css
SelectorError
.
TYPE
(
"invalid '%c' character."
.
printf
(
css
[
position
]));
}
foreach
(
var
data
in
list
)
print
(
"%s\n"
,
data
.
selector_type
.
to_string
());
}
public
Gee
.
List
<
SelectorData
?>
selectors
{
public
Gee
.
List
<
Css
SelectorData
?>
selectors
{
get
{
return
list
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment