• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zostera / django-bootstrap4 / 8388360828

22 Mar 2024 09:43AM CUT coverage: 90.998%. First build
8388360828

Pull #696

github

web-flow
Bump dependabot/fetch-metadata from 1.6.0 to 2.0.0

Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 1.6.0 to 2.0.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v1.6.0...v2.0.0)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #696: Bump dependabot/fetch-metadata from 1.6.0 to 2.0.0

304 of 376 branches covered (80.85%)

Branch coverage included in aggregate %.

1283 of 1368 relevant lines covered (93.79%)

14.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.81
/src/bootstrap4/utils.py
1
import re
16✔
2
from collections.abc import Mapping
16✔
3
from urllib.parse import parse_qs, urlparse, urlunparse
16✔
4

5
from django import get_version
16✔
6
from django.forms.utils import flatatt
16✔
7
from django.template.base import FilterExpression, TemplateSyntaxError, Variable, VariableDoesNotExist, kwarg_re
16✔
8
from django.template.loader import get_template
16✔
9
from django.utils.encoding import force_str
16✔
10
from django.utils.html import format_html
16✔
11
from django.utils.http import urlencode
16✔
12
from django.utils.safestring import mark_safe
16✔
13

14
from .text import text_value
16✔
15

16
DJANGO_VERSION = int(get_version()[:1])
16✔
17

18
# RegEx for quoted string
19
QUOTED_STRING = re.compile(r'^["\'](?P<noquotes>.+)["\']$')
16✔
20

21

22
def handle_var(value, context):
16✔
23
    """Handle template tag variable."""
24
    # Resolve FilterExpression and Variable immediately
25
    if isinstance(value, FilterExpression) or isinstance(value, Variable):
16!
26
        return value.resolve(context)
16✔
27
    # Return quoted strings unquoted
28
    # http://djangosnippets.org/snippets/886
29
    stringval = QUOTED_STRING.search(value)
×
30
    if stringval:
×
31
        return stringval.group("noquotes")
×
32
    # Resolve variable or return string value
33
    try:
×
34
        return Variable(value).resolve(context)
×
35
    except VariableDoesNotExist:
×
36
        return value
×
37

38

39
def parse_token_contents(parser, token):
16✔
40
    """Parse template tag contents."""
41
    bits = token.split_contents()
16✔
42
    tag = bits.pop(0)
16✔
43
    args = []
16✔
44
    kwargs = {}
16✔
45
    asvar = None
16✔
46
    if len(bits) >= 2 and bits[-2] == "as":
16!
47
        asvar = bits[-1]
×
48
        bits = bits[:-2]
×
49
    if len(bits):
16!
50
        for bit in bits:
16✔
51
            match = kwarg_re.match(bit)
16✔
52
            if not match:
16!
53
                raise TemplateSyntaxError(f'Malformed arguments to tag "{tag}"')
×
54
            name, value = match.groups()
16✔
55
            if name:
16!
56
                kwargs[name] = parser.compile_filter(value)
16✔
57
            else:
58
                args.append(parser.compile_filter(value))
×
59
    return {"tag": tag, "args": args, "kwargs": kwargs, "asvar": asvar}
16✔
60

61

62
def split_css_classes(css_classes):
16✔
63
    """Turn string into a list of CSS classes."""
64
    classes_list = text_value(css_classes).split(" ")
16✔
65
    return [c for c in classes_list if c]
16✔
66

67

68
def add_css_class(css_classes, css_class, prepend=False):
16✔
69
    """Add a CSS class to a string of CSS classes."""
70
    classes_list = split_css_classes(css_classes)
16✔
71
    classes_to_add = [c for c in split_css_classes(css_class) if c not in classes_list]
16✔
72
    if prepend:
16✔
73
        classes_list = classes_to_add + classes_list
16✔
74
    else:
75
        classes_list += classes_to_add
16✔
76
    return " ".join(classes_list)
16✔
77

78

79
def remove_css_class(css_classes, css_class):
16✔
80
    """Remove a CSS class from a string of CSS classes."""
81
    remove = set(split_css_classes(css_class))
×
82
    classes_list = [c for c in split_css_classes(css_classes) if c not in remove]
×
83
    return " ".join(classes_list)
×
84

85

86
def render_script_tag(url):
16✔
87
    """Build a script tag."""
88
    url_dict = sanitize_url_dict(url)
16✔
89
    url_dict.setdefault("src", url_dict.pop("url", None))
16✔
90
    return render_tag("script", url_dict)
16✔
91

92

93
def render_link_tag(url, rel="stylesheet", media=None):
16✔
94
    """Build a link tag."""
95
    url_dict = sanitize_url_dict(url, url_attr="href")
16✔
96
    url_dict.setdefault("href", url_dict.pop("url", None))
16✔
97
    url_dict["rel"] = rel
16✔
98
    if media:
16!
99
        url_dict["media"] = media
×
100
    return render_tag("link", attrs=url_dict, close=False)
16✔
101

102

103
def render_tag(tag, attrs=None, content=None, close=True):
16✔
104
    """Render a HTML tag."""
105
    builder = "<{tag}{attrs}>{content}"
16✔
106
    if content or close:
16✔
107
        builder += "</{tag}>"
16✔
108
    return format_html(builder, tag=tag, attrs=mark_safe(flatatt(attrs)) if attrs else "", content=text_value(content))
16✔
109

110

111
def render_template_file(template, context=None):
16✔
112
    """Render a Template to unicode."""
113
    assert isinstance(context, Mapping)
16✔
114
    template = get_template(template)
16✔
115
    return template.render(context)
16✔
116

117

118
def url_replace_param(url, name, value):
16✔
119
    """Replace a GET parameter in an URL."""
120
    url_components = urlparse(force_str(url))
16✔
121

122
    params = parse_qs(url_components.query)
16✔
123

124
    if value is None:
16✔
125
        del params[name]
16✔
126
    else:
127
        params[name] = value
16✔
128

129
    return mark_safe(
16✔
130
        urlunparse(
131
            [
132
                url_components.scheme,
133
                url_components.netloc,
134
                url_components.path,
135
                url_components.params,
136
                urlencode(params, doseq=True),
137
                url_components.fragment,
138
            ]
139
        )
140
    )
141

142

143
def sanitize_url_dict(url, url_attr="src"):
16✔
144
    """Sanitize url dict as used in django-bootstrap4 settings."""
145
    if isinstance(url, str):
16✔
146
        return {url_attr: url}
16✔
147
    return url.copy()
16✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc