Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Validation for Bysetpos during initialisation #331

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion build/ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -7090,7 +7090,7 @@ ICAL.RecurIterator = (function() {
return null;
}

if (this.occurrence_number == 0 && this.last.compare(this.dtstart) >= 0) {
if (this.occurrence_number == 0 && (this.last.compare(this.dtstart) >= 0 && this.validate_bysetpos())) {
// First of all, give the instance that was initialized
this.occurrence_number++;
return this.last;
Expand Down Expand Up @@ -7223,6 +7223,51 @@ ICAL.RecurIterator = (function() {
return end_of_data;
},

/*
* Checks if the computed last date is valid for given bysetpos rule
*/
validate_bysetpos: function () {
if (!this.has_by_data("BYSETPOS")) {
return true;
}

var currentJson = this.rule.toJSON();
delete currentJson.bysetpos;
delete currentJson.count;
var recurWithoutBysetPos = ICAL.Recur.fromData(currentJson);
var iterator = recurWithoutBysetPos.iterator(this.dtstart);
var datesList = [];
var endDate;
var date = iterator.next();
if(date == null) {
return false;
}
if(this.rule.freq == "YEARLY") {
endDate = date.endOfYear();
} else if(this.rule.freq == "MONTHLY") {
endDate = date.endOfMonth();
} else if(this.rule.freq == "WEEK") {
endDate = date.endOfWeek();
} else {
return true;
}
datesList.push(date.clone());
while(date = iterator.next()) {
if(endDate.compare(date) < 0) {
break;
}
datesList.push(date.clone());
}

var isValidBysetPos = false;
for(var idx = 0; idx < datesList.length; idx++) {
if(this.last.compare(datesList[idx]) == 0) {
isValidBysetPos = this.check_set_position(idx + 1) || this.check_set_position(idx - datesList.length);
}
}
return isValidBysetPos;
},

/**
* Normalize each by day rule for a given year/month.
* Takes into account ordering and negative rules
Expand Down
2 changes: 1 addition & 1 deletion build/ical.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/ical.min.js.map

Large diffs are not rendered by default.

50 changes: 49 additions & 1 deletion lib/ical/recur_iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ ICAL.RecurIterator = (function() {
return null;
}

if (this.occurrence_number == 0 && this.last.compare(this.dtstart) >= 0) {
if (this.occurrence_number == 0 && (this.last.compare(this.dtstart) >= 0 && this.validate_bysetpos())) {
// First of all, give the instance that was initialized
this.occurrence_number++;
return this.last;
Expand Down Expand Up @@ -460,6 +460,54 @@ ICAL.RecurIterator = (function() {
return end_of_data;
},

/*
* Checks if the computed last date is valid for given bysetpos rule
*/
validate_bysetpos: function () {
if (!this.has_by_data("BYSETPOS")) {
return true;
}

var currentJson = this.rule.toJSON();
delete currentJson.bysetpos;
delete currentJson.count;
var recurWithoutBysetPos = ICAL.Recur.fromData(currentJson);
var startDate,endDate;
if(this.rule.freq == "YEARLY") {
startDate = this.dtstart.clone().startOfYear();
endDate = this.dtstart.clone().endOfYear();
} else if(this.rule.freq == "MONTHLY") {
startDate = this.dtstart.clone().startOfMonth();
endDate = this.dtstart.clone().endOfMonth();
} else if(this.rule.freq == "WEEK") {
startDate = this.dtstart.clone().startOfWeek();
endDate = this.dtstart.clone().endOfWeek();
} else {
return true;
}
var iterator = recurWithoutBysetPos.iterator(startDate);
var datesList = [];
var date = iterator.next();
if(date == null) {
return false;
}
datesList.push(date.clone());
while(date = iterator.next()) {
if(endDate.compare(date) < 0) {
break;
}
datesList.push(date.clone());
}

var isValidBysetPos = false;
for(var idx = 0; idx < datesList.length; idx++) {
if(this.last.compare(datesList[idx]) == 0) {
isValidBysetPos = this.check_set_position(idx + 1) || this.check_set_position(idx - datesList.length);
}
}
return isValidBysetPos;
},

/**
* Normalize each by day rule for a given year/month.
* Takes into account ordering and negative rules
Expand Down
Loading