Current File : //proc/2/root/usr/lib64/python2.7/site-packages/lxml/html/clean.pyo
�
�m}Qc@s3dZddlZddlZyddlmZWn!ek
rUddlmZnXddlmZddl m
Z
ddl mZmZm
Z
ddl mZmZyeWnek
r�eZnXyeWnek
r�eZnXyeWnek
r
eZnXyeWnek
r5eefZnXdd d
ddd
dgZejdejejB�Zejdej�Zejdej�Zejd�jZejdejejB�Z ej!d�Z"ej!ddie
d6�Z#d
e$fd��YZ%e%�Z&e&j'Z'ejdej�ejdej�gZ(dddddd gZ)ejd!ej�ejd"ej�ejd#�gZ*d$gZ+e(e)e*e+d%�Z,d&�Z-d'�Z.e,je._dddgZ/d(gZ0d)e/e0ed*�d+�Z1d,�Z2d-�Z3ejd.ej�Z4d/�Z5dS(0scA cleanup tool for HTML.
Removes unwanted tags and content. See the `Cleaner` class for
details.
i����N(turlsplit(tetree(tdefs(t
fromstringttostringtXHTML_NAMESPACE(t
xhtml_to_htmlt_transform_resultt
clean_htmltcleantCleanertautolinkt
autolink_htmlt
word_breaktword_break_htmlsexpression\s*\(.*?\)s
@\s*imports?\s*(?:javascript|jscript|livescript|vbscript|data|about|mocha):s\s+s\[if[\s\n\r]+.*?][\s\n\r]*>sdescendant-or-self::*[@style]s�descendant-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) != '#']t
namespacestxcBsIeZdZeZeZeZeZeZ eZ
eZeZeZ
eZeZeZdZdZdZeZeZejZeZdZeddg�Zd�Zedddddd d
gdddddddd�Zd
�Z d�Z!d�Z"d�Z#d�Z$dd�Z%d�Z&e'j(de'j)�j*Z+d�Z,d�Z-RS(s�
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 or attributes.
``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*.
tiframetembedcKsYxR|j�D]D\}}t||�sAtd||f��nt|||�q
WdS(NsUnknown parameter: %s=%r(titemsthasattrt TypeErrortsetattr(tselftkwtnametvalue((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyt__init__�s
tscripttsrctlinkthreftapplettcodetobjecttlayertac
Cs�t|d�r|j�}nt|�x |jd�D]}d|_q8W|jsd|j|�nt|jpsd(�}t|j p�d)�}t|j
p�d*�}|jr�|jd�n|j
r$t|j�}xM|j�D]<}|j}x*|j�D]}||kr�||=q�q�Wq�Wn|jr�|j
oE|jtjks�xP|j�D]?}|j}x-|j�D]}|jd�rq||=qqqqWqUWn|j|jdt�|js�x�t|�D]t}|jd�} tjd| �}
tjd|
�}
|j|
�r|jd=q�|
| kr�|jd|
�q�q�Wx�t|jd��D]�}|jd d�j�j �d
kr�|j!�qUn|j"p�d} tjd| �}
tjd| �}
|j|
�r�d|_"qU|
| krU|
|_"qUqUWq�n|js|j#r$|jt$j%�n|j#r@|jt$j&�n|jri|jd�t$j'|d�n|j(r�|jd�nr|js�|jr�x]t|jd��D]C}d
|jdd�j�kr�|j)|�s�|j!�q�q�q�Wn|j*r
|jd�n|j+r&|j,d+�n|j-r�xvt|jd��D]_}t}|j.�}x+|d'k r�|jd,kr�|j.�}q`W|d'krE|j!�qEqEW|j,d-�|j,d.�n|j0r�|j,tj1�n|j2r|jd�|j,d/�n|j3r |j,d0�ng}
g}x||j�D]n}|j|krs|j)|�rcq9n|j4|�q9|j|kr9|j)|�r�q9n|
j4|�q9q9W|
r�|
d |kr�|
j5d �}d!|_|jj6�nM|r6|d |kr6|j5d �}|jdkr)d!|_n|j6�n|j7�x|D]}|j!�qGWx|
D]}|j8�qbW|j
}|j9r�|r�t:d"��nttj;�}n|rPg}x3|j�D]%}|j|kr�|j4|�q�q�W|rP|d |kr/|j5d �}d!|_|jj6�nx|D]}|j8�q6WqPn|j<r�x�t=|�D]r}|j>|�sf|jd�}|r�d#|kr�d$d%|kr�qfnd&|}nd#}|jd|�qfqfWnd'S(1s&
Cleans the document.
tgetroottimagetimgRtontresolve_base_hreftstyletttypestext/javascripts
/* deleted */Rt
stylesheettreltmetatheadthtmlttitletparamR R"RRR#tformtbuttontinputtselectttextareatblinktmarqueeitdivsIIt does not make sense to pass in both allow_tags and remove_unknown_tagstnofollows
nofollow s %s s%s nofollowN((((sheadshtmlstitle(sappletsobject(sapplet(siframesembedslayersobjectR3(R5sinputR7R8(R9R:(?RR%Rtiterttagtcommentstkill_conditional_commentstsett kill_tagstremove_tagst
allow_tagstscriptstaddtsafe_attrs_onlyt
safe_attrstattribtkeyst
javascriptRt
startswitht
rewrite_linkst_remove_javascript_linktFalseR*t_find_styled_elementstgett_css_javascript_retsubt_css_import_ret_has_sneaky_javascripttlisttlowertstript drop_treettexttprocessing_instructionsRtCommenttProcessingInstructiontstrip_attributestlinkst
allow_elementR/tpage_structuretupdatetembeddedt getparenttNonetframest
frame_tagstformst
annoying_tagstappendtpoptcleartreversetdrop_tagtremove_unknown_tagst
ValueErrorttagstadd_nofollowt_find_external_linkstallow_follow(RtdoctelRBRCRDRHRItanametoldtnewtfound_parenttparentt_removet_killtbadR.((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyt__call__�s
$
cCstS(sF
Override to suppress rel="nofollow" on some anchors.
(RO(Rtanchor((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyRt�scCs�|j|jkrtS|j|j}t|ttf�rx=|D]5}|j|�}|satS|j||�sBtSqBWtS|j|�}|s�tS|j||�SdS(N( R>t_tag_link_attrsROt
isinstanceRVttupleRQtallow_embedded_urltTrue(RRvtattrtone_attrturl((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR`�s
cCs�|jdk r%|j|jkr%tSt|�\}}}}}|j�jdd�d}|dkrltS||jkrtStS(Nt:iithttpthttps(R�R�( twhitelist_tagsReR>RORRWtsplitthost_whitelistR�(RRvR�tschemetnetloctpathtquerytfragment((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR��scCs#g}|j|d�tj�dS(s�
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.
cSstj|j�S(N(t_conditional_comment_retsearchRZ(Rv((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyt<lambda>�sN(t_kill_elementsRR\(RRuR~((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR@�s cCs[g}x3|j|�D]"}||�r|j|�qqWx|D]}|j�qCWdS(N(R=RjRY(RRut conditiontiterateR~Rv((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR��s
cCs&td|�}tj|�r"dS|S(NR+(t_substitute_whitespacet_javascript_scheme_reR�(RRRy((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyRN�ss /\*.*?\*/cCsc|jd|�}|jdd�}td|�}|j�}d|krOtSd|kr_tStS(s�
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.
R+s\sjavascript:sexpression((t_substitute_commentstreplaceR�RWR�RO(RR*((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyRU�scCsPt|�}t|t�r*t|�}ntj|�}||�t||�S(N(R,R�t
basestringRtcopytdeepcopyR(RR1tresult_typeRu((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR�s
N((.t__name__t
__module__t__doc__R�RERKR?ROR*R_R/RaR[RcRfRhRiReRCRDRBRoRGRRHRrR�RAR�RtdictR�RRtR`R�R@R�RNtretcompiletSRSR�RUR(((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR
ZsRZ � sb(?P<body>https?://(?P<host>[a-z0-9._-]+)(?:/[/\-_.,a-z0-9%&?;=~]*)?(?:\([/\-_.,a-z0-9%&?;=~]*\))?)s9mailto:(?P<body>[a-z0-9._-]+@(?P<host>[a-z0-9_._]+[a-z]))R8tpreR!R0R7R$s
^localhosts\bexample\.(?:com|org|net)$s^127\.0\.0\.1$tnolinkcCsG|j|krdS|jd�}|rX|j�}x!|D]}||kr;dSq;Wnx�t|�D]�}t|d|d|d|d|�|jret|j||d|j�\}} | r�||_|j|�}
| ||
d|
d+q�qeqeW|j rCt|j ||d|j�\}}|rC||_ ||d *qCndS(
s
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.
Ntclasstlink_regexestavoid_elementstavoid_hostst
avoid_classestfactoryii(
R>RQR�RVRttailt
_link_texttmakeelementtindexRZ(RvR�R�R�R�t
class_nametmatch_classtchildRZt
tail_childrenR�tpre_children((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyRs4
! ! cCs"d}g}d}xd\}}x�|D]�} |}
xh| j|d|
�}|dkr\Pn|jd�}x.|D]%}
|
j|�rr|j�}
PqrqrWPq7|dkr�q(n|dks�|j�|kr(|}|j�}q(q(W|dkr|r
||d_n|}Pn|jd�}|j�}|jd�sM|jd�rd|d8}|d }n||j� }|r�||d_n|}|d �}|jd
|�|jd�}|s�|}n|jd�s�|jd�r�|d }n||_|j |�||}q||fS(
NR+itposthosti����t.t,iR$Rtbody(NN(
ReR�tgrouptendtstartR�tendswithRARZRj(RZR�R�R�tleading_textR_tlast_post
best_matchtbest_postregext regex_postmatchR�t
host_regexRR�t prev_textR�R�((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR�?s^
cOsVt|�}t|t�r*t|�}ntj|�}t|||�t||�S(N(R,R�R�RR�R�RR(R1targsRR�Ru((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyRystnobreaki(i c Cs�|jtkrdS|jd�}|rnt}|j�}x$|D]}||krAt}PqAqAW|rndSn|jr�t|j||�|_nxT|D]L}t|d|d|d|d|�|j r�t|j ||�|_ q�q�WdS(s�
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 ​, 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
NR�t max_widthR�R�tbreak_character(
R>t_avoid_word_break_elementsRQROR�R�RZt_break_textR
R�( RvR�R�R�R�R�t
dont_breaktavoidR�((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR
�s*
cOs5t|�}t|�}t|||�t||�S(N(R,RR
R(R1R�RR�Ru((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR�scCsZ|j�}xG|D]?}t|�|krt|||�}|j||�}qqW|S(N(R�tlent
_insert_breakR�(RZR�R�twordstwordtreplacement((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR��s
s[^a-z]cCs�|}d}x�t|�|kr�|| }ttj|��}|r||d}|j�|dkr|||j� }q|n|||7}|t|�}qW||7}|S(NR+i����i
(R�RVt_break_prefer_retfinditerR�(R�twidthR�t orig_wordtresultR�tbreakst
last_break((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyR��s
(6R�R�R�turlparseRtImportErrorturllib.parsetlxmlRt lxml.htmlRRRRRRtunichrt NameErrortchrtunicodetstrtbytesR�t__all__R�R�tIRRRTR�RSR�R�tXPathRPRsR"R
R Rt
_link_regexest_avoid_elementst_avoid_hostst_avoid_classesRR�RR�t_avoid_word_break_classesR
RR�R�R�(((s5/usr/lib64/python2.7/site-packages/lxml/html/clean.pyt<module>s�
�� * : &