Skip to content

Commit

Permalink
Proper parsing of font-face format() values; testcases in csst/specia…
Browse files Browse the repository at this point in the history
…l/font-face.csst
  • Loading branch information
cfinke authored and Cerdic committed Apr 5, 2012
1 parent b3de694 commit b78cb0f
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 7 deletions.
80 changes: 78 additions & 2 deletions class.csstidy.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,23 @@ function parse($string) {
$this->optimise->subvalue();
if ($this->sub_value != '') {
if (substr($this->sub_value, 0, 6) == 'format') {
$this->sub_value = str_replace(array('format(', ')'), array('format("', '")'), $this->sub_value);
$format_strings = csstidy::parse_string_list(substr($this->sub_value, 7, -1));
if (!$format_strings) {
$this->sub_value = "";
}
else {
$this->sub_value = "format(";

foreach ($format_strings as $format_string) {
$this->sub_value .= '"' . str_replace('"', '\\"', $format_string) . '",';
}

$this->sub_value = substr($this->sub_value, 0, -1) . ")";
}
}
if ($this->sub_value != '') {
$this->sub_value_arr[] = $this->sub_value;
}
$this->sub_value_arr[] = $this->sub_value;
$this->sub_value = '';
}

Expand Down Expand Up @@ -1119,4 +1133,66 @@ function property_is_valid($property) {
return (isset($all_properties[$property]) && strpos($all_properties[$property], strtoupper($this->get_cfg('css_level'))) !== false );
}

/**
* Accepts a list of strings (e.g., the argument to format() in a @font-face src property)
* and returns a list of the strings. Converts things like:
*
* format(abc) => format("abc")
* format(abc def) => format("abc","def")
* format(abc "def") => format("abc","def")
* format(abc, def, ghi) => format("abc","def","ghi")
* format("abc",'def') => format("abc","def")
* format("abc, def, ghi") => format("abc, def, ghi")
*
* @param string
* @return array
*/

function parse_string_list($value) {
$value = trim($value);

// Case: empty
if (!$value) return array();

$strings = array();

$in_str = false;
$current_string = "";

for ($i = 0, $_len = strlen($value); $i < $_len; $i++) {
if (($value{$i} == "," || $value{$i} === " ") && $in_str === true) {
$in_str = false;
$strings[] = $current_string;
$current_string = "";
}
else if ($value{$i} == '"' || $value{$i} == "'"){
if ($in_str === $value{$i}) {
$strings[] = $current_string;
$in_str = false;
$current_string = "";
continue;
}
else if (!$in_str) {
$in_str = $value{$i};
}
}
else {
if ($in_str){
$current_string .= $value{$i};
}
else {
if (!preg_match("/[\s,]/", $value{$i})) {
$in_str = true;
$current_string = $value{$i};
}
}
}
}

if ($current_string) {
$strings[] = $current_string;
}

return $strings;
}
}
84 changes: 79 additions & 5 deletions testing/unit-tests/csst/special/font-face.csst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
--CSS--
@font-face {
font-family: GillSans;
src:url("/generic/fonts/GillSansLightC.otf") format('opentype');
src:url("/generic/fonts/GillSansLightC.otf") format("opentype");
font-style: normal;
font-weight: normal;
}
Expand All @@ -14,20 +14,94 @@
font-style: normal;
font-weight: bold;
}

@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format("opentype", "woff");
}

@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format();
}

@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format("opentype" "svg");
}

@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format("opentype", 'woff', 'svg');
}
@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format(opentype);
}
@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format(opentype woff);
}
@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format(opentype, woff);
}
@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format(opentype, woff, "svg");
}
@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format(opentype woff, "svg");
}
@font-face {
src:url("/generic/fonts/GillSansLightC.otf") format(opentype,woff);
}
--EXPECT--
array (
'@font-face' =>
'@font-face' =>
array (
'font-family' => 'GillSans',
'src' => 'url(/generic/fonts/GillSansLightC.otf) format("opentype")',
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype")',
'font-style' => 'normal',
'font-weight' => '400',
),
'@font-face ' =>
'@font-face ' =>
array (
'font-family' => 'GillSans',
'src' => 'url(/generic/fonts/GillSansC.otf) format("opentype")',
'src' => 'url("/generic/fonts/GillSansC.otf") format("opentype")',
'font-style' => 'normal',
'font-weight' => '700',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","woff")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","svg")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","woff","svg")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","woff")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","woff")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","woff","svg")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","woff","svg")',
),
'@font-face ' =>
array (
'src' => 'url("/generic/fonts/GillSansLightC.otf") format("opentype","woff")',
),
)

0 comments on commit b78cb0f

Please sign in to comment.