Skip to content

Commit b03bca9

Browse files
committed
Extract to a mixin
1 parent 195643b commit b03bca9

File tree

4 files changed

+468
-331
lines changed

4 files changed

+468
-331
lines changed

lib/mixins/property-effects.html

+80-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<link rel="import" href="../utils/path.html">
1515
<!-- for notify, reflect -->
1616
<link rel="import" href="../utils/case-map.html">
17-
<link rel="import" href="../utils/binding-parser.html">
1817
<link rel="import" href="property-accessors.html">
1918
<!-- for annotated effects -->
2019
<link rel="import" href="template-stamp.html">
@@ -802,6 +801,24 @@
802801

803802
const emptyArray = [];
804803

804+
// Regular expressions used for binding
805+
const IDENT = '(?:' + '[a-zA-Z_$][\\w.:$\\-*]*' + ')';
806+
const NUMBER = '(?:' + '[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?' + ')';
807+
const SQUOTE_STRING = '(?:' + '\'(?:[^\'\\\\]|\\\\.)*\'' + ')';
808+
const DQUOTE_STRING = '(?:' + '"(?:[^"\\\\]|\\\\.)*"' + ')';
809+
const STRING = '(?:' + SQUOTE_STRING + '|' + DQUOTE_STRING + ')';
810+
const ARGUMENT = '(?:(' + IDENT + '|' + NUMBER + '|' + STRING + ')\\s*' + ')';
811+
const ARGUMENTS = '(?:' + ARGUMENT + '(?:,\\s*' + ARGUMENT + ')*' + ')';
812+
const ARGUMENT_LIST = '(?:' + '\\(\\s*' +
813+
'(?:' + ARGUMENTS + '?' + ')' +
814+
'\\)\\s*' + ')';
815+
const BINDING = '(' + IDENT + '\\s*' + ARGUMENT_LIST + '?' + ')'; // Group 3
816+
const OPEN_BRACKET = '(\\[\\[|{{)' + '\\s*';
817+
const CLOSE_BRACKET = '(?:]]|}})';
818+
const NEGATE = '(?:(!)\\s*)?'; // Group 2
819+
const EXPRESSION = OPEN_BRACKET + NEGATE + BINDING + CLOSE_BRACKET;
820+
const bindingRegex = new RegExp(EXPRESSION, "g");
821+
805822
/**
806823
* Create a string from binding parts of all the literal parts
807824
*
@@ -2506,9 +2523,67 @@
25062523
* @protected
25072524
*/
25082525
static _parseBindings(text, templateInfo) {
2509-
const parserParts = Polymer.BindingParser.parse(text, templateInfo);
2510-
if (parserParts.length) {
2511-
return parserParts;
2526+
let parts = [];
2527+
let lastIndex = 0;
2528+
let m;
2529+
// Example: "literal1{{prop}}literal2[[!compute(foo,bar)]]final"
2530+
// Regex matches:
2531+
// Iteration 1: Iteration 2:
2532+
// m[1]: '{{' '[['
2533+
// m[2]: '' '!'
2534+
// m[3]: 'prop' 'compute(foo,bar)'
2535+
while ((m = bindingRegex.exec(text)) !== null) {
2536+
// Add literal part
2537+
if (m.index > lastIndex) {
2538+
parts.push({literal: text.slice(lastIndex, m.index)});
2539+
}
2540+
// Add binding part
2541+
let mode = m[1][0];
2542+
let negate = Boolean(m[2]);
2543+
let source = m[3].trim();
2544+
let customEvent = false, notifyEvent = '', colon = -1;
2545+
if (mode == '{' && (colon = source.indexOf('::')) > 0) {
2546+
notifyEvent = source.substring(colon + 2);
2547+
source = source.substring(0, colon);
2548+
customEvent = true;
2549+
}
2550+
let signature = parseMethod(source);
2551+
let dependencies = [];
2552+
if (signature) {
2553+
// Inline computed function
2554+
let {args, methodName} = signature;
2555+
for (let i=0; i<args.length; i++) {
2556+
let arg = args[i];
2557+
if (!arg.literal) {
2558+
dependencies.push(arg);
2559+
}
2560+
}
2561+
let dynamicFns = templateInfo.dynamicFns;
2562+
if (dynamicFns && dynamicFns[methodName] || signature.static) {
2563+
dependencies.push(methodName);
2564+
signature.dynamicFn = true;
2565+
}
2566+
} else {
2567+
// Property or path
2568+
dependencies.push(source);
2569+
}
2570+
parts.push({
2571+
source, mode, negate, customEvent, signature, dependencies,
2572+
event: notifyEvent
2573+
});
2574+
lastIndex = bindingRegex.lastIndex;
2575+
}
2576+
// Add a final literal part
2577+
if (lastIndex && lastIndex < text.length) {
2578+
let literal = text.substring(lastIndex);
2579+
if (literal) {
2580+
parts.push({
2581+
literal: literal
2582+
});
2583+
}
2584+
}
2585+
if (parts.length) {
2586+
return parts;
25122587
} else {
25132588
return null;
25142589
}
@@ -2615,4 +2690,4 @@
26152690
};
26162691

26172692
})();
2618-
</script>
2693+
</script>

0 commit comments

Comments
 (0)