Skip to content

Commit 85b8c01

Browse files
Nathanael AndersonAlexander Vakrilov
authored andcommitted
feat(styling): Added 2 functions to control applicationAdditionalSelectors (#6124)
* Added getAdditionalSelectors function so that nativescript-theme can be functional again in NS 4.x * Change to a better more extensible additional css system. * removed redunant function on the name. * Fix lint issues * Adding mergeSelectors to the remove function * Added test of new add/remove css functions. * fix: revert testRunner.ts changes that disabled all tests except style * refactor: fix typo * chore: fix typo and change test to no not affect global styles
1 parent cf034dd commit 85b8c01

3 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎tests/app/ui/styling/style-tests.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as tabViewModule from "tns-core-modules/ui/tab-view";
99
import * as helper from "../../ui/helper";
1010
import * as types from "tns-core-modules/utils/types";
1111
import * as viewModule from "tns-core-modules/ui/core/view";
12-
import { resolveFileNameFromUrl } from "tns-core-modules/ui/styling/style-scope";
12+
import { resolveFileNameFromUrl, removeTaggedAdditionalCSS, addTaggedAdditionalCSS } from "tns-core-modules/ui/styling/style-scope";
1313
import { unsetValue } from "tns-core-modules/ui/core/view";
1414
import * as color from "tns-core-modules/color";
1515

@@ -1479,6 +1479,36 @@ export function test_resolveFileNameFromUrl_unexisting_file() {
14791479
TKUnit.assertNull(result, "Shouldn't resolve unexisting file");
14801480
}
14811481

1482+
export function test_checkAddRemoveCSS() {
1483+
const css1 = "#test_checkAddRemoveCSS_label { color: #FF0000; }";
1484+
const css2 = "#test_checkAddRemoveCSS_label { color: #00FF00; }";
1485+
const label = new labelModule.Label();
1486+
label.text = "color coming from updated rules";
1487+
label.id = "test_checkAddRemoveCSS_label";
1488+
1489+
helper.buildUIAndRunTest(label, function (views: Array<viewModule.View>) {
1490+
const page = <pageModule.Page>views[1];
1491+
1492+
// Add Red, we have to then trigger the page's CSS state change, for it to refresh the label's css with the new global rule
1493+
addTaggedAdditionalCSS(css1, "red");
1494+
page._onCssStateChange();
1495+
helper.assertViewColor(label, "#FF0000");
1496+
1497+
// Add Green (should override red)
1498+
addTaggedAdditionalCSS(css2, "green");
1499+
page._onCssStateChange();
1500+
helper.assertViewColor(label, "#00FF00");
1501+
1502+
// Remove Green (Should revert to red, since we removed the green rule)
1503+
removeTaggedAdditionalCSS("green");
1504+
page._onCssStateChange();
1505+
helper.assertViewColor(label, "#FF0000");
1506+
1507+
//Cleanup
1508+
removeTaggedAdditionalCSS("red");
1509+
});
1510+
}
1511+
14821512
// <snippet module="ui/styling" title="styling">
14831513
// For information and example how to use style properties please refer to special [**Styling**](../../../styling.md) topic.
14841514
// </snippet>

‎tns-core-modules/ui/styling/css-selector/css-selector.d.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ export class RuleSet {
4343
* Gets the key-value list of declarations for the ruleset.
4444
*/
4545
declarations: Declaration[];
46+
47+
/**
48+
* Optional Tag for rules
49+
**/
50+
tag: string | Number;
4651
}
4752

4853
export class SelectorsMap {
@@ -68,7 +73,7 @@ export class SelectorsMatch<T extends Node> {
6873
selectors: SelectorCore[];
6974

7075
/**
71-
* Gets a map of nodes to attributes and pseudo classes, that may affect the state of the dynamic
76+
* Gets a map of nodes to attributes and pseudo classes, that may affect the state of the dynamic
7277
*/
7378
changeMap: ChangeMap<T>;
7479
}

‎tns-core-modules/ui/styling/style-scope.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,35 @@ class CSSSource {
262262
}
263263
}
264264

265+
export function removeTaggedAdditionalCSS(tag: String | Number): Boolean {
266+
let changed = false;
267+
for (let i = 0; i < applicationAdditionalSelectors.length; i++) {
268+
if (applicationAdditionalSelectors[i].tag === tag) {
269+
applicationAdditionalSelectors.splice(i, 1);
270+
i--;
271+
changed = true;
272+
}
273+
}
274+
if (changed) { mergeCssSelectors(); }
275+
return changed;
276+
}
277+
278+
export function addTaggedAdditionalCSS(cssText: string, tag?: string | Number): Boolean {
279+
const parsed: RuleSet[] = CSSSource.fromSource(cssText, applicationKeyframes, undefined).selectors;
280+
let changed = false;
281+
if (parsed && parsed.length) {
282+
changed = true;
283+
if (tag != null) {
284+
for (let i = 0; i < parsed.length; i++) {
285+
parsed[i].tag = tag;
286+
}
287+
}
288+
applicationAdditionalSelectors.push.apply(applicationAdditionalSelectors, parsed);
289+
mergeCssSelectors();
290+
}
291+
return changed;
292+
}
293+
265294
const onCssChanged = profile("\"style-scope\".onCssChanged", (args: applicationCommon.CssChangedEventData) => {
266295
if (args.cssText) {
267296
const parsed = CSSSource.fromSource(args.cssText, applicationKeyframes, args.cssFile).selectors;
@@ -414,7 +443,7 @@ export class CssState {
414443
* Calculate the difference between the previously applied property values,
415444
* and the new set of property values that have to be applied for the provided selectors.
416445
* Apply the values and ensure each property setter is called at most once to avoid excessive change notifications.
417-
* @param matchingSelectors
446+
* @param matchingSelectors
418447
*/
419448
private setPropertyValues(matchingSelectors: SelectorCore[]): void {
420449
const newPropertyValues = new this.view.style.PropertyBag();

0 commit comments

Comments
 (0)