591.tag-validator.go 907 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. func isValid(code string) bool {
  2. st := make([]string, 0)
  3. for i := 0; i < len(code); i++ {
  4. if 0 < i && len(st) == 0 { // Unmatched content
  5. return false
  6. }
  7. if strings.HasPrefix(code[i:], "<![CDATA[") { // Parse cdata
  8. j := strings.Index(code[i+9:], "]]>")
  9. if j < 0 {
  10. return false
  11. }
  12. i += j + 11
  13. } else if strings.HasPrefix(code[i:], "</") { // Parse end
  14. i += 2
  15. j := strings.Index(code[i:], ">")
  16. if j < 0 {
  17. return false
  18. }
  19. name := code[i : i+j]
  20. if l := len(st); l == 0 || st[l-1] != name {
  21. return false
  22. } else {
  23. st = st[:l-1]
  24. }
  25. i += j
  26. } else if code[i] == '<' { // Parse begin
  27. i += 1
  28. j := strings.Index(code[i:], ">")
  29. if j < 1 || 9 < j {
  30. return false
  31. }
  32. name := code[i : i+j]
  33. for _, r := range name {
  34. if r < 'A' || 'Z' < r {
  35. return false
  36. }
  37. }
  38. st = append(st, name)
  39. i += j
  40. }
  41. }
  42. return len(st) == 0
  43. }