Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 73e7414

Browse files
author
Narciso Jaramillo
committed
Update label in rule list when a selector is edited
1 parent df1d141 commit 73e7414

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/editor/CSSInlineEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ define(function (require, exports, module) {
246246

247247
CSSUtils.findMatchingRules(selectorName, hostEditor.document)
248248
.done(function (rules) {
249-
cssInlineEditor = new MultiRangeInlineEditor(CSSUtils.consolidateRules(rules) || [], _getNoRulesMsg);
249+
cssInlineEditor = new MultiRangeInlineEditor(CSSUtils.consolidateRules(rules) || [], _getNoRulesMsg, CSSUtils.getRangeSelectors);
250250
cssInlineEditor.load(hostEditor);
251251

252252
var $header = $(".inline-editor-header", cssInlineEditor.$htmlContent);

src/editor/MultiRangeInlineEditor.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ define(function (require, exports, module) {
7474
SearchResultItem.prototype.textRange = null;
7575
SearchResultItem.prototype.$listItem = null;
7676

77-
function _updateRangeLabel(listItem, range) {
77+
function _updateRangeLabel(listItem, range, labelCB) {
78+
if (labelCB) {
79+
range.name = labelCB(range.textRange);
80+
}
7881
var text = StringUtils.htmlEscape(range.name) + " <span class='related-file'>— " + StringUtils.htmlEscape(range.textRange.document.file.name) + " : " + (range.textRange.startLine + 1) + "</span>";
7982
listItem.html(text);
8083
listItem.attr("title", listItem.text());
@@ -85,9 +88,11 @@ define(function (require, exports, module) {
8588
* @param {Array.<{name:String,document:Document,lineStart:number,lineEnd:number}>} ranges The text ranges to display.
8689
* @param {function(): $.Promise} messageCB An optional callback that returns a promise that will be resolved with a message to show
8790
* when no matches are available.
91+
* @param {function(range): string} labelCB An optional callback that returns an updated label string for the given range. Called
92+
* when we detect that the content of one of the ranges has changed.
8893
* @extends {InlineTextEditor}
8994
*/
90-
function MultiRangeInlineEditor(ranges, messageCB) {
95+
function MultiRangeInlineEditor(ranges, messageCB, labelCB) {
9196
InlineTextEditor.call(this);
9297

9398
// Store the results to show in the range list. This creates TextRanges bound to the Document,
@@ -96,6 +101,7 @@ define(function (require, exports, module) {
96101
return new SearchResultItem(rangeResult);
97102
});
98103
this._messageCB = messageCB;
104+
this._labelCB = labelCB;
99105

100106
this._selectedRangeIndex = -1;
101107
}
@@ -113,6 +119,7 @@ define(function (require, exports, module) {
113119
MultiRangeInlineEditor.prototype._ranges = null;
114120
MultiRangeInlineEditor.prototype._selectedRangeIndex = null;
115121
MultiRangeInlineEditor.prototype._messageCB = null;
122+
MultiRangeInlineEditor.prototype._labelCB = null;
116123

117124
/**
118125
* @private
@@ -133,6 +140,8 @@ define(function (require, exports, module) {
133140
// Update list item as TextRange changes
134141
$(range.textRange).on("change", function () {
135142
_updateRangeLabel($rangeItem, range);
143+
}).on("contentChange", function () {
144+
_updateRangeLabel($rangeItem, range, self._labelCB);
136145
});
137146

138147
// If TextRange lost sync, remove it from the list (and close the widget if no other ranges are left)

src/language/CSSUtils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,23 @@ define(function (require, exports, module) {
12681268
return newRules;
12691269
}
12701270

1271+
/**
1272+
* Given a TextRange, extracts the selector(s) at the beginning of the range and returns it.
1273+
* @param {TextRange} range The range to extract the selector(s) from.
1274+
* @return {string} The selector(s) at the beginning of the range.
1275+
*/
1276+
function getRangeSelectors(range) {
1277+
// There's currently no immediate way to access a given line in a Document, because it's just
1278+
// stored as a string. Eventually, we should have Documents cache the lines in the document
1279+
// as well, or make them use CodeMirror documents which do the same thing.
1280+
var i, startIndex = 0, text = range.document.getText();
1281+
for (i = 0; i < range.startLine; i++) {
1282+
startIndex = text.indexOf("\n", startIndex) + 1;
1283+
}
1284+
var nextBrace = text.indexOf("{", startIndex);
1285+
return text.substring(startIndex, nextBrace).trim().replace("\n", " ");
1286+
}
1287+
12711288
exports._findAllMatchingSelectorsInText = _findAllMatchingSelectorsInText; // For testing only
12721289
exports.findMatchingRules = findMatchingRules;
12731290
exports.extractAllSelectors = extractAllSelectors;
@@ -1276,6 +1293,7 @@ define(function (require, exports, module) {
12761293
exports.reduceStyleSheetForRegExParsing = reduceStyleSheetForRegExParsing;
12771294
exports.addRuleToDocument = addRuleToDocument;
12781295
exports.consolidateRules = consolidateRules;
1296+
exports.getRangeSelectors = getRangeSelectors;
12791297

12801298
exports.SELECTOR = SELECTOR;
12811299
exports.PROP_NAME = PROP_NAME;

test/spec/CSSUtils-test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ define(function (require, exports, module) {
3232
FileUtils = require("file/FileUtils"),
3333
CSSUtils = require("language/CSSUtils"),
3434
HTMLUtils = require("language/HTMLUtils"),
35-
SpecRunnerUtils = require("spec/SpecRunnerUtils");
35+
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
36+
TextRange = require("document/TextRange").TextRange;
3637

3738
var testPath = SpecRunnerUtils.getTestPath("/spec/CSSUtils-test-files"),
3839
simpleCssFileEntry = new NativeFileSystem.FileEntry(testPath + "/simple.css"),
@@ -1653,6 +1654,20 @@ define(function (require, exports, module) {
16531654
rules[5]
16541655
]);
16551656
});
1657+
1658+
it("should extract selectors at the beginning of a text range", function () {
1659+
var doc = SpecRunnerUtils.createMockDocument(".foo {}\n.bar, #baz {}\nh2 {}\n"),
1660+
range = new TextRange(doc, 1, 1);
1661+
expect(CSSUtils.getRangeSelectors(range)).toBe(".bar, #baz");
1662+
range.dispose();
1663+
});
1664+
1665+
it("should extract selectors spanning multiple lines at the beginning of a text range, with newlines replaced", function () {
1666+
var doc = SpecRunnerUtils.createMockDocument(".foo {}\n.bar,\n#baz {}\nh2 {}\n"),
1667+
range = new TextRange(doc, 1, 2);
1668+
expect(CSSUtils.getRangeSelectors(range)).toBe(".bar, #baz");
1669+
range.dispose();
1670+
});
16561671
});
16571672

16581673
// Unit Tests: "HTMLUtils (css)"

0 commit comments

Comments
 (0)