textcontent-parsing

From Microformats Wiki
Revision as of 01:40, 28 August 2018 by GRegorLove (talk | contribs) (→‎Element to string: s/value/child/)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is a draft specification for parsing textContent based on Martijn van der Ven's algorithm. [1]

Status

This is a draft specification.

Algorithm

Plain text of element

To get the plain text for an Element input:

  1. Let output be the result of running Element to string on input
  2. Remove any sequence of one or more consecutive U+0020 SPACE code points directly before and after an U+000A LF code point from output
  3. Strip leading and trailing ASCII whitespace from output
  4. Replace any sequence of one or more consecutive U+0020 SPACE code points in output with a single U+0020 SPACE code point
  5. Return output

Element to string

To get the string value for an Element input:

  1. Let output be an empty list
  2. Let children be the children of input in tree order
  3. For each child in children:
    • If child is a Text node:
      1. Let value be the textContent of child
      2. Replace any U+0009 TAB, U+000A LF, and U+000D CR code points in value with a single U+0020 SPACE code point
      3. Append value to output
    • If child is an Element, switch on its tagName:
      • SCRIPT
      • STYLE
      • IMG
        1. If child has an alt attribute, then:
          1. Let value be the contents of the alt attribute
          2. Strip leading and trailing ASCII whitespace from value
        2. Else if child has a src attribute, then:
          1. Let value be the contents of the src attribute
          2. Strip leading and trailing ASCII whitespace from value
          3. Set value to the absolute URL created by resolving value following the containing document’s language’s rules
        3. Else continue
        4. Append and prepend a single U+0020 SPACE code point to value
        5. Append value to output
      • BR
        • Append a string containing a single U+000A LF code point to output
      • P
        1. Let value be the result of running this algorithm on child
        2. Prepend a single U+000A LF code point to value
        3. Append value to output
      • Any other value
        1. Let value be the result of running this algorithm on child
        2. Append value to output
    • Else continue
  4. Return the concatenation of output

Implementations

List parsers that have implemented this algorithm. Note any differences as this specification evolves.

Brainstorming

Discuss issues and improvements to the algorithm here.

whitespace in pre elements

This algorithm doesn't currently preserve whitespace in pre elements. There's some agreement in this issue that it should be preserved and mf2py currently does that.

add a new topic

References