87 lines
1.4 KiB
Go
87 lines
1.4 KiB
Go
|
package htmlx
|
||
|
|
||
|
import "github.com/d5/tengo/v2"
|
||
|
|
||
|
type HTML struct{
|
||
|
tengo.ObjectImpl
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
|
||
|
html.div({
|
||
|
id: "some-el-id",
|
||
|
value: "shit value"
|
||
|
}).body(
|
||
|
html.raw("cock "),
|
||
|
html.strong("something")
|
||
|
)
|
||
|
|
||
|
*/
|
||
|
|
||
|
func (html *HTML) IndexGet(
|
||
|
index tengo.Object,
|
||
|
) (tengo.Object, error) {
|
||
|
str, ok := tengo.ToString(index)
|
||
|
if !ok {
|
||
|
return nil, tengo.ErrInvalidIndexValueType
|
||
|
}
|
||
|
|
||
|
fn := func(args ...tengo.Object) (tengo.Object, error) {
|
||
|
if len(args) > 1 {
|
||
|
return nil, tengo.ErrWrongNumArguments
|
||
|
}
|
||
|
var arg tengo.Object
|
||
|
if len(args) == 1 {
|
||
|
arg = args[0]
|
||
|
}
|
||
|
|
||
|
if arg == nil {
|
||
|
return &Element{
|
||
|
Tag: str,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
if can := arg.CanIterate() ; !can {
|
||
|
return nil, tengo.ErrInvalidArgumentType{
|
||
|
Name: "first",
|
||
|
Expected: "iterable",
|
||
|
Found: arg.TypeName(),
|
||
|
}
|
||
|
}
|
||
|
attr := map[string] string{}
|
||
|
iter := arg.Iterate()
|
||
|
for iter.Next() {
|
||
|
key, val := iter.Key(), iter.Value()
|
||
|
skey, ok := tengo.ToString(key)
|
||
|
if !ok {
|
||
|
return nil, tengo.ErrInvalidArgumentType{
|
||
|
Name: "attribute(key)",
|
||
|
Expected: "stringer",
|
||
|
Found: key.TypeName(),
|
||
|
}
|
||
|
}
|
||
|
sval, ok := tengo.ToString(val)
|
||
|
if !ok {
|
||
|
return nil, tengo.ErrInvalidArgumentType{
|
||
|
Name: "attribute(value)",
|
||
|
Expected: "stringer",
|
||
|
Found: val.TypeName(),
|
||
|
}
|
||
|
}
|
||
|
attr[skey] = sval
|
||
|
}
|
||
|
return &Element{
|
||
|
Tag: str,
|
||
|
Attr: attr,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
return &tengo.UserFunction{
|
||
|
Name: str,
|
||
|
Value: fn,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
|
||
|
|