| 486 | // Main parsing entry point |
| 487 | |
| 488 | bool CssParser::loadFromStream(HalFile& source) { |
| 489 | if (!source) { |
| 490 | LOG_ERR("CSS", "Cannot read from invalid file"); |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | size_t totalRead = 0; |
| 495 | |
| 496 | // Use stack-allocated buffers for parsing to avoid heap reallocations |
| 497 | StackBuffer selector; |
| 498 | StackBuffer declBuffer; |
| 499 | |
| 500 | bool inComment = false; |
| 501 | bool maybeSlash = false; |
| 502 | bool prevStar = false; |
| 503 | |
| 504 | bool inAtRule = false; |
| 505 | int atDepth = 0; |
| 506 | |
| 507 | int bodyDepth = 0; |
| 508 | bool skippingRule = false; |
| 509 | CssStyle currentStyle; |
| 510 | |
| 511 | auto handleChar = [&](const char c) { |
| 512 | if (inAtRule) { |
| 513 | if (c == '{') { |
| 514 | ++atDepth; |
| 515 | } else if (c == '}') { |
| 516 | if (atDepth > 0) --atDepth; |
| 517 | if (atDepth == 0) inAtRule = false; |
| 518 | } else if (c == ';' && atDepth == 0) { |
| 519 | inAtRule = false; |
| 520 | } |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (bodyDepth == 0) { |
| 525 | if (selector.empty() && isCssWhitespace(c)) { |
| 526 | return; |
| 527 | } |
| 528 | if (c == '@' && selector.empty()) { |
| 529 | inAtRule = true; |
| 530 | atDepth = 0; |
| 531 | return; |
| 532 | } |
| 533 | if (c == '{') { |
| 534 | bodyDepth = 1; |
| 535 | currentStyle = CssStyle{}; |
| 536 | declBuffer.clear(); |
| 537 | if (selector.size() > MAX_SELECTOR_LENGTH * 4) { |
| 538 | skippingRule = true; |
| 539 | } |
| 540 | return; |
| 541 | } |
| 542 | selector.push_back(c); |
| 543 | return; |
| 544 | } |
| 545 |
no test coverage detected