view libervia/web/pages/_browser/tools.py @ 1637:29dd52585984 default tip

tests (browser/unit/chat): Add tests for forwarding, rich editing and extra recipients: fix 461
author Goffi <goffi@goffi.org>
date Fri, 04 Jul 2025 18:15:48 +0200
parents 698eaabfca0e
children
line wrap: on
line source

"""Common useful tools"""
from browser import window, html


def is_touch_device():
    return hasattr(window, 'ontouchstart')


def remove_ids(element):
    """Recursively remove "id" attribute of element and all its descendants."""
    element.removeAttribute('id')

    for child in element.children:
        remove_ids(child)


def make_placeholder(original_elt, placeholder_class="visual-placeholder"):
    """Create a visual placeholder that matches an element's appearance.

    The placeholder will be a clone of the original element but without any ID attributes
    to prevent DOM conflicts.

    @param original_elt: Element to create placeholder for.
    @param placeholder_class: CSS class to add to placeholder element.
    @return: Placeholder element.
    """
    placeholder = original_elt.cloneNode(True)
    remove_ids(placeholder)
    placeholder.classList.add(placeholder_class)
    placeholder.attrs["aria-hidden"] = "true"
    placeholder.attrs["data-placeholder-for"] = original_elt.id or ""
    return placeholder