Skip to content

Commit

Permalink
fix: count printed bytes so that we can skip over them later
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Jul 12, 2024
1 parent 71636d1 commit d5ffa2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions internal/printer/print-to-tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const (
func getTextType(n *astro.Node) TextType {
if script := n.Closest(isScript); script != nil {
attr := astro.GetAttribute(script, "type")
if attr == nil || (attr != nil && ScriptMimeTypes[strings.ToLower(attr.Val)]) {
if attr == nil || ScriptMimeTypes[strings.ToLower(attr.Val)] {
return ScriptText
}
}
Expand Down Expand Up @@ -513,10 +513,10 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
endLoc = a.ValLoc.Start
}
if _, ok := htmlEvents[a.Key]; ok {
p.addTSXScript(a.ValLoc.Start, endLoc, a.Val, "event-attribute")
p.addTSXScript(a.ValLoc.Start-p.bytesToSkip, endLoc-p.bytesToSkip, a.Val, "event-attribute")
}
if a.Key == "style" {
p.addTSXStyle(a.ValLoc.Start, endLoc, a.Val, "style-attribute")
p.addTSXStyle(a.ValLoc.Start-p.bytesToSkip, endLoc-p.bytesToSkip, a.Val, "style-attribute")
}
case astro.EmptyAttribute:
p.print(a.Key)
Expand Down Expand Up @@ -679,7 +679,7 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
}
p.print(">")

startTagEnd := endLoc
startTagEnd := endLoc - p.bytesToSkip

// Render any child nodes
for c := n.FirstChild; c != nil; c = c.NextSibling {
Expand All @@ -693,10 +693,10 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI

if n.FirstChild != nil && (n.DataAtom == atom.Script || n.DataAtom == atom.Style) {
if n.DataAtom == atom.Script {
p.addTSXScript(startTagEnd, endLoc, n.FirstChild.Data, getScriptTypeForNode(*n))
p.addTSXScript(startTagEnd, endLoc-p.bytesToSkip, n.FirstChild.Data, getScriptTypeForNode(*n))
}
if n.DataAtom == atom.Style {
p.addTSXStyle(startTagEnd, endLoc, n.FirstChild.Data, "tag")
p.addTSXStyle(startTagEnd, endLoc-p.bytesToSkip, n.FirstChild.Data, "tag")
}
}

Expand Down
6 changes: 6 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type printer struct {

// Optional, used only for TSX output
ranges TSXRanges
// Keep track of how many multi-byte characters we've printed so that they can be skipped whenever we need a character-based index
// This could be directly in the token / node information, however this would require a fairly large refactor
bytesToSkip int
}

var TEMPLATE_TAG = "$$render"
Expand Down Expand Up @@ -115,6 +118,9 @@ func (p *printer) printTextWithSourcemap(text string, l loc.Loc) {
continue
}
_, nextCharByteSize := utf8.DecodeRuneInString(text[pos:])
if nextCharByteSize > 1 {
p.bytesToSkip += nextCharByteSize - 1
}
p.addSourceMapping(loc.Loc{Start: start})
p.print(string(c))
start += nextCharByteSize
Expand Down

0 comments on commit d5ffa2c

Please sign in to comment.