This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 25475 - pad example has width instead of pad
Summary: pad example has width instead of pad
Status: RESOLVED NEEDSINFO
Alias: None
Product: CSS
Classification: Unclassified
Component: Counter Styles (show other bugs)
Version: unspecified
Hardware: PC Windows NT
: P2 normal
Target Milestone: ---
Assignee: Tab Atkins Jr.
QA Contact: public-css-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-04-26 10:10 UTC by Jim Michaels
Modified: 2014-04-28 05:11 UTC (History)
0 users

See Also:


Attachments

Description Jim Michaels 2014-04-26 10:10:40 UTC
http://www.w3.org/TR/css-counter-styles-3/#at-counter-style
example 9's code in the pad example has an error or maybe old code in it, has width: 3 "0"; when it should probably have pad: 3 "0";

please document in this section also what happens if you use pad:0 "0"; or pad:0 " "; I should hope this zero or space-pads the digits to fit the largest amount of digits in the column (column width). perhaps the function below could assist in this.

also, regarding pad and anything that has to do with numbers and width, I cwould like to contribute an algorithm based on an equation I got from my professor years and years ago.


could this function also somehow be made available as a css function like calc()? for people doing line numbers, the width of the field is important, and it would be nice to auto-size it according to the amount of code. there are a lot of color syntax highlighters out there (and more in use too). should I make a separate feature request for this? it could simply be called numdigits. I would like to see it used with calc and attr().

/* js - this function works with integers, positive, negative, and 0, and tells you the number of digits required. */
function NumberOfDigits(n, base, countMinusSign) {
    //type checking
    if ('number'!=typeof(number)) {
        alert("NumberOfDigits() was fed type "+typeof(n)+" for n, should be Number");
        return 0;//failure
    }
    if ('number'!=typeof(base)) {
        alert("NumberOfDigits() was fed type "+typeof(base)+" for base, should be Number");
        return 0;//failure
    }
    if ('boolean'!=typeof(countMinusSign)) {
        alert("NumberOfDigits() was fed type "+typeof(countMinusSign)+" for countMinusSign, should be Boolean");
        return 0;//failure
    }
    //zero special case
    if (0 == number) {
        return 1;
    }
    //equation
    return Math.ceil(Math.log(Math.abs(number) + 1) / Math.log(base)) + 
           ((countMinusSign && (number < 0))?1:0);
}

this will determine the integer number of digits for a number, maybe it will be useful for speeding up calculation of number of digits in a number (without having to count all the digits and doing string conversion). from my web page at 
http://jesusnjim.com/programming/number-of-digits-in-a-number-for-any-given-base.html#floatingpoint
Comment 1 Tab Atkins Jr. 2014-04-27 20:59:22 UTC
Thanks for the 'width' notice; it's fixed.

The spec *does* specify what happens if you provide a width of 0.  Just follow the algorithm as it's written; when the width is 0, 'pad' has no effect.  We can't make it somehow figure out how large it would need to be, because it's not tied to values in any way; you can apply the same counter style to multiple different counters.  Even if we explicitly associated it with a given counter as well, two independent lists could have the same counter name and counter style, but different max widths.

Can you explain what the use-case is for knowing the number of digits a number would occupy in a given base, from within CSS?
Comment 2 Jim Michaels 2014-04-28 05:03:32 UTC
the counter itself would probably be implemented probably an int or int64_t (c++) or a Number (js) for the case of system:numeric; (implies an integer counter number I should think?)). the rest is simple number-base conversion. 
the digits you extract from the counter value)

http://jesusnjim.com/programming/number-base-conversion.html#nbc-btb
http://jesusnjim.com/programming/number-base-conversion.html#algorithm-tostring
note the summation equation. it's short (the first part, there are several = signs...
from what I saw of the examples in the counter-style stuff, it appears to be a base-n counter where n is the number of symbols. cool stuff.

hope this comment helps someone for implementation.
Comment 3 Jim Michaels 2014-04-28 05:11:32 UTC
forgot to add: the last count in a set you can extract the number of digits from to obtain the width in symbols for use in an "auto" pad value were it to be implemeneted.
would be very useful. a lot of people (people who make code display libraries) might prefer auto to a fixed width.

having an "auto" at least with this algorithm probably means rendering twice, one maybe without display just for width calculation purposes and one for final display. (unless you already have a .length of the numbber of elements in question, which would speed things up and eliminate the need for a double-render, since you can go straight to using that .length as your n for the number of digits in a number function. you then use that as the number of symbols.