gtkcssparser.c 23.1 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GTK - The GIMP Toolkit
 * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Benjamin Otte's avatar
Benjamin Otte committed
16 17 18 19 20
 */

#include "config.h"

#include "gtkcssparserprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
21 22

#include "gtkcssnumbervalueprivate.h"
23
#include "gtkwin32themeprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#include <errno.h>
#include <string.h>

/* just for the errors, yay! */
#include "gtkcssprovider.h"

#define NEWLINE_CHARS "\r\n"
#define WHITESPACE_CHARS "\f \t"
#define NMSTART "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define NMCHAR NMSTART "01234567890-_"
#define URLCHAR NMCHAR "!#$%&*~"

#define GTK_IS_CSS_PARSER(parser) ((parser) != NULL)

struct _GtkCssParser
{
41 42 43 44
  const char            *data;
  GFile                 *file;
  GtkCssParserErrorFunc  error_func;
  gpointer               user_data;
Benjamin Otte's avatar
Benjamin Otte committed
45

46 47
  const char            *line_start;
  guint                  line;
Benjamin Otte's avatar
Benjamin Otte committed
48 49 50 51
};

GtkCssParser *
_gtk_css_parser_new (const char            *data,
52
                     GFile                 *file,
Benjamin Otte's avatar
Benjamin Otte committed
53 54 55 56 57 58
                     GtkCssParserErrorFunc  error_func,
                     gpointer               user_data)
{
  GtkCssParser *parser;

  g_return_val_if_fail (data != NULL, NULL);
59
  g_return_val_if_fail (file == NULL || G_IS_FILE (file), NULL);
Benjamin Otte's avatar
Benjamin Otte committed
60 61 62 63

  parser = g_slice_new0 (GtkCssParser);

  parser->data = data;
64 65
  if (file)
    parser->file = g_object_ref (file);
Benjamin Otte's avatar
Benjamin Otte committed
66 67 68 69
  parser->error_func = error_func;
  parser->user_data = user_data;

  parser->line_start = data;
70
  parser->line = 0;
Benjamin Otte's avatar
Benjamin Otte committed
71 72 73 74 75 76 77 78 79

  return parser;
}

void
_gtk_css_parser_free (GtkCssParser *parser)
{
  g_return_if_fail (GTK_IS_CSS_PARSER (parser));

80 81 82
  if (parser->file)
    g_object_unref (parser->file);

Benjamin Otte's avatar
Benjamin Otte committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  g_slice_free (GtkCssParser, parser);
}

gboolean
_gtk_css_parser_is_eof (GtkCssParser *parser)
{
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), TRUE);

  return *parser->data == 0;
}

gboolean
_gtk_css_parser_begins_with (GtkCssParser *parser,
                             char          c)
{
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), TRUE);

  return *parser->data == c;
}

103 104 105 106 107 108 109 110 111
gboolean
_gtk_css_parser_has_prefix (GtkCssParser *parser,
                            const char   *prefix)
{
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), FALSE);

  return g_ascii_strncasecmp (parser->data, prefix, strlen (prefix)) == 0;
}

Benjamin Otte's avatar
Benjamin Otte committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
guint
_gtk_css_parser_get_line (GtkCssParser *parser)
{
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), 1);

  return parser->line;
}

guint
_gtk_css_parser_get_position (GtkCssParser *parser)
{
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), 1);

  return parser->data - parser->line_start;
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static GFile *
gtk_css_parser_get_base_file (GtkCssParser *parser)
{
  GFile *base;

  if (parser->file)
    {
      base = g_file_get_parent (parser->file);
    }
  else
    {
      char *dir = g_get_current_dir ();
      base = g_file_new_for_path (dir);
      g_free (dir);
    }

  return base;
}

GFile *
_gtk_css_parser_get_file_for_path (GtkCssParser *parser,
                                   const char   *path)
{
  GFile *base, *file;

  g_return_val_if_fail (parser != NULL, NULL);
  g_return_val_if_fail (path != NULL, NULL);

  base = gtk_css_parser_get_base_file (parser);
  file = g_file_resolve_relative_path (base, path);
  g_object_unref (base);

  return file;
}

GFile *
_gtk_css_parser_get_file (GtkCssParser *parser)
{
  g_return_val_if_fail (parser != NULL, NULL);

  return parser->file;
}

171 172 173 174 175 176 177 178 179
void
_gtk_css_parser_take_error (GtkCssParser *parser,
                            GError       *error)
{
  parser->error_func (parser, error, parser->user_data);

  g_error_free (error);
}

Benjamin Otte's avatar
Benjamin Otte committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
void
_gtk_css_parser_error (GtkCssParser *parser,
                       const char   *format,
                       ...)
{
  GError *error;

  va_list args;

  va_start (args, format);
  error = g_error_new_valist (GTK_CSS_PROVIDER_ERROR,
                              GTK_CSS_PROVIDER_ERROR_SYNTAX,
                              format, args);
  va_end (args);

195
  _gtk_css_parser_take_error (parser, error);
Benjamin Otte's avatar
Benjamin Otte committed
196 197
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
void
_gtk_css_parser_error_full (GtkCssParser        *parser,
                            GtkCssProviderError  code,
                            const char          *format,
                            ...)
{
  GError *error;

  va_list args;

  va_start (args, format);
  error = g_error_new_valist (GTK_CSS_PROVIDER_ERROR,
                              code, format, args);
  va_end (args);

  _gtk_css_parser_take_error (parser, error);
}
Benjamin Otte's avatar
Benjamin Otte committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
static gboolean
gtk_css_parser_new_line (GtkCssParser *parser)
{
  gboolean result = FALSE;

  if (*parser->data == '\r')
    {
      result = TRUE;
      parser->data++;
    }
  if (*parser->data == '\n')
    {
      result = TRUE;
      parser->data++;
    }

  if (result)
    {
      parser->line++;
      parser->line_start = parser->data;
    }

  return result;
}

static gboolean
gtk_css_parser_skip_comment (GtkCssParser *parser)
{
  if (parser->data[0] != '/' ||
      parser->data[1] != '*')
    return FALSE;

  parser->data += 2;

  while (*parser->data)
    {
      gsize len = strcspn (parser->data, NEWLINE_CHARS "/");

      parser->data += len;
  
      if (gtk_css_parser_new_line (parser))
        continue;

      parser->data++;

260
      if (len > 0 && parser->data[-2] == '*')
Benjamin Otte's avatar
Benjamin Otte committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        return TRUE;
      if (parser->data[0] == '*')
        _gtk_css_parser_error (parser, "'/*' in comment block");
    }

  /* FIXME: position */
  _gtk_css_parser_error (parser, "Unterminated comment");
  return TRUE;
}

void
_gtk_css_parser_skip_whitespace (GtkCssParser *parser)
{
  size_t len;

  while (*parser->data)
    {
      if (gtk_css_parser_new_line (parser))
        continue;

      len = strspn (parser->data, WHITESPACE_CHARS);
      if (len)
        {
          parser->data += len;
          continue;
        }
      
      if (!gtk_css_parser_skip_comment (parser))
        break;
    }
}

gboolean
_gtk_css_parser_try (GtkCssParser *parser,
                     const char   *string,
                     gboolean      skip_whitespace)
{
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), FALSE);
  g_return_val_if_fail (string != NULL, FALSE);

  if (g_ascii_strncasecmp (parser->data, string, strlen (string)) != 0)
    return FALSE;

  parser->data += strlen (string);

  if (skip_whitespace)
    _gtk_css_parser_skip_whitespace (parser);
  return TRUE;
}

static guint
get_xdigit (char c)
{
  if (c >= 'a')
    return c - 'a' + 10;
  else if (c >= 'A')
    return c - 'A' + 10;
  else
    return c - '0';
}

static void
_gtk_css_parser_unescape (GtkCssParser *parser,
                          GString      *str)
{
  guint i;
  gunichar result = 0;

  g_assert (*parser->data == '\\');

  parser->data++;

  for (i = 0; i < 6; i++)
    {
      if (!g_ascii_isxdigit (parser->data[i]))
        break;

      result = (result << 4) + get_xdigit (parser->data[i]);
    }

  if (i != 0)
    {
      g_string_append_unichar (str, result);
      parser->data += i;

      /* NB: gtk_css_parser_new_line() forward data pointer itself */
      if (!gtk_css_parser_new_line (parser) &&
          *parser->data &&
          strchr (WHITESPACE_CHARS, *parser->data))
        parser->data++;
      return;
    }

  if (gtk_css_parser_new_line (parser))
    return;

  g_string_append_c (str, *parser->data);
  parser->data++;

  return;
}

static gboolean
_gtk_css_parser_read_char (GtkCssParser *parser,
                           GString *     str,
                           const char *  allowed)
{
  if (*parser->data == 0)
    return FALSE;

  if (strchr (allowed, *parser->data))
    {
      g_string_append_c (str, *parser->data);
      parser->data++;
      return TRUE;
    }
  if (*parser->data >= 127)
    {
      gsize len = g_utf8_skip[(guint) *(guchar *) parser->data];

      g_string_append_len (str, parser->data, len);
      parser->data += len;
      return TRUE;
    }
  if (*parser->data == '\\')
    {
      _gtk_css_parser_unescape (parser, str);
      return TRUE;
    }

  return FALSE;
}

char *
_gtk_css_parser_try_name (GtkCssParser *parser,
                          gboolean      skip_whitespace)
{
  GString *name;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), NULL);

  name = g_string_new (NULL);

  while (_gtk_css_parser_read_char (parser, name, NMCHAR))
    ;

  if (skip_whitespace)
    _gtk_css_parser_skip_whitespace (parser);

  return g_string_free (name, FALSE);
}

char *
_gtk_css_parser_try_ident (GtkCssParser *parser,
                           gboolean      skip_whitespace)
{
  const char *start;
  GString *ident;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), NULL);

  start = parser->data;
  
  ident = g_string_new (NULL);

  if (*parser->data == '-')
    {
      g_string_append_c (ident, '-');
      parser->data++;
    }

  if (!_gtk_css_parser_read_char (parser, ident, NMSTART))
    {
      parser->data = start;
      g_string_free (ident, TRUE);
      return NULL;
    }

  while (_gtk_css_parser_read_char (parser, ident, NMCHAR))
    ;

  if (skip_whitespace)
    _gtk_css_parser_skip_whitespace (parser);

  return g_string_free (ident, FALSE);
}

gboolean
_gtk_css_parser_is_string (GtkCssParser *parser)
{
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), FALSE);

  return *parser->data == '"' || *parser->data == '\'';
}

char *
_gtk_css_parser_read_string (GtkCssParser *parser)
{
  GString *str;
  char quote;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), NULL);

  quote = *parser->data;
  
  if (quote != '"' && quote != '\'')
467 468 469 470
    {
      _gtk_css_parser_error (parser, "Expected a string.");
      return NULL;
    }
Benjamin Otte's avatar
Benjamin Otte committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  
  parser->data++;
  str = g_string_new (NULL);

  while (TRUE)
    {
      gsize len = strcspn (parser->data, "\\'\"\n\r\f");

      g_string_append_len (str, parser->data, len);

      parser->data += len;

      switch (*parser->data)
        {
        case '\\':
          _gtk_css_parser_unescape (parser, str);
          break;
        case '"':
        case '\'':
          if (*parser->data == quote)
            {
              parser->data++;
              _gtk_css_parser_skip_whitespace (parser);
              return g_string_free (str, FALSE);
            }
          
          g_string_append_c (str, *parser->data);
          parser->data++;
          break;
        case '\0':
          /* FIXME: position */
          _gtk_css_parser_error (parser, "Missing end quote in string.");
          g_string_free (str, TRUE);
          return NULL;
        default:
          _gtk_css_parser_error (parser, 
                                 "Invalid character in string. Must be escaped.");
          g_string_free (str, TRUE);
          return NULL;
        }
    }

  g_assert_not_reached ();
  return NULL;
}

gboolean
_gtk_css_parser_try_int (GtkCssParser *parser,
                         int          *value)
{
  gint64 result;
  char *end;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), FALSE);
  g_return_val_if_fail (value != NULL, FALSE);

  /* strtoll parses a plus, but we are not allowed to */
  if (*parser->data == '+')
    return FALSE;

  errno = 0;
  result = g_ascii_strtoll (parser->data, &end, 10);
  if (errno)
    return FALSE;
  if (result > G_MAXINT || result < G_MININT)
    return FALSE;
  if (parser->data == end)
    return FALSE;

  parser->data = end;
  *value = result;

  _gtk_css_parser_skip_whitespace (parser);

  return TRUE;
}

gboolean
_gtk_css_parser_try_uint (GtkCssParser *parser,
550
                          guint        *value)
Benjamin Otte's avatar
Benjamin Otte committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
{
  guint64 result;
  char *end;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), FALSE);
  g_return_val_if_fail (value != NULL, FALSE);

  errno = 0;
  result = g_ascii_strtoull (parser->data, &end, 10);
  if (errno)
    return FALSE;
  if (result > G_MAXUINT)
    return FALSE;
  if (parser->data == end)
    return FALSE;

  parser->data = end;
  *value = result;

  _gtk_css_parser_skip_whitespace (parser);

  return TRUE;
}

gboolean
_gtk_css_parser_try_double (GtkCssParser *parser,
                            gdouble      *value)
{
  gdouble result;
  char *end;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), FALSE);
  g_return_val_if_fail (value != NULL, FALSE);

  errno = 0;
  result = g_ascii_strtod (parser->data, &end);
  if (errno)
    return FALSE;
  if (parser->data == end)
    return FALSE;

  parser->data = end;
  *value = result;

  _gtk_css_parser_skip_whitespace (parser);

  return TRUE;
}

Benjamin Otte's avatar
Benjamin Otte committed
600 601 602 603 604 605 606
gboolean
_gtk_css_parser_has_number (GtkCssParser *parser)
{
  /* ahem */
  return strchr ("+-0123456789.", parser->data[0]) != NULL;
}

Benjamin Otte's avatar
Benjamin Otte committed
607 608
GtkCssValue *
_gtk_css_number_value_parse (GtkCssParser           *parser,
Benjamin Otte's avatar
Benjamin Otte committed
609 610 611 612 613 614 615
                             GtkCssNumberParseFlags  flags)
{
  static const struct {
    const char *name;
    GtkCssUnit unit;
    GtkCssNumberParseFlags required_flags;
  } units[] = {
616 617 618 619 620 621 622 623 624 625 626
    { "px",   GTK_CSS_PX,      GTK_CSS_PARSE_LENGTH },
    { "pt",   GTK_CSS_PT,      GTK_CSS_PARSE_LENGTH },
    { "em",   GTK_CSS_EM,      GTK_CSS_PARSE_LENGTH },
    { "ex",   GTK_CSS_EX,      GTK_CSS_PARSE_LENGTH },
    { "pc",   GTK_CSS_PC,      GTK_CSS_PARSE_LENGTH },
    { "in",   GTK_CSS_IN,      GTK_CSS_PARSE_LENGTH },
    { "cm",   GTK_CSS_CM,      GTK_CSS_PARSE_LENGTH },
    { "mm",   GTK_CSS_MM,      GTK_CSS_PARSE_LENGTH },
    { "rad",  GTK_CSS_RAD,     GTK_CSS_PARSE_ANGLE  },
    { "deg",  GTK_CSS_DEG,     GTK_CSS_PARSE_ANGLE  },
    { "grad", GTK_CSS_GRAD,    GTK_CSS_PARSE_ANGLE  },
627 628 629
    { "turn", GTK_CSS_TURN,    GTK_CSS_PARSE_ANGLE  },
    { "s",    GTK_CSS_S,       GTK_CSS_PARSE_TIME   },
    { "ms",   GTK_CSS_MS,      GTK_CSS_PARSE_TIME   }
Benjamin Otte's avatar
Benjamin Otte committed
630
  };
Benjamin Otte's avatar
Benjamin Otte committed
631 632 633
  char *end, *unit_name;
  double value;
  GtkCssUnit unit;
Benjamin Otte's avatar
Benjamin Otte committed
634

Benjamin Otte's avatar
Benjamin Otte committed
635
  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), NULL);
Benjamin Otte's avatar
Benjamin Otte committed
636 637

  errno = 0;
Benjamin Otte's avatar
Benjamin Otte committed
638
  value = g_ascii_strtod (parser->data, &end);
Benjamin Otte's avatar
Benjamin Otte committed
639 640 641
  if (errno)
    {
      _gtk_css_parser_error (parser, "not a number: %s", g_strerror (errno));
Benjamin Otte's avatar
Benjamin Otte committed
642
      return NULL;
Benjamin Otte's avatar
Benjamin Otte committed
643 644 645 646
    }
  if (parser->data == end)
    {
      _gtk_css_parser_error (parser, "not a number");
Benjamin Otte's avatar
Benjamin Otte committed
647
      return NULL;
Benjamin Otte's avatar
Benjamin Otte committed
648 649 650 651 652
    }

  parser->data = end;

  if (flags & GTK_CSS_POSITIVE_ONLY &&
Benjamin Otte's avatar
Benjamin Otte committed
653
      value < 0)
Benjamin Otte's avatar
Benjamin Otte committed
654 655
    {
      _gtk_css_parser_error (parser, "negative values are not allowed.");
Benjamin Otte's avatar
Benjamin Otte committed
656
      return NULL;
Benjamin Otte's avatar
Benjamin Otte committed
657 658
    }

Benjamin Otte's avatar
Benjamin Otte committed
659
  unit_name = _gtk_css_parser_try_ident (parser, FALSE);
Benjamin Otte's avatar
Benjamin Otte committed
660

Benjamin Otte's avatar
Benjamin Otte committed
661
  if (unit_name)
Benjamin Otte's avatar
Benjamin Otte committed
662 663 664 665 666 667
    {
      guint i;

      for (i = 0; i < G_N_ELEMENTS (units); i++)
        {
          if (flags & units[i].required_flags &&
Benjamin Otte's avatar
Benjamin Otte committed
668
              g_ascii_strcasecmp (unit_name, units[i].name) == 0)
Benjamin Otte's avatar
Benjamin Otte committed
669 670 671 672 673
            break;
        }

      if (i >= G_N_ELEMENTS (units))
        {
Benjamin Otte's avatar
Benjamin Otte committed
674
          _gtk_css_parser_error (parser, "`%s' is not a valid unit.", unit_name);
675
          g_free (unit_name);
Benjamin Otte's avatar
Benjamin Otte committed
676
          return NULL;
Benjamin Otte's avatar
Benjamin Otte committed
677 678
        }

Benjamin Otte's avatar
Benjamin Otte committed
679
      unit = units[i].unit;
680 681

      g_free (unit_name);
Benjamin Otte's avatar
Benjamin Otte committed
682 683 684 685 686 687
    }
  else
    {
      if ((flags & GTK_CSS_PARSE_PERCENT) &&
          _gtk_css_parser_try (parser, "%", FALSE))
        {
Benjamin Otte's avatar
Benjamin Otte committed
688
          unit = GTK_CSS_PERCENT;
Benjamin Otte's avatar
Benjamin Otte committed
689
        }
Benjamin Otte's avatar
Benjamin Otte committed
690
      else if (value == 0.0)
Benjamin Otte's avatar
Benjamin Otte committed
691 692
        {
          if (flags & GTK_CSS_PARSE_NUMBER)
Benjamin Otte's avatar
Benjamin Otte committed
693
            unit = GTK_CSS_NUMBER;
Benjamin Otte's avatar
Benjamin Otte committed
694
          else if (flags & GTK_CSS_PARSE_LENGTH)
Benjamin Otte's avatar
Benjamin Otte committed
695 696 697 698 699
            unit = GTK_CSS_PX;
          else if (flags & GTK_CSS_PARSE_ANGLE)
            unit = GTK_CSS_DEG;
          else if (flags & GTK_CSS_PARSE_TIME)
            unit = GTK_CSS_S;
Benjamin Otte's avatar
Benjamin Otte committed
700
          else
Benjamin Otte's avatar
Benjamin Otte committed
701
            unit = GTK_CSS_PERCENT;
Benjamin Otte's avatar
Benjamin Otte committed
702 703 704
        }
      else if (flags & GTK_CSS_NUMBER_AS_PIXELS)
        {
705 706 707
          _gtk_css_parser_error_full (parser,
                                      GTK_CSS_PROVIDER_ERROR_DEPRECATED,
                                      "Not using units is deprecated. Assuming 'px'.");
Benjamin Otte's avatar
Benjamin Otte committed
708
          unit = GTK_CSS_PX;
Benjamin Otte's avatar
Benjamin Otte committed
709 710 711
        }
      else if (flags & GTK_CSS_PARSE_NUMBER)
        {
Benjamin Otte's avatar
Benjamin Otte committed
712
          unit = GTK_CSS_NUMBER;
Benjamin Otte's avatar
Benjamin Otte committed
713 714 715 716
        }
      else
        {
          _gtk_css_parser_error (parser, "Unit is missing.");
Benjamin Otte's avatar
Benjamin Otte committed
717
          return NULL;
Benjamin Otte's avatar
Benjamin Otte committed
718 719 720 721 722
        }
    }

  _gtk_css_parser_skip_whitespace (parser);

Benjamin Otte's avatar
Benjamin Otte committed
723
  return _gtk_css_number_value_new (value, unit);
Benjamin Otte's avatar
Benjamin Otte committed
724 725
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
/* XXX: we should introduce GtkCssLenght that deals with
 * different kind of units */
gboolean
_gtk_css_parser_try_length (GtkCssParser *parser,
                            int          *value)
{
  if (!_gtk_css_parser_try_int (parser, value))
    return FALSE;

  /* FIXME: _try_uint skips spaces while the
   * spec forbids them
   */
  _gtk_css_parser_try (parser, "px", TRUE);

  return TRUE;
}

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
gboolean
_gtk_css_parser_try_enum (GtkCssParser *parser,
			  GType         enum_type,
			  int          *value)
{
  GEnumClass *enum_class;
  gboolean result;
  const char *start;
  char *str;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), FALSE);
  g_return_val_if_fail (value != NULL, FALSE);

  result = FALSE;

  enum_class = g_type_class_ref (enum_type);

  start = parser->data;

  str = _gtk_css_parser_try_ident (parser, TRUE);
  if (str == NULL)
    return FALSE;

  if (enum_class->n_values)
    {
      GEnumValue *enum_value;

      for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
	{
	  if (enum_value->value_nick &&
	      g_ascii_strcasecmp (str, enum_value->value_nick) == 0)
	    {
	      *value = enum_value->value;
	      result = TRUE;
	      break;
	    }
	}
    }

782
  g_free (str);
783 784 785 786 787 788 789 790
  g_type_class_unref (enum_class);

  if (!result)
    parser->data = start;

  return result;
}

791 792 793
gboolean
_gtk_css_parser_try_hash_color (GtkCssParser *parser,
                                GdkRGBA      *rgba)
Benjamin Otte's avatar
Benjamin Otte committed
794 795 796 797 798 799 800 801 802 803
{
  if (parser->data[0] == '#' &&
      g_ascii_isxdigit (parser->data[1]) &&
      g_ascii_isxdigit (parser->data[2]) &&
      g_ascii_isxdigit (parser->data[3]))
    {
      if (g_ascii_isxdigit (parser->data[4]) &&
          g_ascii_isxdigit (parser->data[5]) &&
          g_ascii_isxdigit (parser->data[6]))
        {
804 805 806 807
          rgba->red   = ((get_xdigit (parser->data[1]) << 4) + get_xdigit (parser->data[2])) / 255.0;
          rgba->green = ((get_xdigit (parser->data[3]) << 4) + get_xdigit (parser->data[4])) / 255.0;
          rgba->blue  = ((get_xdigit (parser->data[5]) << 4) + get_xdigit (parser->data[6])) / 255.0;
          rgba->alpha = 1.0;
Benjamin Otte's avatar
Benjamin Otte committed
808 809 810 811
          parser->data += 7;
        }
      else
        {
812 813 814 815
          rgba->red   = get_xdigit (parser->data[1]) / 15.0;
          rgba->green = get_xdigit (parser->data[2]) / 15.0;
          rgba->blue  = get_xdigit (parser->data[3]) / 15.0;
          rgba->alpha = 1.0;
Benjamin Otte's avatar
Benjamin Otte committed
816 817 818 819 820
          parser->data += 4;
        }

      _gtk_css_parser_skip_whitespace (parser);

821
      return TRUE;
Benjamin Otte's avatar
Benjamin Otte committed
822 823
    }

824
  return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
825 826
}

827
GFile *
828
_gtk_css_parser_read_url (GtkCssParser *parser)
829 830
{
  gchar *path;
831
  char *scheme;
832 833 834 835 836 837 838 839 840
  GFile *file;

  if (_gtk_css_parser_try (parser, "url", FALSE))
    {
      if (!_gtk_css_parser_try (parser, "(", TRUE))
        {
          _gtk_css_parser_skip_whitespace (parser);
          if (_gtk_css_parser_try (parser, "(", TRUE))
            {
841 842 843
              _gtk_css_parser_error_full (parser,
                                          GTK_CSS_PROVIDER_ERROR_DEPRECATED,
                                          "Whitespace between 'url' and '(' is deprecated");
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
            }
          else
            {
              _gtk_css_parser_error (parser, "Expected '(' after 'url'");
              return NULL;
            }
        }

      path = _gtk_css_parser_read_string (parser);
      if (path == NULL)
        return NULL;

      if (!_gtk_css_parser_try (parser, ")", TRUE))
        {
          _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
          g_free (path);
          return NULL;
        }
862 863 864 865 866 867 868 869 870

      scheme = g_uri_parse_scheme (path);
      if (scheme != NULL)
	{
	  file = g_file_new_for_uri (path);
	  g_free (path);
	  g_free (scheme);
	  return file;
	}
871 872 873 874 875 876 877 878 879 880 881
    }
  else
    {
      path = _gtk_css_parser_try_name (parser, TRUE);
      if (path == NULL)
        {
          _gtk_css_parser_error (parser, "Not a valid url");
          return NULL;
        }
    }

882
  file = _gtk_css_parser_get_file_for_path (parser, path);
883 884 885 886 887
  g_free (path);

  return file;
}

888 889 890 891 892
static void
gtk_css_parser_resync_internal (GtkCssParser *parser,
                                gboolean      sync_at_semicolon,
                                gboolean      read_sync_token,
                                char          terminator)
Benjamin Otte's avatar
Benjamin Otte committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
{
  gsize len;

  do {
    len = strcspn (parser->data, "\\\"'/()[]{};" NEWLINE_CHARS);
    parser->data += len;

    if (gtk_css_parser_new_line (parser))
      continue;

    if (_gtk_css_parser_is_string (parser))
      {
        /* Hrm, this emits errors, and i suspect it shouldn't... */
        char *free_me = _gtk_css_parser_read_string (parser);
        g_free (free_me);
        continue;
      }

    if (gtk_css_parser_skip_comment (parser))
      continue;

    switch (*parser->data)
      {
916
      case '\\':
Benjamin Otte's avatar
Benjamin Otte committed
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
        {
          GString *ignore = g_string_new (NULL);
          _gtk_css_parser_unescape (parser, ignore);
          g_string_free (ignore, TRUE);
        }
        break;
      case ';':
        if (sync_at_semicolon && !read_sync_token)
          return;
        parser->data++;
        if (sync_at_semicolon)
          {
            _gtk_css_parser_skip_whitespace (parser);
            return;
          }
        break;
      case '(':
        parser->data++;
        _gtk_css_parser_resync (parser, FALSE, ')');
936 937
        if (*parser->data)
          parser->data++;
Benjamin Otte's avatar
Benjamin Otte committed
938 939 940 941
        break;
      case '[':
        parser->data++;
        _gtk_css_parser_resync (parser, FALSE, ']');
942 943
        if (*parser->data)
          parser->data++;
Benjamin Otte's avatar
Benjamin Otte committed
944 945 946 947
        break;
      case '{':
        parser->data++;
        _gtk_css_parser_resync (parser, FALSE, '}');
948 949
        if (*parser->data)
          parser->data++;
Benjamin Otte's avatar
Benjamin Otte committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
        if (sync_at_semicolon || !terminator)
          {
            _gtk_css_parser_skip_whitespace (parser);
            return;
          }
        break;
      case '}':
      case ')':
      case ']':
        if (terminator == *parser->data)
          {
            _gtk_css_parser_skip_whitespace (parser);
            return;
          }
        parser->data++;
        continue;
966 967
      case '\0':
        break;
968
      case '/':
Benjamin Otte's avatar
Benjamin Otte committed
969
      default:
970
        parser->data++;
Benjamin Otte's avatar
Benjamin Otte committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
        break;
      }
  } while (*parser->data);
}

char *
_gtk_css_parser_read_value (GtkCssParser *parser)
{
  const char *start;
  char *result;

  g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), NULL);

  start = parser->data;

  /* This needs to be done better */
987
  gtk_css_parser_resync_internal (parser, TRUE, FALSE, '}');
Benjamin Otte's avatar
Benjamin Otte committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

  result = g_strndup (start, parser->data - start);
  if (result)
    {
      g_strchomp (result);
      if (result[0] == 0)
        {
          g_free (result);
          result = NULL;
        }
    }

  if (result == NULL)
    _gtk_css_parser_error (parser, "Expected a property value");

  return result;
}

void
_gtk_css_parser_resync (GtkCssParser *parser,
                        gboolean      sync_at_semicolon,
                        char          terminator)
{
  g_return_if_fail (GTK_IS_CSS_PARSER (parser));

1013
  gtk_css_parser_resync_internal (parser, sync_at_semicolon, TRUE, terminator);
Benjamin Otte's avatar
Benjamin Otte committed
1014
}