mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 12:57:42 +08:00
Merge pull request #1773 from NativeScript/css-property
Plain component properties now can be applied from CSS
This commit is contained in:
@ -30,6 +30,28 @@ export function test_css_dataURI_is_applied_to_backgroundImageSource() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function test_css_is_applied_to_normal_properties() {
|
||||||
|
var stack = new stackModule.StackLayout();
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(stack, function (views: Array<viewModule.View>) {
|
||||||
|
var page = <pageModule.Page>views[1];
|
||||||
|
var expected = "horizontal";
|
||||||
|
page.css = `StackLayout { orientation: ${expected}; }`;
|
||||||
|
TKUnit.assertEqual(stack.orientation, expected);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function test_css_is_applied_to_special_properties() {
|
||||||
|
var stack = new stackModule.StackLayout();
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(stack, function (views: Array<viewModule.View>) {
|
||||||
|
var page = <pageModule.Page>views[1];
|
||||||
|
var expected = "test";
|
||||||
|
page.css = `StackLayout { class: ${expected}; }`;
|
||||||
|
TKUnit.assertEqual(stack.className, expected);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Test for inheritance in different containers
|
// Test for inheritance in different containers
|
||||||
export function test_css_is_applied_inside_StackLayout() {
|
export function test_css_is_applied_inside_StackLayout() {
|
||||||
var testButton = new buttonModule.Button();
|
var testButton = new buttonModule.Button();
|
||||||
|
@ -6,6 +6,7 @@ import {File, Folder, path, knownFolders} from "file-system";
|
|||||||
import {getBindingOptions, bindingConstants} from "./binding-builder";
|
import {getBindingOptions, bindingConstants} from "./binding-builder";
|
||||||
import * as debugModule from "utils/debug";
|
import * as debugModule from "utils/debug";
|
||||||
import * as platformModule from "platform";
|
import * as platformModule from "platform";
|
||||||
|
import {convertString} from "utils/utils";
|
||||||
|
|
||||||
//the imports below are needed for special property registration
|
//the imports below are needed for special property registration
|
||||||
import "ui/layouts/dock-layout";
|
import "ui/layouts/dock-layout";
|
||||||
@ -183,19 +184,7 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
|
|||||||
attrHandled = (<any>instance)._applyXmlAttribute(propertyName, propertyValue);
|
attrHandled = (<any>instance)._applyXmlAttribute(propertyName, propertyValue);
|
||||||
}
|
}
|
||||||
if (!attrHandled) {
|
if (!attrHandled) {
|
||||||
if (propertyValue.trim() === "") {
|
instance[propertyName] = convertString(propertyValue);
|
||||||
instance[propertyName] = propertyValue;
|
|
||||||
} else {
|
|
||||||
// Try to convert value to number.
|
|
||||||
var valueAsNumber = +propertyValue;
|
|
||||||
if (!isNaN(valueAsNumber)) {
|
|
||||||
instance[propertyName] = valueAsNumber;
|
|
||||||
} else if (propertyValue && (propertyValue.toLowerCase() === "true" || propertyValue.toLowerCase() === "false")) {
|
|
||||||
instance[propertyName] = propertyValue.toLowerCase() === "true" ? true : false;
|
|
||||||
} else {
|
|
||||||
instance[propertyName] = propertyValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import * as trace from "trace";
|
|||||||
import * as styleProperty from "ui/styling/style-property";
|
import * as styleProperty from "ui/styling/style-property";
|
||||||
import * as types from "utils/types";
|
import * as types from "utils/types";
|
||||||
import * as utils from "utils/utils";
|
import * as utils from "utils/utils";
|
||||||
|
import {getSpecialPropertySetter} from "ui/builder/special-properties";
|
||||||
|
|
||||||
var ID_SPECIFICITY = 1000000;
|
var ID_SPECIFICITY = 1000000;
|
||||||
var ATTR_SPECIFITY = 10000;
|
var ATTR_SPECIFITY = 10000;
|
||||||
@ -57,12 +58,26 @@ export class CssSelector {
|
|||||||
|
|
||||||
public apply(view: view.View) {
|
public apply(view: view.View) {
|
||||||
this.eachSetter((property, value) => {
|
this.eachSetter((property, value) => {
|
||||||
|
if(types.isString(property)) {
|
||||||
|
let attrHandled = false;
|
||||||
|
let specialSetter = getSpecialPropertySetter(property);
|
||||||
|
|
||||||
|
if (!attrHandled && specialSetter) {
|
||||||
|
specialSetter(view, value);
|
||||||
|
attrHandled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!attrHandled && property in view) {
|
||||||
|
view[property] = utils.convertString(value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
view.style._setValue(property, value, observable.ValueSource.Css);
|
view.style._setValue(property, value, observable.ValueSource.Css);
|
||||||
}
|
}
|
||||||
catch (ex) {
|
catch (ex) {
|
||||||
trace.write("Error setting property: " + property.name + " view: " + view + " value: " + value + " " + ex, trace.categories.Style, trace.messageType.error);
|
trace.write("Error setting property: " + property.name + " view: " + view + " value: " + value + " " + ex, trace.categories.Style, trace.messageType.error);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,6 +100,8 @@ export class CssSelector {
|
|||||||
let pair = pairs[j];
|
let pair = pairs[j];
|
||||||
callback(pair.property, pair.value);
|
callback(pair.property, pair.value);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
callback(declaration.property, declaration.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,26 @@ export function escapeRegexSymbols(source: string): string {
|
|||||||
return source.replace(escapeRegex, "\\$&");
|
return source.replace(escapeRegex, "\\$&");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function convertString(value: string): any {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
if (value.trim() === "") {
|
||||||
|
result = value;
|
||||||
|
} else {
|
||||||
|
// Try to convert value to number.
|
||||||
|
var valueAsNumber = +value;
|
||||||
|
if (!isNaN(valueAsNumber)) {
|
||||||
|
result = valueAsNumber;
|
||||||
|
} else if (value && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
|
||||||
|
result = value.toLowerCase() === "true" ? true : false;
|
||||||
|
} else {
|
||||||
|
result = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export module layout {
|
export module layout {
|
||||||
|
|
||||||
var MODE_SHIFT = 30;
|
var MODE_SHIFT = 30;
|
||||||
|
6
utils/utils.d.ts
vendored
6
utils/utils.d.ts
vendored
@ -235,4 +235,10 @@
|
|||||||
* @param source The original value.
|
* @param source The original value.
|
||||||
*/
|
*/
|
||||||
export function escapeRegexSymbols(source: string): string
|
export function escapeRegexSymbols(source: string): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts string value to number or boolean.
|
||||||
|
* @param value The original value.
|
||||||
|
*/
|
||||||
|
export function convertString(value: string): any
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user