Skip to content

Commit

Permalink
Merge pull request #123 from Lullabot/case-sensitivity-svg
Browse files Browse the repository at this point in the history
Case sensitivity in svg
  • Loading branch information
sidkshatriya authored Jul 5, 2016
2 parents 875fcdc + 1d7b8ad commit 0b2fde0
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Pass/AmpImgFixPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function pass()
if (in_array($error->code, [ValidationErrorCode::INCONSISTENT_UNITS_FOR_WIDTH_AND_HEIGHT, ValidationErrorCode::MANDATORY_ATTR_MISSING, ValidationErrorCode::INVALID_ATTR_VALUE, ValidationErrorCode::IMPLIED_LAYOUT_INVALID, ValidationErrorCode::SPECIFIED_LAYOUT_INVALID]) &&
!$error->resolved &&
!empty($error->dom_tag) &&
$error->dom_tag->tagName == 'amp-img'
strtolower($error->dom_tag->tagName) == 'amp-img'
) {
$amp_img_el = new DOMQuery($error->dom_tag);

Expand Down
3 changes: 2 additions & 1 deletion src/Pass/MinimumValidFixPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public function pass()
/** @var \DOMElement $tag */
foreach ($all_tags as $tag) {
$local_context->attachDomTag($tag);
$this->parsed_rules->validateTag($local_context, $tag->nodeName, $this->encounteredAttributes($tag), $temp_validation_result);
$tagname = mb_strtolower($tag->tagName, 'UTF-8');
$this->parsed_rules->validateTag($local_context, $tagname, $this->encounteredAttributes($tag), $temp_validation_result);
$this->parsed_rules->validateTagOnExit($local_context, $temp_validation_result);
$local_context->detachDomTag();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Pass/PreliminaryPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function pass()
$dom_el = $el->get(0);
$context_string = $this->getContextString($dom_el);
$lineno = $this->getLineNo($dom_el);
$message = "$dom_el->tagName tag matches CSS selector '$remove_this'";
$tagname = mb_strtolower($dom_el->tagName, 'UTF-8');
$message = "$tagname tag matches CSS selector '$remove_this'";
$this->addActionTaken(new ActionTakenLine($message, ActionTakenType::BLACKLISTED_TAG_REMOVED, $lineno, $context_string));
$el->remove();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pass/StandardFixPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function pass()
continue;
}

$tag_name = $error->dom_tag->tagName;
$tag_name = mb_strtolower($error->dom_tag->tagName, 'UTF-8');

// Property value pairs
if (in_array($error->code, $this->remove_properties_for_codes)
Expand Down
8 changes: 5 additions & 3 deletions src/Pass/StandardFixPassTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public function pass()
$test_validation_result->status = ValidationResultStatus::UNKNOWN;

$local_context->attachDomTag($error->dom_tag);
$this->parsed_rules->validateTag($local_context, $error->dom_tag->nodeName, $this->encounteredAttributes($error->dom_tag), $test_validation_result);
$tagname = mb_strtolower($error->dom_tag->tagName, 'UTF-8');
$this->parsed_rules->validateTag($local_context, $tagname, $this->encounteredAttributes($error->dom_tag), $test_validation_result);
$this->parsed_rules->validateTagOnExit($local_context, $test_validation_result);

if ($test_validation_result->status == ValidationResultStatus::UNKNOWN) {
Expand All @@ -78,9 +79,10 @@ public function pass()
if ($test_validation_result->status !== ValidationResultStatus::PASS &&
in_array('head', $local_context->getAncestorTagNames())
) {
if ($error->dom_tag->tagName !== 'style' || !$error->dom_tag->hasAttribute('amp-custom')) {
if (strtolower($error->dom_tag->tagName) !== 'style' || !$error->dom_tag->hasAttribute('amp-custom')) {
$error->dom_tag->parentNode->removeChild($error->dom_tag);
$error->addGroupActionTaken(new ActionTakenLine($error->dom_tag->nodeName, ActionTakenType::TAG_REMOVED_FROM_HEAD_AFTER_REVALIDATE_FAILED));
$tagname = mb_strtolower($error->dom_tag->tagName, 'UTF-8');
$error->addGroupActionTaken(new ActionTakenLine($tagname, ActionTakenType::TAG_REMOVED_FROM_HEAD_AFTER_REVALIDATE_FAILED));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Pass/StandardScanPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function pass()
foreach ($all_tags as $tag) {
$count++;
$this->context->attachDomTag($tag);
$this->parsed_rules->validateTag($this->context, $tag->nodeName, $this->encounteredAttributes($tag), $this->validation_result);
$tagname = mb_strtolower($tag->tagName, 'UTF-8');
$this->parsed_rules->validateTag($this->context, $tagname, $this->encounteredAttributes($tag), $this->validation_result);
$this->parsed_rules->validateTagOnExit($this->context, $this->validation_result);
$this->context->detachDomTag();
}
Expand Down
11 changes: 7 additions & 4 deletions src/Validate/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected function setAncestorTagNames()
$ancestor_tag_names = [];
$tag = $this->dom_tag;
while (($tag = $tag->parentNode) && !empty($tag->tagName)) {
$ancestor_tag_names[] = $tag->tagName;
$ancestor_tag_names[] = mb_strtolower($tag->tagName, 'UTF-8');;
}
$ancestor_tag_names[] = '!doctype';
$this->ancestor_tag_names = $ancestor_tag_names;
Expand All @@ -256,7 +256,9 @@ protected function setChildTagNames()
/** @var \DOMNode $child_node */
foreach ($this->dom_tag->childNodes as $child_node) {
if ($child_node->nodeType == XML_ELEMENT_NODE) {
$this->child_tag_names[] = $child_node->nodeName;
/** @var \DOMElement $child_node */
$tagname = mb_strtolower($child_node->tagName, 'UTF-8');
$this->child_tag_names[] = $tagname;
}
}
}
Expand All @@ -277,7 +279,7 @@ protected function setParentTagName()
if (empty($this->dom_tag->parentNode->tagName)) {
$this->parent_tag_name = '!doctype';
} else {
$this->parent_tag_name = $this->dom_tag->parentNode->tagName;
$this->parent_tag_name = mb_strtolower($this->dom_tag->parentNode->tagName, 'UTF-8');
}
}

Expand Down Expand Up @@ -377,7 +379,8 @@ public function getContextString(\DOMElement $dom_el)

/** @var string[] $attributes */
$attributes = $this->encounteredAttributes($dom_el);
$context_string = "<$dom_el->tagName";
$tagname = mb_strtolower($dom_el->tagName, 'UTF-8');
$context_string = "<$tagname";
foreach ($attributes as $attr_name => $attr_value) {
// Skip embedded line numbers
if ($attr_name == AMP::AMP_LINENUM_ATTRIBUTE) {
Expand Down
3 changes: 1 addition & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ Note: An explicit commit is noted as these files can change.
There is a slight custom modification made by us to this file (see our repo's 36c67aace ) and that is to close the `<amp-audio>` tags in the file that are not closed. This causes problems for PHP dom.
* `test-data/full-html/several_errors.html` from https://github.com/ampproject/amphtml/blob/96335e0540532264b3b38d498070f17473692b21/validator/testdata/feature_tests/several_errors.html
* `test-data/full-html/spec_example.html` https://github.com/ampproject/amphtml/blob/96335e0540532264b3b38d498070f17473692b21/validator/testdata/feature_tests/spec_example.html
* `test-data/full-html/svg.html` https://github.com/ampproject/amphtml/blob/96335e0540532264b3b38d498070f17473692b21/validator/testdata/feature_tests/svg.html
Small modification: css id `svg_3` was repeated and has been made unique by changing line 68 to `another_svg_circle`.
* `test-data/full-html/svg.html` https://github.com/ampproject/amphtml/raw/8ab5d550fae93b9a1bb8a06d9fb82ffc08569b44/validator/testdata/feature_tests/svg.html
* `test-data/full-html/track_tag.html` https://github.com/ampproject/amphtml/blob/27ee29ffc3d809fcc8143044d22df9d176ad8169/validator/testdata/feature_tests/track_tag.html
* `test-data/full-html/urls.html` from https://github.com/ampproject/amphtml/blob/eddc6fd2224559cb7ccc6a1e27484e52de3d9301/validator/testdata/feature_tests/urls.html
* `test-data/full-html/validator-amp-accordion.html` https://github.com/ampproject/amphtml/blob/27ee29ffc3d809fcc8143044d22df9d176ad8169/extensions/amp-accordion/0.1/test/validator-amp-accordion.html
Expand Down
17 changes: 16 additions & 1 deletion tests/test-data/full-html/svg.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,22 @@ <h2>SVG</h2>
stuff in HTML5, we allow it just like browsers would do in practice.
-->
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="640" height="480">
<circle id="another_svg_circle" r="89.560036" cy="275" cx="315" stroke-linecap="null" stroke-linejoin="null" stroke-width="0" stroke="#000000" fill="#007f00"/>
<circle id="svg_3_2" r="89.560036" cy="275" cx="315" stroke-linecap="null" stroke-linejoin="null" stroke-width="0" stroke="#000000" fill="#007f00"/>
</svg>

<!-- Allow version to be 1.1 and stop tags for lineargradient and
radialgradient -->
<svg version = "1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<lineargradient x1="30%" y1="-55%" x2="58%" y2="94%">
<stop offset="0%" stop-color="#fff" offset="0%" />
<stop offset="0%" stop-color="#aaa" offset="67.36%" />
<stop offset="0%" stop-color="#333" offset="100%" />
</lineargradient>
<radialgradient cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" stop-color="#fff" offset="0%" />
<stop offset="0%" stop-color="#aaa" offset="67.36%" />
<stop offset="0%" stop-color="#333" offset="100%" />
</radialgradient>
</svg>
</body>
</html>
40 changes: 35 additions & 5 deletions tests/test-data/full-html/svg.html.out
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,23 @@
stuff in HTML5, we allow it just like browsers would do in practice.
-->
<svg version="1.0" width="640" height="480" xmlns:xlink="http://www.w3.org/1999/xlink">
<circle id="another_svg_circle" r="89.560036" cy="275" cx="315" stroke-linecap="null" stroke-linejoin="null" stroke-width="0" stroke="#000000" fill="#007f00" />
<circle id="svg_3_2" r="89.560036" cy="275" cx="315" stroke-linecap="null" stroke-linejoin="null" stroke-width="0" stroke="#000000" fill="#007f00" />
</svg>

<!-- Allow version to be 1.1 and stop tags for lineargradient and
radialgradient -->
<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
<linearGradient x1="30%" y1="-55%" x2="58%" y2="94%">
<stop offset="0%" stop-color="#fff" />
<stop offset="67.36%" stop-color="#aaa" />
<stop offset="100%" stop-color="#333" />
</linearGradient>
<radialGradient cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" stop-color="#fff" />
<stop offset="67.36%" stop-color="#aaa" />
<stop offset="100%" stop-color="#333" />
</radialGradient>
</svg>
</body>

</html>
Expand Down Expand Up @@ -138,11 +153,26 @@ Line 64: output from graphics tools. While it's a bit weird to have this na
Line 65: stuff in HTML5, we allow it just like browsers would do in practice.
Line 66: -->
Line 67: <svg version="1.0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="640" height="480">
Line 68: <circle id="another_svg_circle" r="89.560036" cy="275" cx="315" stroke-linecap="null" stroke-linejoin="null" stroke-width="0" stroke="#000000" fill="#007f00"/>
Line 68: <circle id="svg_3_2" r="89.560036" cy="275" cx="315" stroke-linecap="null" stroke-linejoin="null" stroke-width="0" stroke="#000000" fill="#007f00"/>
Line 69: </svg>
Line 70: </body>
Line 71: </html>
Line 72:
Line 70:
Line 71: <!-- Allow version to be 1.1 and stop tags for lineargradient and
Line 72: radialgradient -->
Line 73: <svg version = "1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
Line 74: <lineargradient x1="30%" y1="-55%" x2="58%" y2="94%">
Line 75: <stop offset="0%" stop-color="#fff" offset="0%" />
Line 76: <stop offset="0%" stop-color="#aaa" offset="67.36%" />
Line 77: <stop offset="0%" stop-color="#333" offset="100%" />
Line 78: </lineargradient>
Line 79: <radialgradient cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
Line 80: <stop offset="0%" stop-color="#fff" offset="0%" />
Line 81: <stop offset="0%" stop-color="#aaa" offset="67.36%" />
Line 82: <stop offset="0%" stop-color="#333" offset="100%" />
Line 83: </radialgradient>
Line 84: </svg>
Line 85: </body>
Line 86: </html>
Line 87:



Expand Down

0 comments on commit 0b2fde0

Please sign in to comment.