MCPcopy Create free account
hub / github.com/cogentcore/core / SetHTMLNoPre

Method SetHTMLNoPre

paint/text.go:429–583  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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
429func (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

Callers 2

SetHTMLMethod · 0.95
SetHTMLBytesMethod · 0.95

Calls 12

SetStylePropertiesXMLFunction · 0.92
SubPropertiesFunction · 0.92
OpenFontFunction · 0.85
SetHTMLSimpleTagFunction · 0.85
FontStyleCSSFunction · 0.85
SetDecorationMethod · 0.80
AppendRuneMethod · 0.80
SetNewParaMethod · 0.80
AppendStringMethod · 0.80
InitMethod · 0.65
TokenMethod · 0.45
SetStylePropertiesMethod · 0.45

Tested by

no test coverage detected