• File: clean.cpython-37.pyc
  • Full Path: /home3/farmfre2/public_html/ALFA_DATA/alfasymlink/root/opt/alt/python37/lib64/python3.7/site-packages/lxml/html/__pycache__/clean.cpython-37.pyc
  • Date Modified: 04/24/2023 6:38 PM
  • File size: 16.53 KB
  • MIME-type: text/x-bytecode.python
  • Charset: 8 bit
B

o] i@sdZddlmZddlZddlZyddlmZddlmZWn$e	k
r`ddl
mZmZYnXddlmZddl
mZdd	l
mZmZdd
l
mZmZyeWnek
reZYnXyeWnek
reZYnXyeWnek
reefZYnXddd
ddddgZedejejBZedejZedejj Z!edejj Z"ddZ#edj$Z%edejejBZ&e'dZ(ej'ddeidZ)Gdd
d
e*Z+e+Z,e,j-Z-edejed ejgZ.d!d"d#d$d%d&gZ/ed'ejed(ejed)gZ0d*gZ1e.e/e0e1fd+dZ2d,d-Z3d.dZ4e2je4_d"d!d#gZ5d/gZ6d0e5e6ed1fd2dZ7d3dZ8d4d5Z9ed6ejZ:d7d8Z;dS)9zcA cleanup tool for HTML.

Removes unwanted tags and content.  See the `Cleaner` class for
details.
)absolute_importN)urlsplit)unquote_plus)rr)etree)defs)
fromstringXHTML_NAMESPACE)
xhtml_to_html_transform_result
clean_htmlcleanCleanerautolink
autolink_html
word_breakword_break_htmlzexpression\s*\(.*?\)z
@\s*importz^data:image/.+;base64z<(?:javascript|jscript|livescript|vbscript|data|about|mocha):cCst|rdSt|S)N)_is_image_dataurl_is_possibly_malicious_scheme)srB/opt/alt/python37/lib64/python3.7/site-packages/lxml/html/clean.py_is_javascript_schemeNsrz[\s\x00-\x08\x0B\x0C\x0E-\x19]+z\[if[\s\n\r]+.*?][\s\n\r]*>zdescendant-or-self::*[@style]zdescendant-or-self::a  [normalize-space(@href) and substring(normalize-space(@href),1,1) != '#'] |descendant-or-self::x:a[normalize-space(@href) and substring(normalize-space(@href),1,1) != '#']x)Z
namespacesc	@seZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdZdZdZdZdZdZdZejZdZdZddhZdd	Zed
ddd
gd
d
d
ddZddZddZddZ ddZ!ddZ"d"ddZ#ddZ$e%&de%j'j(Z)ddZ*d d!Z+dS)#r
a
    Instances cleans the document of each of the possible offending
    elements.  The cleaning is controlled by attributes; you can
    override attributes in a subclass, or set them in the constructor.

    ``scripts``:
        Removes any ``<script>`` tags.

    ``javascript``:
        Removes any Javascript, like an ``onclick`` attribute. Also removes stylesheets
        as they could contain Javascript.

    ``comments``:
        Removes any comments.

    ``style``:
        Removes any style tags.

    ``inline_style``
        Removes any style attributes.  Defaults to the value of the ``style`` option.

    ``links``:
        Removes any ``<link>`` tags

    ``meta``:
        Removes any ``<meta>`` tags

    ``page_structure``:
        Structural parts of a page: ``<head>``, ``<html>``, ``<title>``.

    ``processing_instructions``:
        Removes any processing instructions.

    ``embedded``:
        Removes any embedded objects (flash, iframes)

    ``frames``:
        Removes any frame-related tags

    ``forms``:
        Removes any form tags

    ``annoying_tags``:
        Tags that aren't *wrong*, but are annoying.  ``<blink>`` and ``<marquee>``

    ``remove_tags``:
        A list of tags to remove.  Only the tags will be removed,
        their content will get pulled up into the parent tag.

    ``kill_tags``:
        A list of tags to kill.  Killing also removes the tag's content,
        i.e. the whole subtree, not just the tag itself.

    ``allow_tags``:
        A list of tags to include (default include all).

    ``remove_unknown_tags``:
        Remove any tags that aren't standard parts of HTML.

    ``safe_attrs_only``:
        If true, only include 'safe' attributes (specifically the list
        from the feedparser HTML sanitisation web site).

    ``safe_attrs``:
        A set of attribute names to override the default list of attributes
        considered 'safe' (when safe_attrs_only=True).

    ``add_nofollow``:
        If true, then any <a> tags will have ``rel="nofollow"`` added to them.

    ``host_whitelist``:
        A list or set of hosts that you can use for embedded content
        (for content like ``<object>``, ``<link rel="stylesheet">``, etc).
        You can also implement/override the method
        ``allow_embedded_url(el, url)`` or ``allow_element(el)`` to
        implement more complex rules for what can be embedded.
        Anything that passes this test will be shown, regardless of
        the value of (for instance) ``embedded``.

        Note that this parameter might not work as intended if you do not
        make the links absolute before doing the cleaning.

        Note that you may also need to set ``whitelist_tags``.

    ``whitelist_tags``:
        A set of tags that can be included with ``host_whitelist``.
        The default is ``iframe`` and ``embed``; you may wish to
        include other tags like ``script``, or you may want to
        implement ``allow_embedded_url`` for more control.  Set to None to
        include all tags.

    This modifies the document *in place*.
    TFNriframeembedcKsZx:|D].\}}t||s,td||ft|||q
W|jdkrVd|krV|j|_dS)NzUnknown parameter: %s=%rinline_style)itemshasattr	TypeErrorsetattrrstyle)selfkwnamevaluerrr__init__s
zCleaner.__init__srchrefcodeobject)scriptlinkappletrrlayeracCst|dr|}t|x|dD]
}d|_q&W|jsD||t|jpNd}t|j	p\d}t|j
pjd}|jr~|d|j
rt|j}x:|tjD]*}|j}x|D]}||kr||=qWqW|jr2|j
r|jtjks(x@|tjD]0}|j}x$|D]}|dr||=qWqW|j|jdd|jsx`t|D]T}|d	}	td
|	}
td
|
}
||
r|jd	=n|
|	krJ|d	|
qJW|js2xt |d	D]t}|dd
!"dkr|#q|j$pd
}	td
|	}
td
|	}
||
rd
|_$n|
|	kr|
|_$qW|jsB|j%rN|tj&|j%rb|tj'|jrt|d	|jrt(|d	|j)r|dnT|js|jrxBt |dD]0}d|dd
!kr|*|s|#qW|j+r|d|j,r|-d|j.rx\t |dD]J}d}|/}x$|dk	r`|jdkr`|/}q>W|dkr,|#q,W|-d|-d|j0r|-tj1|j2r|d|-d|j3r|-dg}
g}x`|D]T}|j|kr|*|rq|4|n&|j|kr|*|r*q|
4|qW|
rj|
d|krj|
5d}d|_|j6n8|r|d|kr|5d}|jdkrd|_|6|7x|D]}|#qWx|
D]}|8qW|j9r|rt:dttj;}|rtg}x(|D]}|j|kr|4|qW|rt|d|kr\|5d}d|_|j6x|D]}|8qbW|j<rxdt=|D]X}|>|s|d}|rd|krd d!|krqd"|}nd}|d|qWdS)#z&
        Cleans the document.
        getrootZimageZimgrr*ZonF)Zresolve_base_hrefr typeztext/javascriptz
/* deleted */r+Z
stylesheetrelmeta)headhtmltitleparamN)r,r))r,)rrr-r)r7Zform)Zbuttoninputselecttextarea)ZblinkZmarqueerZdivr5zIIt does not make sense to pass in both allow_tags and remove_unknown_tagsZnofollowz
 nofollow z %s z%s nofollow)?rr/r	itertagcommentskill_conditional_commentsset	kill_tagsremove_tags
allow_tagsscriptsaddsafe_attrs_only
safe_attrsrZElementattribkeys
javascriptr
startswithZ
rewrite_links_remove_javascript_linkr_find_styled_elementsget_css_javascript_resub_css_import_re_has_sneaky_javascriptr listlowerstrip	drop_treetextprocessing_instructionsCommentZProcessingInstructionZstrip_attributeslinks
allow_elementr3page_structureupdateembeddedZ	getparentframesZ
frame_tagsforms
annoying_tagsappendpopclearreverseZdrop_tagremove_unknown_tags
ValueErrorZtagsadd_nofollow_find_external_linksallow_follow)r!docelr@rArBrFrGZanameoldnewZfound_parentparentZ_removeZ_killbadr2rrr__call__s




























zCleaner.__call__cCsdS)zF
        Override to suppress rel="nofollow" on some anchors.
        Fr)r!anchorrrrriszCleaner.allow_followcCs|j|jkrdS|j|j}t|ttfr^x.|D]&}||}|sFdS|||s0dSq0WdS||}|spdS|||SdS)z
        Decide whether an element is configured to be accepted or rejected.

        :param el: an element.
        :return: true to accept the element or false to reject/discard it.
        FTN)r<_tag_link_attrs
isinstancerRtuplerMallow_embedded_url)r!rkattrZone_attrurlrrrrZs


zCleaner.allow_elementcCs^|jdk	r|j|jkrdSt|\}}}}}|ddd}|dkrLdS||jkrZdSdS)a
        Decide whether a URL that was found in an element's attributes or text
        if configured to be accepted or rejected.

        :param el: an element.
        :param url: a URL found on the element.
        :return: true to accept the URL and false to reject it.
        NF:r)ZhttpZhttpsT)whitelist_tagsr<rrSsplithost_whitelist)r!rkrwZschemeZnetlocpathZqueryZfragmentrrrrus	
zCleaner.allow_embedded_urlcCsg}||ddtjdS)z
        IE conditional comments basically embed HTML that the parser
        doesn't normally see.  We can't allow anything like that, so
        we'll kill any comments that could be conditional.
        cSst|jS)N)_conditional_comment_researchrV)rkrrr<lambda>z3Cleaner.kill_conditional_comments.<locals>.<lambda>N)_kill_elementsrrX)r!rjrorrrr>sz!Cleaner.kill_conditional_commentscCsDg}x$||D]}||r||qWx|D]}|q0WdS)N)r;rarU)r!rjZ	conditionZiteraterorkrrrrs
zCleaner._kill_elementscCstdt|}t|rdS|S)Nr0)_substitute_whitespacerr)r!r+rmrrrrKszCleaner._remove_javascript_linkz	/\*.*?\*/cCsF|d|}|dd}td|}|}d|kr6dSd|krBdSdS)a
        Depending on the browser, stuff like ``e x p r e s s i o n(...)``
        can get interpreted, or ``expre/* stuff */ssion(...)``.  This
        checks for attempt to do stuff like this.

        Typically the response will be to kill the entire style; if you
        have just a bit of Javascript in the style another rule will catch
        that and remove only the Javascript from the style; this catches
        more sneaky attempts.
        r0\zjavascript:Tzexpression(F)_substitute_commentsreplacerrS)r!r rrrrQs
zCleaner._has_sneaky_javascriptcCs8t|}t|trt|}n
t|}||t||S)N)r1rs
basestringrcopydeepcopyr
)r!r5result_typerjrrrrs


zCleaner.clean_html)N),__name__
__module____qualname____doc__rCrIr=r rrYr3r[rWr]r^r_r`rArBr@rerErrFrgr|rzr%dictrrrprirZrur>rrKrecompileSrOrrQrrrrrr
csV]	5
zb(?P<body>https?://(?P<host>[a-z0-9._-]+)(?:/[/\-_.,a-z0-9%&?;=~]*)?(?:\([/\-_.,a-z0-9%&?;=~]*\))?)z9mailto:(?P<body>[a-z0-9._-]+@(?P<host>[a-z0-9_.-]+[a-z]))r:Zprer(r4r9r.z
^localhostz\bexample\.(?:com|org|net)$z^127\.0\.0\.1$ZnolinkcCs|j|krdS|d}|r>|}x|D]}||kr*dSq*Wxht|D]\}t|||||d|jrHt|j|||jd\}}	|	rH||_||}
|	||
d|
d<qHW|j	rt|j	|||jd\}}|r||_	||dd<dS)a
    Turn any URLs into links.

    It will search for links identified by the given regular
    expressions (by default mailto and http(s) links).

    It won't link text in an element in avoid_elements, or an element
    with a class in avoid_classes.  It won't link to anything with a
    host that matches one of the regular expressions in avoid_hosts
    (default localhost and 127.0.0.1).

    If you pass in an element, the element's tail will not be
    substituted, only the contents of the element.
    Nclass)link_regexesavoid_elementsavoid_hosts
avoid_classes)factoryryr)
r<rMr{rRrtail
_link_textZmakeelementindexrV)rkrrrr
class_nameZmatch_classchildrVZ
tail_childrenrZpre_childrenrrrr/s4



cCsd}g}d}xd\}}x|D]~}	|}
xJ|	j||
d}|dkr@P|d}x"|D]}
|
|rP|}
PqPWPq(W|dkr|q|dks||kr|}|}qW|dkr|r|djrt||d_n|rt|}P|d}|}|ds|dr|d	8}|dd}|d|}|rB|djr6t||d_n|rLt|}|d
}|d||d}|sx|}|ds|dr|dd}||_|	|||d}qW||fS)
Nr0r)NN)poshost.,ryr.r'body)
rgroupendstartrAssertionErrorendswithr?rVra)rVrrrZleading_textrYZlast_posZ
best_matchZbest_posZregexZ	regex_posmatchrZ
host_regexr+rZ	prev_textrqrrrrr\sf







rcOs@t|}t|trt|}n
t|}t|f||t||S)N)r1rsrrrrrr
)r5argsr"rrjrrrrs


Znobreak(i c	Cs|jtkrdS|d}|rLd}|}x|D]}||kr.d}Pq.W|rLdS|jrbt|j|||_x4|D],}t|||||d|jrht|j|||_qhWdS)a
    Breaks any long words found in the body of the text (not attributes).

    Doesn't effect any of the tags in avoid_elements, by default
    ``<textarea>`` and ``<pre>``

    Breaks words by inserting &#8203;, which is a unicode character
    for Zero Width Space character.  This generally takes up no space
    in rendering, but does copy as a space, and in monospace contexts
    usually takes up space.

    See http://www.cs.tut.fi/~jkorpela/html/nobr.html for a discussion
    NrFT)	max_widthrrbreak_character)r<_avoid_word_break_elementsrMr{rV_break_textrr)	rkrrrrrZ
dont_breakZavoidrrrrrs*



cOs*t|}t|}t|f||t||S)N)r1rrr
)r5rr"rrjrrrrscCs>|}x0|D](}t||krt|||}|||}qW|S)N)r{len
_insert_breakr)rVrrZwordswordZreplacementrrrrs
rz[^a-z]cCs|}d}xrt||krz|d|}tt|}|r\|d}||dkr\|d|}|||7}|t|d}q
W||7}|S)Nr0r
)rrR_break_prefer_refinditerr)rwidthrZ	orig_wordresultrZbreaksZ
last_breakrrrrsr)<rZ
__future__rrrZurlparserZurllibrImportErrorZurllib.parseZlxmlrZ	lxml.htmlrrrr	r
Zunichr	NameErrorchrZunicodestrrbytes__all__rrIrNrPrrrrrOrr~ZXPathrLrhr)r
rrZ
_link_regexesZ_avoid_elementsZ_avoid_hostsZ_avoid_classesrrrrZ_avoid_word_break_classesrrrrrrrrr<module>s




8*:	
&