turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on v1.0.2 · k33g · 15h ago
html.go · 142 lines · 3.9 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package syntax

import "strings"

// highlightHTML colours an HTML document, one slice of spans per line.
//
// The contents of <script> and <style> are left as plain text rather than
// coloured as JavaScript and CSS. Doing that means following the element to
// its closing tag across lines and mapping another scanner's columns back out,
// and there is no CSS scanner here to reach for anyway.
func highlightHTML(src string) [][]Span {
	return ScanLines(src, scanHTMLLine)
}

// htmlCarry is what a line can leave open. Only a comment spans lines in what
// this scanner recognises.
type htmlCarry uint8

const (
	htmlGround htmlCarry = iota
	htmlInComment
)

// scanHTMLLine returns the spans of one line, and what is left open.
func scanHTMLLine(line []rune, carry htmlCarry) ([]Span, htmlCarry) {
	s := &LineScanner{line: line}

	if carry == htmlInComment && !FinishBlockComment(s, "-->", ClassComment) {
		return s.spans, htmlInComment
	}

	for !s.AtEnd() {
		if openComment := stepHTML(s); openComment {
			return s.spans, htmlInComment
		}
	}
	return s.spans, htmlGround
}

// stepHTML recognises whatever starts at the current position, and reports
// whether it left a comment open at the end of the line.
func stepHTML(s *LineScanner) bool {
	switch {
	case s.HasPrefix(0, "<!--"):
		return !OpenBlockComment(s, "<!--", "-->", ClassComment)
	case s.HasPrefix(0, "<!"):
		takeDoctype(s)
	case s.line[s.pos] == '<':
		takeTag(s)
	case s.line[s.pos] == '&':
		takeEntity(s)
	default:
		s.pos++
	}
	return false
}

// takeDoctype colours <!DOCTYPE html> and the other declarations.
func takeDoctype(s *LineScanner) {
	start := s.pos
	for !s.AtEnd() && s.line[s.pos] != '>' {
		s.pos++
	}
	if !s.AtEnd() {
		s.pos++ // the closing bracket
	}
	s.Emit(start, s.pos, ClassKeyword)
}

// takeEntity colours &amp; and its numeric forms.
//
// A bare ampersand is left alone: it is legal text, and colouring the rest of
// a paragraph after one would be worse than colouring nothing.
func takeEntity(s *LineScanner) {
	for at := s.pos + 1; at < len(s.line) && at <= s.pos+entityLimit; at++ {
		if s.line[at] == ';' {
			s.Take(at-s.pos+1, ClassConstant)
			return
		}
		if !IsWordRune(s.line[at]) && s.line[at] != '#' {
			break
		}
	}
	s.pos++
}

// entityLimit is how far past an ampersand a semicolon may be and still make
// an entity. The longest named entity in HTML is well under this.
const entityLimit = 32

// takeTag colours an element: its brackets and name as a tag, then the
// attributes inside it, up to the closing bracket.
func takeTag(s *LineScanner) {
	start := s.pos
	s.pos++ // the opening bracket
	if !s.AtEnd() && s.line[s.pos] == '/' {
		s.pos++ // a closing tag
	}

	for !s.AtEnd() && (IsWordRune(s.line[s.pos]) || s.line[s.pos] == '-' || s.line[s.pos] == ':') {
		s.pos++
	}
	s.Emit(start, s.pos, ClassTag)

	takeAttributes(s)
}

// takeAttributes colours what is between an element's name and its closing
// bracket.
func takeAttributes(s *LineScanner) {
	for !s.AtEnd() {
		switch {
		case s.line[s.pos] == '>':
			s.Take(1, ClassTag)
			return
		case s.HasPrefix(0, "/>"):
			s.Take(2, ClassTag)
			return
		case s.line[s.pos] == '"' || s.line[s.pos] == '\'':
			TakeQuoted(s, s.line[s.pos], ClassString)
		case s.line[s.pos] == '=':
			s.Take(1, ClassOperator)
		case IsWordRune(s.line[s.pos]) || s.line[s.pos] == '-' || s.line[s.pos] == ':' || s.line[s.pos] == '@':
			s.TakeWhile(ClassAttribute, isAttributeRune)
		default:
			s.pos++
		}
	}
}

// attributeExtras are the characters an attribute's name may hold besides
// letters, digits and underscores.
//
// Hyphens, colons, at-signs and dots are in because data-*, xlink:href and the
// framework spellings such as @click and v-bind.prop are all attribute names
// in the wild.
const attributeExtras = "-:@."

// isAttributeRune reports whether a rune can appear in an attribute's name.
func isAttributeRune(r rune) bool {
	return IsWordRune(r) || strings.ContainsRune(attributeExtras, r)
}