2020-03-03 06:25:03 -05:00
|
|
|
// Copyright 2020 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package publisher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-02 07:23:25 -05:00
|
|
|
"regexp"
|
2020-03-03 06:25:03 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-05-13 07:10:32 -04:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2020-12-02 07:23:25 -05:00
|
|
|
|
|
|
|
"golang.org/x/net/html"
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2023-07-01 04:37:38 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2021-04-12 17:42:51 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
)
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
const eof = -1
|
|
|
|
|
|
|
|
var (
|
|
|
|
htmlJsonFixer = strings.NewReplacer(", ", "\n")
|
2023-07-19 11:32:19 -04:00
|
|
|
jsonAttrRe = regexp.MustCompile(`'?(.*?)'?:\s.*`)
|
2021-05-13 07:10:32 -04:00
|
|
|
classAttrRe = regexp.MustCompile(`(?i)^class$|transition`)
|
|
|
|
|
|
|
|
skipInnerElementRe = regexp.MustCompile(`(?i)^(pre|textarea|script|style)`)
|
|
|
|
skipAllElementRe = regexp.MustCompile(`(?i)^!DOCTYPE`)
|
|
|
|
|
|
|
|
exceptionList = map[string]bool{
|
|
|
|
"thead": true,
|
|
|
|
"tbody": true,
|
|
|
|
"tfoot": true,
|
|
|
|
"td": true,
|
|
|
|
"tr": true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-07-02 05:02:47 -04:00
|
|
|
func newHTMLElementsCollector(conf config.BuildStats) *htmlElementsCollector {
|
2021-05-13 07:10:32 -04:00
|
|
|
return &htmlElementsCollector{
|
2023-07-01 04:37:38 -04:00
|
|
|
conf: conf,
|
2021-05-13 07:10:32 -04:00
|
|
|
elementSet: make(map[string]bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHTMLElementsCollectorWriter(collector *htmlElementsCollector) *htmlElementsCollectorWriter {
|
|
|
|
w := &htmlElementsCollectorWriter{
|
|
|
|
collector: collector,
|
|
|
|
state: htmlLexStart,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.defaultLexElementInside = w.lexElementInside(htmlLexStart)
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2020-03-03 06:25:03 -05:00
|
|
|
// HTMLElements holds lists of tags and attribute values for classes and id.
|
|
|
|
type HTMLElements struct {
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
Classes []string `json:"classes"`
|
|
|
|
IDs []string `json:"ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTMLElements) Merge(other HTMLElements) {
|
|
|
|
h.Tags = append(h.Tags, other.Tags...)
|
|
|
|
h.Classes = append(h.Classes, other.Classes...)
|
|
|
|
h.IDs = append(h.IDs, other.IDs...)
|
|
|
|
|
|
|
|
h.Tags = helpers.UniqueStringsReuse(h.Tags)
|
|
|
|
h.Classes = helpers.UniqueStringsReuse(h.Classes)
|
|
|
|
h.IDs = helpers.UniqueStringsReuse(h.IDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTMLElements) Sort() {
|
|
|
|
sort.Strings(h.Tags)
|
|
|
|
sort.Strings(h.Classes)
|
|
|
|
sort.Strings(h.IDs)
|
|
|
|
}
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
type htmlElement struct {
|
|
|
|
Tag string
|
|
|
|
Classes []string
|
|
|
|
IDs []string
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:42:51 -04:00
|
|
|
type htmlElementsCollector struct {
|
2023-07-02 05:02:47 -04:00
|
|
|
conf config.BuildStats
|
2023-07-01 04:37:38 -04:00
|
|
|
|
2021-04-12 17:42:51 -04:00
|
|
|
// Contains the raw HTML string. We will get the same element
|
|
|
|
// several times, and want to avoid costly reparsing when this
|
|
|
|
// is used for aggregated data only.
|
|
|
|
elementSet map[string]bool
|
|
|
|
|
|
|
|
elements []htmlElement
|
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *htmlElementsCollector) getHTMLElements() HTMLElements {
|
|
|
|
var (
|
|
|
|
classes []string
|
|
|
|
ids []string
|
|
|
|
tags []string
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, el := range c.elements {
|
|
|
|
classes = append(classes, el.Classes...)
|
|
|
|
ids = append(ids, el.IDs...)
|
2023-07-02 05:02:47 -04:00
|
|
|
if !c.conf.DisableTags {
|
2023-07-01 04:37:38 -04:00
|
|
|
tags = append(tags, el.Tag)
|
|
|
|
}
|
2021-04-12 17:42:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
classes = helpers.UniqueStringsSorted(classes)
|
|
|
|
ids = helpers.UniqueStringsSorted(ids)
|
|
|
|
tags = helpers.UniqueStringsSorted(tags)
|
|
|
|
|
|
|
|
els := HTMLElements{
|
|
|
|
Classes: classes,
|
|
|
|
IDs: ids,
|
|
|
|
Tags: tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
return els
|
|
|
|
}
|
|
|
|
|
|
|
|
type htmlElementsCollectorWriter struct {
|
2020-03-03 06:25:03 -05:00
|
|
|
collector *htmlElementsCollector
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
r rune // Current rune
|
|
|
|
width int // The width in bytes of r
|
|
|
|
input []byte // The current slice written to Write
|
|
|
|
pos int // The current position in input
|
|
|
|
|
|
|
|
err error
|
2021-05-13 07:10:32 -04:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
inQuote rune
|
|
|
|
|
|
|
|
buff bytes.Buffer
|
|
|
|
|
|
|
|
// Current state
|
|
|
|
state htmlCollectorStateFunc
|
|
|
|
|
|
|
|
// Precompiled state funcs
|
|
|
|
defaultLexElementInside htmlCollectorStateFunc
|
2021-05-18 21:45:36 -04:00
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
// Write collects HTML elements from p, which must contain complete runes.
|
|
|
|
func (w *htmlElementsCollectorWriter) Write(p []byte) (int, error) {
|
|
|
|
if p == nil {
|
|
|
|
return 0, nil
|
2021-05-18 21:45:36 -04:00
|
|
|
}
|
2021-04-12 17:42:51 -04:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
w.input = p
|
|
|
|
|
|
|
|
for {
|
|
|
|
w.r = w.next()
|
|
|
|
if w.r == eof || w.r == utf8.RuneError {
|
|
|
|
break
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
w.state = w.state(w)
|
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
w.pos = 0
|
|
|
|
w.input = nil
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *htmlElementsCollectorWriter) backup() {
|
|
|
|
l.pos -= l.width
|
|
|
|
l.r, _ = utf8.DecodeRune(l.input[l.pos:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *htmlElementsCollectorWriter) consumeBuffUntil(condition func() bool, resolve htmlCollectorStateFunc) htmlCollectorStateFunc {
|
|
|
|
var s htmlCollectorStateFunc
|
|
|
|
s = func(*htmlElementsCollectorWriter) htmlCollectorStateFunc {
|
|
|
|
w.buff.WriteRune(w.r)
|
|
|
|
if condition() {
|
|
|
|
w.buff.Reset()
|
|
|
|
return resolve
|
2021-04-12 17:42:51 -04:00
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
func (w *htmlElementsCollectorWriter) consumeRuneUntil(condition func(r rune) bool, resolve htmlCollectorStateFunc) htmlCollectorStateFunc {
|
|
|
|
var s htmlCollectorStateFunc
|
|
|
|
s = func(*htmlElementsCollectorWriter) htmlCollectorStateFunc {
|
|
|
|
if condition(w.r) {
|
|
|
|
return resolve
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2020-11-27 02:46:58 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
// Starts with e.g. "<body " or "<div"
|
|
|
|
func (w *htmlElementsCollectorWriter) lexElementInside(resolve htmlCollectorStateFunc) htmlCollectorStateFunc {
|
|
|
|
var s htmlCollectorStateFunc
|
|
|
|
s = func(w *htmlElementsCollectorWriter) htmlCollectorStateFunc {
|
|
|
|
w.buff.WriteRune(w.r)
|
|
|
|
|
|
|
|
// Skip any text inside a quote.
|
|
|
|
if w.r == '\'' || w.r == '"' {
|
|
|
|
if w.inQuote == w.r {
|
|
|
|
w.inQuote = 0
|
|
|
|
} else if w.inQuote == 0 {
|
|
|
|
w.inQuote = w.r
|
2021-04-20 10:50:03 -04:00
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
}
|
2021-04-20 10:50:03 -04:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
if w.inQuote != 0 {
|
|
|
|
return s
|
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
if w.r == '>' {
|
2021-04-20 10:50:03 -04:00
|
|
|
|
|
|
|
// Work with the bytes slice as long as it's practical,
|
|
|
|
// to save memory allocations.
|
|
|
|
b := w.buff.Bytes()
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
defer func() {
|
|
|
|
w.buff.Reset()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// First check if we have processed this element before.
|
|
|
|
w.collector.mu.RLock()
|
|
|
|
|
2021-04-20 10:50:03 -04:00
|
|
|
seen := w.collector.elementSet[string(b)]
|
2021-04-12 17:42:51 -04:00
|
|
|
w.collector.mu.RUnlock()
|
|
|
|
if seen {
|
2021-05-13 07:10:32 -04:00
|
|
|
return resolve
|
2021-04-20 10:50:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s := w.buff.String()
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
if s == "" {
|
|
|
|
return resolve
|
2021-04-12 17:42:51 -04:00
|
|
|
}
|
2021-04-06 12:19:25 -04:00
|
|
|
|
2021-04-20 10:50:03 -04:00
|
|
|
// Parse each collected element.
|
2023-07-01 04:37:38 -04:00
|
|
|
el, err := w.parseHTMLElement(s)
|
2021-04-12 17:42:51 -04:00
|
|
|
if err != nil {
|
2021-05-13 07:10:32 -04:00
|
|
|
w.err = err
|
|
|
|
return resolve
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
2021-04-12 17:42:51 -04:00
|
|
|
|
2021-04-20 10:50:03 -04:00
|
|
|
// Write this tag to the element set.
|
2021-04-12 17:42:51 -04:00
|
|
|
w.collector.mu.Lock()
|
|
|
|
w.collector.elementSet[s] = true
|
|
|
|
w.collector.elements = append(w.collector.elements, el)
|
|
|
|
w.collector.mu.Unlock()
|
2021-05-13 07:10:32 -04:00
|
|
|
|
|
|
|
return resolve
|
|
|
|
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
|
|
|
|
return s
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
return s
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
func (l *htmlElementsCollectorWriter) next() rune {
|
|
|
|
if l.pos >= len(l.input) {
|
|
|
|
l.width = 0
|
|
|
|
return eof
|
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
runeValue, runeWidth := utf8.DecodeRune(l.input[l.pos:])
|
|
|
|
|
|
|
|
l.width = runeWidth
|
|
|
|
l.pos += l.width
|
|
|
|
return runeValue
|
2021-05-18 21:45:36 -04:00
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
// returns the next state in HTML element scanner.
|
|
|
|
type htmlCollectorStateFunc func(*htmlElementsCollectorWriter) htmlCollectorStateFunc
|
|
|
|
|
|
|
|
// At "<", buffer empty.
|
|
|
|
// Potentially starting a HTML element.
|
|
|
|
func htmlLexElementStart(w *htmlElementsCollectorWriter) htmlCollectorStateFunc {
|
|
|
|
if w.r == '>' || unicode.IsSpace(w.r) {
|
|
|
|
if w.buff.Len() < 2 || bytes.HasPrefix(w.buff.Bytes(), []byte("</")) {
|
|
|
|
w.buff.Reset()
|
|
|
|
return htmlLexStart
|
2020-09-28 16:17:36 -04:00
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
tagName := w.buff.Bytes()[1:]
|
2023-02-06 11:29:12 -05:00
|
|
|
isSelfClosing := tagName[len(tagName)-1] == '/'
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
switch {
|
2023-02-06 11:29:12 -05:00
|
|
|
case !isSelfClosing && skipInnerElementRe.Match(tagName):
|
2021-05-13 07:10:32 -04:00
|
|
|
// pre, script etc. We collect classes etc. on the surrounding
|
|
|
|
// element, but skip the inner content.
|
|
|
|
w.backup()
|
2021-05-13 07:10:32 -04:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
// tagName will be overwritten, so make a copy.
|
|
|
|
tagNameCopy := make([]byte, len(tagName))
|
|
|
|
copy(tagNameCopy, tagName)
|
|
|
|
|
|
|
|
return w.lexElementInside(
|
|
|
|
w.consumeBuffUntil(
|
|
|
|
func() bool {
|
|
|
|
if w.r != '>' {
|
|
|
|
return false
|
|
|
|
}
|
2023-02-05 09:14:30 -05:00
|
|
|
return isClosedByTag(w.buff.Bytes(), tagNameCopy)
|
2021-05-13 07:10:32 -04:00
|
|
|
},
|
|
|
|
htmlLexStart,
|
|
|
|
))
|
|
|
|
case skipAllElementRe.Match(tagName):
|
|
|
|
// E.g. "<!DOCTYPE ..."
|
|
|
|
w.buff.Reset()
|
|
|
|
return w.consumeRuneUntil(func(r rune) bool {
|
|
|
|
return r == '>'
|
|
|
|
}, htmlLexStart)
|
|
|
|
default:
|
|
|
|
w.backup()
|
|
|
|
return w.defaultLexElementInside
|
|
|
|
}
|
2021-04-12 17:42:51 -04:00
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
w.buff.WriteRune(w.r)
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
// If it's a comment, skip to its end.
|
|
|
|
if w.r == '-' && bytes.Equal(w.buff.Bytes(), []byte("<!--")) {
|
|
|
|
w.buff.Reset()
|
|
|
|
return htmlLexToEndOfComment
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
return htmlLexElementStart
|
2021-04-12 17:42:51 -04:00
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
// Entry state func.
|
|
|
|
// Looks for a opening bracket, '<'.
|
|
|
|
func htmlLexStart(w *htmlElementsCollectorWriter) htmlCollectorStateFunc {
|
|
|
|
if w.r == '<' {
|
|
|
|
w.backup()
|
|
|
|
w.buff.Reset()
|
|
|
|
return htmlLexElementStart
|
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
return htmlLexStart
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
// After "<!--", buff empty.
|
|
|
|
func htmlLexToEndOfComment(w *htmlElementsCollectorWriter) htmlCollectorStateFunc {
|
|
|
|
w.buff.WriteRune(w.r)
|
2020-03-03 06:25:03 -05:00
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
if w.r == '>' && bytes.HasSuffix(w.buff.Bytes(), []byte("-->")) {
|
|
|
|
// Done, start looking for HTML elements again.
|
|
|
|
return htmlLexStart
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
|
|
|
|
return htmlLexToEndOfComment
|
|
|
|
}
|
2021-04-20 10:50:03 -04:00
|
|
|
|
2023-07-01 04:37:38 -04:00
|
|
|
func (w *htmlElementsCollectorWriter) parseHTMLElement(elStr string) (el htmlElement, err error) {
|
|
|
|
conf := w.collector.conf
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
tagName := parseStartTag(elStr)
|
|
|
|
|
|
|
|
el.Tag = strings.ToLower(tagName)
|
|
|
|
tagNameToParse := el.Tag
|
2021-04-12 17:42:51 -04:00
|
|
|
|
|
|
|
// The net/html parser does not handle single table elements as input, e.g. tbody.
|
|
|
|
// We only care about the element/class/ids, so just store away the original tag name
|
|
|
|
// and pretend it's a <div>.
|
2021-05-13 07:10:32 -04:00
|
|
|
if exceptionList[el.Tag] {
|
2021-04-12 17:42:51 -04:00
|
|
|
elStr = strings.Replace(elStr, tagName, "div", 1)
|
2021-05-13 07:10:32 -04:00
|
|
|
tagNameToParse = "div"
|
2021-04-12 17:42:51 -04:00
|
|
|
}
|
|
|
|
|
2020-03-03 06:25:03 -05:00
|
|
|
n, err := html.Parse(strings.NewReader(elStr))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-13 07:10:32 -04:00
|
|
|
|
2020-03-03 06:25:03 -05:00
|
|
|
var walk func(*html.Node)
|
|
|
|
walk = func(n *html.Node) {
|
2021-05-13 07:10:32 -04:00
|
|
|
if n.Type == html.ElementNode && n.Data == tagNameToParse {
|
2020-03-03 06:25:03 -05:00
|
|
|
for _, a := range n.Attr {
|
|
|
|
switch {
|
|
|
|
case strings.EqualFold(a.Key, "id"):
|
|
|
|
// There should be only one, but one never knows...
|
2023-07-02 05:02:47 -04:00
|
|
|
if !conf.DisableIDs {
|
2023-07-01 04:37:38 -04:00
|
|
|
el.IDs = append(el.IDs, a.Val)
|
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
default:
|
2023-07-02 05:02:47 -04:00
|
|
|
if conf.DisableClasses {
|
2023-07-01 04:37:38 -04:00
|
|
|
continue
|
|
|
|
}
|
2023-07-19 11:32:19 -04:00
|
|
|
|
2020-07-23 08:18:13 -04:00
|
|
|
if classAttrRe.MatchString(a.Key) {
|
2020-03-03 06:25:03 -05:00
|
|
|
el.Classes = append(el.Classes, strings.Fields(a.Val)...)
|
|
|
|
} else {
|
|
|
|
key := strings.ToLower(a.Key)
|
|
|
|
val := strings.TrimSpace(a.Val)
|
2023-07-19 11:32:19 -04:00
|
|
|
|
|
|
|
if strings.Contains(key, ":class") {
|
|
|
|
if strings.HasPrefix(val, "{") {
|
|
|
|
// This looks like a Vue or AlpineJS class binding.
|
|
|
|
val = htmlJsonFixer.Replace(strings.Trim(val, "{}"))
|
|
|
|
lines := strings.Split(val, "\n")
|
|
|
|
for i, l := range lines {
|
|
|
|
lines[i] = strings.TrimSpace(l)
|
|
|
|
}
|
|
|
|
val = strings.Join(lines, "\n")
|
|
|
|
|
|
|
|
val = jsonAttrRe.ReplaceAllString(val, "$1")
|
|
|
|
|
|
|
|
el.Classes = append(el.Classes, strings.Fields(val)...)
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
2023-07-19 11:32:19 -04:00
|
|
|
// Also add single quoted strings.
|
|
|
|
// This may introduce some false positives, but it covers some missing cases in the above.
|
|
|
|
// E.g. AlpinesJS' :class="isTrue 'class1' : 'class2'"
|
|
|
|
el.Classes = append(el.Classes, extractSingleQuotedStrings(val)...)
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
walk(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walk(n)
|
|
|
|
|
2021-05-13 07:10:32 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variants of s
|
2023-02-05 09:14:30 -05:00
|
|
|
//
|
|
|
|
// <body class="b a">
|
|
|
|
// <div>
|
2021-05-13 07:10:32 -04:00
|
|
|
func parseStartTag(s string) string {
|
|
|
|
spaceIndex := strings.IndexFunc(s, func(r rune) bool {
|
|
|
|
return unicode.IsSpace(r)
|
|
|
|
})
|
|
|
|
|
|
|
|
if spaceIndex == -1 {
|
2023-02-06 11:29:12 -05:00
|
|
|
s = s[1 : len(s)-1]
|
|
|
|
} else {
|
|
|
|
s = s[1:spaceIndex]
|
2021-04-12 17:42:51 -04:00
|
|
|
}
|
|
|
|
|
2023-02-06 11:29:12 -05:00
|
|
|
if s[len(s)-1] == '/' {
|
|
|
|
// Self closing.
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
2023-02-05 09:14:30 -05:00
|
|
|
|
|
|
|
// isClosedByTag reports whether b ends with a closing tag for tagName.
|
|
|
|
func isClosedByTag(b, tagName []byte) bool {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if b[len(b)-1] != '>' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lo int
|
|
|
|
hi int
|
|
|
|
|
|
|
|
state int
|
|
|
|
inWord bool
|
|
|
|
)
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for i := len(b) - 2; i >= 0; i-- {
|
|
|
|
switch {
|
|
|
|
case b[i] == '<':
|
|
|
|
if state != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
state = 2
|
|
|
|
break LOOP
|
|
|
|
case b[i] == '/':
|
|
|
|
if state != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
state++
|
|
|
|
if inWord {
|
|
|
|
lo = i + 1
|
|
|
|
inWord = false
|
|
|
|
}
|
|
|
|
case isSpace(b[i]):
|
|
|
|
if inWord {
|
|
|
|
lo = i + 1
|
|
|
|
inWord = false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if !inWord {
|
|
|
|
hi = i + 1
|
|
|
|
inWord = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 11:29:12 -05:00
|
|
|
if state != 2 || lo >= hi {
|
2023-02-05 09:14:30 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.EqualFold(tagName, b[lo:hi])
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSpace(b byte) bool {
|
|
|
|
return b == ' ' || b == '\t' || b == '\n'
|
|
|
|
}
|
2023-07-19 11:32:19 -04:00
|
|
|
|
|
|
|
func extractSingleQuotedStrings(s string) []string {
|
|
|
|
var (
|
|
|
|
inQuote bool
|
|
|
|
lo int
|
|
|
|
hi int
|
|
|
|
)
|
|
|
|
|
|
|
|
var words []string
|
|
|
|
|
|
|
|
for i, r := range s {
|
|
|
|
switch {
|
|
|
|
case r == '\'':
|
|
|
|
if !inQuote {
|
|
|
|
inQuote = true
|
|
|
|
lo = i + 1
|
|
|
|
} else {
|
|
|
|
inQuote = false
|
|
|
|
hi = i
|
|
|
|
words = append(words, strings.Fields(s[lo:hi])...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return words
|
|
|
|
}
|