diff --git a/modules/renderer/background.js b/modules/renderer/background.js index a4ff533d7..44d79e638 100644 --- a/modules/renderer/background.js +++ b/modules/renderer/background.js @@ -271,6 +271,7 @@ export function rendererBackground(context) { context.history().photoOverlaysUsed(photoOverlaysUsed); }; + let _checkedBlocklists; background.sources = (extent, zoom, includeCurrent) => { if (!_imageryIndex) return []; // called before init()? @@ -283,15 +284,19 @@ export function rendererBackground(context) { const osm = context.connection(); const blocklists = osm && osm.imageryBlocklists(); - const isBlocked = blocklists && function(source) { - return blocklists.some(function(blocklist) { - return new RegExp(blocklist).test(source.template()); + + if (blocklists && blocklists !== _checkedBlocklists) { + _imageryIndex.backgrounds.forEach(source => { + source.isBlocked = blocklists.some(function(blocklist) { + return blocklist.test(source.template()); + }); }); - }; + _checkedBlocklists = blocklists; + } return _imageryIndex.backgrounds.filter(source => { - if (includeCurrent && currSource === source) return true; // optionally include the current imagery - if (isBlocked && isBlocked(source)) return false; // even bundled sources may be blocked - #7905 + if (includeCurrent && currSource === source) return true; // optionally always include the current imagery + if (source.isBlocked) return false; // even bundled sources may be blocked - #7905 if (!source.polygon) return true; // always include imagery with worldwide coverage if (zoom && zoom < 6) return false; // optionally exclude local imagery at low zooms return visible[source.id]; // include imagery visible in given extent @@ -320,19 +325,15 @@ export function rendererBackground(context) { let regex; for (let i = 0; i < blocklists.length; i++) { - try { - regex = new RegExp(blocklists[i]); - fail = regex.test(template); - tested++; - if (fail) break; - } catch (e) { - /* noop */ - } + regex = blocklists[i]; + fail = regex.test(template); + tested++; + if (fail) break; } // ensure at least one test was run. if (!tested) { - regex = new RegExp('.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*'); + regex = /.*\.google(apis)?\..*\/(vt|kh)[\?\/].*([xyz]=.*){3}.*/; fail = regex.test(template); } diff --git a/modules/services/osm.js b/modules/services/osm.js index 8fa670cbd..d9887a5bf 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -24,7 +24,7 @@ var oauth = osmAuth({ done: authDone }); // hardcode default block of Google Maps -var _imageryBlocklists = ['.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*']; +var _imageryBlocklists = [/.*\.google(apis)?\..*\/(vt|kh)[\?\/].*([xyz]=.*){3}.*/]; var _tileCache = { toLoad: {}, loaded: {}, inflight: {}, seen: {}, rtree: new RBush() }; var _noteCache = { toLoad: {}, loaded: {}, inflight: {}, inflightPost: {}, note: {}, closed: {}, rtree: new RBush() }; var _userCache = { toLoad: {}, user: {} }; @@ -923,9 +923,14 @@ export default { var elements = xml.getElementsByTagName('blacklist'); var regexes = []; for (var i = 0; i < elements.length; i++) { - var regex = elements[i].getAttribute('regex'); // needs unencode? - if (regex) { - regexes.push(regex); + var regexString = elements[i].getAttribute('regex'); // needs unencode? + if (regexString) { + try { + var regex = new RegExp(regexString); + regexes.push(regex); + } catch (e) { + /* noop */ + } } } if (regexes.length) { diff --git a/modules/svg/data.js b/modules/svg/data.js index f16f8c803..42ebb35a3 100644 --- a/modules/svg/data.js +++ b/modules/svg/data.js @@ -388,19 +388,15 @@ export function svgData(projection, context, dispatch) { var regex; for (var i = 0; i < blocklists.length; i++) { - try { - regex = new RegExp(blocklists[i]); - fail = regex.test(val); - tested++; - if (fail) break; - } catch (e) { - /* noop */ - } + regex = blocklists[i]; + fail = regex.test(val); + tested++; + if (fail) break; } // ensure at least one test was run. if (!tested) { - regex = new RegExp('.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*'); + regex = /.*\.google(apis)?\..*\/(vt|kh)[\?\/].*([xyz]=.*){3}.*/; fail = regex.test(val); } } diff --git a/test/spec/services/osm.js b/test/spec/services/osm.js index 8e73b66e2..3619e3270 100644 --- a/test/spec/services/osm.js +++ b/test/spec/services/osm.js @@ -732,7 +732,7 @@ describe('iD.serviceOsm', function () { it('updates imagery blocklists', function(done) { connection.status(function() { var blocklists = connection.imageryBlocklists(); - expect(blocklists).to.deep.equal(['\.foo\.com','\.bar\.org']); + expect(blocklists).to.deep.equal([new RegExp('\.foo\.com'), new RegExp('\.bar\.org')]); done(); });