directives

package
v0.0.0-...-277be3d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Component = &sht.Directive{
	Name:       "component",
	Restrict:   sht.ELEMENT,
	Priority:   1000,
	Terminal:   true,
	Transclude: true,
	Compile: func(node *sht.Node, attrs *sht.Attributes, t *sht.Compiler) (methods *sht.DirectiveMethods, einlineJsErrr error) {

		var style *sht.Node
		var script *sht.Node

		node.Transverse(func(child *sht.Node) (stop bool) {
			stop = false
			if child == node || child.Type != sht.ElementNode {
				return
			}

			switch child.Data {
			case "component":

				einlineJsErrr = errorCompNested(node.DebugTag(), child.DebugTag())

			case "style":
				if style != nil {

					einlineJsErrr = errorCompStyleSingle(style.DebugTag(), child.DebugTag())

				} else if child.Parent != node {

					einlineJsErrr = errorCompStyleLocation(node.DebugTag(), child.DebugTag())

				} else {
					style = child
				}

			case "script":
				if script != nil {

					einlineJsErrr = errorCompScriptSingle(script.DebugTag(), child.DebugTag())

				} else if child.Parent != node {

					einlineJsErrr = errorCompScriptLocation(node.DebugTag(), child.DebugTag())

				} else {
					script = child
				}
			}

			if einlineJsErrr != nil {
				stop = true
				return
			}

			return
		})

		if einlineJsErrr != nil {
			return
		}

		inlineJs, inlineJsErr := jsc.Compile(node, script, t.Sequence)
		if inlineJsErr != nil {
			return nil, inlineJsErr
		}
		if inlineJs != nil {
			t.RegisterAssetJsContent(inlineJs.Content)
		}

		return
	},
}

Component Responsible for creating components declaratively

@TODO: Javascript directives?

View Source
var IFAttribute = &sht.Directive{
	Name:       "if",
	Restrict:   sht.ATTRIBUTE,
	Priority:   899,
	Terminal:   true,
	Transclude: "element",
	Compile: func(node *sht.Node, attrs *sht.Attributes, t *sht.Compiler) (*sht.DirectiveMethods, error) {
		return createIfDirective(attrs, attrIF), nil
	},
}

IFAttribute `<element if="true"/>`

View Source
var IFElement = &sht.Directive{
	Name:       "if",
	Restrict:   sht.ELEMENT,
	Priority:   900,
	Terminal:   true,
	Transclude: true,
	Compile: func(node *sht.Node, attrs *sht.Attributes, t *sht.Compiler) (*sht.DirectiveMethods, error) {
		return createIfDirective(attrs, attrCOND), nil

	},
}

IFElement `<if cond="true"/>` @TODO `<if cond="true"></if> <else-if cond="true"></else-if> <else></else>`

View Source
var LinkDirective = &sht.Directive{
	Name:     "link",
	Restrict: sht.ELEMENT,

	Terminal: true,
	Priority: 1000,
}
View Source
var LinkDirectiveFunc = func(node *sht.Node, attrs *sht.Attributes, c *sht.Compiler) {
	relAttr := attrs.Get("rel")
	if relAttr != "include" {
		return
	}

	hrefAttr := attrs.Get("href")
	if hrefAttr == "" {
		log.Fatal("A tag <link rel=\"include\"> espera expr atributo href='string'")
	}

	currentFilepath := node.File

	includeFilepath := path.Join(path.Dir(currentFilepath), hrefAttr)

	if !strings.HasSuffix(includeFilepath, ".html") {
		log.Fatal("Só é permitido expr include de arquivos .html")
	}

	// evita que sejam feitos includes cíclicos/recursivos
	var parents sht.StringSet
	parentsI := c.Context.Get(keyIncludeParents)
	if parentsI != nil {
		parents = parentsI.(sht.StringSet)
	} else {
		parents = sht.StringSet{}
		c.Context.Set(keyIncludeParents, parents)
	}

	if parents.Contains(includeFilepath) {

	}

	c.Context.Set(keyIncludeParents, parents.Clone(currentFilepath))

	c.SafeRemove(node)

	c.Context.Set(keyIncludeParents, parents)
}

LinkDirectiveFunc faz expr processamento de <link rel="include" href="file.html"/>

View Source
var Script = &sht.Directive{
	Name:       "script",
	Restrict:   sht.ELEMENT,
	Priority:   990,
	Terminal:   true,
	Transclude: true,
	Compile: func(node *sht.Node, attrs *sht.Attributes, t *sht.Compiler) (*sht.DirectiveMethods, error) {

		var assets []string

		priorityStr := attrs.Get("priority")
		priority := 0
		if priorityStr != "" {
			var errAtoi error
			priority, errAtoi = strconv.Atoi(priorityStr)
			if errAtoi != nil {

				log.Print("Warn: invalid priority value in " + node.DebugTag() + ", msg:" + errAtoi.Error())
			}
		}

		if src := node.Attributes.Get("src"); src != "" {

			if strings.HasPrefix(src, "http") || strings.HasPrefix(src, "//") {
				asset, err := t.RegisterAssetJsURL(src)
				if err != nil {
					return nil, err
				}

				if value := node.Attributes.Get("integrity"); value != "" {
					asset.Integrity = value
				}

				if value := node.Attributes.Get("crossorigin"); value != "" {
					asset.CrossOrigin = value
				}

				if value := node.Attributes.Get("referrerpolicy"); value != "" {
					asset.ReferrerPolicy = value
				}

				asset.Priority = priority

				assets = append(assets, asset.Name)
			} else {
				asset, err := t.RegisterAssetJsFilepath(path.Join(path.Dir(node.File), src))
				if err != nil {
					return nil, err
				}

				asset.Priority = priority

				assets = append(assets, asset.Name)
			}
		} else {
			inlineJs, inlineJsErr := jsc.Compile(node.Parent, node, t.Sequence)
			if inlineJsErr != nil {
				return nil, inlineJsErr
			}
			if inlineJs != nil {
				asset := t.RegisterAssetJsContent(inlineJs.Content)
				asset.Priority = priority
				assets = append(assets, asset.Name)
			}
		}

		if assets == nil {
			return nil, nil
		}

		node.FirstChild = nil
		node.LastChild = nil

		return &sht.DirectiveMethods{
			Process: func(scope *sht.Scope, attrs *sht.Attributes, transclude sht.TranscludeFunc) *sht.Rendered {

				return &sht.Rendered{Assets: assets}
			},
		}, nil
	},
}

Script It handles scripts that are not inside components, which must therefore be executed after page load

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL