css_tools.py
CSS This module is a set of tools to analyze CSS syntax as well as properties and values.
Declaration
A property and value pair.
A declaration is a pairing of a property with a specific value.
Examples include: font-family: Helvetica;
which changes the
font to Helvetica. Another example could be min-height: 100px
which sets the height of the element to be at the very least
100 pixels.
Attributes:
Name | Type | Description |
---|---|---|
text |
str
|
the text of the declaration in the form of
|
property |
str
|
the thing you want to change (like |
value |
str
|
what you want to change it to (like |
Source code in webcode_tk/css_tools.py
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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
|
__init__(text)
Inits a Declaration object.
Source code in webcode_tk/css_tools.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
|
get_declaration()
Returns the declaration in the form of property: value
Returns:
Name | Type | Description |
---|---|---|
declaration |
str
|
a property and its value separated by |
str
|
a colon. Example: |
Source code in webcode_tk/css_tools.py
707 708 709 710 711 712 713 714 715 |
|
set_declaration()
Sets the property and value based on the text (CSS code).
Note: this only gets run if the declaration was valid, and we already ran the validation. Had the code not been valid, it would have already thrown an exception, and we wouldn't be in this method.
Source code in webcode_tk/css_tools.py
602 603 604 605 606 607 608 609 610 611 |
|
validate_declaration()
Raises a ValueError if any part of the Declaration is invalid.
Source code in webcode_tk/css_tools.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
|
validate_property(property)
checks property to make sure it is a valid CSS property.
A CSS property is valid if there are no spaces in between the text. In future versions, we could check against a list of valid properties, but that might take us down a rabbit hole of ever changing properties.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property
|
str
|
the property of the Declaration which might or might not be valid. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if the property is an invalid property |
Source code in webcode_tk/css_tools.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
|
validate_value(value, property=None)
Raises a ValueError if the value is invalid.
Caveat: this is by no means a comprehensive validation, and so there is much room for improvement. For now, we're focusing on the basics, such as there can be no text after the semi- colon and there should be no units if the value is 0.
In future versions, we could extend the validation to make sure the units match the property, which is why we added a default value for property.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
the code after the colon (what specifically do you want the property set to) |
required |
property
|
str
|
the property which defaults to None. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
if the value is invalid. |
Source code in webcode_tk/css_tools.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
|
DeclarationBlock
A set of properties and values that go with a selector
In CSS a declaration block is a block of code set off by curly
brackets {}
. They come after a selector and contain one or more
declarations (pairs of properties and values such as
width: 200px
).
Attributes:
Name | Type | Description |
---|---|---|
text |
str
|
full text of the declaration block including curly brackets. |
declarations |
a list of Declaration objects (see the Declaration class below). |
Source code in webcode_tk/css_tools.py
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
__init__(text)
Inits a declaration block
Source code in webcode_tk/css_tools.py
527 528 529 530 531 |
|
__set_declarations()
converts text into a list of declarations.
Source code in webcode_tk/css_tools.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
NestedAtRule
An at-rule rule that is nested, such as @media or @keyframes.
Nested at-rules include animation keyframes, styles for print (@media print), and breakpoints (@media screen). Each nested at-rule has an at-rule, which works like a selector, and a ruleset for that at-rule. The ruleset may contain any number of selectors and their declaration blocks.
You can almost think of them as stylesheets within a stylesheet "A dweam within a dweam" -The Impressive Clergyman. "We have to go deeper" -Dom Cobb.
Nested at-rules are defined in the global variable: nested_at_rules. For more information on nested at-rules, you want to refer to MDN's [nested] (https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule#nested)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
at_rule
|
str
|
the full at-rule such as '@media only and (min-width: 520px)'. |
required |
text
|
str
|
the text of the code (without the at_rule). Provide the text if you do not provide a list of rulesets. |
''
|
rules
|
list
|
a list of Ruleset objects. This is optional and defaults to None. Just be sure to add text if you don't provide a list. |
None
|
Attributes: at_rule (str): the full at-rule such as '@media only and (min-width: 520px)'. rulesets (list): a list of Ruleset objects. selectors (list): a list of all selectors from the rulesets has_repeat_selectors (bool): whether there are any repeated selectors in the NestedAtRule. repeated_selectors (list): a list of any selectors that are repeated.
Source code in webcode_tk/css_tools.py
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 |
|
__init__(at_rule, text='', rules=None)
Inits a Nested @rule object.
Raises:
Type | Description |
---|---|
ValueError
|
an error is raised if neither at_rule nor text is provided for the constructor or both are provided but they do not match. |
Source code in webcode_tk/css_tools.py
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 |
|
check_repeat_selectors()
Checks to see if there are any repeated selectors
Source code in webcode_tk/css_tools.py
428 429 430 431 432 433 434 |
|
set_rulesets(text)
Converts string of text into a list of ruleset objects
Source code in webcode_tk/css_tools.py
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 |
|
Ruleset
Creates a ruleset: a selector with a declaration block.
For more information about Rulesets, please read MDN's article on [Rulesets] (https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax#css_rulesets)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
the CSS code in text form. |
required |
Attributes:
Name | Type | Description |
---|---|---|
__text |
str
|
the CSS code. |
selector |
str
|
the selector of the Ruleset |
declaration_block |
DeclarationBlock
|
a DeclarationBlock object. |
is_valid |
bool
|
whether the Ruleset is valid or not. |
Source code in webcode_tk/css_tools.py
463 464 465 466 467 468 469 470 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 |
|
__init__(text)
Inits a DeclarationBlock object using CSS code
Source code in webcode_tk/css_tools.py
481 482 483 484 485 486 487 488 |
|
initialize()
converts the text into a DeclarationBlock.
Source code in webcode_tk/css_tools.py
490 491 492 493 494 495 496 |
|
validate()
Determines whether the code is valid or not
Source code in webcode_tk/css_tools.py
498 499 500 501 502 503 504 505 506 507 508 509 510 |
|
Stylesheet
A Stylesheet object with details about the sheet and its components.
The stylesheet object has the full code, a list of comments from the stylesheet, a list of nested @rules, rulesets pertaining to colors, a list of all selectors, and information about repeated selectors.
About repeated selectors, front-end developers should always employ the DRY principle: Don't Repeat Yourself. In other words, if you use a selector once in your stylesheet, the only other place you would logically put the same selector would be in a nested at-rule (in particular, an @media or @print breakpoint)
For this reason, both the Stylesheet object and the NesteAtRule objects have attributes that show whether there are repeated selectors or not as well as which selectors get repeated.
Attributes:
Name | Type | Description |
---|---|---|
href |
the filename (not path), which may end with .css or .html (if stylesheet object comes from a style tag). |
|
text |
the actual code itself of the entire file or style tag. |
|
type |
whether it's a file or local if it's from an style tag. |
|
nested_at_rules |
a list of all nested at-rules. |
|
rulesets |
a list of all rulesets. |
|
comments |
a list of all comments in string format. |
|
color_rulesets |
a list of all rulesets that target color or background colors. |
|
selectors |
a list of all selectors. |
|
has_repeat_selectors |
bool
|
whether there are any repeated selectors anywhere in the stylesheet (including in the NestedAtRule. |
repeated_selectors |
list
|
a list of any selectors that are repeated. They might be repeated in the main stylesheet or they might be repeated in one of the nested @rules. |
Source code in webcode_tk/css_tools.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 260 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 |
|
__clean_text()
cleans up CSS like removing extra line returns
This is here because if a student has more than 2 blank lines, it could trigger an attribute error (at least it did in the past)
Source code in webcode_tk/css_tools.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
__extract_comments()
Gets all comments from the code and stores in a list.
Source code in webcode_tk/css_tools.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
__extract_nested_at_rules()
Pulls out any nested at-rule and stores them in a list.
Algorithm: get # of @ signs
Source code in webcode_tk/css_tools.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 |
|
__extract_rulesets()
Separates all code into individual rulesets.
Source code in webcode_tk/css_tools.py
251 252 253 254 255 256 257 258 259 |
|
__init__(href, text, stylesheet_type='file')
Inits Stylesheet with href, text (CSS code), and type.
Source code in webcode_tk/css_tools.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
__minify()
Removes all whitespace, line returns, and tabs from text.
Source code in webcode_tk/css_tools.py
161 162 163 |
|
__replace_variables()
Looks for and replaces any variables set in stylesheet with the variable's values.
Source code in webcode_tk/css_tools.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
__set_selectors()
Adds all selectors from stylesheet to selectors attribute.
Source code in webcode_tk/css_tools.py
343 344 345 346 347 348 349 |
|
get_color_ruleset(ruleset)
Returns a list of all rules targetting color or background color.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ruleset(Ruleset)
|
a Ruleset object complete with selector and declaration block. |
required |
Returns:
Name | Type | Description |
---|---|---|
color_rulesets |
list
|
a list of all selectors that target color in some way, but just with the color-based declarations. |
Source code in webcode_tk/css_tools.py
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 |
|
sort_selectors()
Puts all selectors in alphabetical order.
Source code in webcode_tk/css_tools.py
351 352 353 |
|
adjust_overrides(file_path, rules)
Returns a dictionary with a single global ruleset.
Gets the final computed value of all rulesets in a file. It loops through the rulesets, and whenever there is an override (due to a repeat selector), it replaces whichever value is in the repeated selector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
path to the file in question, to be used as a key in the adjusted rule |
required |
rules
|
dict
|
a dictionary where the key is the filename and the value is a list of rules. |
required |
Returns:
Name | Type | Description |
---|---|---|
adjusted_rule |
dict
|
a dictionary where the key is the same, but there is only one ruleset (the computed ruleset). |
Source code in webcode_tk/css_tools.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 |
|
border_checks_out(declaration)
returns whether the border would display or not.
In order for the border shorthand to display, the border style must be a valid border style.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
declaration
|
Declaration
|
the declaration with a border shorthand property. |
required |
Returns:
Name | Type | Description |
---|---|---|
checks_out |
bool
|
whether the border would be visible or not |
Source code in webcode_tk/css_tools.py
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 |
|
check_for_inherited_colors(rules, condensed, source_file)
Double-check and fix any necessary overrides.
This is a tough one. The goal is to look for advanced selectors (descendant, class, id, pseudo, attribute), and if they did NOT specify color or bg color, then replace it with the nearest ancestor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rules
|
list
|
a list of all color rules. |
required |
condensed
|
dict
|
the already condensed set of color rules |
required |
source_file
|
str
|
the file we are looking at. |
required |
Source code in webcode_tk/css_tools.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
|
condense_the_rules(rules, source_file)
takes a list of color rules and returns only the unique color rulesets
Brings together both background and foreground color for each selector (when present)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rules
|
list
|
list of tuples that contain filename, selector, property, and value |
required |
source_file
|
str
|
the HTML document the contains the rules |
required |
Returns: condensed: a dictionary with file and all selectors that target colors with what was set for background-color and color
Source code in webcode_tk/css_tools.py
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
|
file_applies_property_by_selector(file_path, selector, property)
determines whether a specific property is applied to selector or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
path to html doc in question. |
required |
selector
|
str
|
CSS selector (or element) that the property is applied. |
required |
property
|
str
|
the CSS property we are looking for. |
required |
Returns:
Name | Type | Description |
---|---|---|
applies_property |
bool
|
whether that selector applies the property or not. |
Source code in webcode_tk/css_tools.py
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 |
|
fonts_applied_report(project_dir, min=1, max=2)
returns a report of all files in a project folder that apply font families.
You can set the minimum and maximum number of fonts applied per page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
the relative path to the project folder we want to check. |
required |
min
|
the minimum number of fonts applied per file. |
1
|
|
max
|
the maximum number of fonts applied per file. |
2
|
Returns: report: a list of font data results.
Source code in webcode_tk/css_tools.py
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 |
|
get_all_color_rules(file)
gets all color rulesets from html file
Gets all color rulesets applied to an HTML file, whether that be through a style tag or linked stylesheet and condensing them.
Creates a list of tuples that include filename, selector, color, and background color, and adjusts for overrides. In other words, it should be each selector and the final color applied.
Caveats: It does not yet account for inheritance. That would require traversing the DOM. It also does not yet account for @media rules. As of now, it's ignoring any @media breakpoint rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
an html file |
required |
Returns:
Name | Type | Description |
---|---|---|
all_color_rules |
list
|
a dictionary of all color rulesets applied to an |
list
|
html document. |
Source code in webcode_tk/css_tools.py
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 |
|
get_all_font_rules(sheet)
returns a list of all rules targetting font properties.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet
|
Stylesheet
|
a stylesheet object. |
required |
Returns:
Name | Type | Description |
---|---|---|
font_rules |
list
|
a list of all font rules. |
Source code in webcode_tk/css_tools.py
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 |
|
get_all_link_rules(sheet)
returns all rules that target a hyperlink
returns all rules that target a link
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet
|
Stylesheet
|
the stylesheet object. |
required |
Returns:
Name | Type | Description |
---|---|---|
rules |
list
|
a list of all rules that target a link |
Source code in webcode_tk/css_tools.py
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 |
|
get_all_link_selectors(sheet)
returns all selectors that target a link
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet
|
Stylesheet
|
the stylesheet object. |
required |
Returns:
Name | Type | Description |
---|---|---|
selectors |
list
|
a list of all selectors that target a link |
Source code in webcode_tk/css_tools.py
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
get_all_project_stylesheets(project_dir)
returns a list of all styles and stylesheets from a project folder.
This includes styles from style tags as well as linked stylesheets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
the relative link to the folder with the web docs. |
required |
Returns:
Name | Type | Description |
---|---|---|
all_files_styles |
list
|
a list of all stylesheets and style tag contents. |
Source code in webcode_tk/css_tools.py
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 |
|
get_all_styles_in_order(project_path)
returns a list of all files' stylesheets in order of appearance.
The goal is to allows user to identify the cascade order of selectors and their values. This will allow one to determine if one ruleset overrides another (same specificity)
Iterates through each html file in a project folder and extracts any style tags and local stylesheets. Each styletag or stylesheet is converted into a Stylesheet object and appended to a dictionary of file names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_path
|
str
|
the path to the main project folder. |
required |
Returns:
Name | Type | Description |
---|---|---|
styles_by_html_files |
list
|
a list of dictionary objects. Each dictionary has two keys: file and stylesheets. |
Source code in webcode_tk/css_tools.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 |
|
get_all_stylesheets_by_file(file_path)
returns a list of all Stylesheet objects from an HTML file in order of appearance.
This will check an HTML file for any links to stylesheets or style tags, and get each stylesheet in the order in which they were called (in case there is a CSS override).
It will only accept links to local stylesheets and ignore any external stylesheets called with an http or https.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
the path to an HTML file. |
required |
Returns:
Name | Type | Description |
---|---|---|
all_styles |
list
|
a list of stylesheet objects in the order in which they are called (as a link or style tag). |
Source code in webcode_tk/css_tools.py
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 |
|
get_background_color(declaration)
Returns a color value from a declaration with a property of background
Parameters:
Name | Type | Description | Default |
---|---|---|---|
declaration
|
Declaration
|
the declaration we want to test. |
required |
Returns:
Name | Type | Description |
---|---|---|
color_value |
Union[str, None]
|
either a valid color value, None, or gradient - if it's a gradient |
Source code in webcode_tk/css_tools.py
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
|
get_class_score(selector)
receives a selector and returns the class score
The class score represents the combined number of class, pseudo-class, and attribute selectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
the complete CSS selector |
required |
Returns:
Name | Type | Description |
---|---|---|
score |
int
|
the number of class selectors, which includes attribute |
int
|
and pseudoclass selectors (but NOT pseudo-elements). |
Source code in webcode_tk/css_tools.py
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 |
|
get_color_codes_of_type(color_type, gradient)
returns all color codes of a particular type from a gradient
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_type
|
str
|
the type of color code it might be (hex, rgb, hsl, or keyword) |
required |
gradient
|
str
|
the gradient code. |
required |
Returns:
Name | Type | Description |
---|---|---|
colors |
list
|
any color values that were found. |
Source code in webcode_tk/css_tools.py
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 |
|
get_color_data(file, color_details, level='aaa')
returns the color contrast data on a color.
pulls out the selector, background color, text color, contrast ratio, and whether it passes color contrast.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
just the name of the file (not path). |
required |
color_details
|
dict
|
tuple of full color & bg color details. |
required |
level
|
the level of normal text (AAA or AA). |
'aaa'
|
Returns:
Name | Type | Description |
---|---|---|
color_data |
tuple
|
a tuple with filename and results as a string |
Source code in webcode_tk/css_tools.py
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 |
|
get_color_rules_from_stylesheet(stylesheet)
Gets all color-based rules from a stylesheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stylesheet
|
Stylesheet
|
Stylesheet object. |
required |
Returns:
Name | Type | Description |
---|---|---|
rules |
list
|
a list of color-based rules |
Source code in webcode_tk/css_tools.py
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 |
|
get_colors_from_gradient(gradient)
extract all color codes from gradient
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gradient
|
str
|
the CSS color gradient value. |
required |
Returns:
Name | Type | Description |
---|---|---|
colors |
list
|
a list of all colors found in the gradient. |
Source code in webcode_tk/css_tools.py
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 |
|
get_comment_positions(code)
looks for index positions of first opening and closing comment.
From this function, you can create a slice of a comment from the code. You would do this if you want to extract the comments from the code, or if you wanted to inspect what was in the comments, or even identify if there are comments.
Note: this only works for the first comment in code. You would want to loop through the code extracting each comment one at a time using this function until it returns None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
the CSS code you want to extract comments from. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
Union[list, None]
|
a list of the index positions for the beginning and end of the first occuring comment in the code. |
Source code in webcode_tk/css_tools.py
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 |
|
get_declaration_value_by_property(block, property)
returns the value of a property from a declaration block
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block
|
Union[str, DeclarationBlock]
|
the declaration block in question (could be as a string or as a DeclarationBlock) |
required |
property
|
str
|
the property we are looking for |
required |
Returns:
Name | Type | Description |
---|---|---|
value |
str
|
the value of the property |
Source code in webcode_tk/css_tools.py
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 |
|
get_families(declaration_block)
returns a list of all font families in a declaration block
Source code in webcode_tk/css_tools.py
1329 1330 1331 1332 1333 1334 1335 1336 |
|
get_font_families(sheet)
returns a list of all font families targeted in a stylesheet
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet
|
Stylesheet
|
a stylesheet object, which could be a style tag or entire stylesheet (*.css file) |
required |
Returns:
Name | Type | Description |
---|---|---|
font_families |
list
|
a list of dictionary objects that contain all selectors and their values (but only if the value is a font family) |
Source code in webcode_tk/css_tools.py
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 |
|
get_global_color_details(rulesets)
receives rulesets and returns data on global colors
Note: a global selector is any selector that targets all elements
in the DOM. Examples include html
, body
, :root
, and
the universal selector: *
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rulesets
|
Union[list, tuple]
|
a list or tuple of Ruleset objects |
required |
Returns:
Name | Type | Description |
---|---|---|
global_rulesets |
list
|
a list of dictionary objects that each contain a selector, a background color, a text color, contrast ratio, whether it passes at various levels. |
Source code in webcode_tk/css_tools.py
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 |
|
get_global_color_report(project_dir, level='aaa')
Returns a report on which files in a project apply global colors
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
the project folder path. |
required |
level
|
whether we are testing for Normal AAA or Normal AA |
'aaa'
|
Returns:
Name | Type | Description |
---|---|---|
report |
list
|
a list of files and a pass or fail message for each. |
Source code in webcode_tk/css_tools.py
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 |
|
get_global_colors(file_path)
Returns a dictionary of color rules applied the entire document.
Global colors (in this context) are colors that apply to an entire document. Selectors that target the entire document are *, html, and body.
Since it's possible that an author could accidentally override a color or background color, this function will remove any previous rules that are overridden in a file.
NOTE: This should not consider an override if the would-be selector is in an @media ruleset, we won't treat it as an override.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
the path to the file. |
required |
Returns:
Name | Type | Description |
---|---|---|
global_color_rules |
dict
|
a dictionary of filenames and their global rulesets. |
Source code in webcode_tk/css_tools.py
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 |
|
get_header_color_details(rulesets)
receives rulesets and returns data on colors set by headers
This function will look through all rules in a ruleset and extracts the rules that target color or background color for a heading (h1 -h6).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rulesets
|
Union[list, tuple]
|
a list or tuple of Ruleset objects. |
required |
Returns:
Name | Type | Description |
---|---|---|
header_rulesets |
list
|
a list of dictionary objects that each contain a selector, a background color, and a text color. |
Source code in webcode_tk/css_tools.py
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 |
|
get_header_selectors(selector)
takes selector and returns any selector that selects an h1-h6
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
A CSS selector |
required |
Returns:
Name | Type | Description |
---|---|---|
header_selectors |
list
|
a list of selectors that target a heading. |
Source code in webcode_tk/css_tools.py
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 |
|
get_heading_color_report(project_dir)
Returns a report on which files in a project apply heading colors
For now, we just want to have at least a color or background color applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
the project folder path. |
required |
Returns:
Name | Type | Description |
---|---|---|
report |
list
|
a list of files and a pass or fail message for each. |
Source code in webcode_tk/css_tools.py
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 |
|
get_id_score(selector)
receives a selector and returns # of ID selectors
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
the complete CSS selector |
required |
Returns:
Name | Type | Description |
---|---|---|
score |
int
|
the number of ID selectors. |
Source code in webcode_tk/css_tools.py
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 |
|
get_link_color_data(project_path)
returns all colors applied to links.
Identifies all selectors that target a link, and gets a list of dictionaries that identify colors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_path
|
str
|
path to project folder |
required |
Returns:
Name | Type | Description |
---|---|---|
link_styles |
list
|
a list of link color data applied to each file that includes a link color data |
Source code in webcode_tk/css_tools.py
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 |
|
get_number_required_selectors(selector_type, sheet)
returns # of a specific selector type in a stylesheet
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector_type
|
str
|
what kind of selector we're looking for. |
required |
sheet
|
Stylesheet
|
the Stylesheet object we're inspecting. |
required |
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
the number of occurrences of the selector. |
Source code in webcode_tk/css_tools.py
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 |
|
get_project_color_contrast(project_path, normal_goal='Normal AAA', large_goal='Large AAA')
checks all color rules for each file in a project folder for contrast
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_path
|
str
|
path to project folder. |
required |
normal_goal
|
color contrast goal for most text in document (all except headers) - could be 'Normal AAA' or 'Normal AA' (default set to 'Normal AAA') |
'Normal AAA'
|
|
large_goal
|
color contrast goal for large (headings) text. May be 'Large AAA' or 'Large AA' (default is set to 'Large AAA') |
'Large AAA'
|
Returns:
Name | Type | Description |
---|---|---|
results |
list
|
a list of tuples. Each tuple contains a filename, selector, goal, color, bg_color, computed contrast ratio, passes_color |
Source code in webcode_tk/css_tools.py
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 |
|
get_project_color_contrast_report(project_dir, level='AAA')
returns a report of pass or fail for each element targetting color.
NOTE: We are replacing this report with the one in the cascade_tools for two reasons: one, it more accurately targest a large or regular sized font; and two, it only targets any element with direct text because it's the text that is visible in the browser that we are concerned with.
See issue #28: Color Contrast issue with cascade https://github.com/HundredVisionsGuy/webcode-tk/issues/28
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
the project folder where the html and css files are found. |
required |
level
|
the level for the report (AAA or AA) |
'AAA'
|
Returns: report: a report of every targetted color and whether it passes or fails.
Source code in webcode_tk/css_tools.py
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 |
|
get_project_global_colors(project_path)
Returns a dictionary of color rules applied to all html files in a project folder.
Global colors (in this context) are colors that apply to an entire document. Selectors that target the entire document are *, html, and body.
Since it's possible that an author could accidentally override a color or background color, this function will remove any previous rules that are overridden in a file.
NOTE: This should not consider an override if the would-be selector is in an @media ruleset, we won't treat it as an override.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_path
|
str
|
the project folder path. |
required |
Returns:
Name | Type | Description |
---|---|---|
global_color_rules |
dict
|
a dictionary of filenames and their global rulesets. |
Source code in webcode_tk/css_tools.py
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 |
|
get_properties_applied_report(project_dir, goals)
returns a report on any elements that fail to have a property applied
goals should be a dictionary with a key for each element we are checking. Each key has as it's value a dictionary with 1 to 3 possible keys... 1. (required) properties (the properties that should be applied) 2. (optional) min_required: If min_required is specified, then to pass it only requires the minimum number of properties to be present.
Sample goals might look like the following: goals_simple = { "figure": { "properties": ("box-shadow", "border-radius", "animation"), } } goals_complex = { "figure": { "properties": ("box-shadow", "border-radius", "animation"), "min_required": 2, } }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
the path to the project folder |
required |
goals
|
dict
|
a dictionary of elements and the properties expected to be present |
required |
Returns:
Name | Type | Description |
---|---|---|
report |
list
|
a list of pass or fail messages that indicates the file, the element, and the missing property. |
Source code in webcode_tk/css_tools.py
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 |
|
get_selector_type(selector)
returns the type of selector it is.
Cycles through selector regexes to see which one it is. When it has a match, it returns the key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
the selector in question. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the key of the selector regex dictionary if there's a match. |
Source code in webcode_tk/css_tools.py
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 |
|
get_shorthand(sel)
returns a shorthand version of a property if it could be a portion of a shorthand property
It's more efficient if you check for a dash before passing it as an argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sel
|
str
|
a CSS selector (should be one with a dash) |
required |
Returns:
Name | Type | Description |
---|---|---|
shorthand |
str
|
the shorthand version of a property if it is or an empty string if not. |
Source code in webcode_tk/css_tools.py
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 |
|
get_specificity(selector)
Gets the specificity score on the selector.
According to MDN's article on Specificity, Specificity is the algorithm used by browsers to determine the CSS declaration that is the most relevant to an element, which in turn, determines the property value to apply to the element.
The specificity algorithm calculates the weight of a CSS selector to determine which rule from competing CSS declarations gets applied to an element.
The specificity score is basically a number, and if two selectors target the same element, the selector with the highest specificity score wins. The number is like a 3-digit number, where the "ones" place is the number of type selectors, the "tens" place is the number of class selectors, and the "hundreds" place is the number of id selectors.
For example, the selector: h1, h2, h3
has a specificity of 003
because there are neither id nor class selectors, but there are 3
type selectors.
The selector: nav#main ul
has a specificity of 102
because
there is one id selector (#main
) and two type selectors (nav
and ul
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
the CSS selector in question. |
required |
Returns:
Name | Type | Description |
---|---|---|
specificity |
str
|
the specificity score. |
Source code in webcode_tk/css_tools.py
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 |
|
get_styles_by_html_files(project_path)
Returns a list of filenames with their stylesheets in order of appearance.
This will identify all HTML documents in the project folder. For each HTML document, it will create a dictionary with two keys: filename for the HTML document and stylesheets for a list of the css styles created through link tags or style tags.
As it uses get_all_stylesheets_by_files, you can be sure that no external stylesheets (https://...) will be included in the list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_path
|
str
|
a string of the path to the project folder you want to test. |
required |
Returns:
Name | Type | Description |
---|---|---|
styles_by_html_files |
list
|
a list of dictionary objects indicating each html document and its styles in order of appearance. |
Source code in webcode_tk/css_tools.py
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 |
|
get_type_score(selector)
receives a selector and returns the number of type selectors
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
the complete CSS selector |
required |
Returns:
Name | Type | Description |
---|---|---|
score |
int
|
the number of type selectors. |
Source code in webcode_tk/css_tools.py
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 |
|
get_unique_font_rules(project_folder)
Returns list of files with only unique font rules applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_folder
|
str
|
a string path to the project folder we are testing. |
required |
Returns:
Name | Type | Description |
---|---|---|
project_font_data |
list
|
a list of dictionary objects that each store the file where styles are applied and their unique font-related rules. |
Source code in webcode_tk/css_tools.py
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 |
|
get_variables(text)
returns a list of css variables and their values.
This will extract any variables if they exist, copy the variable name and its value and create a dictionary object and append to list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
full text of css stylesheet |
required |
Returns:
Name | Type | Description |
---|---|---|
variables |
list
|
a list of variable dictionaries each with a key for the variable and a value for its value. |
Source code in webcode_tk/css_tools.py
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 |
|
has_link_selector(sheet)
returns whether any style in a stylesheet targets a hyperlink
There could be one or more link selectors. This will check each possible link selector (or psuedoselector). It only has to target a link and not a descendant of a link.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sheet
|
Stylesheet
|
the stylesheet object. |
required |
Returns:
Name | Type | Description |
---|---|---|
has_selector |
bool
|
whether there is a selector that targets a link |
Source code in webcode_tk/css_tools.py
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 |
|
has_required_property(property, sheet)
checks stylesheet for a particular property
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property
|
str
|
the property we're looking for |
required |
sheet
|
Stylesheet
|
the Stylesheet object we're inspecting |
required |
Returns:
Name | Type | Description |
---|---|---|
has_property |
bool
|
whether the Stylesheet has the property or not. |
Source code in webcode_tk/css_tools.py
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 |
|
has_vendor_prefix(property)
Checks a property to see if it uses a vendor prefix or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property
|
str
|
A CSS property in string format. |
required |
Returns:
Name | Type | Description |
---|---|---|
has_prefix |
bool
|
whether the property uses a vendor prefix or not. |
Source code in webcode_tk/css_tools.py
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 |
|
is_gradient(value)
checks a CSS value to see if it's using a gradient or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str
|
a CSS value. |
required |
Returns:
Name | Type | Description |
---|---|---|
uses_gradient |
bool
|
whether it uses a gradient or not. |
Source code in webcode_tk/css_tools.py
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 |
|
is_link_selector(selector)
returns a true if selector targets a link
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector
|
str
|
the selector in question |
required |
Source code in webcode_tk/css_tools.py
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 |
|
is_required_selector(selector_type, selector)
checks selector to see if it's required type or not.
Example: you may wish to loop through selectors and see which ones are class selectors, or id selectors, etc..
It makes use of the list of regex_patterns for selector type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selector_type
|
str
|
the type of selector in question, such as an id, class, type, etc. |
required |
selector
|
str
|
the selector we are checking. |
required |
Returns:
Name | Type | Description |
---|---|---|
match |
bool
|
whether the selector matches the type. |
Source code in webcode_tk/css_tools.py
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 |
|
is_selector_at_end_of_descendant(selector, cur_selector)
returns whether a selector is at the end of a descendant selector
Source code in webcode_tk/css_tools.py
2236 2237 2238 2239 2240 2241 2242 2243 2244 |
|
minify_code(text)
remove all new lines, tabs, and double spaces from text
This is a classic function for web developers to minify their code by removing new lines, tabs, and any double spaces from text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
the code you want to minify. |
required |
Returns:
Name | Type | Description |
---|---|---|
text |
str
|
the code without all the additional whitespace. |
Source code in webcode_tk/css_tools.py
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 |
|
no_style_attributes_allowed_report(project_dir)
returns a report on whether HTML docs use style attributes or not.
Only call this report if you do not allow a style attribute in an HTML doc
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
the relative link to the folder with the web docs. |
required |
Returns:
Name | Type | Description |
---|---|---|
report |
list
|
a list of all HTML docs and a pass or fail message. |
Source code in webcode_tk/css_tools.py
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 |
|
passes_global_color_contrast(file, goal='Normal AAA')
determines whether a file passes global color contrast
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
path to file in question. |
required |
goal
|
what's the color contrast goal - set to Normal AAA by default. |
'Normal AAA'
|
Returns:
Name | Type | Description |
---|---|---|
meets |
bool
|
whether it passes contrast goals or not |
Source code in webcode_tk/css_tools.py
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 |
|
process_gradient(code)
returns list of all colors from gradient sorted light to dark
This function is a work in progress. The goal is to eventually use it to determine whether a gradient meets color contrast accessibility ratings when compared against another color or color gradient.
In order to do this, the plan is to find the lightest color and the darkest color, so we can check both sides of the range. If the lightest or darkest color fails color contrast, then it's a fail. If both pass, then all colors in between will pass.
Note: we may be adding more to this and refactoring functionality.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
the color gradient value |
required |
Returns:
Name | Type | Description |
---|---|---|
only_colors |
list
|
a list of just color codes sorted by luminance |
Source code in webcode_tk/css_tools.py
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 |
|
remove_alpha(color_code)
removes the alpha channel from rgba or hsla
Honestly, I'm not sure if this is even needed. I am looking to eventually move over to the APCA algorithm for testing color contrast accessibility, but at this point, I cannot find the algorithm. If and when I do, I will work to replace the current algorithm (WCAG AA/AAA rating).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_code
|
str
|
the color code (hex, rgb, or hsl) with an alpha channel. |
required |
Returns:
Name | Type | Description |
---|---|---|
color_code |
str
|
the color code without the alpha channel. |
Source code in webcode_tk/css_tools.py
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 |
|
restore_braces(split)
restore the missing braces removed by the .split() method
This is more of a helper function to make sure that after splitting at-rule code by two curly braces, we restore it back.
In CSS, to find the end of a nested @rule, you can use the
following code: css_code.split("}}")
This is because a nested
@rule ends with two closing curly braces: one for the last
declaration, and the other for the end of the nested @rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
split
|
list
|
a list created by the split method on CSS code |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
the list but with the double closing braces restored from the split. |
Source code in webcode_tk/css_tools.py
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 |
|
separate_code(code)
splits code into two lists: code & comments
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
the stylesheet or style tag code |
required |
Returns:
Name | Type | Description |
---|---|---|
splitzky |
dict
|
a dictionary with two lists: a list of code snippets without comments, and a list of comments. |
Raises:
Type | Description |
---|---|
ValueError
|
if there is only one comment symbol: either / or / but not both (a syntax error) |
Source code in webcode_tk/css_tools.py
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 |
|
sort_color_codes(codes)
sorts color codes from light to dark (luminance)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
codes
|
Union[list, tuple]
|
a list or tuple of color values. |
required |
Returns:
Name | Type | Description |
---|---|---|
sorted |
list
|
a list of initial color values but in order from lightest to darkest (using luminance). |
Source code in webcode_tk/css_tools.py
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 |
|
styles_applied_report(project_dir)
returns a report of all files in a project folder that apply styles
This lets us know for each HTML doc if they apply styles (pass) or if they do not apply styles (fail)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
a relative path to the project folder. |
required |
Returns:
Name | Type | Description |
---|---|---|
report |
list
|
a list of HTML docs and whether they pass or fail (pass) means they did apply styles and fail is the opposite. |
Source code in webcode_tk/css_tools.py
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 |
|