Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit 0250263

Browse files
authored
Prefix all CSS classes with hxr- (#205)
* Prefix all CSS classes with `hxr-` Avoid collisioning with other CSS frameworks/themes/etc. See executablebooks/sphinx-book-theme#577 Closes #180 * Update tests to use the prefix `hxr-` * Refactor code to use CSS prefix variable
1 parent 21c1df2 commit 0250263

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

hoverxref/_static/css/tooltipster.custom.css hoverxref/_static/css/tooltipster.custom.css_t

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.hoverxref {
1+
.{{ hoverxref_css_class_prefix }}hoverxref {
22
border-bottom: 1px dotted;
33
border-color: gray;
44
}

hoverxref/_static/js/hoverxref.js_t

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ $(document).ready(function() {
9696
// Remove ``title=`` attribute for intersphinx nodes that have hoverxref enabled.
9797
// It doesn't make sense the browser shows the default tooltip (browser's built-in)
9898
// and immediately after that our tooltip was shown.
99-
$('.hoverxref.external').each(function () { $(this).removeAttr('title') });
99+
$('.{{ hoverxref_css_class_prefix }}hoverxref.external').each(function () { $(this).removeAttr('title') });
100100

101-
$('.hoverxref.tooltip').tooltipster({
101+
$('.{{ hoverxref_css_class_prefix }}hoverxref.{{ hoverxref_css_class_prefix }}tooltip').tooltipster({
102102
theme: {{ hoverxref_tooltip_theme }},
103103
interactive: {{ 'true' if hoverxref_tooltip_interactive else 'false' }},
104104
maxWidth: {{ hoverxref_tooltip_maxwidth }},
@@ -235,7 +235,7 @@ $(document).ready(function() {
235235
};
236236

237237
var delay = {{ hoverxref_modal_hover_delay }}, setTimeoutConst;
238-
$('.hoverxref.modal').hover(function(event) {
238+
$('.{{ hoverxref_css_class_prefix }}hoverxref.{{ hoverxref_css_class_prefix }}modal').hover(function(event) {
239239
var element = $(this);
240240
console.debug('Event: ' + event + ' Element: ' + element);
241241
event.preventDefault();

hoverxref/domains.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ class HoverXRefBaseDomain:
1414
)
1515

1616
def _inject_hoverxref_data(self, env, refnode, typ):
17-
classes = ['hoverxref']
17+
from .extension import CSS_CLASSES, CSS_DEFAULT_CLASS
18+
19+
classes = [CSS_DEFAULT_CLASS]
1820
type_class = None
1921
if typ == 'hoverxreftooltip':
2022
type_class = 'tooltip'
21-
classes.append(type_class)
2223
elif typ == 'hoverxrefmodal':
2324
type_class = 'modal'
24-
classes.append(type_class)
25+
2526
if not type_class:
2627
type_class = env.config.hoverxref_role_types.get(typ)
2728
if not type_class:
@@ -33,7 +34,9 @@ def _inject_hoverxref_data(self, env, refnode, typ):
3334
default,
3435
typ,
3536
)
36-
classes.append(type_class)
37+
38+
# Examples: hxr-tooltip, hxr-modal
39+
classes.append(CSS_CLASSES[type_class])
3740

3841
refnode.replace_attr('classes', classes)
3942
# TODO: log something else here, so we can unique identify this node

hoverxref/extension.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
logger = logging.getLogger(__name__)
2121

22+
CSS_CLASS_PREFIX = 'hxr-'
23+
CSS_DEFAULT_CLASS = f'{CSS_CLASS_PREFIX}hoverxref'
24+
CSS_CLASSES = {
25+
'tooltip': f'{CSS_CLASS_PREFIX}tooltip',
26+
'modal': f'{CSS_CLASS_PREFIX}modal',
27+
}
2228

2329
HOVERXREF_ASSETS_FILES = [
2430
'js/hoverxref.js_t', # ``_t`` tells Sphinx this is a template
@@ -27,7 +33,7 @@
2733
TOOLTIP_ASSETS_FILES = [
2834
# Tooltipster's Styles
2935
'js/tooltipster.bundle.min.js',
30-
'css/tooltipster.custom.css',
36+
'css/tooltipster.custom.css_t',
3137
'css/tooltipster.bundle.min.css',
3238

3339
# Tooltipster's Themes
@@ -65,6 +71,7 @@ def copy_asset_files(app, exception):
6571
# Then, add the values that the user overrides
6672
context[attr] = getattr(app.config, attr)
6773

74+
context['hoverxref_css_class_prefix'] = CSS_CLASS_PREFIX
6875
context['http_hoverxref_version'] = __version__
6976

7077
# Finally, add some non-hoverxref extra configs
@@ -73,10 +80,13 @@ def copy_asset_files(app, exception):
7380
context[attr] = getattr(app.config, attr)
7481

7582
for f in ASSETS_FILES:
83+
# Example: "./_static/js/hoverxref.js_t"
7684
path = os.path.join(os.path.dirname(__file__), '_static', f)
85+
# Example: "<app.outdir>/_static/css" or "<app.outdir>/_static/js"
86+
output = os.path.join(app.outdir, '_static', f.split('/')[0])
7787
copy_asset(
7888
path,
79-
os.path.join(app.outdir, '_static', f.split('.')[-1].replace('js_t', 'js')),
89+
output,
8090
context=context,
8191
)
8292

@@ -264,7 +274,7 @@ def missing_reference(app, env, node, contnode):
264274
hoverxref_type = hoverxref_type or app.config.hoverxref_default_type
265275

266276
classes = newnode.get('classes')
267-
classes.extend(['hoverxref', hoverxref_type])
277+
classes.extend([CSS_DEFAULT_CLASS, CSS_CLASSES[hoverxref_type]])
268278
newnode.replace_attr('classes', classes)
269279

270280
return newnode
@@ -374,11 +384,13 @@ def setup(app):
374384

375385
app.connect('missing-reference', missing_reference)
376386

387+
# Include all assets previously copied/rendered by ``copy_asset_files`` as
388+
# Javascript and CSS files into the Sphinx application
377389
for f in ASSETS_FILES:
378390
if f.endswith('.js') or f.endswith('.js_t'):
379391
app.add_js_file(f.replace('.js_t', '.js'))
380-
if f.endswith('.css'):
381-
app.add_css_file(f)
392+
if f.endswith('.css') or f.endswith('.css_t'):
393+
app.add_css_file(f.replace('.css_t', '.css'))
382394

383395
return {
384396
'version': __version__,

0 commit comments

Comments
 (0)