Skip to content

Commit 5d28b1e

Browse files
committed
Merge pull request NativeScript#2120 from NativeScript/hdeshev/set-style-property
Export single CSS property resolution API.
2 parents ddf4ed2 + 48aa2b6 commit 5d28b1e

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

.ctags_exclude

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules
2+
android17.d.ts
3+
ios.d.ts
4+
*.js
25
bin

ui/styling/css-selector.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import observable = require("ui/core/dependency-observable");
33
import cssParser = require("css");
44
import * as trace from "trace";
5-
import * as styleProperty from "ui/styling/style-property";
5+
import {StyleProperty, ResolvedStylePropertyHandler, withStyleProperty} from "ui/styling/style-property";
66
import * as types from "utils/types";
77
import * as utils from "utils/utils";
88
import keyframeAnimation = require("ui/animation/keyframe-animation");
@@ -69,22 +69,24 @@ export class CssSelector {
6969
let modifier = valueSourceModifier || this.valueSourceModifier;
7070
this.eachSetter((property, value) => {
7171
if (types.isString(property)) {
72+
const propertyName = <string>property;
7273
let attrHandled = false;
73-
let specialSetter = getSpecialPropertySetter(property);
74+
let specialSetter = getSpecialPropertySetter(propertyName);
7475

7576
if (!attrHandled && specialSetter) {
7677
specialSetter(view, value);
7778
attrHandled = true;
7879
}
7980

80-
if (!attrHandled && property in view) {
81-
view[property] = utils.convertString(value);
81+
if (!attrHandled && propertyName in view) {
82+
view[propertyName] = utils.convertString(value);
8283
}
8384
} else {
85+
const resolvedProperty = <StyleProperty>property;
8486
try {
85-
view.style._setValue(property, value, modifier);
87+
view.style._setValue(resolvedProperty, value, modifier);
8688
} catch (ex) {
87-
trace.write("Error setting property: " + property.name + " view: " + view + " value: " + value + " " + ex, trace.categories.Style, trace.messageType.error);
89+
trace.write("Error setting property: " + resolvedProperty.name + " view: " + view + " value: " + value + " " + ex, trace.categories.Style, trace.messageType.error);
8890
}
8991
}
9092
});
@@ -102,29 +104,12 @@ export class CssSelector {
102104
}
103105
}
104106

105-
public eachSetter(callback: (property, resolvedValue: any) => void) {
107+
public eachSetter(callback: ResolvedStylePropertyHandler) {
106108
for (let i = 0; i < this._declarations.length; i++) {
107109
let declaration = this._declarations[i];
108110
let name = declaration.property;
109111
let resolvedValue = declaration.value;
110-
111-
let property = styleProperty.getPropertyByCssName(name);
112-
113-
if (property) {
114-
// The property.valueConverter is now used to convert the value later on in DependencyObservable._setValueInternal.
115-
callback(property, resolvedValue);
116-
}
117-
else {
118-
let pairs = styleProperty.getShorthandPairs(name, resolvedValue);
119-
if (pairs) {
120-
for (let j = 0; j < pairs.length; j++) {
121-
let pair = pairs[j];
122-
callback(pair.property, pair.value);
123-
}
124-
} else {
125-
callback(declaration.property, declaration.value);
126-
}
127-
}
112+
withStyleProperty(name, resolvedValue, callback);
128113
}
129114
}
130115

@@ -483,7 +468,8 @@ class InlineStyleSelector extends CssSelector {
483468

484469
public apply(view: view.View) {
485470
this.eachSetter((property, value) => {
486-
view.style._setValue(property, value, observable.ValueSource.Local);
471+
const resolvedProperty = <StyleProperty>property;
472+
view.style._setValue(resolvedProperty, value, observable.ValueSource.Local);
487473
});
488474
}
489475

ui/styling/style-property.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import definition = require("ui/styling");
33
import observable = require("ui/core/dependency-observable");
44

5+
export type StyleProperty = Property;
6+
export type ResolvedStylePropertyHandler = (property: Property | string, value: any) => void;
7+
export function withStyleProperty(name: string, value: any, resolvedCallback: ResolvedStylePropertyHandler): void;
58
export function getShorthandPairs(name: string, value: any): Array<KeyValuePair<Property, any>>;
69

710
export function registerShorthandCallback(name: string, callback: (value: any) => Array<KeyValuePair<Property, any>>): void;
@@ -25,4 +28,4 @@
2528
property: K;
2629
value: V;
2730
}
28-
}
31+
}

ui/styling/style-property.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ function registerProperty(property: Property) {
2020
}
2121
}
2222

23+
export type StyleProperty = definition.Property;
24+
export type ResolvedStylePropertyHandler = (property: definition.Property | string, value: any) => void;
25+
export function withStyleProperty(name: string, value: any, resolvedCallback: ResolvedStylePropertyHandler): void {
26+
let property = getPropertyByCssName(name);
27+
28+
if (property) {
29+
// The property.valueConverter is now used to convert the value later on in DependencyObservable._setValueInternal.
30+
resolvedCallback(property, value);
31+
}
32+
else {
33+
let pairs = getShorthandPairs(name, value);
34+
if (pairs) {
35+
for (let j = 0; j < pairs.length; j++) {
36+
let pair = pairs[j];
37+
resolvedCallback(pair.property, pair.value);
38+
}
39+
} else {
40+
//Fall back to the string property name as a last resort and let
41+
//the callback handle it further.
42+
resolvedCallback(name, value);
43+
}
44+
}
45+
}
46+
2347
export function getShorthandPairs(name: string, value: any): Array<definition.KeyValuePair<definition.Property, any>> {
2448
var callback = callbackByShorthandName.get(name);
2549
if (callback) {
@@ -106,4 +130,4 @@ export class Property extends observable.Property implements definition.Property
106130
entry.valueSource = observable.ValueSource.Default;
107131
return this.metadata.defaultValue;
108132
}
109-
}
133+
}

0 commit comments

Comments
 (0)