mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-26 23:43:46 +00:00
Add util function for generating classes/ids from any string
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { t } from '../util/locale';
|
||||
|
||||
import { utilSafeClassName } from '../util/util';
|
||||
|
||||
export function presetField(id, field) {
|
||||
field = Object.assign({}, field); // shallow copy
|
||||
@@ -7,7 +7,7 @@ export function presetField(id, field) {
|
||||
field.id = id;
|
||||
|
||||
// for use in classes, element ids, css selectors
|
||||
field.safeid = id.replace(/[^_a-zA-Z0-9\-]/g, '_');
|
||||
field.safeid = utilSafeClassName(id);
|
||||
|
||||
field.matchGeometry = function(geometry) {
|
||||
return !field.geometry || field.geometry === geometry;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { t } from '../util/locale';
|
||||
import { osmAreaKeys } from '../osm/tags';
|
||||
import { utilArrayUniq, utilObjectOmit } from '../util';
|
||||
import { utilSafeClassName } from '../util/util';
|
||||
|
||||
|
||||
export function presetPreset(id, preset, fields, visible, rawPresets) {
|
||||
@@ -8,6 +9,9 @@ export function presetPreset(id, preset, fields, visible, rawPresets) {
|
||||
|
||||
preset.id = id;
|
||||
|
||||
// for use in classes, element ids, css selectors
|
||||
preset.safeid = utilSafeClassName(id);
|
||||
|
||||
preset.parentPresetID = function() {
|
||||
var endIndex = preset.id.lastIndexOf('/');
|
||||
if (endIndex < 0) return null;
|
||||
|
||||
@@ -34,6 +34,7 @@ export { utilPrefixDOMProperty } from './util';
|
||||
export { utilPreset } from './util';
|
||||
export { utilQsString } from './util';
|
||||
export { utilRebind } from './rebind';
|
||||
export { utilSafeClassName } from './util';
|
||||
export { utilSetTransform } from './util';
|
||||
export { utilSessionMutex } from './session_mutex';
|
||||
export { utilStringQs } from './util';
|
||||
|
||||
@@ -417,3 +417,9 @@ export function utilHashcode(str) {
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
// returns version of `str` with all runs of special characters replaced by `_`;
|
||||
// suitable for HTML ids, classes, selectors, etc.
|
||||
export function utilSafeClassName(str) {
|
||||
return str.toLowerCase().replace(/[^a-z0-9]+/g, '_');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user