src/cm/utils/html.py
author Simon Descarpentries <sid@sopinspace.com>
Mon, 21 Oct 2013 16:37:07 +0200
changeset 553 bf26fb47a14c
parent 175 4f072edc51a1
permissions -rw-r--r--
To allow scrolling in Safari mobile, we set the content of text_view_comments frame in a jQuery UI layout. So the automated scrolling operations in c_sync.js must be adjustable to the right part to scroll. Also, if a comment have to be shown outside of the current viewport, we scroll the correct part to that viewport and then set the comment top Y offset to juste what it needs to avoid the "Add comment" button after scrolling operation. If not in Safari mobile, we add an offset here to avoid comment to display under the "Add comment" button.

"""
Package to manipulage html chunks
"""

from string_utils import strip_cr
from BeautifulSoup import BeautifulSoup, Comment

def surrond_text_node(html_chunk, start_html, end_html):
    """
    Surround text nodes in html_chunk
    """
    soup = BeautifulSoup(html_chunk)
    text_nodes = get_text_nodes(soup)
    for text_node in text_nodes:        
        if text_node.string.strip():
            text_node.replaceWith(start_html + text_node.string + end_html)
    return unicode(soup)
    

#utilities    
def get_text_nodes(soup):
    return soup(text=lambda text:not isinstance(text, Comment))

#WARNING behavior changed also for mardown. but really shouldn't hurt 20100212
#it is text as received from textarea
def on_content_receive(txt, format) :
    #because textarea content arent packaged the same way on windows IE and linux FF, dhouldn't't hurt to clean it for any format
    return strip_cr(txt)