diff --git a/serializer.js b/serializer.js index 08ae751..724da10 100644 --- a/serializer.js +++ b/serializer.js @@ -1,3 +1,14 @@ +HTMLElement.prototype.isChildOf = function(parent){ + var node = this.parentNode; + while (node != null) { + if (node == parent) { + return true; + } + node = node.parentNode; + } + return false; +} + class SerializerField { /** * constructor @@ -36,12 +47,15 @@ class Serializer { * @returns {Serializer} */ constructor(element){ - let fields = element.querySelectorAll(`:scope > input:not([type="submit"]), :scope > select, :scope > textarea, :scope > group`); + let fields = element.querySelectorAll(`:scope input:not([type="submit"]), :scope select, :scope textarea, :scope group`); + let groups = [].slice.call(element.querySelectorAll(`:scope group`)); this._fields = new Map(); for(let f of fields){ - this.addField(f); + if(f.isChildOf(element) && groups.every(g => !f.isChildOf(g))){ + this.addField(f); + } } } diff --git a/tests/main.go b/tests/main.go index 7b93d94..4074621 100644 --- a/tests/main.go +++ b/tests/main.go @@ -101,6 +101,7 @@ func postTest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { var p []*Person body := json.NewDecoder(r.Body) + err := body.Decode(&p) if err != nil { fmt.Println(color.RedString(err.Error())) @@ -128,18 +129,25 @@ func isEq(p1, p2 Person) { assert(p1.Surname, p2.Surname, "Surname") assert(p1.Age, p2.Age, "Age") assert(p1.BirthDate, p2.BirthDate, "BirthDate") - for k := range p1.Languages { + if !assert(len(p2.Languages), len(p1.Languages), "Languages length") { + return + } + for k := range p2.Languages { assert(p1.Languages[k].Language, p2.Languages[k].Language, "Languages.Language") assert(p1.Languages[k].SkillLevel, p2.Languages[k].SkillLevel, "Languages.SkillLevel") assert(p1.Languages[k].Native, p2.Languages[k].Native, "Languages.Native") } } -func assert(v, v2 interface{}, ident string) { +func assert(v, v2 interface{}, ident string) bool { + pass := true if !reflect.DeepEqual(v, v2) { fmt.Fprint(tw, color.RedString("FAIL\t")) + pass = false } else { fmt.Fprint(tw, color.GreenString("PASS\t")) } fmt.Fprintf(tw, "%s\t%s\n", color.BlueString(ident), color.YellowString(fmt.Sprintf("'%v'\t'%v'", v, v2))) + + return pass } diff --git a/tests/templates/test.gohtml b/tests/templates/test.gohtml index dd15d84..5947be9 100644 --- a/tests/templates/test.gohtml +++ b/tests/templates/test.gohtml @@ -25,7 +25,9 @@ {{ range $p.Languages }} - +