PluginProbe
Hello Plus / trunk
Hello Plus vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / tests / playwright / assets / elements-utils.ts

elements-utils.ts in Hello Plus trunk, at tests/playwright/assets/elements-utils.ts

74 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { $eType, ElementorType } from '../types/types';
2
3 /**
4 * Add element to the page using model and parent container.
5 * @param {Object} props
6 * @param {Object} props.model
7 * @param {string | null} props.container
8 * @param {boolean} props.isContainerASection
9 * @return {string | undefined}
10 */
11
12 let parent: unknown;
13 let elementor: ElementorType;
14 let $e: $eType;
15 export const addElement = ( props: { model: unknown, container: null | string, isContainerASection: boolean } ): string | undefined => {
16 if ( props.container ) {
17 parent = elementor.getContainer( props.container );
18 } else {
19 // If a `container` isn't supplied - create a new Section.
20 parent = $e.run(
21 'document/elements/create',
22 {
23 model: { elType: 'section' },
24 columns: 1,
25 container: elementor.getContainer( 'document' ),
26 },
27 );
28
29 props.isContainerASection = true;
30 }
31
32 if ( props.isContainerASection && 'object' === typeof parent && 'children' in parent ) {
33 parent = parent.children[ 0 ];
34 }
35
36 const element = $e.run(
37 'document/elements/create',
38 {
39 model: props.model,
40 container: parent,
41 },
42 );
43
44 if ( element && 'object' === typeof element && 'id' in element && 'string' === typeof element.id ) {
45 return element.id;
46 }
47 return undefined;
48 };
49
50 /**
51 * Make an Elementor element CSS selector using Container ID.
52 *
53 * @param {string} id - Container ID.
54 *
55 * @return {string} css selector
56 */
57 export const getElementSelector = ( id: string ) => {
58 return `[data-id = "${ id }"]`;
59 };
60
61 export function compareVersions( a: string, b: string ): number {
62 const aParts = a.split( '.' ).map( Number );
63 const bParts = b.split( '.' ).map( Number );
64
65 for ( let i = 0; i < Math.max( aParts.length, bParts.length ); i++ ) {
66 const aVal = isNaN( aParts[ i ] ) ? Infinity : ( aParts[ i ] || 0 );
67 const bVal = isNaN( bParts[ i ] ) ? Infinity : ( bParts[ i ] || 0 );
68 if ( aVal !== bVal ) {
69 return aVal - bVal;
70 }
71 }
72 return 0;
73 }
74