textcontent-parsing: Difference between revisions

From Microformats Wiki
Jump to navigation Jump to search
(New page: This is a '''draft''' specification for parsing <code>textContent</code> based on Martijn van der Ven's algorithm. [https://wiki.zegnat.net/media/textparsing.html] == Stat...)
 
Line 54: Line 54:
List parsers that have implemented this algorithm. Note any differences as this specification evolves.
List parsers that have implemented this algorithm. Note any differences as this specification evolves.


* '''php-mf2''' implements the [http://microformats.org/wiki/index.php?title=textcontent-parsing&oldid=66933 initial version] of this algorithm as of [https://github.com/microformats/php-mf2/tree/v0.4.4 v0.4.4]
* ...
* ...



Revision as of 20:21, 27 August 2018

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 value
        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.

  • ...

References