bgYwddlmZddlZddlmZddlmZddlZddgZ ddl m Z n#e $r ddl m Z YnwxYw eZn #e$reZYnwxYw en #e$reZYnwxYwdZefd Zd Zd Zd Zd ZdZdZdZdZdGdZdZGddZGddZ Gdde!Z"dZ#dZ$dZ%dZ&dZ'd Z(Gd!d"eZ)Gd#d$e)Z*Gd%d&e)Z+dHd(Z,dHd)Z-ej.d*ej/ej0zZ1ej.d+ej/ej0zZ2ej.d,ej/ej0zZ3d-Z4ej.d.Z5d/Z6d0Z7d1Z8d2Z9d3Z:dGd4Z;ej.d5ej<Z=d6Z>ej.d7Z?d8Z@d9ZAd:ZBd;ZCd<ZDd=ZEdGd>ZFd?ZGd@ZHdAZIdBZJGdCdDejKZLeMdEkrddFlmNZNeNjOdSdS)I)absolute_importN)etree)fragment_fromstring html_annotatehtmldiff)escapecJdtt|dd|dS)Nz z) html_escape_unicode)textversions a/builddir/build/BUILD/cloudlinux-venv-1.0.7/venv/lib64/python3.11/site-packages/lxml/html/diff.pydefault_markuprs/HW%%q))))444 11cd|D}|d}|ddD]}t|||}t|}t||}d|S)a doclist should be ordered from oldest to newest, like:: >>> version1 = 'Hello World' >>> version2 = 'Goodbye World' >>> print(html_annotate([(version1, 'version 1'), ... (version2, 'version 2')])) Goodbye World The documents must be *fragments* (str/UTF8 or unicode), not complete documents The markup argument is a function to markup the spans of words. This function is called like markup('Hello', 'version 2'), and returns HTML. The first argument is text and never includes any markup. The default uses a span with a title: >>> print(default_markup('Some Text', 'by Joe')) Some Text c4g|]\}}t||S)tokenize_annotated).0docrs r z!html_annotate..=s6...!S'$C11...rrr N)html_annotate_merge_annotationscompress_tokensmarkup_serialize_tokensjoinstrip)doclistmarkup tokenlist cur_tokenstokensresults rrr#s4..%,...I1JABB-' F;;; !,,J $Z 8 8F 776?? " ""rc@t|d}|D] }||_ |S)zFTokenize a document and add an annotation attribute to each token F include_hrefs)tokenize annotation)rr)r#toks rrrKs3c / / /F$$# Mrct||}|}|D]2\}}}}}|dkr$|||} |||} t| | 3dS)zMerge the annotations from tokens_old into tokens_new, when the tokens in the new document already existed in the old document. abequalN)InsensitiveSequenceMatcher get_opcodescopy_annotations) tokens_old tokens_newscommandscommandi1i2j1j2eq_oldeq_news rrrSs #Z:>>>A}}H#+--RR g  2&F2&F VV , , , --rct|t|ksJt||D]\}}|j|_dS)zN Copy annotations from the tokens listed in src to the tokens in dest N)lenzipr))srcdestsrc_tokdest_toks rr2r2`sU s88s4yy  d^^11%011rc|dg}|ddD]R}|djs.|js'|dj|jkrt||=||S|S)zm Combine adjacent tokens when there is no HTML between the tokens, and they share an annotation rr N) post_tagspre_tagsr)compress_merge_backappend)r#r$r*s rrrhs Qi[Fabbzr $    2J !S^ 3 3  , , , , MM#     MrcL|d}t|tust|tur||dSt|}|jr ||jz }||z }t||j|j|j}|j|_||d<dS)zY Merge tok into the last element of tokens (modifying the list of tokens in-place). rFrHrGtrailing_whitespaceN)typetokenrJr rMrHrGr))r#r*lastr mergeds rrIrIws ":D Dzz$s))5"8"8 c~~  # - D, ,D  t $ !$+.+BDDD!Or rc#K|D]X}|jD]}|V|}|||j}|jr ||jz }|V|jD]}|VYdS)zz Serialize the list of tokens into a list of text chunks, calling markup_func around text to add annotations. N)rHhtmlr)rMrG)r# markup_funcrOprerSposts rrrs   >  CIIIIzz||{4!122  $ . E- -D O  DJJJJ   rct|}t|}t||}d|}t |S)a Do a diff of the old and new document. The documents are HTML *fragments* (str/UTF8 or unicode), they are not complete documents (i.e., no tag). Returns HTML with and tags added around the appropriate text. Markup is generally ignored, with the markup from new_html preserved, and possibly some markup from old_html (though it is considered acceptable to lose some of the old markup). Only the words in the HTML are diffed. The exception is tags, which are treated like words, and the href attribute of tags, which are noted inside the tag itself when there are changes. r)r(htmldiff_tokensrrfixup_ins_del_tags)old_htmlnew_htmlold_html_tokensnew_html_tokensr$s rrrsV"x((Ox((O _o > >F WWV__ " " $ $F f % %%rct||}|}g}|D]\}}}}} |dkr-|t||| d;|dks|dkr't||| } t | ||dks|dkr't|||} t | |t |}|S)z] Does a diff on the tokens themselves, returning a list of text chunks (not tokens). r,r/T)r/insertreplacedelete)r0r1extend expand_tokens merge_insert merge_deletecleanup_delete) html1_tokens html2_tokensr5r6r$r7r8r9r:r; ins_tokens del_tokenss rrXrXs" #\\BBBA}}H F#+ - -RR g   MM- RU(;4HHH I I I  h  'Y"6"6&|BrE':;;J V , , , h  'Y"6"6&|BrE':;;J V , , , F # #F MrFc#K|D]c}|jD]}|V|r|js<|jr||jzVn|V|jD]}|VddS)zeGiven a list of tokens, return a generator of the chunks of text for the data in the tokens. N)rHhide_when_equalrMrSrG)r#r/rOrUrVs rrcrcs  >  CIIII #E1 #( #jjllU%>>>>>>jjll"""O  DJJJJ   rct|\}}}|||r+|dds|dxxdz cc<|d|r.|ddr|ddd|d<|||d||dS)z| doc is the already-handled document (as a list of text chunks); here we add ins_chunks to the end of that. rF zNz )split_unbalancedrbendswithrJ) ins_chunksrunbalanced_startbalancedunbalanced_ends rrdrds 2B*1M1M.hJJ    3r7##C(( B3JJw)HRL))#..)|CRC( JJxJJyJJ~rceZdZdS) DEL_STARTN__name__ __module__ __qualname__rrrrvrvDrrvceZdZdS)DEL_ENDNrwrrrr}r}r{rr}ceZdZdZdS) NoDeleteszY Raised when the document no longer contains any pending deletes (DEL_START/DEL_END) N)rxryrz__doc__rrrrrsrrc|t|||tdS)z Adds the text chunks in del_chunks to the document doc (another list of text chunks) with marker to show it is a delete. cleanup_delete later resolves these markers into tags.N)rJrvrbr}) del_chunksrs rrere s@JJyJJzJJwrc* t|\}}}n#t$rYnwxYwt|\}}}t|||t ||||}|r+|dds|dxxdz cc<|d|r.|ddr|ddd|d<|||d|||}|S)a Cleans up any DEL_START/DEL_END markers in the document, replacing them with . To do this while keeping the document valid, it may need to drop some tags (either start or end tags). It may also move the del into adjacent tags to try to move it to a similar location where it was originally located (e.g., moving a delete into preceding
tag, if the del looks like (DEL_START, 'Text
', DEL_END)r rFrnzNz ) split_deleterrolocate_unbalanced_startlocate_unbalanced_endrprJrb)chunks pre_deletera post_deleterrrsrtrs rrfrfsL .:6.B.B +J     E  6Ff5M5M2(N 0*kJJJnj+FFF  s2w'',,  GGGsNGGG 7  - --c22 -#B<,HRL 8 9 ;78 Ms  $$c.g}g}g}g}|D]Z}|ds||.|ddk}|dd}|tvr|||r|rE|dd|kr3|||\}}} | ||<|r8|d|Dg}|| || ||t||f|d\|d |Dd |D}|||fS) a]Return (unbalanced_start, balanced, unbalanced_end), where each is a list of text and tag chunks. unbalanced_start is a list of all the tags that are opened, but not closed in this span. Similarly, unbalanced_end is a list of tags that are closed but were not opened. Extracting these might mean some reordering of the chunks./rFcg|]\}}}| Srr)rnamepostags rrz$split_unbalanced..TsBBBndCcBBBrNcg|]\}}}| Srr)rrrchunks rrz$split_unbalanced..]s111#4e111rcg|]}||SNr)rrs rrz$split_unbalanced..^sAAA%u/@/@/@/@r) startswithrJsplitr empty_tagspoprbr?) rstartend tag_stackrsrendtagrrrs rroro9s E CIH""$$  OOE " " " qS{{}}Q%%e,, :   OOE " " "   " "Yr]1-55&&&!*c3 #  " BB BBBCCC  5!!!! 5!!!!   dCMM59 : : : OOD ! ! ! ! LL11y111333AA8AAAH (C rc |t}n#t$rtwxYw|t}|d|||dz|||dzdfS)z Returns (stuff_before_DEL_START, stuff_inside_DEL_START_END, stuff_after_DEL_END). Returns the first case found (there may be more DEL_STARTs in stuff_after_DEL_END). Raises NoDeletes if there's no DEL_START found. Nr )indexrv ValueErrorrr})rrpos2s rrrasy ll9%%  << D $3$<Ad +VDFGG_ <|d||dnd S) a pre_delete and post_delete implicitly point to a place in the document (where the two were split). This moves that point (by popping items from one and pushing them onto the other). It moves the point to try to find a place where unbalanced_start applies. As an example:: >>> unbalanced_start = ['
'] >>> doc = ['

', 'Text', '

', '
', 'More Text', '
'] >>> pre, post = doc[:3], doc[3:] >>> pre, post (['

', 'Text', '

'], ['
', 'More Text', '
']) >>> locate_unbalanced_start(unbalanced_start, pre, post) >>> pre, post (['

', 'Text', '

', '
'], ['More Text', '
']) As you can see, we moved the point so that the dangling
that we found will be effectively replaced by the div in the original document. If this doesn't work out, we just throw away unbalanced_start without doing anything. r rz<>rrinsdelzUnexpected delete tag: %rN)rrrvrrrJ)rrrrfinding finding_namenextrs rrrms,  E"1%}}q)//55   E1~ 9  DOOC$8$8  E 7c>> Ezz||A$$T** 5== Eu}}} '$ .}} <     # # #   kooa00 1 1 1 1 E5rc |sdS|d}|dd}|sdS|d}|tus|dsdS|dd}|dks|dkrdS||kr=||d|ndS) zt like locate_unbalanced_start, except handling end tags and possibly moving the point earlier in the document. r rFrr tag, which takes up visible space just like a word but is only represented in a document by a tag. Nrct|td||||}||_||_||_|S)Nz: rL)rOrrNrdata html_repr)rrrrrHrGrMrs rrztag_token.__new__sPmmCTTT44!8%-&/0CEE!  rc hd|jd|jd|jd|jd|jd|jd S)Nz tag_token(rz , html_repr=z , post_tags=z , pre_tags=z, trailing_whitespace=r)rrrrHrGrMrs rrztag_token.__repr__sG HHH III NNN MMM NNN  $ $ $ & &rc|jSr)rrs rrSztag_token.htmls ~rr)rxryrzrrrrSrrrrrsX555946    &&&rrceZdZdZdZdZdS) href_tokenzh Represents the href in an anchor tag. Unlike other words, we only show the href when it changes. Tc d|zS)Nz Link: %srrs rrSzhref_token.htmls T!!rN)rxryrzrrlrSrrrrrs4((O"""""rrTctj|r|}nt|d}t|d|}t |S)ak Parse the given HTML and returns token objects (words with attached tags). This parses only the content of a page; anything in the head is ignored, and the and elements are themselves optional. The content is then parsed by lxml, which ensures the validity of the resulting parsed document (though lxml may make incorrect guesses when the markup is particular bad). and tags are also eliminated from the document, as that gets confusing. If include_hrefs is true, then the href attribute of tags is included as a special kind of diffable token.Tcleanup)skip_tagr')r iselement parse_html flatten_el fixup_chunks)rSr'body_elrs rr(r(sQ t1T4000 $m L L LF   rcF|rt|}t|dS)a Parses an HTML fragment, returning an lxml element. Note that the HTML will be wrapped in a
tag that was not in the original document. If cleanup is true, make sure there's no or , and get rid of any and tags. T) create_parent) cleanup_htmlr)rSrs rrr s,"D!! t4 8 8 88rz z zct|}|r||d}t|}|r|d|}t d|}|S)z This 'cleans' the HTML, meaning that any page structure is removed (only the contents of are used, if there is any and tags are removed. Nr)_body_researchr _end_body_rer _ins_del_resub)rSmatchs rrr1s| OOD ! !E "EIIKKLL!    % %E $NU[[]]N# ??2t $ $D Krz [ \t\n\r]$clt|}|d|||dfS)zP This function takes a word, such as 'test ' and returns ('test',' ') rN)r?rstrip)wordstripped_lengths rsplit_trailing_whitespacerAs9$++--((O /! "D)9)9$: ::rc xg}d}g}|D]{}t|tr|ddkrL|d}t|d\}}td||||}g}||n=|ddkr1|d}t ||d }g}||t |r> )B5)I)I &E&UYL_```HI MM( # # # # %    U # # # #     1  ''''999xx899x"))%0000 5 /b9---..r ##I... Mr) paramrareabrbasefontinputbasemetalinkcol)address blockquotecenterdirdivdlfieldsetformh1h2h3h4h5h6hrisindexmenunoframesnoscriptolprUtableul) dddtframesetlitbodytdtfootththeadtrc#rK|sD|jdkr(d|dt|fVnt|V|jtvr|jst |s |jsdSt|j}|D]}t|V|D]}t||D]}|V|jdkr0|dr|rd|dfV|s;t|Vt|j}|D]}t|VdSdS)a Takes an lxml element el, and generates all the text chunks for that tag. Each start tag is a chunk, each word is a chunk, and each end tag is a chunk. If skip_tag is true, then the outermost container tag is not returned (just its contents).rrANr&r-r) rget start_tagrr r?tail split_wordsr rend_tag)elr'r start_wordsrchilditem end_wordss rrrs  6U??"&&--27 7 7 7 7B--    vBGCGGBGbg&&K  $uMBBB  DJJJJ  v}}}M}rvvf~~&&&& $bkk((  $ $Dd## # # # # $$ $ $rz \S+(?:\s+|$)cj|r|sgSt|}|S)z_ Splits some text into words. Includes trailing whitespace on each word when appropriate. )rsplit_words_refindall)r wordss rrrs8 tzz||  " "4 ( (E Lrz ^[ \t\n\r]cd|jdd|jDdS)z= The text representation of the start tag for a tag. rrc Bg|]\}}d|dt|ddS)rnz="T")r )rrvalues rrzstart_tag..sG???(T55(,tt[-E-E-E-EF???r>)rrattribitems)rs rrrs_ ??,.IOO,=,=???@@@@ AArcr|jr"t|jrd}nd}d|jd|S)zg The text representation of an end tag for a tag. Includes trailing whitespace when appropriate. rnrrr!)rstart_whitespace_rerr)rextras rrrsF w&--bg66 &&rc.|d S)Nrrr*s rrrs~~c"" ""rc,|dS)Nrr(r)s rrrs >>$  rcX|do|d S)Nrrr(r)s rrrs( >>#   ;s~~d';';#;;rcht|d}t|t|d}|S)z Given an html string, move any or tags inside of any block-level elements, e.g. transform

word

to

word

FrT) skip_outer)r_fixup_ins_del_tagsserialize_html_fragment)rSrs rrYrYs; T5 ) ) )C "34 8 8 8D Krc(t|tr Jd|ztj|dt}|rQ||ddzd}|d|d}|S|S)z Serialize a single lxml element as HTML. The serialized form includes the elements tail. If skip_outer is true, then don't serialize the outermost tag z3You should pass in an element, not a string like %rrS)methodencodingr!r Nr)r basestringrtostringr findrfindr)rr-rSs rr/r/s "j))DD=BDD ) >"Vh ? ? ?DDIIcNN1$%%&$TZZ__$%zz|| rcdD]R}|d|zD]7}t|st|||8SdS)z?fixup_ins_del_tags that works on an lxml document in-place )rrzdescendant-or-self::%s)rN)xpath_contains_block_level_tag_move_el_inside_blockdrop_tag)rrrs rr.r. sx))4s:;;  B,R00  !"# . . . . KKMMMM  rcp|jtvs|jtvrdS|D]}t|rdSdS)zPTrue if the element contains any block-level elements, like

, , etc. TF)rblock_level_tagsblock_level_container_tagsr9)rrs rr9r9sT v!!!RV/I%I%It $U + + 44  5rc|D]}t|rnTtj|}|j|_d|_|t ||g|dd<dSt |D]}t|rkt |||jrStj|}|j|_d|_|| |dz||tj|}| ||| ||jr?tj|}|j|_d|_|d|dSdS)zt helper for _fixup_ins_del_tags; actually takes the etc tags and moves them inside any block-level tags. Nr r) r9rElementr rblistr:rr_rr`rJ)rrr children_tagtail_tag child_tagtext_tags rr:r:s   $U + +  E }S)) G DHH%%%111b $ $ $U + + $ !% - - -z 7 =-- %  !  "((5//!+X666 c**I JJui ( ( (   U # # # # w=%%  !X rc|}|jpd}|jrUt|s ||jz }n;|djr|dxj|jz c_n|j|d_||}|rU|dkrd}n ||dz }| |jr|xj|z c_n'||_n|jr|xj|z c_n||_||||dz<dS)z Removes an element, but merges its contents into its place, e.g., given

Hi there!

, if you remove the element you get

Hi there!

rrFrNr ) getparentr rr?r getchildren)rparentr rpreviouss r_merge_element_contentsrK?s \\^^F 7=bD w&2ww & BGODD"v{ &2 rw&  g2 LL  E % A::HHeAgH  { # t# " } % % $ NN,,F5q=rceZdZdZdZdZdS)r0zt Acts like SequenceMatcher, but tries not to find very small equal blocks amidst large spans of changes rctt|jt|j}t|j|dz tj|}fd|DS)Nc<g|]}|dks|d|S)rr)rr thresholds rrzBInsensitiveSequenceMatcher.get_matching_blocks..ms=   7Y&&Aw'&&&r)minr?r.rPdifflibSequenceMatcherget_matching_blocks)rsizeactualrPs @rrTz.InsensitiveSequenceMatcher.get_matching_blocksisw3tv;;DF ,,q11 (<rUrrr%rrrrrrYr/r.r9r:rKrSr0rxrXmainrrrrjs^'&&&&&)))))) J '***********))))))))*HHHHHJJJJJ111#1&#&#&#&#P - - -111   $*&&&.$$$L    .                 %%%N& & & P = = =000d4'''''H'''R8""""""""    0 9 9 9 9 2:lBDI . .rz-bd33 bj,bd24i88    BJ}--;;;222l# 6 $$$$8ORT22!bj//AAA'''###   <<<$@ - - -D     !8    z&&&&&&Ls-' 55<AA A AA