Why does HTML think “chucknorris” is a color?

后端 未结 9 1918
醉梦人生
醉梦人生 2020-11-22 05:07

How come certain random strings produce colors when entered as background colors in HTML? For example:

9条回答
  •  孤街浪徒
    2020-11-22 05:36

    The rules for parsing colors on legacy attributes involves additional steps than those mentioned in existing answers. The truncate component to 2 digits part is described as:

    1. Discard all characters except the last 8
    2. Discard leading zeros one by one as long as all components have a leading zero
    3. Discard all characters except the first 2

    Some examples:

    oooFoooFoooF
    000F 000F 000F                <- replace, pad and chunk
    0F 0F 0F                      <- leading zeros truncated
    0F 0F 0F                      <- truncated to 2 characters from right
    
    oooFooFFoFFF
    000F 00FF 0FFF                <- replace, pad and chunk
    00F 0FF FFF                   <- leading zeros truncated
    00 0F FF                      <- truncated to 2 characters from right
    
    ABCooooooABCooooooABCoooooo
    ABC000000 ABC000000 ABC000000 <- replace, pad and chunk
    BC000000 BC000000 BC000000    <- truncated to 8 characters from left
    BC BC BC                      <- truncated to 2 characters from right
    
    AoCooooooAoCooooooAoCoooooo
    A0C000000 A0C000000 A0C000000 <- replace, pad and chunk
    0C000000 0C000000 0C000000    <- truncated to 8 characters from left
    C000000 C000000 C000000       <- leading zeros truncated
    C0 C0 C0                      <- truncated to 2 characters from right
    

    Below is a partial implementation of the algorithm. It does not handle errors or cases where the user enters a valid color.

    function parseColor(input) {
      // todo: return error if input is ""
      input = input.trim();
      // todo: return error if input is "transparent"
      // todo: return corresponding #rrggbb if input is a named color
      // todo: return #rrggbb if input matches #rgb
      // todo: replace unicode code points greater than U+FFFF with 00
      if (input.length > 128) {
        input = input.slice(0, 128);
      }
      if (input.charAt(0) === "#") {
        input = input.slice(1);
      }
      input = input.replace(/[^0-9A-Fa-f]/g, "0");
      while (input.length === 0 || input.length % 3 > 0) {
        input += "0";
      }
      var r = input.slice(0, input.length / 3);
      var g = input.slice(input.length / 3, input.length * 2 / 3);
      var b = input.slice(input.length * 2 / 3);
      if (r.length > 8) {
        r = r.slice(-8);
        g = g.slice(-8);
        b = b.slice(-8);
      }
      while (r.length > 2 && r.charAt(0) === "0" && g.charAt(0) === "0" && b.charAt(0) === "0") {
        r = r.slice(1);
        g = g.slice(1);
        b = b.slice(1);
      }
      if (r.length > 2) {
        r = r.slice(0, 2);
        g = g.slice(0, 2);
        b = b.slice(0, 2);
      }
      return "#" + r.padStart(2, "0") + g.padStart(2, "0") + b.padStart(2, "0");
    }
    
    $(function() {
      $("#input").on("change", function() {
        var input = $(this).val();
        var color = parseColor(input);
        var $cells = $("#result tbody td");
        $cells.eq(0).attr("bgcolor", input);
        $cells.eq(1).attr("bgcolor", color);
    
        var color1 = $cells.eq(0).css("background-color");
        var color2 = $cells.eq(1).css("background-color");
        $cells.eq(2).empty().append("bgcolor: " + input, "
    ", "getComputedStyle: " + color1); $cells.eq(3).empty().append("bgcolor: " + color, "
    ", "getComputedStyle: " + color2); }); });
    body { font: medium monospace; }
    input { width: 20em; }
    table { table-layout: fixed; width: 100%; }
    
    
    

    Left Color Right Color
       
       

提交回复
热议问题