// Copyright (c) 2009-2022 The MathJax Consortium
// SPDX-License-Identifier: Apache-2.0
//
// This file is a Go translation and modification of MathJax 3.2.2.
// Sources: ts/input/tex/base/BaseMethods.ts (MmlToken/MmlTokenAllow),
// ts/input/tex/ParseUtil.ts (MmlFilterAttribute), and NodeUtil.ts.

package tex

import (
	
	
	

	
)

// JavaScript's non-Unicode /i [a-z] is ASCII, while its \s includes these
// Unicode whitespace characters. Go's regexp shorthand classes differ.
const mmlTokenSpace = `[\t\n\v\f\r \x{00A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}]`

var (
	mmlTokenLeadingSpace = regexp.MustCompile(`^` + mmlTokenSpace + `+`)
	mmlTokenAttribute    = regexp.MustCompile(`^([a-zA-Z]+)` + mmlTokenSpace + `*=` + mmlTokenSpace + `*('[^']*'|"[^"]*"|[^ ,]*)` + mmlTokenSpace + `*,?` + mmlTokenSpace + `*`)
)

func mmlTokenAllowed( string) bool {
	switch  {
	case "fontfamily", "fontsize", "fontweight", "fontstyle", "color", "background", "id", "class", "href", "style":
		return true
	}
	return false
}

func ( *parser) ( string) ([]*mml.Node, error) {
	, ,  := .readArgument(, false)
	if  != nil {
		return nil, 
	}
	, ,  := .readBrackets(nil)
	if  != nil {
		var  *Error
		if errors.As(, &) && .ID == "MissingCloseBracket" {
			return nil, texError(.ID, "Could not find closing ']' for argument to %s", "\\"+)
		}
		return nil, 
	}
	 = mmlTokenLeadingSpace.ReplaceAllString(, "")
	, ,  := .readArgument(, false)
	if  != nil {
		return nil, 
	}
	,  := texMMLFactory.Definition()
	if ! || !.Flags.Token {
		return nil, texError("NotMathMLToken", "%s is not a token element", )
	}
	 := .noteMO(node())
	var  mjSourceObject
	var  []string
	for  != "" {
		 := mmlTokenAttribute.FindStringSubmatch()
		if  == nil {
			return nil, texError("InvalidMathMLAttr", "Invalid MathML attribute: %s", )
		}
		 := [1]
		if !.Attributes.HasDefault() && !mmlTokenAllowed() {
			return nil, texError("UnknownAttrForElement", "%s is not a recognized attribute for %s", , )
		}
		 := [2]
		// The pinned unquote regexp uses '.', without dotAll. Quoted values
		// containing a JS line terminator therefore retain their quotes.
		if len() >= 2 && ([0] == '\'' || [0] == '"') && [len()-1] == [0] && !strings.ContainsAny([1:len()-1], "\n\r\u2028\u2029") {
			 = [1 : len()-1]
		}
		// MmlFilterAttribute is the identity function in the pinned package.
		if  != "" {
			var  any = 
			if strings.ToLower() == "true" {
				 = true
			} else if strings.ToLower() == "false" {
				 = false
			}
			 = append(, mjSourceProperty{Name: , Value: })
			 = append(, )
		}
		 = [len([0]):]
	}
	if len() != 0 {
		 = append(, mjSourceProperty{Name: "mjx-keep-attrs", Value: strings.Join(, " ")})
	}
	// Upstream's inherited-attribute pass trims MmlMspace's text child to
	// its zero arity. Do not retain that child in the compiled token.
	if .Flags.Arity != 0 {
		.SetChildren([]*mml.Node{mml.NewText()})
	}
	applySourceObject(, )
	return []*mml.Node{}, nil
}