fix(16.2): WR-04 serve content_html for markdown that starts with a tag
LooksLikeHTML decided by the first character alone, so a markdown post opening with a hero <img>, a <div> or a comment was served as raw markdown and its first edit stored ## and ** markers as HTML in both columns. Load now serves such a value as HTML only when content_html is empty or missing, or holds that same HTML (the value or SanitizeHTML of it, white space at the ends ignored); otherwise it serves content_html.
This commit is contained in:
@@ -65,9 +65,10 @@ var (
|
||||
// one. Both render BlogEditorElement. Decode stores what the HTML policy
|
||||
// keeps (SanitizeHTMLReport) and reports what it removed as notices; it
|
||||
// never refuses a string because of its markup. Load serves HTML content as
|
||||
// it is and a markdown value (one that does not start with '<') as the
|
||||
// rendered HTML of the htmlColumn option, and a blank value as the
|
||||
// htmlColumn attribute when that holds a body. Upload stores an image the editor
|
||||
// it is and a markdown value as the rendered HTML of the htmlColumn option,
|
||||
// and a blank value as the htmlColumn attribute when that holds a body; a
|
||||
// value that starts with '<' counts as HTML only when the htmlColumn
|
||||
// attribute is empty or holds that same HTML (loadBlogHTML). Upload stores an image the editor
|
||||
// inserts through StoreMedia, in the media bucket the app publishes. app is
|
||||
// asked for the booted application at request time; it may be nil in tests,
|
||||
// where Upload then fails.
|
||||
@@ -107,9 +108,11 @@ func BlogEditorTypes(app func() *backpack.App) []pact.FormFieldType {
|
||||
}
|
||||
}
|
||||
|
||||
// LooksLikeHTML reports whether s is stored HTML rather than markdown: its
|
||||
// first character that is not white space is '<' (an HTML comment
|
||||
// included). Phase 16.2 assumption A1.
|
||||
// LooksLikeHTML reports whether s may be stored HTML rather than markdown:
|
||||
// its first character that is not white space is '<' (an HTML comment
|
||||
// included). Phase 16.2 assumption A1. Markdown can start with a tag too,
|
||||
// so Load also compares the value with its htmlColumn attribute before it
|
||||
// serves the value as HTML.
|
||||
func LooksLikeHTML(s string) bool {
|
||||
t := strings.TrimLeftFunc(s, unicode.IsSpace)
|
||||
return strings.HasPrefix(t, "<")
|
||||
@@ -198,38 +201,68 @@ func removalNames(names []string, cut bool) string {
|
||||
return joined
|
||||
}
|
||||
|
||||
// loadBlogHTML serves a stored value to the editor (D-07). HTML content is
|
||||
// served as it is. A markdown value is served as its rendered HTML: the
|
||||
// htmlColumn attribute in the same locale, or FormatHTML of the value when
|
||||
// that is empty or cannot be read (the value itself when rendering fails).
|
||||
// A blank value is served as the htmlColumn attribute in the same locale
|
||||
// when that is not empty, so a locale that carries only content_html (an
|
||||
// API-written translation, FormatTranslation) opens with the body the site
|
||||
// renders instead of an empty editor whose first edit would replace it; it
|
||||
// is served as stored otherwise.
|
||||
// loadBlogHTML serves a stored value to the editor (D-07). Without the
|
||||
// htmlColumn option every value is served as stored. With it:
|
||||
//
|
||||
// - A blank value is served as the htmlColumn attribute in the same locale
|
||||
// when that is not empty, so a locale that carries only content_html (an
|
||||
// API-written translation, FormatTranslation) opens with the body the
|
||||
// site renders instead of an empty editor whose first edit would replace
|
||||
// it; it is served as stored otherwise.
|
||||
// - A value that looks like HTML (LooksLikeHTML) is served as it is when
|
||||
// the htmlColumn attribute is empty or missing, or holds the same HTML:
|
||||
// the value itself or the policy's output for it (SanitizeHTML), white
|
||||
// space at either end ignored. Otherwise the value is markdown that
|
||||
// merely starts with a tag (a hero <img>, a leading <div> or comment),
|
||||
// and it is served as the htmlColumn attribute, so the first edit does
|
||||
// not store raw markdown as HTML.
|
||||
// - Any other value is markdown, served as the htmlColumn attribute, or as
|
||||
// FormatHTML of the value when that is empty or cannot be read (the
|
||||
// value itself when rendering fails).
|
||||
func loadBlogHTML(_ context.Context, in pact.FormFieldValue) (any, error) {
|
||||
text := storedText(in.Value)
|
||||
column, _ := in.Options["htmlColumn"].(string)
|
||||
if column == "" || LooksLikeHTML(text) {
|
||||
if column == "" {
|
||||
return text, nil
|
||||
}
|
||||
html := ""
|
||||
if in.Translated != nil {
|
||||
html, found, err := in.Translated(column)
|
||||
value, found, err := in.Translated(column)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if found && html != "" {
|
||||
return html, nil
|
||||
if found {
|
||||
html = value
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(text) == "" {
|
||||
switch {
|
||||
case strings.TrimSpace(text) == "":
|
||||
if html != "" {
|
||||
return html, nil
|
||||
}
|
||||
return text, nil
|
||||
case LooksLikeHTML(text):
|
||||
if html == "" || sameStoredHTML(text, html) {
|
||||
return text, nil
|
||||
}
|
||||
return html, nil
|
||||
case html != "":
|
||||
return html, nil
|
||||
}
|
||||
html, err := FormatHTML(text)
|
||||
rendered, err := FormatHTML(text)
|
||||
if err != nil {
|
||||
return text, nil
|
||||
}
|
||||
return html, nil
|
||||
return rendered, nil
|
||||
}
|
||||
|
||||
// sameStoredHTML reports whether rendered, a stored htmlColumn value, is the
|
||||
// HTML text itself: equal to text or to the policy's output for it, with
|
||||
// white space at either end ignored. It is false when rendered came from
|
||||
// rendering text as markdown in a way that changed it.
|
||||
func sameStoredHTML(text, rendered string) bool {
|
||||
rendered = strings.TrimSpace(rendered)
|
||||
return rendered == strings.TrimSpace(text) || rendered == strings.TrimSpace(SanitizeHTML(text))
|
||||
}
|
||||
|
||||
// storedText is a stored column value as text: a string, a *string (nil is
|
||||
|
||||
Reference in New Issue
Block a user