Repository navigation
The Unraid watch checks every supported release: contracts, Unraid CSS by release, CSS fallbacks #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
The Unraid watch checks every supported release: contracts, Unraid CSS by release, CSS fallbacks #185
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
90f2c02
ci: the Unraid watch checks what FolderView3 relies on in every suppo…
chodeus 1168627
fix: the Unraid watch reads nested CSS, fails closed on empty input, …
chodeus d5102ad
docs: the palette's watched reason says the plugin reads its variable…
chodeus 8e04e04
fix: a declaration after a nested rule keeps its place, each theme's …
chodeus ebb7db4
fix: a watch.json with no contracts, themes or files, or a contract w…
chodeus 14faa05
fix: the CSS comparison keeps !important over a later plain declarati…
chodeus ae7ff67
fix: the element a selector styles is found past guards nested in :wh…
chodeus 69ca571
fix: a group rule written @media(...) with no space is read, and code…
chodeus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| """Stylesheets, FolderView3's and Unraid's: rules as text, the elements FolderView3 styles, and what Unraid sets on them per release.""" | ||
| import re | ||
|
|
||
| TOKEN = re.compile(r'[#.][A-Za-z_][\w-]*') | ||
| COMMENT = re.compile(r'/\*.*?\*/', re.S) | ||
| VAR = re.compile(r'var\(\s*(--[\w-]+)\s*([,)])') | ||
| DEFINED = re.compile(r'(?:^|[;{\s])(--[\w-]+)\s*:') | ||
| # At-rules whose block holds rules for elements; @keyframes, @font-face and @page hold none | ||
| GROUPS = ('@media', '@supports', '@container', '@layer', '@scope') | ||
| # What moves or wraps an element. Colours and fonts change with each theme by design, so they are left out | ||
| LAYOUT = frozenset({ | ||
| 'display', 'position', 'white-space', 'overflow', 'overflow-x', 'overflow-y', 'float', 'clear', 'box-sizing', 'flex', 'flex-direction', | ||
| 'flex-wrap', 'flex-grow', 'flex-shrink', 'flex-basis', 'align-items', 'justify-content', 'gap', 'grid-template-columns', 'width', | ||
| 'min-width', 'max-width', 'height', 'min-height', 'max-height', 'margin', 'margin-top', 'margin-bottom', 'margin-left', 'margin-right', | ||
| 'padding', 'padding-top', 'padding-bottom', 'padding-left', 'padding-right', 'line-height', 'font-size', 'vertical-align', 'text-align', | ||
| 'text-overflow', 'word-break', 'overflow-wrap', 'table-layout', 'border-collapse', 'border-spacing', 'top', 'left', 'right', 'bottom', | ||
| 'transform', 'z-index', 'visibility', 'content'}) | ||
|
|
||
|
|
||
| def uncommented(text): | ||
| """The text with each comment blanked out, line breaks kept so positions still give line numbers.""" | ||
| return COMMENT.sub(lambda m: '\n' * m.group(0).count('\n'), text) | ||
|
|
||
|
|
||
| def _plain(text): | ||
| """[(index, char)] for characters outside quoted strings, so a "}" or ";" in a content value is just text.""" | ||
| out, quote = [], None | ||
| for i, c in enumerate(text): | ||
| if quote: | ||
| quote = None if c == quote and text[i - 1] != '\\' else quote | ||
| elif c in '"\'': | ||
| quote = c | ||
| else: | ||
| out.append((i, c)) | ||
| return out | ||
|
|
||
|
|
||
| def _split(text, at): | ||
| """text cut at each `at` character outside parentheses and strings (a url() or :not() list keeps its ; and ,).""" | ||
| parts, depth, start = [], 0, 0 | ||
| for i, c in _plain(text): | ||
| depth += (c == '(') - (c == ')') | ||
| if c == at and depth <= 0: | ||
| parts.append(text[start:i]) | ||
| start = i + 1 | ||
| return parts + [text[start:]] | ||
|
|
||
|
|
||
| def _selector(text): | ||
| """One spelling per selector: spacing around combinators and quotes around attribute values do not make a rule new.""" | ||
| text = re.sub(r'\s*([>+~])\s*', r' \1 ', text) | ||
| text = re.sub(r'\[\s*([\w-]+)\s*([~|^$*]?=)\s*["\']?([^"\'\]]*?)["\']?\s*\]', r'[\1\2\3]', text) | ||
| return ' '.join(text.split()) | ||
|
|
||
|
|
||
| def _condition(text): | ||
| """One spelling per at-rule condition, `@media (max-width: 767px)`, however the stylesheet spaced it.""" | ||
| text = re.sub(r'\s*([():,])\s*', r'\1', text) | ||
| text = re.sub(r'(?<=[\w)])\(', ' (', text).replace(':', ': ').replace(',', ', ') | ||
| return ' '.join(text.split()) | ||
|
|
||
|
|
||
| def _parts(body): | ||
| """[(None, declarations) or (head, block)] of a body in source order: its own declarations and the rules nested in it.""" | ||
| items, depth, paren, start, item = [], 0, 0, 0, 0 | ||
| for i, c in _plain(body): | ||
| paren += (c == '(') - (c == ')') if depth == 0 else 0 | ||
| if c == '{': | ||
| if depth == 0: | ||
| items.append((None, body[start:item])) | ||
| head, opened = ' '.join(body[item:i].split()), i + 1 | ||
| depth += 1 | ||
| elif c == '}' and depth: | ||
| depth -= 1 | ||
| if depth == 0: | ||
| items.append((head, body[opened:i])) | ||
| start = item = i + 1 | ||
| elif c == ';' and depth == 0 and paren <= 0: | ||
| item = i + 1 | ||
| return items + [(None, body[start:])] | ||
|
|
||
|
|
||
| def _set(decls, name, value): | ||
| """A later declaration wins, except over an !important one that it is not itself.""" | ||
| if not (decls.get(name, '').endswith('!important') and not value.endswith('!important')): | ||
| decls[name] = value | ||
|
|
||
|
|
||
| def _declarations(text): | ||
| decls = {} | ||
| for part in _split(text, ';'): | ||
| name, colon, value = part.partition(':') | ||
| if colon and name.strip() and not name.strip().startswith('@'): | ||
| # `4px!important` and `4px !important` are the same value | ||
| _set(decls, name.strip().lower(), ' '.join(re.sub(r'\s*!\s*important\b', ' !important', value).split())) | ||
| return decls | ||
|
|
||
|
|
||
| def _walk(body, conditions, parents, out): | ||
| # In source order: a declaration after a nested rule comes after it, as the browser applies them | ||
| for head, block in _parts(body): | ||
| if head is None: | ||
| decls = _declarations(block) if parents else {} | ||
| out += [(conditions, parent, decls) for parent in parents if decls] | ||
| continue | ||
| if head.startswith('@'): | ||
| # `@media(max-width:600px)` needs no space after the keyword | ||
| if re.match(r'@[\w-]+', head).group(0).lower() in GROUPS: | ||
| _walk(block, ' '.join(filter(None, (conditions, _condition(head)))), parents, out) | ||
| continue | ||
| selectors = [_selector(s) for s in _split(head, ',') if s.strip()] | ||
| # Nesting: & is the parent rule; a nested selector without one is a descendant of it | ||
| resolved = [s.replace('&', p) if '&' in s else f'{p} {s}' for p in parents for s in selectors] if parents else selectors | ||
| _walk(block, conditions, resolved, out) | ||
|
|
||
|
|
||
| def rules(text): | ||
| """[(at-rule conditions, selector, {property: value})] in document order, nested rules resolved.""" | ||
| out = [] | ||
| _walk(uncommented(text), '', [], out) | ||
| return out | ||
|
|
||
|
|
||
| def subject(selector): | ||
| """The ids and classes of the element a selector styles; none for a ::before box, nothing an attribute value or :not() names.""" | ||
| # Innermost first, until none is left: 7.4 wraps its guards as :where(:not(.unapi *)) | ||
| bare, previous = re.sub(r'\[[^\]]*\]', '', selector), None | ||
| while bare != previous: | ||
| bare, previous = re.sub(r'\([^()]*\)', '', bare), bare | ||
| last = re.split(r'[\s>+~]+', bare.strip())[-1] | ||
| return [] if '::' in last else TOKEN.findall(last) | ||
|
|
||
|
|
||
| def styled(texts): | ||
| """{id or class: {(at-rule conditions, property)} FolderView3 sets on elements that carry it} from its own stylesheets.""" | ||
| out = {} | ||
| for text in texts: | ||
| for conditions, selector, decls in rules(text): | ||
| for token in subject(selector): | ||
| out.setdefault(token, set()).update((conditions, name) for name in decls) | ||
| return out | ||
|
|
||
|
|
||
| def merged(texts): | ||
| """{(at-rule conditions, selector, property): value} for stylesheets in load order: a later one wins, !important aside.""" | ||
| out = {} | ||
| for text in texts: | ||
| for conditions, selector, decls in rules(text): | ||
| for name, value in decls.items(): | ||
| _set(out, (conditions, selector, name), value) | ||
| return out | ||
|
|
||
|
|
||
| def _covers(ours, token, conditions, name): | ||
| # The plugin's own rule for this property covers Unraid's when it holds everywhere, or under the same condition | ||
| return ('', name) in ours[token] or (conditions, name) in ours[token] | ||
|
|
||
|
|
||
| def changes(by_ref, refs, ours): | ||
| """[(conditions, selector, property, [(ref, value)])]: layout Unraid changed on elements FolderView3 styles but does not set.""" | ||
| out = [] | ||
| for conditions, selector, name in sorted(set().union(*(by_ref[r] for r in refs))): | ||
| tokens = [t for t in subject(selector) if t in ours] | ||
| if name not in LAYOUT or not tokens or any(_covers(ours, t, conditions, name) for t in tokens): | ||
| continue | ||
| values = [(ref, by_ref[ref].get((conditions, selector, name))) for ref in refs] | ||
| if len({v for _, v in values}) > 1: | ||
| out.append((conditions, selector, name, values)) | ||
| return out | ||
|
|
||
|
|
||
| def fallbacks(stylesheets, code): | ||
| """Problems: var(--x) with no fallback where --x is Unraid's, not declared or keyed by FolderView3's own CSS or code.""" | ||
| texts = {f: uncommented(f.read_text(encoding='utf-8')) for f in stylesheets} | ||
| ours = {name for text in texts.values() for name in DEFINED.findall(text)} | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| named = '\n'.join(f.read_text(encoding='utf-8') for f in code) | ||
|
|
||
| def keyed(name): | ||
| # The CSS tool's presets ('fv3-x': '#fff') and the generated theme CSS ('x' => '#fff') key the plugin's own variables; | ||
| # a name that is only read (getPropertyValue('--x')) is not defined by that | ||
| return re.search(r"['\"](?:--)?" + re.escape(name[2:]) + r"['\"]\s*(?::|=>)", named) | ||
| return [f'{f.name}:{text.count(chr(10), 0, m.start()) + 1}: var({m.group(1)}) has no fallback and FolderView3 does not ' | ||
| f'define {m.group(1)}, so the style needs every Unraid release to; give it a fallback' | ||
| for f, text in texts.items() for m in VAR.finditer(text) | ||
| if m.group(2) == ')' and m.group(1) not in ours and not keyed(m.group(1))] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.