Skip to content

Commit 7f60326

Browse files
committed
Replace parser with string matching
1 parent a0cfd7b commit 7f60326

File tree

6 files changed

+46
-82
lines changed

6 files changed

+46
-82
lines changed

examples/bootstrap4/app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
bootstrap = Bootstrap(app)
1010
login = LoginManager(app)
11-
login.login_view = 'login'
11+
login.login_view = 'index'
1212
modal = Modal(app)
1313

1414

flask_modals/modal.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from flask import (Blueprint, render_template, get_flashed_messages,
44
_app_ctx_stack, request)
55
from jinja2 import Markup
6-
from flask_modals.parser import ModalParser
6+
from flask_modals.partial import get_partial
77

88

99
def modal_messages():
@@ -28,23 +28,16 @@ def render_template_modal(*args, **kwargs):
2828
# prevent flash messages from showing both outside and
2929
# inside the modal
3030
ctx._modal = True
31-
html = render_template(*args, **kwargs)
32-
parser = ModalParser(html, modal)
33-
parser.feed(html)
34-
return f'<template>{parser.stream}</template>'
31+
partial = get_partial(modal, *args, **kwargs)
32+
return f'<template>{partial}</template>'
3533
else:
3634
return render_template(*args, **kwargs)
3735

3836

3937
def can_stream():
4038
'''Returns `True` if the client accepts streams.'''
4139

42-
stream_mimetype = 'text/modal-stream.html'
43-
if stream_mimetype not in request.accept_mimetypes.values():
44-
return False
45-
best = request.accept_mimetypes.best_match([
46-
stream_mimetype, 'text/html'])
47-
return best == stream_mimetype
40+
return 'text/modal-stream.html' in request.accept_mimetypes.values()
4841

4942

5043
def response(template=None):
@@ -107,7 +100,7 @@ def show_flashed_messages(*args, **kwargs):
107100

108101
return get_flashed_messages(*args, **kwargs)
109102

110-
def load(self, url=None):
103+
def load(self):
111104
'''Load the following markup:
112105
113106
1. nprogress.html - NProgress js library for progress bar

flask_modals/parser.py

Lines changed: 0 additions & 64 deletions
This file was deleted.

flask_modals/partial.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import re
2+
3+
from flask import render_template
4+
5+
6+
def get_partial(modal, *args, **kwargs):
7+
'''Return the modal body.'''
8+
9+
html = render_template(*args, **kwargs)
10+
lines = html.splitlines(keepends=True)
11+
12+
found_modal = False
13+
found_modal_body = False
14+
div_count = 0
15+
partial = ''
16+
for line in lines:
17+
if f'id="{modal}"' in line:
18+
found_modal = True
19+
if found_modal:
20+
if 'class="modal-body' in line:
21+
found_modal_body = True
22+
if found_modal_body:
23+
partial += line
24+
startdivs = re.findall(r'<div.*?>', line, re.IGNORECASE)
25+
enddivs = re.findall(r'</div>', line, re.IGNORECASE)
26+
div_count += len(startdivs) - len(enddivs)
27+
if div_count <= 0:
28+
break
29+
return partial

flask_modals/static/js/main.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@
66
if (modalBodyEl) {
77
el.addEventListener('submit', e => {
88
e.preventDefault()
9-
fetchData(el, modalBodyEl)
9+
fetchData(el, modalBodyEl, e.submitter)
1010
})
1111
}
1212
})
1313

14-
function fetchData(el, modalBodyEl) {
14+
function fetchData(el, modalBodyEl, submitter) {
1515
let url
16+
const body = new FormData(el)
17+
if (submitter) {
18+
const name = submitter.getAttribute('name')
19+
const value = submitter.getAttribute('value')
20+
body.append(name, value)
21+
}
1622
NProgress.start()
1723
fetch(el.action, {
1824
method: el.method,
19-
body: new FormData(el),
25+
body: body,
2026
headers: {
2127
Accept: 'text/modal-stream.html'
2228
}
@@ -40,7 +46,7 @@
4046
const el = modalBodyEl.querySelector('form')
4147
el.addEventListener('submit', e => {
4248
e.preventDefault()
43-
fetchData(el, modalBodyEl)
49+
fetchData(el, modalBodyEl, e.submitter)
4450
})
4551
} else {
4652
if (location.href !== url) {

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='Flask-Modals',
8-
version='0.4.0',
8+
version='0.4.1',
99
author='Debashish Palit',
1010
author_email='[email protected]',
1111
description='Use forms in Bootstrap modals with Flask.',

0 commit comments

Comments
 (0)