Template:Str left/doc: Difference between revisions
(text editing) |
m (move used in system template to top) |
||
Line 1: | Line 1: | ||
{{used in system}} |
|||
{{Documentation subpage}} |
|||
<!-- Categories go where indicated at the bottom of this page, please; interwikis go to Wikidata (see also: [[Wikipedia:Wikidata]]). --> |
|||
== Usage == |
== Usage == |
||
: {{ |
: {{Template link expanded|Str left|<string>|<count>}} |
||
Gives the resultant <count> of characters ''creating a substring of characters'' from the start of the [[w:Trim (programming)|trimmed]] string (i.e. the substring returned will have length <count>, exclusive of leading whitespace characters, which are trimmed first before <count> is invoked). |
Gives the resultant <count> of characters ''creating a substring of characters'' from the start of the [[w:Trim (programming)|trimmed]] string (i.e. the substring returned will have length <count>, exclusive of leading whitespace characters, which are trimmed first before <count> is invoked). |
||
Line 24: | Line 28: | ||
All the limitations of use for this template and shown below are caused by the current implementation of the <nowiki>{{padleft:}}</nowiki> parser function used in this template, which does not filter its third parameter containing the characters used for padding a string to the specified length (it currently counts incorrectly the characters to extract from the padding string, and incorrectly measures its effective length; in addition this parameter is limited to 500 bytes). |
All the limitations of use for this template and shown below are caused by the current implementation of the <nowiki>{{padleft:}}</nowiki> parser function used in this template, which does not filter its third parameter containing the characters used for padding a string to the specified length (it currently counts incorrectly the characters to extract from the padding string, and incorrectly measures its effective length; in addition this parameter is limited to 500 bytes). |
||
The following examples are demonstrating that this template |
The following examples are demonstrating that this template does not interpret character entity references as the character they represent. It treats them as multiple characters and can leave them truncated. |
||
* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&nbsp;c | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a c | 3 }} |
|||
*: The output "a c" may have been intended, but the named character entity was truncated, leaving no length available for the final "c". |
|||
* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&#66;c | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | aBc | 3 }} |
|||
*: This should display "abc", but the named character entity was truncated, leaving no length available for the final "c". |
|||
* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a c | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a c | 3 }} |
|||
*: The result is correct, as expected. |
|||
* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&#32;c | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a c | 3 }} |
|||
*: This last example should be equivalent to the previous one, but it is not. |
|||
This template will erase nowiki tags without counting their length: |
|||
The following example shows that the presence of some wiki markup that should be transparent and ignored, does not allow this template to work as expected (the nowiki section is replaced by a unique index string in the effective runtime value of the parameter; this unique (43-bytes long) string starts by a control character (which will be finally filtered out from the generated HTML page if it remains in the result of this template), followed by "UNIQ" and some unique sequence and terminated by "QINU" and a final control character; it is partially visible below, because the unique string is not complete after truncation, and then cannot be eliminated from the output in the first two lines): |
|||
* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<nowiki/>bcd | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<nowiki/>bcd | 3 }} |
|||
*: Returns three characters as expected |
|||
:*: It effectively returns 3 bytes, but the \x07 (BELL) control in the second position is stripped out when generating the HTML displayed here. |
|||
* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<nowiki>E</nowiki>bcd | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<nowiki>E</nowiki>bcd | 3 }} |
|||
*: Returns three characters, but not aEb as might have been expected. |
|||
:*: It effectively returns 43 bytes, but the \x07 (BELL) control in the second position is stripped out when generating the HTML displayed here, and the final \x07 was truncated |
|||
* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<nowiki/>bc | 10 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<nowiki/>bc | 10 }} |
|||
*: Returns 10 characters as expected. |
|||
:*: It effectively returns 44 bytes, but the complete 43-bytes sequence is filtered out from the output wiki code when generating the final HTML, so there just remains the first leading "a" character . |
|||
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<nowiki/>bc | 45 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<nowiki/>bc | 45 }} |
|||
HTML comments, and the "noinclude", "includeonly" and "onlyinclude" wiki markup tags are also ignored, because they are preprocessed in template parameters, before including it and substituting parameters: |
|||
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<noinclude/>bc | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<noinclude/>bc | 3 }} |
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<noinclude/>bc | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<noinclude/>bc | 3 }} |
||
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<!-- comment -->bc | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<!-- comment -->bc | 3 }} |
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a<!-- comment -->bc | 3 <nowiki>}}</nowiki></code> → {{ {{BASEPAGENAME}} | a<!-- comment -->bc | 3 }} |
||
Line 54: | Line 57: | ||
<templatedata> |
<templatedata> |
||
{ |
{ |
||
⚫ | |||
"description": { |
|||
⚫ | |||
⚫ | |||
"1": { |
|||
"cs": "Šablona pro zadání <count> podřetězce znaků od začátku oříznutého řetězce" |
|||
"label": "String", |
|||
}, |
|||
⚫ | |||
⚫ | |||
⚫ | |||
"1": { |
|||
⚫ | |||
"label": { |
|||
}, |
|||
"en": "String", |
|||
"2": { |
|||
"cs": "Řetězec" |
|||
"label": "Count", |
|||
}, |
|||
⚫ | |||
"description": { |
|||
⚫ | |||
⚫ | |||
"required": true |
|||
"cs": "Řetězec, který má být oříznut a spočítán" |
|||
} |
|||
}, |
|||
} |
|||
⚫ | |||
"required": true |
|||
}, |
|||
"2": { |
|||
"label": { |
|||
"en": "Count", |
|||
"cs": "Počet" |
|||
}, |
|||
"description": { |
|||
⚫ | |||
"cs": "Dává <count> podřetězec znaků od začátku oříznutého řetězce" |
|||
}, |
|||
⚫ | |||
⚫ | |||
} |
|||
} |
|||
} |
} |
||
</templatedata> |
</templatedata> |
||
== See also == |
== See also == |
||
* [[Bugzilla:22555]] (need for correcting padleft/padright functions and to provide better string-handling parser functions) |
* [[Bugzilla:22555]] (historical; need for correcting padleft/padright functions and to provide better string-handling parser functions) |
||
{{String-handling templates |sub}} |
|||
<includeonly>{{Sandbox other|| |
<includeonly>{{Sandbox other|| |
Revision as of 00:07, 21 May 2023
This template is used in system messages. Changes to it can cause immediate changes to the MediaWiki user interface. To avoid large-scale disruption, any changes should first be tested in this template's /sandbox or /testcases subpage, or in your own user space.The tested changes can then be added in one single edit to this template. Please discuss any changes on the talk page before implementing them. |
This is a documentation subpage for Template:Str left. It contains usage information, categories and other content that is not part of the original Template page. |
Usage
{{Str left|<string>|<count>}}
Gives the resultant <count> of characters creating a substring of characters from the start of the trimmed string (i.e. the substring returned will have length <count>, exclusive of leading whitespace characters, which are trimmed first before <count> is invoked).
If <count> is invalid, empty or zero, an empty string is returned. If undefined, it defaults to 1.
Limitations
- Length to 500: The maximum substring stops at 500 long, yet gives no error message. Only 500 characters can be extracted even if the <string> is longer.
- Inexpensive (uses no other templates).
Examples
{{ Str left | Lorem ipsum dolor sit amet | 10 }}
→ Lorem ipsu{{Str left| Lorem ipsum dolor sit amet | 4 }}
→ Lore, (note leading spaces trimmed!){{ Str left | Lorem ipsum dolor sit amet | 1 }}
→ L{{ Str left | Lorem ipsum dolor sit amet | 0 }}
→{{ Str left | Lorem ipsum dolor sit amet | }}
→{{ Str left | Lorem ipsum dolor sit amet }}
→ L{{ Str left | Lorem ipsum dolor sit amet | 40 }}
→ Lorem ipsum dolor sit ametLorem ipsum do{{ Str left | àçé | 2 }}
→ àç- This example is demonstrating the safety of this template with the UTF-8 encoding (this should work now) :
Examples of limitations
All the limitations of use for this template and shown below are caused by the current implementation of the {{padleft:}} parser function used in this template, which does not filter its third parameter containing the characters used for padding a string to the specified length (it currently counts incorrectly the characters to extract from the padding string, and incorrectly measures its effective length; in addition this parameter is limited to 500 bytes).
The following examples are demonstrating that this template does not interpret character entity references as the character they represent. It treats them as multiple characters and can leave them truncated.
{{ Str left | a c | 3 }}
→ a&n- The output "a c" may have been intended, but the named character entity was truncated, leaving no length available for the final "c".
{{ Str left | aBc | 3 }}
→ a&#- This should display "abc", but the named character entity was truncated, leaving no length available for the final "c".
{{ Str left | a c | 3 }}
→ a c- The result is correct, as expected.
{{ Str left | a c | 3 }}
→ a&#- This last example should be equivalent to the previous one, but it is not.
This template will erase nowiki tags without counting their length:
{{ Str left | a<nowiki/>bcd | 3 }}
→ abc- Returns three characters as expected
{{ Str left | a<nowiki>E</nowiki>bcd | 3 }}
→ abc- Returns three characters, but not aEb as might have been expected.
{{ Str left | a<nowiki/>bc | 10 }}
→ abcabcabca- Returns 10 characters as expected.
HTML comments, and the "noinclude", "includeonly" and "onlyinclude" wiki markup tags are also ignored, because they are preprocessed in template parameters, before including it and substituting parameters:
{{ Str left | a<noinclude/>bc | 3 }}
→ abc{{ Str left | a<!-- comment -->bc | 3 }}
→ abc
Note also that the specified length will cause the input string to be padded by repeating it, if this length is larger than the input string length:
{{ Str left | Abc. | 10 }}
→ Abc.Abc.Ab
Template data
This is the TemplateData documentation for this template used by VisualEditor and other tools.
Str left
A template to give the <count> substring of characters from the start of the trimmed string
Parameter | Description | Type | Status | |
---|---|---|---|---|
String | 1 | The string to be trimmed and counted | String | required |
Count | 2 | Gives the <count> substring of characters from the start of the trimmed string | Number | required |
See also
- Bugzilla:22555 (historical; need for correcting padleft/padright functions and to provide better string-handling parser functions)