Changeset 249867 in webkit


Ignore:
Timestamp:
Sep 13, 2019, 7:16:00 PM (5 years ago)
Author:
Joseph Pecoraro
Message:

Web Inspector: HTML Formatter - XML mode
https://bugs.webkit.org/show_bug.cgi?id=201758

Reviewed by Devin Rousso.

Source/WebInspectorUI:

  • Tools/HTMLFormatter/index.html:
  • Tools/SourceMaps/index.html:

Update Tools to more easily test XML.

  • UserInterface/Proxies/FormatterWorkerProxy.js:

(WI.FormatterWorkerProxy.prototype.formatXML):

  • UserInterface/Views/TextEditor.js:

(WI.TextEditor.prototype.hasFormatter):
(WI.TextEditor.prototype._startWorkerPrettyPrint):
Allow formatting XML content.

  • UserInterface/Workers/Formatter/FormatterWorker.js:

(FormatterWorker.prototype.formatHTML):
(FormatterWorker.prototype.formatXML):
Expose "formatXML".

  • UserInterface/Workers/Formatter/HTMLFormatter.js:

(HTMLFormatter.let.dom):
(HTMLFormatter):
(HTMLFormatter.prototype._shouldHaveNoChildren):
(HTMLFormatter.prototype._before):
(HTMLFormatter.prototype._after):

  • UserInterface/Workers/Formatter/HTMLParser.js:

(HTMLParser.prototype.parseDocument):

  • UserInterface/Workers/Formatter/HTMLTreeBuilderFormatter.js:

(HTMLTreeBuilderFormatter.prototype._isEmptyNode):
Give the HTMLFormatter and related classes an XML mode that
has less of the smarts of XML.

LayoutTests:

  • inspector/formatting/formatting-xml-expected.txt: Added.
  • inspector/formatting/formatting-xml.html: Added.
  • inspector/formatting/resources/formatting-utilities.js:

(TestPage.registerInitializer.async.runFormattingTest):

  • inspector/formatting/resources/xml-tests/atom-expected.xml: Added.
  • inspector/formatting/resources/xml-tests/atom.xml: Added.
  • inspector/formatting/resources/xml-tests/basic-expected.xml: Added.
  • inspector/formatting/resources/xml-tests/basic.xml: Added.
  • inspector/formatting/resources/xml-tests/rss-expected.xml: Added.
  • inspector/formatting/resources/xml-tests/rss.xml: Added.
  • inspector/formatting/resources/xml-tests/valid-html-invalid-xml-expected.xml: Added.
  • inspector/formatting/resources/xml-tests/valid-html-invalid-xml.xml: Added.
  • inspector/formatting/resources/xml-tests/xslt-expected.xml: Added.
  • inspector/formatting/resources/xml-tests/xslt.xml: Added.
Location:
trunk
Files:
13 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r249866 r249867  
     12019-09-13  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: HTML Formatter - XML mode
     4        https://bugs.webkit.org/show_bug.cgi?id=201758
     5
     6        Reviewed by Devin Rousso.
     7
     8        * inspector/formatting/formatting-xml-expected.txt: Added.
     9        * inspector/formatting/formatting-xml.html: Added.
     10        * inspector/formatting/resources/formatting-utilities.js:
     11        (TestPage.registerInitializer.async.runFormattingTest):
     12        * inspector/formatting/resources/xml-tests/atom-expected.xml: Added.
     13        * inspector/formatting/resources/xml-tests/atom.xml: Added.
     14        * inspector/formatting/resources/xml-tests/basic-expected.xml: Added.
     15        * inspector/formatting/resources/xml-tests/basic.xml: Added.
     16        * inspector/formatting/resources/xml-tests/rss-expected.xml: Added.
     17        * inspector/formatting/resources/xml-tests/rss.xml: Added.
     18        * inspector/formatting/resources/xml-tests/valid-html-invalid-xml-expected.xml: Added.
     19        * inspector/formatting/resources/xml-tests/valid-html-invalid-xml.xml: Added.
     20        * inspector/formatting/resources/xml-tests/xslt-expected.xml: Added.
     21        * inspector/formatting/resources/xml-tests/xslt.xml: Added.
     22
    1232019-09-13  Joseph Pecoraro  <pecoraro@apple.com>
    224
  • trunk/LayoutTests/inspector/formatting/resources/formatting-utilities.js

    r249831 r249867  
    5252                workerProxy.formatHTML(testText, indentString, callback);
    5353                break;
     54            case "text/xml":
     55                workerProxy.formatXML(testText, indentString, callback);
     56                break;
    5457            }
    5558        });
  • trunk/Source/WebInspectorUI/ChangeLog

    r249866 r249867  
     12019-09-13  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: HTML Formatter - XML mode
     4        https://bugs.webkit.org/show_bug.cgi?id=201758
     5
     6        Reviewed by Devin Rousso.
     7
     8        * Tools/HTMLFormatter/index.html:
     9        * Tools/SourceMaps/index.html:
     10        Update Tools to more easily test XML.
     11
     12        * UserInterface/Proxies/FormatterWorkerProxy.js:
     13        (WI.FormatterWorkerProxy.prototype.formatXML):
     14        * UserInterface/Views/TextEditor.js:
     15        (WI.TextEditor.prototype.hasFormatter):
     16        (WI.TextEditor.prototype._startWorkerPrettyPrint):
     17        Allow formatting XML content.
     18
     19        * UserInterface/Workers/Formatter/FormatterWorker.js:
     20        (FormatterWorker.prototype.formatHTML):
     21        (FormatterWorker.prototype.formatXML):
     22        Expose "formatXML".
     23
     24        * UserInterface/Workers/Formatter/HTMLFormatter.js:
     25        (HTMLFormatter.let.dom):
     26        (HTMLFormatter):
     27        (HTMLFormatter.prototype._shouldHaveNoChildren):
     28        (HTMLFormatter.prototype._before):
     29        (HTMLFormatter.prototype._after):
     30        * UserInterface/Workers/Formatter/HTMLParser.js:
     31        (HTMLParser.prototype.parseDocument):
     32        * UserInterface/Workers/Formatter/HTMLTreeBuilderFormatter.js:
     33        (HTMLTreeBuilderFormatter.prototype._isEmptyNode):
     34        Give the HTMLFormatter and related classes an XML mode that
     35        has less of the smarts of XML.
     36
    1372019-09-13  Joseph Pecoraro  <pecoraro@apple.com>
    238
  • trunk/Source/WebInspectorUI/Tools/HTMLFormatter/index.html

    r249831 r249867  
    4141        <option value="self">Self</option>
    4242    </select>
     43    <select id="source-type">
     44        <option value="text/html">HTML</option>
     45        <option value="text/xml">XML</option>
     46    </select>
    4347    <button id="format">Format</button>
    4448    <button id="select-output">Select Output</button>
     
    6165    // Elements.
    6266    const populatePicker = document.getElementById("populate");
     67    const sourceTypePicker = document.getElementById("source-type");
    6368    const timeOutput = document.getElementById("time");
    6469    const prettyPre = document.getElementById("pretty");
     
    8792
    8893        // Time the formatter.
     94        let sourceType = sourceTypePicker.value === "text/html" ? HTMLFormatter.SourceType.HTML : HTMLFormatter.SourceType.XML;
    8995        let startTime = Date.now();
    90         let formatter = new HTMLFormatter(cm.getValue());
     96        let formatter = new HTMLFormatter(cm.getValue(), sourceType);
    9197        let endTime = Date.now();
    9298
     
    94100        let debugText = "";
    95101        try {
     102            let options = {isXML: sourceType === HTMLFormatter.SourceType.XML};
    96103            let parser = new HTMLParser;
    97104            let treeBuilder = new HTMLTreeBuilderDebug;
    98             parser.parseDocument(cm.getValue(), treeBuilder);
     105            parser.parseDocument(cm.getValue(), treeBuilder, options);
    99106            debugText = treeBuilder.debugText;
    100107        } catch (error) {
     
    178185        let content = cm.getValue();
    179186        let populate = populatePicker.value;
    180         window.location.search = `?content=${encodeURIComponent(content)}&populate=${encodeURIComponent(populate)}`;
     187        let sourceType = sourceTypePicker.value;
     188        window.location.search = `?content=${encodeURIComponent(content)}&populate=${encodeURIComponent(populate)}&sourceType=${encodeURIComponent(sourceType)}`;
    181189    });
    182190
     
    241249    });
    242250
     251    // Parser mode picker.
     252    sourceTypePicker.addEventListener("change", (event) => {
     253        cm.setOption("mode", sourceTypePicker.value);
     254        refresh();
     255    });
     256
    243257    // Restore better initial value from query string.
    244258    (function() {
     
    256270            updateContentFromPicker();
    257271        }
     272        if (queryParams.sourceType) {
     273            sourceTypePicker.value = queryParams.sourceType;
     274            cm.setOption("mode", sourceTypePicker.value);
     275        }
    258276        if (queryParams.content)
    259277            cm.setValue(queryParams.content);
  • trunk/Source/WebInspectorUI/Tools/SourceMaps/index.html

    r249831 r249867  
    3434        <option>javascript</option>
    3535        <option>css</option>
     36        <option>xml</option>
    3637    </select>
    3738    <button id="format">Format</button>
     
    198199            workerProxy.formatCSS(inputCM.getValue(), indentString, includeSourceMapData, formatResult);
    199200            break;
     201        case "xml":
     202            workerProxy.formatXML(inputCM.getValue(), indentString, includeSourceMapData, formatResult);
     203            break;
    200204        }
    201205
     
    236240    const simpleJS = `(function(){let a=1;return a+1;})();`;
    237241    const simpleCSS = `body{color:red;background:blue}*{color:green}`;
     242    const simpleXML = `<?xml version="1.0" encoding="iso8859-5"?><outer><inner attr="value">1</inner></outer>`;
    238243
    239244    // Populate picker
     
    252257            mode = "text/css";
    253258            content = simpleCSS;
     259            break;
     260        case "xml":
     261            mode = "text/xml";
     262            content = simpleXML;
    254263            break;
    255264        default:
  • trunk/Source/WebInspectorUI/UserInterface/Proxies/FormatterWorkerProxy.js

    r249831 r249867  
    6161    }
    6262
     63    formatXML(sourceText, indentString, includeSourceMapData)
     64    {
     65        this.performAction("formatXML", ...arguments);
     66    }
     67
    6368    // Public
    6469
  • trunk/Source/WebInspectorUI/UserInterface/Views/TextEditor.js

    r249831 r249867  
    183183    {
    184184        let mode = this._codeMirror.getMode().name;
    185         return mode === "javascript" || mode === "css" || mode === "htmlmixed";
     185        return mode === "javascript" || mode === "css" || mode === "htmlmixed" || mode === "xml";
    186186    }
    187187
     
    906906        case "htmlmixed":
    907907            workerProxy.formatHTML(sourceText, indentString, includeSourceMapData, formatCallback);
     908            break;
     909        case "xml":
     910            workerProxy.formatXML(sourceText, indentString, includeSourceMapData, formatCallback);
    908911            break;
    909912        default:
  • trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/FormatterWorker.js

    r249831 r249867  
    111111        let result = {formattedText: null};
    112112        const builder = null;
    113         let formatter = new HTMLFormatter(sourceText, builder, indentString);
     113        const sourceType = HTMLFormatter.SourceType.HTML;
     114        let formatter = new HTMLFormatter(sourceText, sourceType, builder, indentString);
     115        if (formatter.success) {
     116            result.formattedText = formatter.formattedText;
     117            if (includeSourceMapData)
     118                result.sourceMapData = formatter.sourceMapData;
     119        }
     120        return result;
     121    }
     122
     123    formatXML(sourceText, indentString, includeSourceMapData)
     124    {
     125        let result = {formattedText: null};
     126        const builder = null;
     127        const sourceType = HTMLFormatter.SourceType.XML;
     128        let formatter = new HTMLFormatter(sourceText, sourceType, builder, indentString);
    114129        if (formatter.success) {
    115130            result.formattedText = formatter.formattedText;
  • trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/HTMLFormatter.js

    r249831 r249867  
    2626HTMLFormatter = class HTMLFormatter
    2727{
    28     constructor(sourceText, builder, indentString = "    ")
    29     {
     28    constructor(sourceText, sourceType, builder, indentString = "    ")
     29    {
     30        console.assert(typeof sourceText === "string");
     31        console.assert(Object.values(HTMLFormatter.SourceType).includes(sourceType));
     32
     33        this._sourceType = sourceType;
     34
    3035        this._success = false;
    3136
    3237        let dom = (function() {
    3338            try {
     39                let options = {
     40                    isXML: sourceType === HTMLFormatter.SourceType.XML,
     41                };
    3442                let parser = new HTMLParser;
    35                 let treeBuilder = new HTMLTreeBuilderFormatter;
    36                 parser.parseDocument(sourceText, treeBuilder);
     43                let treeBuilder = new HTMLTreeBuilderFormatter(options);
     44                parser.parseDocument(sourceText, treeBuilder, options);
    3745                return treeBuilder.dom;
    3846            } catch (e) {
     
    106114    _shouldHaveNoChildren(node)
    107115    {
    108         return HTMLTreeBuilderFormatter.TagNamesWithoutChildren.has(node.lowercaseName);
     116        switch (this._sourceType) {
     117        case HTMLFormatter.SourceType.HTML:
     118            return HTMLTreeBuilderFormatter.TagNamesWithoutChildren.has(node.lowercaseName);
     119        case HTMLFormatter.SourceType.XML:
     120            return false;
     121        }
     122
     123        console.assert(false, "Unknown source type", this._sourceType);
     124        return false;
    109125    }
    110126
     
    197213
    198214            if (!node.__inlineContent) {
    199                 if (node.lowercaseName !== "html")
     215                if (node.lowercaseName !== "html" || this._sourceType === HTMLFormatter.SourceType.XML)
    200216                    this._builder.indent();
    201217                this._builder.appendNewline();
     
    269285                return;
    270286            if (!node.__inlineContent) {
    271                 if (node.lowercaseName !== "html")
     287                if (node.lowercaseName !== "html" || this._sourceType === HTMLFormatter.SourceType.XML)
    272288                    this._builder.dedent();
    273289                this._builder.appendNewline();
     
    358374    }
    359375};
     376
     377HTMLFormatter.SourceType = {
     378    HTML: "html",
     379    XML: "xml",
     380};
  • trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/HTMLParser.js

    r249866 r249867  
    2828    // Public
    2929
    30     parseDocument(sourceText, treeBuilder)
     30    parseDocument(sourceText, treeBuilder, {isXML} = {})
    3131    {
    3232        console.assert(typeof sourceText === "string");
     
    4040        this._data = sourceText;
    4141        this._bogusCommentOpener = null;
     42        this._isXML = !!isXML;
    4243
    4344        if (this._treeBuilder.begin)
     
    449450        // Custom mode for some elements.
    450451        if (node.type === HTMLParser.NodeType.OpenTag) {
    451             if (node.name.toLowerCase() === "script")
     452            if (!this._isXML && node.name.toLowerCase() === "script")
    452453                this._mode = HTMLParser.Mode.ScriptData;
    453454        }
  • trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/HTMLTreeBuilderFormatter.js

    r249831 r249867  
    3030// whitespace reformatter for input text and not generate the ultimate
    3131// html tree that a browser would generate.
     32//
     33// When run with the XML option, all HTML specific cases are disabled.
    3234
    3335HTMLTreeBuilderFormatter = class HTMLTreeBuilderFormatter
    3436{
     37    constructor({isXML} = {})
     38    {
     39        this._isXML = !!isXML;
     40    }
     41
    3542    // Public
    3643
     
    131138            return true;
    132139
    133         if (HTMLTreeBuilderFormatter.TagNamesWithoutChildren.has(node.lowercaseName))
     140        if (!this._isXML && HTMLTreeBuilderFormatter.TagNamesWithoutChildren.has(node.lowercaseName))
    134141            return true;
    135142
Note: See TracChangeset for help on using the changeset viewer.