Skip to content

Commit

Permalink
Skip blank lines when computing WS prefix of code blocks. Fixes #2209.
Browse files Browse the repository at this point in the history
  • Loading branch information
tabatkins committed Jan 20, 2022
1 parent 1512507 commit bd9a15d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 163 deletions.
11 changes: 9 additions & 2 deletions bikeshed/markdown/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ def inlineElementStart(line):
token["line"] = line
tokens.append(token)

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

return tokens

Expand Down Expand Up @@ -369,6 +370,10 @@ def stripCommonWsPrefix(tokens):


def commonPrefix(line1, line2):
if line1 is None:
return line2
if line2 is None:
return line1
prefixSoFar = ""
for i, char in enumerate(line1):
if i == len(line2):
Expand All @@ -381,6 +386,8 @@ def commonPrefix(line1, line2):


def getWsPrefix(line):
if line.strip() == "":
return None
return re.match(r"(\s*)", line).group(1)


Expand Down
57 changes: 23 additions & 34 deletions tests/github/WICG/sanitizer-api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -814,24 +814,18 @@ <h3 class="heading settled" data-level="2.3" id="config"><span class="secno">2.3
elements. Using both types is possible, but is probably of little practical
use. The same applies to <code>allowAttributes</code> and <code>dropAttributes</code>.</p>
<p>Examples:</p>
<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->

<c- c1>// "Some text &lt;b>with&lt;/b> text tags."</c->
<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->

<c- c1>// "Some text &lt;i>with&lt;/i> &lt;blink>tags&lt;/blink>."</c->
<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->

<c- c1>// "Some text &lt;blink>tags&lt;/blink>."</c->
<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->

<c- c1>// Note: The default configuration handles XSS-relevant input:</c->

<c- c1>// Non-scripting input will be passed through:</c->
<c- k>new</c-> Sanitizer<c- p>().</c->sanitizeToString<c- p>(</c->sample<c- p>);</c-> <c- c1>// Will output sample unmodified.</c->

<c- c1>// Scripts will be blocked: "abc alert(1) def"</c->
<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->
<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->
<c- c1>// "Some text &lt;b>with&lt;/b> text tags."</c->
<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->
<c- c1>// "Some text &lt;i>with&lt;/i> &lt;blink>tags&lt;/blink>."</c->
<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->
<c- c1>// "Some text &lt;blink>tags&lt;/blink>."</c->
<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->
<c- c1>// Note: The default configuration handles XSS-relevant input:</c->
<c- c1>// Non-scripting input will be passed through:</c->
<c- k>new</c-> Sanitizer<c- p>().</c->sanitizeToString<c- p>(</c->sample<c- p>);</c-> <c- c1>// Will output sample unmodified.</c->
<c- c1>// Scripts will be blocked: "abc alert(1) def"</c->
<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->
</pre>
<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>
<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,
Expand All @@ -841,22 +835,17 @@ <h4 class="heading settled" data-level="2.3.1" id="attr-match-list"><span class=
<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>;
</pre>
<p>Examples for attributes and attribute match lists:</p>
<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->

<c- c1>// Allow only &lt;span style>: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
<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->

<c- c1>// Allow style, but not on span: "&lt;span>...&lt;/span>"</c->
<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->

<c- c1>// Allow style on any elements: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
<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->

<c- c1>// Block &lt;span id>: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
<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->

<c- c1>// Block id, everywhere: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
<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->
<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->
<c- c1>// Allow only &lt;span style>: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
<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->
<c- c1>// Allow style, but not on span: "&lt;span>...&lt;/span>"</c->
<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->
<c- c1>// Allow style on any elements: "&lt;span style='font-weight: bold'>...&lt;/span>"</c->
<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->
<c- c1>// Block &lt;span id>: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
<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->
<c- c1>// Block id, everywhere: "&lt;span class='theclass' style='font-weight: bold'>...&lt;/span>";</c->
<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->
</pre>
<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>
<div class="algorithm" data-algorithm="sanitize">
Expand Down
14 changes: 6 additions & 8 deletions tests/github/WICG/scroll-to-text-fragment/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1088,14 +1088,12 @@ <h4 class="heading settled" data-level="3.3.1" id="processing-the-fragment-direc
<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
fragment directive stripping.
<p>For URL objects:</p>
<pre> let url = new URL('https://example.com#foo:~:bar');
console.log(url.href); // 'https://example.com#foo:~:bar'
console.log(url.hash); // '#foo:~:bar'

document.url = url;
console.log(document.url.href); // 'https://example.com#foo:~:bar'
console.log(document.url.hash); // '#foo:~:bar'

<pre>let url = new URL('https://example.com#foo:~:bar');
console.log(url.href); // 'https://example.com#foo:~:bar'
console.log(url.hash); // '#foo:~:bar'
document.url = url;
console.log(document.url.href); // 'https://example.com#foo:~:bar'
console.log(document.url.hash); // '#foo:~:bar'
</pre>
<p>The <code>&lt;a></code> or <code>&lt;area></code> elements:</p>
<pre>&lt;a id='anchor' href="https://example.com#foo:~:bar">Anchor&lt;/a>
Expand Down
50 changes: 24 additions & 26 deletions tests/github/w3c/PFE/Overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -629,21 +629,20 @@ <h4 class="heading settled" data-level="2.1.3" id="sparsebitset"><span class="se
significant bit in the byte and the bit with the largest index is the most significant bit.</p>
<div class="example" id="example-76606ecb">
<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:
<pre> BitString:
|- header |- lvl 0 |---- level 1 ----|------- level 2 -----------|
| | n0 | n1 n2 | n3 n4 n5 |
[ 10010000 10000100 10001000 10000000 00100000 01000000 00010000 ]

Which then becomes the ByteString:
[
0b00001001,
0b00100001,
0b00010001,
0b00000001,
0b00000100,
0b00000010,
0b00001000
]
<pre>BitString:
|- header |- lvl 0 |---- level 1 ----|------- level 2 -----------|
| | n0 | n1 n2 | n3 n4 n5 |
[ 10010000 10000100 10001000 10000000 00100000 01000000 00010000 ]
Which then becomes the ByteString:
[
0b00001001,
0b00100001,
0b00010001,
0b00000001,
0b00000100,
0b00000010,
0b00001000
]
</pre>
<p>First determine the height of the tree:</p>
<p><i>H</i> = ceil(log<sub>8</sub>(323 + 1)) = 3</p>
Expand Down Expand Up @@ -684,17 +683,16 @@ <h4 class="heading settled" data-level="2.1.3" id="sparsebitset"><span class="se
</div>
<div class="example" id="example-88b1de08">
<a class="self-link" href="#example-88b1de08"></a> The set {0, 1, 2, ..., 17} can be encoded with a branching factor of 4 as:
<pre> BitString:
|- header | l0 |- lvl 1 -| l2 |
| | n0 | n1 | n2 | n3 |
[ 00010000 1100 0000 1000 1100 ]

ByteString:
[
0b00001000,
0b00000011,
0b00110001
]
<pre>BitString:
|- header | l0 |- lvl 1 -| l2 |
| | n0 | n1 | n2 | n3 |
[ 00010000 1100 0000 1000 1100 ]
ByteString:
[
0b00001000,
0b00000011,
0b00110001
]
</pre>
<p>First determine the height of the tree:</p>
<p><i>H</i> = ceil(log<sub>4</sub>(17 + 1)) = 3</p>
Expand Down
Loading

0 comments on commit bd9a15d

Please sign in to comment.