package postcard
import (
"context"
"errors"
"strings"
"testing"
xhtml "golang.org/x/net/html"
)
// schemeFiller surrounds a case's markup, so a snippet that points at the
// offending markup is told apart from one that starts the document.
var schemeFiller = strings.Repeat("plain words here ", 6)
// TestURLSchemeCheck covers the anchored, decoded URL-scheme check of
// rendered HTML (WR-05): prose and URLs that merely contain a scheme word
// pass; every URL-bearing attribute, CSS, meta refresh and every
// character-reference or whitespace-split spelling of a script or data URL
// is refused, on a send with inlining on and off and in the editor. A
// raw-text end tag or a comment's end inside a quoted attribute does not hide
// the rest of the tag (CR-02 residual).
func TestURLSchemeCheck(t *testing.T) {
t.Parallel()
accepts := []struct{ name, html string }{
{"prose_metadata", `
See the metadata: field
`},
{"prose_big_data", `
Big Data: Summit
`},
{"img_alt", ``},
{"title_text", `
Tips
`},
{"scheme_in_query", `x`},
// Escaped markup in a raw-text element is text to every reader.
{"textarea_escaped_link", ``},
{"title_escaped_link", ``},
{"noscript_safe_link", ``},
// Text shaped like an event handler is text (WR-02).
{"prose_handler_shaped", `
`},
{"title_escaped_handler", ``},
{"outlook_conditional_vml", ``},
// The tag reading, where quoting decides where a tag ends, refuses
// nothing mail uses (CR-02 residual).
{"conditional_comment_quoted_attrs", ``},
{"conditional_comment_not_mso_safe_link", `x`},
{"style_css_quoted_strings", ``},
{"title_escaped_end_in_attr", ``},
{"noscript_safe_link_end_in_attr", ``},
// Only the "<" of a comment opener is blanked, so a literal "`, `onerror="alert(1)"`, "event handlers"},
// An end tag inside a quoted attribute ends the raw-text element for
// the tokenizer but not for a conforming parser, which keeps the
// rest of the tag together (CR-02 residual).
{"svg_style_end_in_attr", ``, `href="javascript:alert(1)"`, schemeMsg},
{"noscript_href_end_in_attr", ``, `href="javascript:alert(1)"`, schemeMsg},
{"noscript_handler_end_in_attr", ``, `onerror=alert(1)`, "event handlers"},
{"svg_title_end_in_attr", ``, `href="javascript:alert(1)"`, schemeMsg},
{"svg_textarea_end_in_attr", ``, `href="javascript:alert(1)"`, schemeMsg},
{"noembed_end_in_attr", `x`, `href="javascript:alert(1)"`, schemeMsg},
{"xmp_end_in_attr", `x`, `href="javascript:alert(1)"`, schemeMsg},
{"conditional_comment_end_in_attr", `" href="javascript:alert(1)">x`, `href="javascript:alert(1)"`, schemeMsg},
// Whitespace-split schemes in list, meta, CSS and comment values
// (WR-01).
{"values_tab_split", ``, `values="java script:alert(1)"`, schemeMsg},
{"meta_refresh_tab_split", ``, `url=java script:alert(1)`, schemeMsg},
{"srcset_newline_split", ``, `java
script:x 2x`, schemeMsg},
{"style_tab_split", `