1
0
mirror of https://github.com/golang/go synced 2024-11-05 18:56:10 -07:00
go/present/video.go
Dmitri Shuralyov b2104f82a9 present: check if too few arguments to image, iframe, or video
Previously, using incorrect syntax for one of image, iframe, or video
functions caused an index out of range panic. Add a check to prevent
the panic and return an error instead.

Fixes golang/go#35142

Change-Id: Ifffb4cc5daded5331d617a3db7cad84e37abadc8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/203477
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
2019-10-26 03:49:45 +00:00

55 lines
1.2 KiB
Go

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package present
import (
"fmt"
"strings"
)
func init() {
Register("video", parseVideo)
}
type Video struct {
URL string
SourceType string
Width int
Height int
}
func (v Video) TemplateName() string { return "video" }
func parseVideo(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
args := strings.Fields(text)
if len(args) < 3 {
return nil, fmt.Errorf("incorrect video invocation: %q", text)
}
vid := Video{URL: args[1], SourceType: args[2]}
a, err := parseArgs(fileName, lineno, args[3:])
if err != nil {
return nil, err
}
switch len(a) {
case 0:
// no size parameters
case 2:
// If a parameter is empty (underscore) or invalid
// leave the field set to zero. The "video" action
// template will then omit that vid tag attribute and
// the browser will calculate the value to preserve
// the aspect ratio.
if v, ok := a[0].(int); ok {
vid.Height = v
}
if v, ok := a[1].(int); ok {
vid.Width = v
}
default:
return nil, fmt.Errorf("incorrect video invocation: %q", text)
}
return vid, nil
}