This is the No-Pre parser that uses the golang XML decoder system, which strips all whitespace and is thus unsuitable for any Pre case
(str []byte, font *styles.FontRender, txtSty *styles.Text, ctxt *units.Context, cssAgg map[string]any)
| 427 | // This is the No-Pre parser that uses the golang XML decoder system, which |
| 428 | // strips all whitespace and is thus unsuitable for any Pre case |
| 429 | func (tr *Text) SetHTMLNoPre(str []byte, font *styles.FontRender, txtSty *styles.Text, ctxt *units.Context, cssAgg map[string]any) { |
| 430 | // errstr := "core.Text SetHTML" |
| 431 | sz := len(str) |
| 432 | if sz == 0 { |
| 433 | return |
| 434 | } |
| 435 | tr.Spans = make([]Span, 1) |
| 436 | tr.Links = nil |
| 437 | curSp := &(tr.Spans[0]) |
| 438 | initsz := min(sz, 1020) |
| 439 | curSp.Init(initsz) |
| 440 | |
| 441 | spcstr := bytes.Join(bytes.Fields(str), []byte(" ")) |
| 442 | |
| 443 | reader := bytes.NewReader(spcstr) |
| 444 | decoder := xml.NewDecoder(reader) |
| 445 | decoder.Strict = false |
| 446 | decoder.AutoClose = xml.HTMLAutoClose |
| 447 | decoder.Entity = xml.HTMLEntity |
| 448 | decoder.CharsetReader = charset.NewReaderLabel |
| 449 | |
| 450 | font.Font = OpenFont(font, ctxt) |
| 451 | |
| 452 | // set when a </p> is encountered |
| 453 | nextIsParaStart := false |
| 454 | curLinkIndex := -1 // if currently processing an <a> link element |
| 455 | |
| 456 | fstack := make([]*styles.FontRender, 1, 10) |
| 457 | fstack[0] = font |
| 458 | for { |
| 459 | t, err := decoder.Token() |
| 460 | if err != nil { |
| 461 | if err == io.EOF { |
| 462 | break |
| 463 | } |
| 464 | // log.Printf("%v parsing error: %v for string\n%v\n", errstr, err, string(str)) |
| 465 | break |
| 466 | } |
| 467 | switch se := t.(type) { |
| 468 | case xml.StartElement: |
| 469 | curf := fstack[len(fstack)-1] |
| 470 | fs := *curf |
| 471 | nm := strings.ToLower(se.Name.Local) |
| 472 | curLinkIndex = -1 |
| 473 | if !SetHTMLSimpleTag(nm, &fs, ctxt, cssAgg) { |
| 474 | switch nm { |
| 475 | case "a": |
| 476 | fs.Color = colors.Scheme.Primary.Base |
| 477 | fs.SetDecoration(styles.Underline) |
| 478 | curLinkIndex = len(tr.Links) |
| 479 | tl := &TextLink{StartSpan: len(tr.Spans) - 1, StartIndex: len(curSp.Text)} |
| 480 | sprop := make(map[string]any, len(se.Attr)) |
| 481 | tl.Properties = sprop |
| 482 | for _, attr := range se.Attr { |
| 483 | if attr.Name.Local == "href" { |
| 484 | tl.URL = attr.Value |
| 485 | } |
| 486 | sprop[attr.Name.Local] = attr.Value |
no test coverage detected