Skip to content

Commit

Permalink
Warn about bad jQuery selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
laffra committed Apr 24, 2024
1 parent 6ced56f commit fe3dece
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
34 changes: 32 additions & 2 deletions ltk/jquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,43 @@ class MonkeyPatchedTimeModuleForMicroPython:


jQuery = window.jQuery
find = jQuery
create = jQuery
parse_int = window.parseInt
parse_float = window.parseFloat
local_storage = window.localStorage
timers = {}


def find(selector):
is_element = not isinstance(selector, str)
is_body = selector == "body"
is_selector = isinstance(selector, str) and (selector.startswith(".") or selector.startswith("#"))
is_html = isinstance(selector, str) and selector.startswith("<")
if not is_element and not is_body and not is_selector:
try:
error = f"A jQuery selector should start with '.' or '#', not '{selector}'"
if is_html:
error += ". To generate HTML, use ltk.create(...)"
print(f"Error: {error}")
import traceback
traceback.print_stack()
except:
pass
return jQuery(selector)


def create(html):
html = html.strip()
if not html or html[0] != "<":
try:
error = f"A jQuery html fragment should start with '<', not '{html}'"
print(f"Error: {error}")
import traceback
traceback.print_stack()
except:
pass
return jQuery(html)


KB = 1024
MB = KB * KB
GB = MB * MB
Expand Down
4 changes: 4 additions & 0 deletions ltk/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Logger(ltk.Div):
classes = [ "ltk-log-list" ]
level = logging.INFO
messages = []
callback_count = 0
icons = {
logging.CRITICAL : '💥',
logging.ERROR : '🔥️',
Expand Down Expand Up @@ -123,6 +124,9 @@ def _add(self, level, *args, **argv):
try:
message = " ".join(map(str, args))
if message.startswith("js_callable_proxy"):
self.callback_count += 1
if self.callback_count % 1000 == 0:
print(f"{self.callback_count} Python functions called by jQuery: {message}")
return
self.messages.append(message)
ltk.find(".ltk-log-header").after(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyscript-ltk"
version = "0.1.28"
version = "0.1.29"
description = "A little toolkit for writing UIs in PyScript"
readme = "README.md"
authors = [{ name = "Chris Laffra", email = "chris@chrislaffra.com" }]
Expand Down

0 comments on commit fe3dece

Please sign in to comment.