Skip to content

Commit bd9a15d

Browse files
committed
Skip blank lines when computing WS prefix of code blocks. Fixes #2209.
1 parent 1512507 commit bd9a15d

File tree

5 files changed

+144
-163
lines changed

5 files changed

+144
-163
lines changed

bikeshed/markdown/markdown.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ def inlineElementStart(line):
252252
token["line"] = line
253253
tokens.append(token)
254254

255-
# for token in tokens:
256-
# print(f"{' ' * (11 - len(token['type']))}{token['type']}: {token['line'].text.rstrip()}")
255+
if False:
256+
for i,token in enumerate(tokens):
257+
print(f"{' '*(2-len(str(i)))}{i} {' ' * (11 - len(token['type']))}{token['type']}: {token['line'].text.rstrip()}")
257258

258259
return tokens
259260

@@ -369,6 +370,10 @@ def stripCommonWsPrefix(tokens):
369370

370371

371372
def commonPrefix(line1, line2):
373+
if line1 is None:
374+
return line2
375+
if line2 is None:
376+
return line1
372377
prefixSoFar = ""
373378
for i, char in enumerate(line1):
374379
if i == len(line2):
@@ -381,6 +386,8 @@ def commonPrefix(line1, line2):
381386

382387

383388
def getWsPrefix(line):
389+
if line.strip() == "":
390+
return None
384391
return re.match(r"(\s*)", line).group(1)
385392

386393

tests/github/WICG/sanitizer-api/index.html

+23-34
Original file line numberDiff line numberDiff line change
@@ -814,24 +814,18 @@ <h3 class="heading settled" data-level="2.3" id="config"><span class="secno">2.3
814814
elements. Using both types is possible, but is probably of little practical
815815
use. The same applies to <code>allowAttributes</code> and <code>dropAttributes</code>.</p>
816816
<p>Examples:</p>
817-
<pre class="language-js highlight"> <c- a>const</c-> sample <c- o>=</c-> <c- u>"Some text &lt;b>&lt;i>with&lt;/i>&lt;/b> &lt;blink>tags&lt;/blink>."</c-><c- p>;</c->
818-
819-
<c- c1>// "Some text &lt;b>with&lt;/b> text tags."</c->
820-
<c- k>new</c-> Sanitizer<c- p>({</c->allowElements<c- o>:</c-> <c- p>[</c-> <c- u>"b"</c-> <c- p>]).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
821-
822-
<c- c1>// "Some text &lt;i>with&lt;/i> &lt;blink>tags&lt;/blink>."</c->
823-
<c- k>new</c-> Sanitizer<c- p>({</c->blockElements<c- o>:</c-> <c- p>[</c-> <c- u>"b"</c-> <c- p>]).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
824-
825-
<c- c1>// "Some text &lt;blink>tags&lt;/blink>."</c->
826-
<c- k>new</c-> Sanitizer<c- p>({</c->dropElements<c- o>:</c-> <c- p>[</c-> <c- u>"b"</c-> <c- p>]).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
827-
828-
<c- c1>// Note: The default configuration handles XSS-relevant input:</c->
829-
830-
<c- c1>// Non-scripting input will be passed through:</c->
831-
<c- k>new</c-> Sanitizer<c- p>().</c->sanitizeToString<c- p>(</c->sample<c- p>);</c-> <c- c1>// Will output sample unmodified.</c->
832-
833-
<c- c1>// Scripts will be blocked: "abc alert(1) def"</c->
834-
<c- k>new</c-> Sanitizer<c- p>().</c->sanitizeToString<c- p>(</c-><c- u>"abc &lt;script>alert(1)&lt;/script> def"</c-><c- p>);</c->
817+
<pre class="language-js highlight"><c- a>const</c-> sample <c- o>=</c-> <c- u>"Some text &lt;b>&lt;i>with&lt;/i>&lt;/b> &lt;blink>tags&lt;/blink>."</c-><c- p>;</c->
818+
<c- c1>// "Some text &lt;b>with&lt;/b> text tags."</c->
819+
<c- k>new</c-> Sanitizer<c- p>({</c->allowElements<c- o>:</c-> <c- p>[</c-> <c- u>"b"</c-> <c- p>]).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
820+
<c- c1>// "Some text &lt;i>with&lt;/i> &lt;blink>tags&lt;/blink>."</c->
821+
<c- k>new</c-> Sanitizer<c- p>({</c->blockElements<c- o>:</c-> <c- p>[</c-> <c- u>"b"</c-> <c- p>]).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
822+
<c- c1>// "Some text &lt;blink>tags&lt;/blink>."</c->
823+
<c- k>new</c-> Sanitizer<c- p>({</c->dropElements<c- o>:</c-> <c- p>[</c-> <c- u>"b"</c-> <c- p>]).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
824+
<c- c1>// Note: The default configuration handles XSS-relevant input:</c->
825+
<c- c1>// Non-scripting input will be passed through:</c->
826+
<c- k>new</c-> Sanitizer<c- p>().</c->sanitizeToString<c- p>(</c->sample<c- p>);</c-> <c- c1>// Will output sample unmodified.</c->
827+
<c- c1>// Scripts will be blocked: "abc alert(1) def"</c->
828+
<c- k>new</c-> Sanitizer<c- p>().</c->sanitizeToString<c- p>(</c-><c- u>"abc &lt;script>alert(1)&lt;/script> def"</c-><c- p>);</c->
835829
</pre>
836830
<h4 class="heading settled" data-level="2.3.1" id="attr-match-list"><span class="secno">2.3.1. </span><span class="content">Attribute Match Lists</span><a class="self-link" href="#attr-match-list"></a></h4>
837831
<p>An <dfn class="dfn-paneled" data-dfn-type="dfn" data-noexport id="attribute-match-list">attribute match list</dfn> is a map of attribute names to element names,
@@ -841,22 +835,17 @@ <h4 class="heading settled" data-level="2.3.1" id="attr-match-list"><span class=
841835
<pre class="idl highlight def"><c- b>typedef</c-> <a data-link-type="dfn" href="https://heycam.github.io/webidl/#idl-record" id="ref-for-idl-record"><c- b>record</c-></a>&lt;<a class="idl-code" data-link-type="interface" href="https://heycam.github.io/webidl/#idl-DOMString" id="ref-for-idl-DOMString⑤"><c- b>DOMString</c-></a>, <a data-link-type="dfn" href="https://heycam.github.io/webidl/#idl-sequence" id="ref-for-idl-sequence③"><c- b>sequence</c-></a>&lt;<a class="idl-code" data-link-type="interface" href="https://heycam.github.io/webidl/#idl-DOMString" id="ref-for-idl-DOMString⑥"><c- b>DOMString</c-></a>>> <dfn class="dfn-paneled idl-code" data-dfn-type="typedef" data-export id="typedefdef-attributematchlist"><code><c- g>AttributeMatchList</c-></code></dfn>;
842836
</pre>
843837
<p>Examples for attributes and attribute match lists:</p>
844-
<pre class="language-js highlight"> <c- a>const</c-> sample <c- o>=</c-> <c- u>"&lt;span id='span1' class='theclass' style='font-weight: bold'>hello&lt;/span>"</c-><c- p>;</c->
845-
846-
<c- c1>// Allow only &lt;span style>: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
847-
<c- k>new</c-> Sanitizer<c- p>({</c->allowAttributes<c- o>:</c-> <c- p>{</c-><c- u>"style"</c-><c- o>:</c-> <c- p>[</c-><c- u>"span"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
848-
849-
<c- c1>// Allow style, but not on span: "&lt;span>...&lt;/span>"</c->
850-
<c- k>new</c-> Sanitizer<c- p>({</c->allowAttributes<c- o>:</c-> <c- p>{</c-><c- u>"style"</c-><c- o>:</c-> <c- p>[</c-><c- u>"div"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
851-
852-
<c- c1>// Allow style on any elements: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
853-
<c- k>new</c-> Sanitizer<c- p>({</c->allowAttributes<c- o>:</c-> <c- p>{</c-><c- u>"style"</c-><c- o>:</c-> <c- p>[</c-><c- u>"*"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
854-
855-
<c- c1>// Block &lt;span id>: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
856-
<c- k>new</c-> Sanitizer<c- p>({</c->blockAttributes<c- o>:</c-> <c- p>{</c-><c- u>"id"</c-><c- o>:</c-> <c- p>[</c-><c- u>"span"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
857-
858-
<c- c1>// Block id, everywhere: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
859-
<c- k>new</c-> Sanitizer<c- p>({</c->blockAttributes<c- o>:</c-> <c- p>{</c-><c- u>"id"</c-><c- o>:</c-> <c- p>[</c-><c- u>"*"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
838+
<pre class="language-js highlight"><c- a>const</c-> sample <c- o>=</c-> <c- u>"&lt;span id='span1' class='theclass' style='font-weight: bold'>hello&lt;/span>"</c-><c- p>;</c->
839+
<c- c1>// Allow only &lt;span style>: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
840+
<c- k>new</c-> Sanitizer<c- p>({</c->allowAttributes<c- o>:</c-> <c- p>{</c-><c- u>"style"</c-><c- o>:</c-> <c- p>[</c-><c- u>"span"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
841+
<c- c1>// Allow style, but not on span: "&lt;span>...&lt;/span>"</c->
842+
<c- k>new</c-> Sanitizer<c- p>({</c->allowAttributes<c- o>:</c-> <c- p>{</c-><c- u>"style"</c-><c- o>:</c-> <c- p>[</c-><c- u>"div"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
843+
<c- c1>// Allow style on any elements: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
844+
<c- k>new</c-> Sanitizer<c- p>({</c->allowAttributes<c- o>:</c-> <c- p>{</c-><c- u>"style"</c-><c- o>:</c-> <c- p>[</c-><c- u>"*"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
845+
<c- c1>// Block &lt;span id>: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
846+
<c- k>new</c-> Sanitizer<c- p>({</c->blockAttributes<c- o>:</c-> <c- p>{</c-><c- u>"id"</c-><c- o>:</c-> <c- p>[</c-><c- u>"span"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
847+
<c- c1>// Block id, everywhere: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
848+
<c- k>new</c-> Sanitizer<c- p>({</c->blockAttributes<c- o>:</c-> <c- p>{</c-><c- u>"id"</c-><c- o>:</c-> <c- p>[</c-><c- u>"*"</c-><c- p>]}}).</c->sanitizeToString<c- p>(</c->sample<c- p>);</c->
860849
</pre>
861850
<h3 class="heading settled" data-level="2.4" id="algorithms"><span class="secno">2.4. </span><span class="content">Algorithms</span><a class="self-link" href="#algorithms"></a></h3>
862851
<div class="algorithm" data-algorithm="sanitize">

tests/github/WICG/scroll-to-text-fragment/index.html

+6-8
Original file line numberDiff line numberDiff line change
@@ -1088,14 +1088,12 @@ <h4 class="heading settled" data-level="3.3.1" id="processing-the-fragment-direc
10881088
<a class="self-link" href="#example-42953739"></a> In other cases where a Document’s URL is not set by the UA, there is no
10891089
fragment directive stripping.
10901090
<p>For URL objects:</p>
1091-
<pre> let url = new URL('https://example.com#foo:~:bar');
1092-
console.log(url.href); // 'https://example.com#foo:~:bar'
1093-
console.log(url.hash); // '#foo:~:bar'
1094-
1095-
document.url = url;
1096-
console.log(document.url.href); // 'https://example.com#foo:~:bar'
1097-
console.log(document.url.hash); // '#foo:~:bar'
1098-
1091+
<pre>let url = new URL('https://example.com#foo:~:bar');
1092+
console.log(url.href); // 'https://example.com#foo:~:bar'
1093+
console.log(url.hash); // '#foo:~:bar'
1094+
document.url = url;
1095+
console.log(document.url.href); // 'https://example.com#foo:~:bar'
1096+
console.log(document.url.hash); // '#foo:~:bar'
10991097
</pre>
11001098
<p>The <code>&lt;a></code> or <code>&lt;area></code> elements:</p>
11011099
<pre>&lt;a id='anchor' href="https://example.com#foo:~:bar">Anchor&lt;/a>

tests/github/w3c/PFE/Overview.html

+24-26
Original file line numberDiff line numberDiff line change
@@ -629,21 +629,20 @@ <h4 class="heading settled" data-level="2.1.3" id="sparsebitset"><span class="se
629629
significant bit in the byte and the bit with the largest index is the most significant bit.</p>
630630
<div class="example" id="example-76606ecb">
631631
<a class="self-link" href="#example-76606ecb"></a> The set {2, 33, 323} in a tree with a branching factor of 8 is encoded as the bit string:
632-
<pre> BitString:
633-
|- header |- lvl 0 |---- level 1 ----|------- level 2 -----------|
634-
| | n0 | n1 n2 | n3 n4 n5 |
635-
[ 10010000 10000100 10001000 10000000 00100000 01000000 00010000 ]
636-
637-
Which then becomes the ByteString:
638-
[
639-
0b00001001,
640-
0b00100001,
641-
0b00010001,
642-
0b00000001,
643-
0b00000100,
644-
0b00000010,
645-
0b00001000
646-
]
632+
<pre>BitString:
633+
|- header |- lvl 0 |---- level 1 ----|------- level 2 -----------|
634+
| | n0 | n1 n2 | n3 n4 n5 |
635+
[ 10010000 10000100 10001000 10000000 00100000 01000000 00010000 ]
636+
Which then becomes the ByteString:
637+
[
638+
0b00001001,
639+
0b00100001,
640+
0b00010001,
641+
0b00000001,
642+
0b00000100,
643+
0b00000010,
644+
0b00001000
645+
]
647646
</pre>
648647
<p>First determine the height of the tree:</p>
649648
<p><i>H</i> = ceil(log<sub>8</sub>(323 + 1)) = 3</p>
@@ -684,17 +683,16 @@ <h4 class="heading settled" data-level="2.1.3" id="sparsebitset"><span class="se
684683
</div>
685684
<div class="example" id="example-88b1de08">
686685
<a class="self-link" href="#example-88b1de08"></a> The set {0, 1, 2, ..., 17} can be encoded with a branching factor of 4 as:
687-
<pre> BitString:
688-
|- header | l0 |- lvl 1 -| l2 |
689-
| | n0 | n1 | n2 | n3 |
690-
[ 00010000 1100 0000 1000 1100 ]
691-
692-
ByteString:
693-
[
694-
0b00001000,
695-
0b00000011,
696-
0b00110001
697-
]
686+
<pre>BitString:
687+
|- header | l0 |- lvl 1 -| l2 |
688+
| | n0 | n1 | n2 | n3 |
689+
[ 00010000 1100 0000 1000 1100 ]
690+
ByteString:
691+
[
692+
0b00001000,
693+
0b00000011,
694+
0b00110001
695+
]
698696
</pre>
699697
<p>First determine the height of the tree:</p>
700698
<p><i>H</i> = ceil(log<sub>4</sub>(17 + 1)) = 3</p>

0 commit comments

Comments
 (0)