forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic.js
More file actions
75 lines (66 loc) · 2.31 KB
/
magic.js
File metadata and controls
75 lines (66 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import messages from '../:messages.json?module';
import {join} from './lib/url.js';
/**
* @fileoverview Magic helpers for development. These should only be used as tagged templates and,
* in production, are replaced with raw strings.
*
* These have matching externs in build/transpile/externs.js, for our Closure code.
*/
/**
* Returns the English development string for Santa Tracker.
*
* @param {string} id of message
* @return {string|?} resulting text of message
*/
export function _msg(id) {
const data = messages[id.toString()];
return data && (data.raw || data.message) || '?';
}
/**
* Returns the given path under Santa Tracker's static serving path.
*
* @param {string} path to join to static
* @return {string} resolved path
*/
export function _static(path) {
// FIXME: we're hoping that a compile can do something with `import.meta.url`.
return join(import.meta.url, '..', path.toString());
}
/**
* Upgrades all elements with `msgid="..."` as a single startup task. This is compiled for release
* and we don't expect to see any inside Shadow DOM (should use `_msg`).
*
* This makes this file have side effects. This is probably fine.
*/
(function() {
function upgrade(el) {
if (el.localName === 'i18n-msg') {
if (el.textContent !== 'PLACEHOLDER_i18n') {
console.warn('i18n-msg with bad text', el.textContent)
}
} else if (el.childNodes.length) {
console.warn('[msgid] with childNodes', el);
}
if (document.head.contains(el)) {
// Don't do anything. It's not really worth testing anything here, just for release.
return;
}
el.innerHTML = _msg(el.getAttribute('msgid'));
}
Array.from(document.body.querySelectorAll('[msgid]')).map(upgrade);
}());