Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Autocomplete CSS properties. http://webkit.org/b/17374
Browse files Browse the repository at this point in the history
  • Loading branch information
NV committed May 4, 2010
1 parent c9ad542 commit fdb0292
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
35 changes: 35 additions & 0 deletions CSS.js
@@ -0,0 +1,35 @@
WebInspector.CSS = {
properties: (function getCSSProperties(){
var properties = Array.convert(document.defaultView.getComputedStyle(document.documentElement, ''));
// Add shorthands
for (var i=0; i<properties.length; i++) {

This comment has been minimized.

Copy link
@JosephPecoraro

JosephPecoraro May 4, 2010

You use properties.length in the loop condition AND you append to properties with properties.push down below. It looks like you could be processing the same values multiple times? Sound to me like you should cache the length of properties before you loop. Clever solution too though =).

var s = properties[i].split('-');
var j = s.length;
while (--j) {
var prop = s.slice(0, j).join('-');
if (typeof document.documentElement.style[prop] !== 'undefined' && properties.indexOf(prop) < 0) {
properties.push(prop);
}
}
}
return properties;
})()
}

WebInspector.CSS.properties.startsWith = function startsWith(str)
{
return this.filter(function(prop){
return prop.indexOf(str) === 0
});
};

WebInspector.CSS.properties.firstStartsWith = function firstStartsWith(str)
{
if (!str) return '';
for (var i=0; i<this.length; i++) {
if (this[i].indexOf(str) === 0) {
return this[i];
}
}
return '';
};
20 changes: 17 additions & 3 deletions StylesSidebarPane.js
Expand Up @@ -1298,8 +1298,6 @@ WebInspector.StylePropertyTreeElement.prototype = {
{
var arrowKeyPressed = (event.keyIdentifier === "Up" || event.keyIdentifier === "Down");
var pageKeyPressed = (event.keyIdentifier === "PageUp" || event.keyIdentifier === "PageDown");
if (!arrowKeyPressed && !pageKeyPressed)
return;

var selection = window.getSelection();
if (!selection.rangeCount)
Expand All @@ -1315,7 +1313,7 @@ WebInspector.StylePropertyTreeElement.prototype = {
var replacementString = wordString;

var matches = /(.*?)(-?\d+(?:\.\d+)?)(.*)/.exec(wordString);
if (matches && matches.length) {
if (arrowKeyPressed || pageKeyPressed && matches && matches.length) {
var prefix = matches[1];
var number = parseFloat(matches[2]);
var suffix = matches[3];
Expand Down Expand Up @@ -1352,6 +1350,22 @@ WebInspector.StylePropertyTreeElement.prototype = {
}

replacementString = prefix + number + suffix;
} else if (/[A-Z-]/.test(event.character)) {
setTimeout(function(){
var element = event.target;
var name = element.querySelector('.name');
var value = element.querySelector('.value');
var property = name.textContent;
var new_property = WebInspector.CSS.properties.firstStartsWith(property);
var n = property.length - new_property.length;
if (new_property != property) {
name.textContent = new_property;
}
if (n < 0) {
name.firstChild.select(n);
}
}, 0);
return;
} else {
// FIXME: this should cycle through known keywords for the current property name.
return;
Expand Down
1 change: 1 addition & 0 deletions inspector.html
Expand Up @@ -80,6 +80,7 @@
<script type="text/javascript" src="PropertiesSidebarPane.js"></script>
<script type="text/javascript" src="EventListenersSidebarPane.js"></script>
<script type="text/javascript" src="Color.js"></script>
<script type="text/javascript" src="CSS.js"></script>
<script type="text/javascript" src="StylesSidebarPane.js"></script>
<script type="text/javascript" src="PanelEnablerView.js"></script>
<script type="text/javascript" src="WelcomeView.js"></script>
Expand Down
2 changes: 0 additions & 2 deletions inspector.js
Expand Up @@ -679,8 +679,6 @@ WebInspector.documentClick = function(event)

WebInspector.documentKeyDown = function(event)
{
if (WebInspector.isEditingAnyField())
return;

if (this.currentFocusElement && this.currentFocusElement.handleKeyEvent) {
this.currentFocusElement.handleKeyEvent(event);
Expand Down
38 changes: 38 additions & 0 deletions utilities.js
Expand Up @@ -312,6 +312,27 @@ Element.prototype.offsetRelativeToWindow = function(targetWindow)
return elementOffset;
}

KeyboardEvent.prototype.__defineGetter__('character', function getCharacter()
{
var code = parseInt(this.keyIdentifier.slice(2), 16);
return String.fromCharCode(code);
});

Text.prototype.select = function select(start, end)
{
end = end || this.textContent.length;
if (start < 0) {
start = end + start;
}
var selection = window.getSelection();
selection.removeAllRanges();
var range = document.createRange();
range.setStart(this, start);
range.setEnd(this, end);
selection.addRange(range);
return this;
};

Node.prototype.isWhitespace = isNodeWhitespace;
Node.prototype.displayName = nodeDisplayName;
Node.prototype.isAncestor = function(node)
Expand Down Expand Up @@ -632,6 +653,12 @@ HTMLTextAreaElement.prototype.moveCursorToEnd = function()
this.setSelectionRange(length, length);
}

Array.convert = function convert(list)
{
// convert array-like object to array
return Array.prototype.slice.call(list);
};

Array.prototype.remove = function(value, onlyFirst)
{
if (onlyFirst) {
Expand All @@ -656,6 +683,17 @@ Array.prototype.keySet = function()
return keys;
}

Array.prototype.__defineGetter__('last', function getLast()
{
return this[this.length - 1 || 0];
});

Array.prototype.__defineSetter__('last', function setLast(value)
{
this[this.length - 1 || 0] = value;
});

This comment has been minimized.

Copy link
@JosephPecoraro

JosephPecoraro May 4, 2010

These two aren't currently used?

This comment has been minimized.

Copy link
@NV

NV May 4, 2010

Author Owner

Whoops, you are right.


function insertionIndexForObjectInListSortedByFunction(anObject, aList, aFunction)
{
// indexOf returns (-lowerBound - 1). Taking (-result - 1) works out to lowerBound.
Expand Down

1 comment on commit fdb0292

@JosephPecoraro
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some inline comments. Looking good so far.

Please sign in to comment.