github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/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  	// stateJSRegexp occurs inside a JavaScript regexp literal.
   125  	stateJSRegexp
   126  	// stateJSBlockCmt occurs inside a JavaScript /* block comment */.
   127  	stateJSBlockCmt
   128  	// stateJSLineCmt occurs inside a JavaScript // line comment.
   129  	stateJSLineCmt
   130  	// stateCSS occurs inside a <style> element or style attribute.
   131  	stateCSS
   132  	// stateCSSDqStr occurs inside a CSS double quoted string.
   133  	stateCSSDqStr
   134  	// stateCSSSqStr occurs inside a CSS single quoted string.
   135  	stateCSSSqStr
   136  	// stateCSSDqURL occurs inside a CSS double quoted url("...").
   137  	stateCSSDqURL
   138  	// stateCSSSqURL occurs inside a CSS single quoted url('...').
   139  	stateCSSSqURL
   140  	// stateCSSURL occurs inside a CSS unquoted url(...).
   141  	stateCSSURL
   142  	// stateCSSBlockCmt occurs inside a CSS /* block comment */.
   143  	stateCSSBlockCmt
   144  	// stateCSSLineCmt occurs inside a CSS // line comment.
   145  	stateCSSLineCmt
   146  	// stateError is an infectious error state outside any valid
   147  	// HTML/CSS/JS construct.
   148  	stateError
   149  	// stateDead marks unreachable code after a {{break}} or {{continue}}.
   150  	stateDead
   151  )
   152  
   153  // isComment is true for any state that contains content meant for template
   154  // authors & maintainers, not for end-users or machines.
   155  func isComment(s state) bool {
   156  	switch s {
   157  	case stateHTMLCmt, stateJSBlockCmt, stateJSLineCmt, stateCSSBlockCmt, stateCSSLineCmt:
   158  		return true
   159  	}
   160  	return false
   161  }
   162  
   163  // isInTag return whether s occurs solely inside an HTML tag.
   164  func isInTag(s state) bool {
   165  	switch s {
   166  	case stateTag, stateAttrName, stateAfterName, stateBeforeValue, stateAttr:
   167  		return true
   168  	}
   169  	return false
   170  }
   171  
   172  // delim is the delimiter that will end the current HTML attribute.
   173  type delim uint8
   174  
   175  //go:generate stringer -type delim
   176  
   177  const (
   178  	// delimNone occurs outside any attribute.
   179  	delimNone delim = iota
   180  	// delimDoubleQuote occurs when a double quote (") closes the attribute.
   181  	delimDoubleQuote
   182  	// delimSingleQuote occurs when a single quote (') closes the attribute.
   183  	delimSingleQuote
   184  	// delimSpaceOrTagEnd occurs when a space or right angle bracket (>)
   185  	// closes the attribute.
   186  	delimSpaceOrTagEnd
   187  )
   188  
   189  // urlPart identifies a part in an RFC 3986 hierarchical URL to allow different
   190  // encoding strategies.
   191  type urlPart uint8
   192  
   193  //go:generate stringer -type urlPart
   194  
   195  const (
   196  	// urlPartNone occurs when not in a URL, or possibly at the start:
   197  	// ^ in "^http://auth/path?k=v#frag".
   198  	urlPartNone urlPart = iota
   199  	// urlPartPreQuery occurs in the scheme, authority, or path; between the
   200  	// ^s in "h^ttp://auth/path^?k=v#frag".
   201  	urlPartPreQuery
   202  	// urlPartQueryOrFrag occurs in the query portion between the ^s in
   203  	// "http://auth/path?^k=v#frag^".
   204  	urlPartQueryOrFrag
   205  	// urlPartUnknown occurs due to joining of contexts both before and
   206  	// after the query separator.
   207  	urlPartUnknown
   208  )
   209  
   210  // jsCtx determines whether a '/' starts a regular expression literal or a
   211  // division operator.
   212  type jsCtx uint8
   213  
   214  //go:generate stringer -type jsCtx
   215  
   216  const (
   217  	// jsCtxRegexp occurs where a '/' would start a regexp literal.
   218  	jsCtxRegexp jsCtx = iota
   219  	// jsCtxDivOp occurs where a '/' would start a division operator.
   220  	jsCtxDivOp
   221  	// jsCtxUnknown occurs where a '/' is ambiguous due to context joining.
   222  	jsCtxUnknown
   223  )
   224  
   225  // element identifies the HTML element when inside a start tag or special body.
   226  // Certain HTML element (for example <script> and <style>) have bodies that are
   227  // treated differently from stateText so the element type is necessary to
   228  // transition into the correct context at the end of a tag and to identify the
   229  // end delimiter for the body.
   230  type element uint8
   231  
   232  //go:generate stringer -type element
   233  
   234  const (
   235  	// elementNone occurs outside a special tag or special element body.
   236  	elementNone element = iota
   237  	// elementScript corresponds to the raw text <script> element
   238  	// with JS MIME type or no type attribute.
   239  	elementScript
   240  	// elementStyle corresponds to the raw text <style> element.
   241  	elementStyle
   242  	// elementTextarea corresponds to the RCDATA <textarea> element.
   243  	elementTextarea
   244  	// elementTitle corresponds to the RCDATA <title> element.
   245  	elementTitle
   246  )
   247  
   248  //go:generate stringer -type attr
   249  
   250  // attr identifies the current HTML attribute when inside the attribute,
   251  // that is, starting from stateAttrName until stateTag/stateText (exclusive).
   252  type attr uint8
   253  
   254  const (
   255  	// attrNone corresponds to a normal attribute or no attribute.
   256  	attrNone attr = iota
   257  	// attrScript corresponds to an event handler attribute.
   258  	attrScript
   259  	// attrScriptType corresponds to the type attribute in script HTML element
   260  	attrScriptType
   261  	// attrStyle corresponds to the style attribute whose value is CSS.
   262  	attrStyle
   263  	// attrURL corresponds to an attribute whose value is a URL.
   264  	attrURL
   265  	// attrSrcset corresponds to a srcset attribute.
   266  	attrSrcset
   267  )