
    tfI                         d Z ddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZ erdd	lmZmZ  ej6                  e      Z G d
 de      Zde	e   defdZ y)zElement handle module.    N)AnyDictListOptionalTYPE_CHECKING)
CDPSession)ExecutionContextJSHandle)ElementHandleErrorNetworkError)
debugError)
merge_dict)FrameFrameManagerc                       e Zd ZdZdededededddd	f fd
Zd,dZ	de
d   fdZd-dZdeeef   fdZde
e   fdZdee   deeeef      fdZd-dZd.dededd	fdZdedefdZd-dZd-dZd.dedededd	fdZd.dedededd	fdZde
eeef      fdZde
e   fd Zd.dededefd!Zd"ede
d    fd#Z d"eded    fd$Z!d"ed%ed&edefd'Z"d"ed%ed&edefd(Z#e Z$e!Z%e"Z&e#Z'd)eded    fd*Z(e(Z)de*fd+Z+ xZ,S )/ElementHandlea  ElementHandle class.

    This class represents an in-page DOM element. ElementHandle can be created
    by the :meth:`pyppeteer.page.Page.querySelector` method.

    ElementHandle prevents DOM element from garbage collection unless the
    handle is disposed. ElementHandles are automatically disposed when their
    origin frame gets navigated.

    ElementHandle isinstance can be used as arguments in
    :meth:`pyppeteer.page.Page.querySelectorEval` and
    :meth:`pyppeteer.page.Page.evaluate` methods.
    contextclientremoteObjectpageframeManagerr   returnNc                 n    t         |   |||       || _        || _        || _        || _        d| _        y )NF)super__init___client_remoteObject_page_frameManager	_disposed)selfr   r   r   r   r   	__class__s         b/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/pyppeteer/element_handle.pyr   zElementHandle.__init__(   s;     	&,7)
)    c                     | S )zReturn this ElementHandle. r!   s    r#   	asElementzElementHandle.asElement2   s    r$   r   c                 ,  K   | j                   j                  dd| j                  j                  d      i       d{   }|j                  di       }t	        |j                  d      t
              sy| j                  j                  |d         S 7 Tw)z|Return the content frame for the element handle.

        Return ``None`` if this handle is not referencing iframe.
        zDOM.describeNodeobjectIdNnodeframeId)r   sendr   get
isinstancestrr   frame)r!   nodeInfonode_objs      r#   contentFramezElementHandle.contentFrame6   s     
 **+=**..z:@
   <<+(,,y137!!''(;<<s   ;BBABc                    K   | j                   j                  d| | j                  j                         d {   }|rt	        |      y 7 w)Na  
            async (element, pageJavascriptEnabled) => {
                if (!element.isConnected)
                    return 'Node is detached from document';
                if (element.nodeType !== Node.ELEMENT_NODE)
                    return 'Node is not of type HTMLElement';
                // force-scroll if page's javascript is disabled.
                if (!pageJavascriptEnabled) {
                    element.scrollIntoView({
                        block: 'center',
                        inline: 'center',
                        behavior: 'instant',
                    });
                    return false;
                }
                const visibleRatio = await new Promise(resolve => {
                    const observer = new IntersectionObserver(entries => {
                        resolve(entries[0].intersectionRatio);
                        observer.disconnect();
                    });
                    observer.observe(element);
                });
                if (visibleRatio !== 1.0)
                    element.scrollIntoView({
                        block: 'center',
                        inline: 'center',
                        behavior: 'instant',
                    });
                return false;
            })executionContextevaluater   _javascriptEnabledr   )r!   errors     r#   _scrollIntoViewIfNeededz%ElementHandle._scrollIntoViewIfNeededC   sQ     ++44 6: 

55;7 7< $U++ =7s   5AA
Ac                   K   d }	 | j                   j                  dd| j                  j                  d      i       d {   }|r|j                  d      st        d      g }|j                  d      D ]3  }| j                  |      }t        |      dkD  s#|j                  |       5 |st        d      |d   }d}d}|D ]  }	||	d   z  }||	d   z  } |d	z  |d	z  d
S 7 # t        $ r}t        t        |       Y d }~d }~ww xY ww)NzDOM.getContentQuadsr*   quads0Node is either not visible or not an HTMLElement   r   xy   r?   r@   )r   r-   r   r.   	Exceptionr   loggerr   _fromProtocolQuad_computeQuadAreaappend)
r!   resulter<   _quad_qquadr?   r@   points
             r#   _clickablePointzElementHandle._clickablePointe   s?    	"<<,,-BD..22:>E  F VZZ0$BD D ZZ( 	!E''.B#a'R 	! $BD D Qx 	EsOAsOA	 UQ''3  	"vq!!	"sH   D:C&  C$C& ADAD$C& &	D	/D?DD		Dc                    K   	 | j                   j                  dd| j                  j                  d      i       d {   }|S 7 # t        $ r}t        t        |       d }Y d }~|S d }~ww xY ww)NzDOM.getBoxModelr*   )r   r-   r   r.   r   r   rD   )r!   rH   rI   s      r#   _getBoxModelzElementHandle._getBoxModel   sq     	+/<<+<+<!T//33J?@, &F &  	vq!F	s>   A0:A AA A0A 	A-A("A0(A--A0rL   c                 V    |d   |d   d|d   |d   d|d   |d   d|d   |d	   dgS )
Nr   r>   rB         rA            r&   )r!   rL   s     r#   rE   zElementHandle._fromProtocolQuad   sL    q'Q(q'Q(q'Q(q'Q(	
 	
r$   c                   K   | j                          d{    | j                          d{   }|j                  dd      }|j                  dd      }| j                  j                  j                  ||       d{    y7 o7 Y7 	w)zMove mouse over to center of this element.

        If needed, this method scrolls element into view. If this element is
        detached from DOM tree, the method raises an ``ElementHandleError``.
        Nr?   r   r@   )r:   rN   r.   r   mousemove)r!   objr?   r@   s       r#   hoverzElementHandle.hover   sx      **,,,((**GGCOGGCOjj##Aq)))	 	-* 	*2   BBBBAB B
BB
Boptionskwargsc                 6  K   t        ||      }| j                          d{    | j                          d{   }|j                  dd      }|j                  dd      }| j                  j
                  j                  |||       d{    y7 p7 Z7 	w)a  Click the center of this element.

        If needed, this method scrolls element into view. If the element is
        detached from DOM, the method raises ``ElementHandleError``.

        ``options`` can contain the following fields:

        * ``button`` (str): ``left``, ``right``, of ``middle``, defaults to
          ``left``.
        * ``clickCount`` (int): Defaults to 1.
        * ``delay`` (int|float): Time to wait between ``mousedown`` and
          ``mouseup`` in milliseconds. Defaults to 0.
        Nr?   r   r@   )r   r:   rN   r.   r   rX   click)r!   r]   r^   rZ   r?   r@   s         r#   r`   zElementHandle.click   s      Wf-**,,,((**GGCOGGCOjj$$Q7333	 	-* 	4s2    BBBBABBBBB	filePathsc                    K   |D cg c]!  }t         j                  j                  |      # }}| j                  j	                  d      }| j
                  j                  d||d       d{   S c c}w 7 	w)zUpload files.r*   zDOM.setFileInputFiles)r*   filesN)ospathabspathr   r.   r   r-   )r!   ra   prc   r*   s        r#   
uploadFilezElementHandle.uploadFile   sm     -67#77%%))*5\\&&#!E2
 
 	
 8
s   A8&A1?A8,A6-
A8c                   K   | j                          d{    | j                          d{   }|j                  dd      }|j                  dd      }| j                  j                  j                  ||       d{    y7 o7 Y7 	w)zTap the center of this element.

        If needed, this method scrolls element into view. If the element is
        detached from DOM, the method raises ``ElementHandleError``.
        Nr?   r   r@   )r:   rN   r.   r   touchscreentap)r!   centerr?   r@   s       r#   rk   zElementHandle.tap   sz      **,,,++--JJsAJJsAjj$$((A...	 	-- 	/r\   c                 X   K   | j                   j                  d|        d{    y7 w)zFocus on this element.zelement => element.focus()Nr6   r7   r'   s    r#   focuszElementHandle.focus   s)     ##,,($0 	0 	0s    *(*textc                    K   t        ||      }| j                          d{    | j                  j                  j	                  ||       d{    y7 37 w)zqFocus the element and then type text.

        Details see :meth:`pyppeteer.input.Keyboard.type` method.
        N)r   ro   r   keyboardtype)r!   rp   r]   r^   s       r#   rs   zElementHandle.type   sL      Wf-jjljj!!&&tW555 	5!    AA-AAAAkeyc                    K   t        ||      }| j                          d{    | j                  j                  j	                  ||       d{    y7 37 w)a  Press ``key`` onto the element.

        This method focuses the element, and then uses
        :meth:`pyppeteer.input.keyboard.down` and
        :meth:`pyppeteer.input.keyboard.up`.

        :arg str key: Name of key to press, such as ``ArrowLeft``.

        This method accepts the following options:

        * ``text`` (str): If specified, generates an input event with this
          text.
        * ``delay`` (int|float): Time to wait between ``keydown`` and
          ``keyup``. Defaults to 0.
        N)r   ro   r   rr   press)r!   ru   r]   r^   s       r#   rw   zElementHandle.press   sL     " Wf-jjljj!!''W555 	5rt   c                 >  K   | j                          d{   }|sy|d   d   }t        |d   |d   |d   |d         }t        |d   |d	   |d
   |d         }t        |d   |d   |d   |d         |z
  }t        |d   |d	   |d
   |d         |z
  }||||dS 7 w)a  Return bounding box of this element.

        If the element is not visible, return ``None``.

        This method returns dictionary of bounding box, which contains:

        * ``x`` (int): The X coordinate of the element in pixels.
        * ``y`` (int): The Y coordinate of the element in pixels.
        * ``width`` (int): The width of the element in pixels.
        * ``height`` (int): The height of the element in pixels.
        Nmodelborderr   rR   rA   rU   r>   rS   rT   rV   )r?   r@   widthheight)rP   minmax)r!   rH   rL   r?   r@   r{   r|   s          r#   boundingBoxzElementHandle.boundingBox   s      ((**gx(Qa$q'473Qa$q'473DGT!Wd1gtAw7!;T!Wd1gtAwQ81<Q&AA +s   BBBBc                   K   | j                          d{   }|sy|j                  di       }| j                  |j                  d            | j                  |j                  d            | j                  |j                  d            | j                  |j                  d            |j                  d      |j                  d      d	S 7 w)
a8  Return boxes of element.

        Return ``None`` if element is not visible. Boxes are represented as an
        list of points; each Point is a dictionary ``{x, y}``. Box points are
        sorted clock-wise.

        Returned value is a dictionary with the following fields:

        * ``content`` (List[Dict]): Content box.
        * ``padding`` (List[Dict]): Padding box.
        * ``border`` (List[Dict]): Border box.
        * ``margin`` (List[Dict]): Margin box.
        * ``width`` (int): Element's width.
        * ``height`` (int): Element's height.
        Nry   contentpaddingrz   marginr{   r|   )r   r   rz   r   r{   r|   )rP   r.   rE   )r!   rH   ry   s      r#   boxModelzElementHandle.boxModel  s       ((**

7B'--eii	.BC--eii	.BC,,UYYx-@A,,UYYx-@AYYw'ii)
 	
 +s   CCB9Cc                 *  K   t        ||      }d}| j                          d{   }|st        d      t        j                  | j
                  j                        }|d   |d   kD  s|d   |d   kD  rt        |d   t        j                  |d               t        |d   t        j                  |d               d}t        j                  |      }|j                  |       | j
                  j                  |       d{    d}| j                          d{    | j                          d{   }|st        d      | j                  j                  d       d{   }|d	   d
   }	|d	   d   }
i }|j                  |       |d   |	z   |d<   |d   |
z   |d<   d|i}|j                  |       | j
                  j                  |       d{   }|r#| j
                  j                  |       d{    |S 7 7 7 7 7 7 77 w)zTake a screenshot of this element.

        If the element is detached from DOM, this method raises an
        ``ElementHandleError``.

        Available options are same as :meth:`pyppeteer.page.Page.screenshot`.
        FNr=   r{   r|   )r{   r|   TzPage.getLayoutMetricslayoutViewportpageXpageYr?   r@   clip)r   r   r   copydeepcopyr   viewportr~   mathceilupdatesetViewportr:   r   r-   
screenshot)r!   r]   r^   needsViewportResetr   original_viewportnewViewportnew_viewport_objr   r   r   opt	imageDatas                 r#   r   zElementHandle.screenshot'  s'     Wf-" ,,..$BD D !MM$***=*=> #4W#==H%(9((CC%g.IIk'23 %h/IIk(34	K  ==):;L,**((666!%**,,, ,,..$BD D \\&&'>??%&w/%&w/K I%S	I%S	tn

7**//44	**(():;;;[ /* 7 	-.
 @ 5 <s}   "HHCHHHH	H6H7/H&H'A0HH$H<H=HH	HHHHHselectorc                    K   | j                   j                  d| |       d{   }|j                         }|r|S |j                          d{    y7 17 w)zReturn first element which matches ``selector`` under this element.

        If no element matches the ``selector``, returns ``None``.
        z6(element, selector) => element.querySelector(selector)N)r6   evaluateHandler(   dispose)r!   r   handleelements       r#   querySelectorzElementHandle.querySelectora  sa     
 ,,;;D(
 
 ""$Nnn
 	s!   !AA+AAAAc                 :  K   | j                   j                  d| |       d{   }|j                          d{   }|j                          d{    g }|j	                         D ]&  }|j                         }|s|j                  |       ( |S 7 q7 [7 Ew)zReturn all elements which match ``selector`` under this element.

        If no element matches the ``selector``, returns empty list (``[]``).
        z9(element, selector) => element.querySelectorAll(selector)Nr6   r   getPropertiesr   valuesr(   rG   )r!   r   arrayHandle
propertiesrH   propelementHandles          r#   querySelectorAllzElementHandle.querySelectorAllp  s     
 !11@@G(
 
 '4466
!!###%%' 	-D NN,Mm,	- 
 7#7   !BBBBBB+B BBBpageFunctionargsc                    K   | j                  |       d{   }|st        d| d       | j                  j                  ||g|  d{   }|j	                          d{    |S 7 U7  7 
w)a  Run ``Page.querySelectorEval`` within the element.

        This method runs ``document.querySelector`` within the element and
        passes it as the first argument to ``pageFunction``. If there is no
        element matching ``selector``, the method raises
        ``ElementHandleError``.

        If ``pageFunction`` returns a promise, then wait for the promise to
        resolve and return its value.

        ``ElementHandle.Jeval`` is a shortcut of this method.

        Example:

        .. code:: python

            tweetHandle = await page.querySelector('.tweet')
            assert (await tweetHandle.querySelectorEval('.like', 'node => node.innerText')) == 100
            assert (await tweetHandle.Jeval('.retweets', 'node => node.innerText')) == 10
        Nz1Error: failed to find element matching selector "")r   r   r6   r7   r   )r!   r   r   r   r   rH   s         r#   querySelectorEvalzElementHandle.querySelectorEval  s     , #00::$CH:QO  6t,,55-0*.0 0##%%% ;
0%s3   A3A-6A3A/A3&A1'A3/A31A3c                    K   | j                   j                  d| |       d{   } | j                   j                  ||g|  d{   }|j                          d{    |S 7 D7  7 
w)a+  Run ``Page.querySelectorAllEval`` within the element.

        This method runs ``Array.from(document.querySelectorAll)`` within the
        element and passes it as the first argument to ``pageFunction``. If
        there is no element matching ``selector``, the method raises
        ``ElementHandleError``.

        If ``pageFunction`` returns a promise, then wait for the promise to
        resolve and return its value.

        Example:

        .. code:: html

            <div class="feed">
                <div class="tweet">Hello!</div>
                <div class="tweet">Hi!</div>
            </div>

        .. code:: python

            feedHandle = await page.J('.feed')
            assert (await feedHandle.JJeval('.tweet', '(nodes => nodes.map(n => n.innerText))')) == ['Hello!', 'Hi!']
        zE(element, selector) => Array.from(element.querySelectorAll(selector))N)r6   r   r7   r   )r!   r   r   r   r   rH   s         r#   querySelectorAllEvalz"ElementHandle.querySelectorAllEval  s}     4 !11@@S(
 
 6t,,55+.(,. .!!###
.#s3   !A.A(%A.	A*
A.!A,"A.*A.,A.
expressionc                 :  K   | j                   j                  d| |       d{   }|j                          d{   }|j                          d{    g }|j	                         D ]&  }|j                         }|s|j                  |       ( |S 7 q7 [7 Ew)zEvaluate the XPath expression relative to this elementHandle.

        If there are no such elements, return an empty list.

        :arg str expression: XPath string to be evaluated.
        a  (element, expression) => {
                const document = element.ownerDocument || element;
                const iterator = document.evaluate(expression, element, null,
                    XPathResult.ORDERED_NODE_ITERATOR_TYPE);
                const array = [];
                let item;
                while ((item = iterator.iterateNext()))
                    array.push(item);
                return array;

            }Nr   )r!   r   r   r   rH   propertyr   s          r#   xpathzElementHandle.xpath  s      !11@@
 
$ $ '4466
!!###"))+ 	-H$..0Mm,	- '$ 7#r   c                 V   K   | j                   j                  d|        d{   S 7 w)z:Return ``True`` if the element is visible in the viewport.a}  async element => {
            const visibleRatio = await new Promise(resolve => {
                const observer = new IntersectionObserver(entries => {
                    resolve(entries[0].intersectionRatio);
                    observer.disconnect();
                });
                observer.observe(element);
            });
            return visibleRatio > 0;
        }Nrn   r'   s    r#   isIntersectingViewportz$ElementHandle.isIntersectingViewport  s3     **33 	5 	 	 		 	s    )'))r   r   )r   N)N)-__name__
__module____qualname____doc__r	   r   dictr   r   r(   r   r4   r:   r   r0   floatrN   rP   r   intrE   r[   r`   rh   rk   ro   rs   rw   r   r   bytesr   r   r   r   r   JJJJevalJJevalr   Jxboolr   __classcell__)r"   s   @r#   r   r      s!    0 * #+.-26=HW$5 = ,D(tCJ'7 (<	HTN 	
d3i 
Dc3h4H 

*44 4# 4$ 4*
3 
4 

/0
6s 6T 6C 666s 6T 6C 66*B8De,<#= B0
 
>8 8s 8u 8tC H_4M s tO7L $ 3 '*/2@!3 !c !*-!25!H 	A	BE!Fc d?.C : 
Bd r$   r   rL   r   c                     d}t        |       D ];  \  }}| |   }| |dz   t        |       z     }||d   |d   z  |d   |d   z  z
  dz  z  }= |S )Nr   r>   r?   r@   rR   )	enumeratelen)rL   areai_p1p2s         r#   rF   rF     ss    D$ <1!W1q5CI%&C2c7"RWr#w%66!;;< Kr$   )!r   r   loggingr   os.pathrd   typingr   r   r   r   r   pyppeteer.connectionr   pyppeteer.execution_contextr	   r
   pyppeteer.errorsr   r   pyppeteer.helperr   pyppeteer.utilr   pyppeteer.frame_managerr   r   	getLoggerr   rD   r   r   rF   r&   r$   r#   <module>r      sk         ; ; + B = ' %; 
		8	$_H _D4: % r$   