From e7e4b65ae66375ec1e85afc8e6b9b4933ae4da81 Mon Sep 17 00:00:00 2001 From: Kunpei Sakai Date: Sat, 30 Nov 2019 04:14:27 +0900 Subject: [PATCH] html: improve coding style Change-Id: I05c0ccbad41f5512f8096b0d15991d7d6b5d726e Reviewed-on: https://go-review.googlesource.com/c/net/+/209398 Reviewed-by: Nigel Tao --- html/charset/charset_test.go | 3 +- html/parse.go | 69 ++++++++++++++++-------------------- html/parse_test.go | 6 ++-- html/token.go | 3 +- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/html/charset/charset_test.go b/html/charset/charset_test.go index e4e7d86b..b71eb43f 100644 --- a/html/charset/charset_test.go +++ b/html/charset/charset_test.go @@ -225,8 +225,7 @@ func TestXML(t *testing.T) { var a struct { Word string } - err := d.Decode(&a) - if err != nil { + if err := d.Decode(&a); err != nil { t.Fatalf("Decode: %v", err) } diff --git a/html/parse.go b/html/parse.go index 8ba9bff8..2cd12fc8 100644 --- a/html/parse.go +++ b/html/parse.go @@ -203,16 +203,17 @@ func (p *parser) generateImpliedEndTags(exceptions ...string) { loop: for i = len(p.oe) - 1; i >= 0; i-- { n := p.oe[i] - if n.Type == ElementNode { - switch n.DataAtom { - case a.Dd, a.Dt, a.Li, a.Optgroup, a.Option, a.P, a.Rb, a.Rp, a.Rt, a.Rtc: - for _, except := range exceptions { - if n.Data == except { - break loop - } + if n.Type != ElementNode { + break + } + switch n.DataAtom { + case a.Dd, a.Dt, a.Li, a.Optgroup, a.Option, a.P, a.Rb, a.Rp, a.Rt, a.Rtc: + for _, except := range exceptions { + if n.Data == except { + break loop } - continue } + continue } break } @@ -380,8 +381,7 @@ findIdenticalElements: // Section 12.2.4.3. func (p *parser) clearActiveFormattingElements() { for { - n := p.afe.pop() - if len(p.afe) == 0 || n.Type == scopeMarkerNode { + if n := p.afe.pop(); len(p.afe) == 0 || n.Type == scopeMarkerNode { return } } @@ -1169,14 +1169,13 @@ func inBodyIM(p *parser) bool { if len(p.templateStack) > 0 { p.im = inTemplateIM return false - } else { - for _, e := range p.oe { - switch e.DataAtom { - case a.Dd, a.Dt, a.Li, a.Optgroup, a.Option, a.P, a.Rb, a.Rp, a.Rt, a.Rtc, a.Tbody, a.Td, a.Tfoot, a.Th, - a.Thead, a.Tr, a.Body, a.Html: - default: - return true - } + } + for _, e := range p.oe { + switch e.DataAtom { + case a.Dd, a.Dt, a.Li, a.Optgroup, a.Option, a.P, a.Rb, a.Rp, a.Rt, a.Rtc, a.Tbody, a.Td, a.Tfoot, a.Th, + a.Thead, a.Tr, a.Body, a.Html: + default: + return true } } } @@ -1502,14 +1501,13 @@ func inCaptionIM(p *parser) bool { case StartTagToken: switch p.tok.DataAtom { case a.Caption, a.Col, a.Colgroup, a.Tbody, a.Td, a.Tfoot, a.Thead, a.Tr: - if p.popUntil(tableScope, a.Caption) { - p.clearActiveFormattingElements() - p.im = inTableIM - return false - } else { + if !p.popUntil(tableScope, a.Caption) { // Ignore the token. return true } + p.clearActiveFormattingElements() + p.im = inTableIM + return false case a.Select: p.reconstructActiveFormattingElements() p.addElement() @@ -1526,14 +1524,13 @@ func inCaptionIM(p *parser) bool { } return true case a.Table: - if p.popUntil(tableScope, a.Caption) { - p.clearActiveFormattingElements() - p.im = inTableIM - return false - } else { + if !p.popUntil(tableScope, a.Caption) { // Ignore the token. return true } + p.clearActiveFormattingElements() + p.im = inTableIM + return false case a.Body, a.Col, a.Colgroup, a.Html, a.Tbody, a.Td, a.Tfoot, a.Th, a.Thead, a.Tr: // Ignore the token. return true @@ -1777,12 +1774,11 @@ func inSelectIM(p *parser) bool { } p.addElement() case a.Select: - if p.popUntil(selectScope, a.Select) { - p.resetInsertionMode() - } else { + if !p.popUntil(selectScope, a.Select) { // Ignore the token. return true } + p.resetInsertionMode() case a.Input, a.Keygen, a.Textarea: if p.elementInScope(selectScope, a.Select) { p.parseImpliedToken(EndTagToken, a.Select, a.Select.String()) @@ -1810,12 +1806,11 @@ func inSelectIM(p *parser) bool { p.oe = p.oe[:i] } case a.Select: - if p.popUntil(selectScope, a.Select) { - p.resetInsertionMode() - } else { + if !p.popUntil(selectScope, a.Select) { // Ignore the token. return true } + p.resetInsertionMode() case a.Template: return inHeadIM(p) } @@ -2352,8 +2347,7 @@ func ParseWithOptions(r io.Reader, opts ...ParseOption) (*Node, error) { f(p) } - err := p.parse() - if err != nil { + if err := p.parse(); err != nil { return nil, err } return p.doc, nil @@ -2411,8 +2405,7 @@ func ParseFragmentWithOptions(r io.Reader, context *Node, opts ...ParseOption) ( } } - err := p.parse() - if err != nil { + if err := p.parse(); err != nil { return nil, err } diff --git a/html/parse_test.go b/html/parse_test.go index eb822882..f8645031 100644 --- a/html/parse_test.go +++ b/html/parse_test.go @@ -278,9 +278,8 @@ func TestParserWithoutScripting(t *testing.T) { | | src="https://golang.org/doc/gopher/doc.png" ` - err := testParseCase(text, want, "", ParseOptionEnableScripting(false)) - if err != nil { + if err := testParseCase(text, want, "", ParseOptionEnableScripting(false)); err != nil { t.Errorf("test with scripting is disabled, %q, %s", text, err) } } @@ -430,8 +429,7 @@ func TestNodeConsistency(t *testing.T) { DataAtom: atom.Frameset, Data: "table", } - _, err := ParseFragment(strings.NewReader("

hello

"), inconsistentNode) - if err == nil { + if _, err := ParseFragment(strings.NewReader("

hello

"), inconsistentNode); err == nil { t.Errorf("got nil error, want non-nil") } } diff --git a/html/token.go b/html/token.go index ae0d1b05..877709f9 100644 --- a/html/token.go +++ b/html/token.go @@ -296,8 +296,7 @@ func (z *Tokenizer) Buffered() []byte { // too many times in succession. func readAtLeastOneByte(r io.Reader, b []byte) (int, error) { for i := 0; i < 100; i++ { - n, err := r.Read(b) - if n != 0 || err != nil { + if n, err := r.Read(b); n != 0 || err != nil { return n, err } }