mirror of
				https://github.com/MontFerret/ferret.git
				synced 2025-10-30 23:37:40 +02:00 
			
		
		
		
	Feature/#265 dom manipulations (#329)
* Added SetInnerHTML method * Added E2E tests * Refactored GetInnerText* methods * Updated e2e tests * Moved related E2E tests to folders * Added error message * Added E2E tests * Added E2E for static driver
This commit is contained in:
		| @@ -13,7 +13,7 @@ import ( | ||||
| // @param doc (Open|GetElement) - Parent document or element. | ||||
| // @param selector (String, optional) - String of CSS selector. | ||||
| // @returns (String) - Inner HTML string if an element found, otherwise empty string. | ||||
| func InnerHTML(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| func GetInnerHTML(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 2) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| @@ -27,7 +27,7 @@ func InnerHTML(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	} | ||||
| 
 | ||||
| 	if len(args) == 1 { | ||||
| 		return el.GetInnerHTML(ctx), nil | ||||
| 		return el.GetInnerHTML(ctx) | ||||
| 	} | ||||
| 
 | ||||
| 	err = core.ValidateType(args[1], types.String) | ||||
| @@ -38,5 +38,5 @@ func InnerHTML(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 
 | ||||
| 	selector := args[1].(values.String) | ||||
| 
 | ||||
| 	return el.InnerHTMLBySelector(ctx, selector), nil | ||||
| 	return el.GetInnerHTMLBySelector(ctx, selector) | ||||
| } | ||||
| @@ -9,11 +9,11 @@ import ( | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | ||||
| ) | ||||
| 
 | ||||
| // InnerHTMLAll returns an array of inner HTML strings of matched elements. | ||||
| // GetInnerHTMLAll returns an array of inner HTML strings of matched elements. | ||||
| // @param doc (HTMLDocument|HTMLElement) - Parent document or element. | ||||
| // @param selector (String) - String of CSS selector. | ||||
| // @returns (String) - An array of inner HTML strings if any element found, otherwise empty array. | ||||
| func InnerHTMLAll(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| func GetInnerHTMLAll(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 2, 2) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| @@ -34,5 +34,5 @@ func InnerHTMLAll(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 
 | ||||
| 	selector := args[1].(values.String) | ||||
| 
 | ||||
| 	return el.InnerHTMLBySelectorAll(ctx, selector), nil | ||||
| 	return el.GetInnerHTMLBySelectorAll(ctx, selector) | ||||
| } | ||||
| @@ -13,7 +13,7 @@ import ( | ||||
| // @param doc (HTMLDocument|HTMLElement) - Parent document or element. | ||||
| // @param selector (String, optional) - String of CSS selector. | ||||
| // @returns (String) - Inner text if an element found, otherwise empty string. | ||||
| func InnerText(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| func GetInnerText(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 2) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| @@ -27,7 +27,7 @@ func InnerText(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	} | ||||
| 
 | ||||
| 	if len(args) == 1 { | ||||
| 		return el.GetInnerText(ctx), nil | ||||
| 		return el.GetInnerText(ctx) | ||||
| 	} | ||||
| 
 | ||||
| 	err = core.ValidateType(args[1], types.String) | ||||
| @@ -38,5 +38,5 @@ func InnerText(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 
 | ||||
| 	selector := args[1].(values.String) | ||||
| 
 | ||||
| 	return el.InnerTextBySelector(ctx, selector), nil | ||||
| 	return el.GetInnerTextBySelector(ctx, selector) | ||||
| } | ||||
| @@ -9,11 +9,11 @@ import ( | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | ||||
| ) | ||||
| 
 | ||||
| // InnerTextAll returns an array of inner text of matched elements. | ||||
| // GetInnerTextAll returns an array of inner text of matched elements. | ||||
| // @param doc (HTMLDocument|HTMLElement) - Parent document or element. | ||||
| // @param selector (String) - String of CSS selector. | ||||
| // @returns (String) - An array of inner text if any element found, otherwise empty array. | ||||
| func InnerTextAll(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| func GetInnerTextAll(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 2, 2) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| @@ -34,5 +34,5 @@ func InnerTextAll(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 
 | ||||
| 	selector := args[1].(values.String) | ||||
| 
 | ||||
| 	return el.InnerTextBySelectorAll(ctx, selector), nil | ||||
| 	return el.GetInnerTextBySelectorAll(ctx, selector) | ||||
| } | ||||
| @@ -29,10 +29,12 @@ func NewLib() map[string]core.Function { | ||||
| 		"ELEMENTS":          Elements, | ||||
| 		"ELEMENTS_COUNT":    ElementsCount, | ||||
| 		"HOVER":             Hover, | ||||
| 		"INNER_HTML":        InnerHTML, | ||||
| 		"INNER_HTML_ALL":    InnerHTMLAll, | ||||
| 		"INNER_TEXT":        InnerText, | ||||
| 		"INNER_TEXT_ALL":    InnerTextAll, | ||||
| 		"INNER_HTML":        GetInnerHTML, | ||||
| 		"INNER_HTML_SET":    SetInnerHTML, | ||||
| 		"INNER_HTML_ALL":    GetInnerHTMLAll, | ||||
| 		"INNER_TEXT":        GetInnerText, | ||||
| 		"INNER_TEXT_SET":    SetInnerText, | ||||
| 		"INNER_TEXT_ALL":    GetInnerTextAll, | ||||
| 		"INPUT":             Input, | ||||
| 		"MOUSE":             MouseMoveXY, | ||||
| 		"NAVIGATE":          Navigate, | ||||
|   | ||||
							
								
								
									
										55
									
								
								pkg/stdlib/html/set_inner_html.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								pkg/stdlib/html/set_inner_html.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/drivers" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | ||||
| ) | ||||
|  | ||||
| // SetInnerHTML sets inner HTML string to a given or matched by CSS selector element | ||||
| // @param doc (Open|GetElement) - Parent document or element. | ||||
| // @param selector (String, optional) - String of CSS selector. | ||||
| // @param innerHTML (String) - String of inner HTML. | ||||
| func SetInnerHTML(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 2, 3) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	el, err := drivers.ToElement(args[0]) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	if len(args) == 2 { | ||||
| 		err := core.ValidateType(args[1], types.String) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			return values.None, err | ||||
| 		} | ||||
|  | ||||
| 		return values.None, el.SetInnerHTML(ctx, values.ToString(args[1])) | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[1], types.String) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[2], types.String) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	selector := values.ToString(args[1]) | ||||
| 	innerHTML := values.ToString(args[2]) | ||||
|  | ||||
| 	return values.None, el.SetInnerHTMLBySelector(ctx, selector, innerHTML) | ||||
| } | ||||
							
								
								
									
										55
									
								
								pkg/stdlib/html/set_inner_text.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								pkg/stdlib/html/set_inner_text.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/drivers" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | ||||
| ) | ||||
|  | ||||
| // SetInnerText sets inner text string to a given or matched by CSS selector element | ||||
| // @param doc (Open|GetElement) - Parent document or element. | ||||
| // @param selector (String, optional) - String of CSS selector. | ||||
| // @param innerText (String) - String of inner text. | ||||
| func SetInnerText(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 2, 3) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	el, err := drivers.ToElement(args[0]) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	if len(args) == 2 { | ||||
| 		err := core.ValidateType(args[1], types.String) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			return values.None, err | ||||
| 		} | ||||
|  | ||||
| 		return values.None, el.SetInnerText(ctx, values.ToString(args[1])) | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[1], types.String) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[2], types.String) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	selector := values.ToString(args[1]) | ||||
| 	innerHTML := values.ToString(args[2]) | ||||
|  | ||||
| 	return values.None, el.SetInnerTextBySelector(ctx, selector, innerHTML) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user