문제

I'm formatting links depending on the kind of file they link to.

a[href$=".doc"] { background: url(icon-doc.png) left; padding-left: 30px; }

My problem is, attribute-matching seems to be case sensitive. Is it possible to have CSS match any case combinations?

Or is it possible to have compass/sass generating all the differing case variations? like

a[href$=".doc"], a[href$=".Doc"], a[href$=".dOc"] { ... }

Or do I need to make some js to lowercase all attributes?

도움이 되었습니까?

해결책

It was harder than i thought but i'm too stubborn to quit :P

$ext-s : ("gif", "jpg", "jpeg", "png", "project");

@function to-upper-case-at($s, $n) {
    @if $n < 1 {
        @return $s;
    }
    @if str-length($s) == 1 {
        @return to-upper-case($s);
    }
    $before : "";
    $after : "";
    $char: "";
    @if $n > 1 {
        $before : str-slice($s, 1, $n - 1); 
    }
    @if $n < str-length($s) {
        $after : str-slice($s, $n + 1); 
    }
    $char: to-upper-case(str-slice($s, $n, $n));
    @return $before + $char + $after;   
}

@function power ($x, $n) {
    $ret: 1;

    @if $n >= 0 {
        @for $i from 1 through $n {
            $ret: $ret * $x;
        } 
    } @else {
        @for $i from $n to 0 {
            $ret: $ret / $x;
        }
    }

    @return $ret;
}

@function bin-mask-list($dec-int) {
    $r : $dec-int;
    $res : ();
    $dig : 0;
    $last : false;
    @while $last == false {
        $m : $r % 2;
        @if $m != 0 {
            $dig : 1;
        }
        @else {
            $dig : 0;
        }
        $res : join($dig, $res);
        @if ($r == 1) or ($r == 0) {
            $last : true;
        } 
        $r : floor($r / 2);
    }
    @return $res;  
}

@function all-cases($string) {
    $case-list: ($string);
    $length : str-length($string);

    @for  $i from 1 to power(2, $length) {
        $mask : bin-mask-list($i);
        $res-string : $string;
        @while $length > length($mask) {
            $mask : join(0, $mask); 
        }
        @for  $j from 1 through $length {
            $digit : nth($mask, $j);
            @if $digit == 1{
                 $res-string : to-upper-case-at($res-string, $j);
            }
        }
        $case-list : append($case-list, $res-string);
    }
    @return $case-list;
}

@each $ext in $ext-s {
  $all-c : all-cases($ext);
  @each $c in $all-c {
    a[href$=".#{$c}"] { background: url(icon-doc.#{$c}) left; padding-left: 30px; }
  }
}

It will output around 2k lines of css like this one:

/* line 85, ../scss/test.scss */
a[href$=".gif"] {
  background: url(icon-doc.gif) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".giF"] {
  background: url(icon-doc.giF) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".gIf"] {
  background: url(icon-doc.gIf) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".gIF"] {
  background: url(icon-doc.gIF) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".Gif"] {
  background: url(icon-doc.Gif) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".GiF"] {
  background: url(icon-doc.GiF) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".GIf"] {
  background: url(icon-doc.GIf) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".GIF"] {
  background: url(icon-doc.GIF) left;
  padding-left: 30px;
}

/* line 85, ../scss/test.scss */
a[href$=".jpg"] {
  background: url(icon-doc.jpg) left;
  padding-left: 30px;
}

다른 팁

URLs are all case sensitive, so yes, you need to make your links consistent. CSS file name case sensitivity & Css file caching

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top