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

tel no. validation #10124

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
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
23 changes: 15 additions & 8 deletions modules/ui/fields/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export function uiFieldText(field, context) {
}





function calcLocked() {
// Protect certain fields that have a companion `*:wikidata` value
var isLocked = (field.id === 'brand' || field.id === 'network' || field.id === 'operator' || field.id === 'flag') &&
Expand Down Expand Up @@ -348,7 +351,7 @@ export function uiFieldText(field, context) {

function updatePhonePlaceholder() {
if (input.empty() || !Object.keys(_phoneFormats).length) return;

change()();
var extent = combinedEntityExtent();
var countryCode = extent && countryCoder.iso1A2Code(extent.center());
var format = countryCode && _phoneFormats[countryCode.toLowerCase()];
Expand Down Expand Up @@ -404,16 +407,20 @@ export function uiFieldText(field, context) {
}
}


function change(onInput) {
return function() {
var t = {};
var val = utilGetSetValue(input);
if (!onInput) val = context.cleanTagValue(val);

// don't override multiple values with blank string
if (field.type === 'tel') {
val = val.replace(/^tel:\/\//, ''); // Remove 'tel://' prefix if present
val = val.replace(/^tel:/, ''); // Remove 'tel:' prefix if present
Comment on lines +416 to +417
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are regular expressions, you could combine these two steps by surrounding the slashes in (\/\/)?. (Technically, RFC 3966 doesn’t allow slashes after the colon, but as #9559 (comment) demonstrates, there’s probably enough confusion about that to go ahead and strip out those slashes too.)

}

if (!onInput) val = context.cleanTagValue(val);

if (!val && getVals(_tags).size > 1) return;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably perform the tel:-stripping after cleaning the tag value. cleanTagValue() removes stuff like leading and trailing whitespace, something that can easily occur if someone is copy-pasting from another website.

var displayVal = val;
if (field.type === 'number' && val) {
var numbers = val.split(';');
Expand All @@ -431,11 +438,11 @@ export function uiFieldText(field, context) {
if (!onInput) utilGetSetValue(input, displayVal);
t[field.key] = val || undefined;
if (field.keys) {
// for multi-key fields with: handle alternative tag keys gracefully
// For multi-key fields: handle alternative tag keys gracefully
// https://github.com/openstreetmap/id-tagging-schema/issues/905
dispatch.call('change', this, tags => {
if (field.keys.some(key => tags[key])) {
// use exiting key(s)
// Use existing key(s)
field.keys.filter(key => tags[key]).forEach(key => {
tags[key] = val || undefined;
});
Expand All @@ -451,7 +458,6 @@ export function uiFieldText(field, context) {
};
}


i.entityIDs = function(val) {
if (!arguments.length) return _entityIDs;
_entityIDs = val;
Expand Down Expand Up @@ -542,3 +548,4 @@ export function uiFieldText(field, context) {

return utilRebind(i, dispatch, 'on');
}

Loading