Fix not direct child issue

This commit is contained in:
Crow Crowcrow 2017-08-29 12:58:02 +02:00
parent c7ac15a287
commit b9c3769189
3 changed files with 29 additions and 5 deletions

View File

@ -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);
}
}
}

View File

@ -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
}

View File

@ -25,7 +25,9 @@
<input name="birth_date" type="date" value="{{ $p.BirthDate.Format "2006-01-02" }}">
{{ range $p.Languages }}
<group name="[]languages">
<input name="language" type="text" value="{{ .Language }}">
<label>
<input name="language" type="text" value="{{ .Language }}">
</label>
<input name="native" type="checkbox" {{ if .Native }} checked {{ end }}>
<input name="skill_level" type="number" value="{{ .SkillLevel }}">
</group>