github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/internal/go_templates/htmltemplate/context.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package template
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
    11  )
    12  
    13  // context describes the state an HTML parser must be in when it reaches the
    14  // portion of HTML produced by evaluating a particular template node.
    15  //
    16  // The zero value of type context is the start context for a template that
    17  // produces an HTML fragment as defined at
    18  // https://www.w3.org/TR/html5/syntax.html#the-end
    19  // where the context element is null.
    20  type context struct {
    21  	state   state
    22  	delim   delim
    23  	urlPart urlPart
    24  	jsCtx   jsCtx
    25  	attr    attr
    26  	element element
    27  	n       parse.Node // for range break/continue
    28  	err     *Error
    29  }
    30  
    31  func (c context) String() string {
    32  	var err error
    33  	if c.err != nil {
    34  		err = c.err
    35  	}
    36  	return fmt.Sprintf("{%v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.attr, c.element, err)
    37  }
    38  
    39  // eq reports whether two contexts are equal.
    40  func (c context) eq(d context) bool {
    41  	return c.state == d.state &&
    42  		c.delim == d.delim &&
    43  		c.urlPart == d.urlPart &&
    44  		c.jsCtx == d.jsCtx &&
    45  		c.attr == d.attr &&
    46  		c.element == d.element &&
    47  		c.err == d.err
    48  }
    49  
    50  // mangle produces an identifier that includes a suffix that distinguishes it
    51  // from template names mangled with different contexts.
    52  func (c context) mangle(templateName string) string {
    53  	// The mangled name for the default context is the input templateName.
    54  	if c.state == stateText {
    55  		return templateName
    56  	}
    57  	s := templateName + "$htmltemplate_" + c.state.String()
    58  	if c.delim != delimNone {
    59  		s += "_" + c.delim.String()
    60  	}
    61  	if c.urlPart != urlPartNone {
    62  		s += "_" + c.urlPart.String()
    63  	}
    64  	if c.jsCtx != jsCtxRegexp {
    65  		s += "_" + c.jsCtx.String()
    66  	}
    67  	if c.attr != attrNone {
    68  		s += "_" + c.attr.String()
    69  	}
    70  	if c.element != elementNone {
    71  		s += "_" + c.element.String()
    72  	}
    73  	return s
    74  }
    75  
    76  // state describes a high-level HTML parser state.
    77  //
    78  // It bounds the top of the element stack, and by extension the HTML insertion
    79  // mode, but also contains state that does not correspond to anything in the
    80  // HTML5 parsing algorithm because a single token production in the HTML
    81  // grammar may contain embedded actions in a template. For instance, the quoted
    82  // HTML attribute produced by
    83  //
    84  //	<div title="Hello {{.World}}">
    85  //
    86  // is a single token in HTML's grammar but in a template spans several nodes.
    87  type state uint8
    88  
    89  //go:generate stringer -type state
    90  
    91  const (
    92  	// stateText is parsed character data. An HTML parser is in
    93  	// this state when its parse position is outside an HTML tag,
    94  	// directive, comment, and special element body.
    95  	stateText state = iota
    96  	// stateTag occurs before an HTML attribute or the end of a tag.
    97  	stateTag
    98  	// stateAttrName occurs inside an attribute name.
    99  	// It occurs between the ^'s in ` ^name^ = value`.
   100  	stateAttrName
   101  	// stateAfterName occurs after an attr name has ended but before any
   102  	// equals sign. It occurs between the ^'s in ` name^ ^= value`.
   103  	stateAfterName
   104  	// stateBeforeValue occurs after the equals sign but before the value.
   105  	// It occurs between the ^'s in ` name =^ ^value`.
   106  	stateBeforeValue
   107  	// stateHTMLCmt occurs inside an <!-- HTML comment -->.
   108  	stateHTMLCmt
   109  	// stateRCDATA occurs inside an RCDATA element (<textarea> or <title>)
   110  	// as described at https://www.w3.org/TR/html5/syntax.html#elements-0
   111  	stateRCDATA
   112  	// stateAttr occurs inside an HTML attribute whose content is text.
   113  	stateAttr
   114  	// stateURL occurs inside an HTML attribute whose content is a URL.
   115  	stateURL
   116  	// stateSrcset occurs inside an HTML srcset attribute.
   117  	stateSrcset
   118  	// stateJS occurs inside an event handler or script element.
   119  	stateJS
   120  	// stateJSDqStr occurs inside a JavaScript double quoted string.
   121  	stateJSDqStr
   122  	// stateJSSqStr occurs inside a JavaScript single quoted string.
   123  	stateJSSqStr
   124  	// stateJSBqStr occurs inside a JavaScript back quoted string.
   125  	stateJSBqStr
   126  	// stateJSRegexp occurs inside a JavaScript regexp literal.
   127  	stateJSRegexp
   128  	// stateJSBlockCmt occurs inside a JavaScript /* block comment */.
   129  	stateJSBlockCmt
   130  	// stateJSLineCmt occurs inside a JavaScript // line comment.
   131  	stateJSLineCmt
   132  	// stateJSHTMLOpenCmt occurs inside a JavaScript <!-- HTML-like comment.
   133  	stateJSHTMLOpenCmt
   134  	// stateJSHTMLCloseCmt occurs inside a JavaScript --> HTML-like comment.
   135  	stateJSHTMLCloseCmt
   136  	// stateCSS occurs inside a <style> element or style attribute.
   137  	stateCSS
   138  	// stateCSSDqStr occurs inside a CSS double quoted string.
   139  	stateCSSDqStr
   140  	// stateCSSSqStr occurs inside a CSS single quoted string.
   141  	stateCSSSqStr
   142  	// stateCSSDqURL occurs inside a CSS double quoted url("...").
   143  	stateCSSDqURL
   144  	// stateCSSSqURL occurs inside a CSS single quoted url('...').
   145  	stateCSSSqURL
   146  	// stateCSSURL occurs inside a CSS unquoted url(...).
   147  	stateCSSURL
   148  	// stateCSSBlockCmt occurs inside a CSS /* block comment */.
   149  	stateCSSBlockCmt
   150  	// stateCSSLineCmt occurs inside a CSS // line comment.
   151  	stateCSSLineCmt
   152  	// stateError is an infectious error state outside any valid
   153  	// HTML/CSS/JS construct.
   154  	stateError
   155  	// stateDead marks unreachable code after a {{break}} or {{continue}}.
   156  	stateDead
   157  )
   158  
   159  // isComment is true for any state that contains content meant for template
   160  // authors & maintainers, not for end-users or machines.
   161  func isComment(s state) bool {
   162  	switch s {
   163  	case stateHTMLCmt, stateJSBlockCmt, stateJSLineCmt, stateJSHTMLOpenCmt, stateJSHTMLCloseCmt, stateCSSBlockCmt, stateCSSLineCmt:
   164  		return true
   165  	}
   166  	return false
   167  }
   168  
   169  // isInTag return whether s occurs solely inside an HTML tag.
   170  func isInTag(s state) bool {
   171  	switch s {
   172  	case stateTag, stateAttrName, stateAfterName, stateBeforeValue, stateAttr:
   173  		return true
   174  	}
   175  	return false
   176  }
   177  
   178  // isInScriptLiteral returns true if s is one of the literal states within a
   179  // <script> tag, and as such occurances of "<!--", "<script", and "</script"
   180  // need to be treated specially.
   181  func isInScriptLiteral(s state) bool {
   182  	// Ignore the comment states (stateJSBlockCmt, stateJSLineCmt,
   183  	// stateJSHTMLOpenCmt, stateJSHTMLCloseCmt) because their content is already
   184  	// omitted from the output.
   185  	switch s {
   186  	case stateJSDqStr, stateJSSqStr, stateJSBqStr, stateJSRegexp:
   187  		return true
   188  	}
   189  	return false
   190  }
   191  
   192  // delim is the delimiter that will end the current HTML attribute.
   193  type delim uint8
   194  
   195  //go:generate stringer -type delim
   196  
   197  const (
   198  	// delimNone occurs outside any attribute.
   199  	delimNone delim = iota
   200  	// delimDoubleQuote occurs when a double quote (") closes the attribute.
   201  	delimDoubleQuote
   202  	// delimSingleQuote occurs when a single quote (') closes the attribute.
   203  	delimSingleQuote
   204  	// delimSpaceOrTagEnd occurs when a space or right angle bracket (>)
   205  	// closes the attribute.
   206  	delimSpaceOrTagEnd
   207  )
   208  
   209  // urlPart identifies a part in an RFC 3986 hierarchical URL to allow different
   210  // encoding strategies.
   211  type urlPart uint8
   212  
   213  //go:generate stringer -type urlPart
   214  
   215  const (
   216  	// urlPartNone occurs when not in a URL, or possibly at the start:
   217  	// ^ in "^http://auth/path?k=v#frag".
   218  	urlPartNone urlPart = iota
   219  	// urlPartPreQuery occurs in the scheme, authority, or path; between the
   220  	// ^s in "h^ttp://auth/path^?k=v#frag".
   221  	urlPartPreQuery
   222  	// urlPartQueryOrFrag occurs in the query portion between the ^s in
   223  	// "http://auth/path?^k=v#frag^".
   224  	urlPartQueryOrFrag
   225  	// urlPartUnknown occurs due to joining of contexts both before and
   226  	// after the query separator.
   227  	urlPartUnknown
   228  )
   229  
   230  // jsCtx determines whether a '/' starts a regular expression literal or a
   231  // division operator.
   232  type jsCtx uint8
   233  
   234  //go:generate stringer -type jsCtx
   235  
   236  const (
   237  	// jsCtxRegexp occurs where a '/' would start a regexp literal.
   238  	jsCtxRegexp jsCtx = iota
   239  	// jsCtxDivOp occurs where a '/' would start a division operator.
   240  	jsCtxDivOp
   241  	// jsCtxUnknown occurs where a '/' is ambiguous due to context joining.
   242  	jsCtxUnknown
   243  )
   244  
   245  // element identifies the HTML element when inside a start tag or special body.
   246  // Certain HTML element (for example <script> and <style>) have bodies that are
   247  // treated differently from stateText so the element type is necessary to
   248  // transition into the correct context at the end of a tag and to identify the
   249  // end delimiter for the body.
   250  type element uint8
   251  
   252  //go:generate stringer -type element
   253  
   254  const (
   255  	// elementNone occurs outside a special tag or special element body.
   256  	elementNone element = iota
   257  	// elementScript corresponds to the raw text <script> element
   258  	// with JS MIME type or no type attribute.
   259  	elementScript
   260  	// elementStyle corresponds to the raw text <style> element.
   261  	elementStyle
   262  	// elementTextarea corresponds to the RCDATA <textarea> element.
   263  	elementTextarea
   264  	// elementTitle corresponds to the RCDATA <title> element.
   265  	elementTitle
   266  )
   267  
   268  //go:generate stringer -type attr
   269  
   270  // attr identifies the current HTML attribute when inside the attribute,
   271  // that is, starting from stateAttrName until stateTag/stateText (exclusive).
   272  type attr uint8
   273  
   274  const (
   275  	// attrNone corresponds to a normal attribute or no attribute.
   276  	attrNone attr = iota
   277  	// attrScript corresponds to an event handler attribute.
   278  	attrScript
   279  	// attrScriptType corresponds to the type attribute in script HTML element
   280  	attrScriptType
   281  	// attrStyle corresponds to the style attribute whose value is CSS.
   282  	attrStyle
   283  	// attrURL corresponds to an attribute whose value is a URL.
   284  	attrURL
   285  	// attrSrcset corresponds to a srcset attribute.
   286  	attrSrcset
   287  )