惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Vulnerabilities – Threatpost
云风的 BLOG
云风的 BLOG
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
Scott Helme
Scott Helme
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
S
Security Affairs
宝玉的分享
宝玉的分享

Henri Sivonen’s pages

Parin vuoden tutkimattomuus crates.io: Rust Package Registry Asiakirjatonta toimintaa It’s not wrong that "🤦🏼‍♂️".length == 7 Koulutartuntojen tilastointimenettely Perusteasiakirjoja hallussapitämättä ikärajoitettu Asiantuntijat ja nukkuva vallan vahtikoira Koronapassilausunto Suppealla tietopohjalla ohimeneväksi väitetty Text Encoding Menu in 2021 The Text Encoding Submenu Is Gone An HTML5 Conformance Checker Not Part of the Technology Stack Browser Technology Stack Bogo-XML Declaration Returns to Gecko A Look at Encoding Detection and Encoding Menu Telemetry from Firefox 86 Why Supporting Unlabeled UTF-8 in HTML on the Web Would Be Problematic Rust Target Names Aren’t Passed to LLVM Toimintamalli Activating Browser Modes with Doctype Johtopäätöksiä mallin rakenteesta Tehtävänmäärittelyä kirjoittamatta ja kuolemia laskematta laumasuojamallinnettu Character Encoding Menu in 2014 Erillissuosituksen tarpeettomuudesta yleissuosituksen poikkeukseksi? STM:n maskiaikajana Rust 2021 Oma-aloitteisesti mallinnettu Kokopinovaatimuksin kilpailutettu chardetng: A More Compact Character Encoding Detector for the Legacy Web Varauksia paisutellen tiedotettu Perusteasiakirjoitta tiedotettu Always Use UTF-8 & Always Label Your HTML Saying So IME Smoke Testing The Validator.nu HTML Parser About the Hiragino Fonts with CSS It’s Time to Stop Adding New Features for Non-Unicode Execution Encodings in C++ Rust 2020 The Last of the Parsing Quirks About about:blank Rust 2019 a Web-Compatible Character Encoding Library in Rust How I Wrote a Modern C++ Library in Rust Using cargo-fuzz to Transfer Code Review of Simple Safe Code to Complex Code that Uses unsafe A Rust Crate that Also Quacks Like a Modern C++ Library #Rust2018 No Namespaces in JSON, Please A Lecture about HTML5 Julkisesti luotettu varmenne ikidomainille TLS:ää (SSL:ää) varten -webkit-HTML5 Lists in Attribute Values The Sad Story of PNG Gamma “Correction” If You Want Software Freedom on Phones, You Should Work on Firefox OS, Custom Hardware and Web App Self-Hostablility HTML5 Parser Improvements ARIA in HTML5 Integration: Document Conformance (Draft, Take Two) Schema.org and Pre-Existing Communities Lowering memory requirements by replacing Schematron HTML5 Parsing in Gecko: A Build Introducing SAX Tree NVDL Support in Validator.nu HOWTO Avoid Being Called a Bozo When Producing XML An Unofficial Q&A about the Discontinuation of the XHTML2 WG Thoughts on HTML5 Becoming a W3C Recommendation Four Finnish Banks Training Users to Give Banking Credentials to Another Site Unimpressed by Leopard Sergeant Semantics The Content Sink Inheritance Diagram – 2006-06-30 What is EME? About Points and Pixels as Units The Performance Cost of the HTML Tree Builder Social Media Impression Management The spacer Element Is Gone Openmind 2006 Performance Mistake XHTML and Mobile Devices WebM-Enabled Browser Usage Share Exceeds H.264-Enabled Browser Usage Share on Desktop (in StatCounter Numbers) HTML5 Parser-Based View Source Syntax Highlighting Vendor Prefixes Are Hurting the Web Accept-Charset Is No More Dualroids Writing Structural Stylable Document in Mozilla Editor ISO-8859-15 on haitallinen Hourglass The Scientific Method According to Hixie Maemo Source Code Karpelan lukkovertaus ontuu Digitaalisesta arkistoinnista ARIA in HTML5 Integration: Document Conformance (Draft) XHTML—What’s the Point? (Draft, incomplete) Mac OS X Browser Comparison HOWTO Spot a Wannabe Web Standards Advocate An Idea About Intermediate Language Trees and Web UI Generation Thoughts on Using SSL/TLS Certificates as the Solution to Phishing Bureaucracy Meets the Web Europe Day HOWTO Establish a 100% Literacy Rate What to Do with All These Photos? Charmod Norm Checking Validator Web Service Interface Ideas DTDs Don’t Work on the Web EFFI’s Day in Court
HTML Syntax Checker in PHP
Henri Sivonen · 2011-12-22 · via Henri Sivonen’s pages

HTMLSyntaxChecker is a PHP class for checking the syntax of HTML input. HTMLSyntaxChecker is a linter. It is not a validator in the SGML sense. The design goal is that the input HTMLSyntaxChecker accepts is also valid HTML 4.01. However, the opposite is not necessarily true. The idea is to restrict the input in order to disallow common mistakes and to make further processing of the markup easier (by requiring end tags for example).

Differences with HTML 4.01 Strict

HTMLSyntaxChecker is designed to be used as a story input checker in an administrative front end of a database-driven PHP-based site. It is being deployed at Macsanomat (a Macintosh news site). The limitations are based on the needs of Macsanomat, but I think they make sense for other similar sites, too. Of course, the source can be customized as needed.

  • If an end tag is possible, it is required.
  • The attribute value delimiter is ". ' is not supported.
  • Forms are not allowed.
  • Scripts are not allowed.
  • The forced line break (<br>) is allowed only in headings, addresses and inside code samples.
  • Images aren’t allowed inside code samples.
  • The id attribute isn’t allowed in most cases.
  • The style attribute is forbidden. The style attribute is <font> in disguise.
  • <pre> is forbidden.

Known limitations

  • In tables only <table>, <tr>, <th> and <td> are supported.
  • Productions of the form (foo)+ are not supported properly. They are treated like (foo)*.
  • Checking for #REQUIRED attributes is still missing. There is only a temporary test for the alt attribute of <img>.
  • HTMLSyntaxChecker does not check the data types of attributes. (SGML validators don’t check them, either.)

Things to Modify

The syntax checker supports the full range of HTML 4.01 character reference entities. It also supports both decimal and hexadecimal NCR s. However, at Macsanomat we decided to remove the support for entities (except &lt;, &gt;, &quot; and &amp;) and for hexadecimal NCRs in order to reduce the number of ways a particular character may be represented in the database.

Also, it may be useful to add custom elements for internal use. Custom elements may be easier to manage internally and can be replaced with more complex HTML when the content is served out.

Usage

The source (available with syntax coloring) consists of a single class definition. When the source has been imported to the program, instances of HTMLSyntaxChecker may be constructed as follows:
$checker = new HTMLSyntaxChecker($HTMLstring, $rootModel);

Here $HTMLstring is a string containing the HTML input and $rootModel describes the content model of the implicit element in which the HTML input is considered to exist. The permitted values are "BLOCK", "INLINE", "FLOW" and "TEXT". These correspond to %Block;, %Inline;, %Flow; and %Text; of the HTML 4.01 Strict DTD. For example, if the root content model is set to "BLOCK", the contents $HTMLstring could appear in an HTML 4.01 Strict document between <body> and </body>.

Then the object can be used like this:
if($checker->succeeded()) {
    $caseNormalizedHTMLstring = $checker->getResult();
} else {
    echo $checker->getError();
}

The checker has three public methods:

succeeded()
Returns true if the syntax of the input was OK and false otherwise.
getResult()
Returns a case-normalized version of the input string. All the element and attribute names are folded to lower case.
getError()
Returns an error message as an HTML snippet that can be echoed out in any context where block content is allowed.