| 1 | from django import template
|
|---|
| 2 | from django.template.defaultfilters import stringfilter
|
|---|
| 3 | from django.utils.safestring import mark_safe
|
|---|
| 4 |
|
|---|
| 5 | register = template.Library()
|
|---|
| 6 |
|
|---|
| 7 | @register.filter
|
|---|
| 8 | @stringfilter
|
|---|
| 9 | def tabindex(widget, index):
|
|---|
| 10 | if isinstance(index, int):
|
|---|
| 11 | if widget.find('/>') != -1:
|
|---|
| 12 | retval = widget.replace('/>', ' tabindex="{0}" />'.format(index))
|
|---|
| 13 | return mark_safe(retval)
|
|---|
| 14 | elif widget.find('>') != -1:
|
|---|
| 15 | retval = widget.replace('>', ' tabindex="{0}">'.format(index))
|
|---|
| 16 | return mark_safe(retval)
|
|---|
| 17 | else:
|
|---|
| 18 | return mark_safe(widget)
|
|---|
| 19 |
|
|---|
| 20 | try:
|
|---|
| 21 | ind = int(index)
|
|---|
| 22 | return tabindex(widget, index)
|
|---|
| 23 | except:
|
|---|
| 24 | return mark_safe(widget)
|
|---|
| 25 | tabindex.is_safe = True
|
|---|